<?php

declare(strict_types=1);

/**
 * This file is part of phpDocumentor.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @link https://phpdoc.org
 */

namespace phpDocumentor\Descriptor\Builder\Reflector\Tags;

use phpDocumentor\Descriptor\Builder\AssemblerAbstract;
use phpDocumentor\Descriptor\DocBlock\DescriptionDescriptor;
use phpDocumentor\Descriptor\TagDescriptor;
use phpDocumentor\Descriptor\Validation\Error;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;

use function sprintf;
use function str_replace;

/** @extends AssemblerAbstract<TagDescriptor, InvalidTag> */
final class InvalidTagAssembler extends AssemblerAbstract
{
    public function create(object $data): TagDescriptor
    {
        $descriptor = new TagDescriptor($data->getName());
        $descriptor->setDescription(new DescriptionDescriptor(
            new Description(str_replace('%', '%%', (string) $data)),
            [],
        ));
        $descriptor->getErrors()->add(
            new Error(
                'ERROR',
                sprintf(
                    'Tag "%s" with body "%s" has error %s',
                    $data->getName(),
                    $data->render(),
                    $data->getException() === null ? '' : $data->getException()->getMessage(),
                ),
                null,
            ),
        );

        return $descriptor;
    }
}
