3 declare(strict_types=1);
6 * This file is part of phpDocumentor.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
11 * @link http://phpdoc.org
14 namespace phpDocumentor\Reflection\DocBlock\Tags;
16 use phpDocumentor\Reflection\DocBlock\Description;
17 use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
18 use phpDocumentor\Reflection\Fqsen;
19 use phpDocumentor\Reflection\FqsenResolver;
20 use phpDocumentor\Reflection\Types\Context as TypeContext;
21 use phpDocumentor\Reflection\Utils;
22 use Webmozart\Assert\Assert;
23 use function array_key_exists;
27 * Reflection class for a @covers tag in a Docblock.
29 final class Covers extends BaseTag implements Factory\StaticMethod
32 protected $name = 'covers';
38 * Initializes this tag.
40 public function __construct(Fqsen $refers, ?Description $description = null)
42 $this->refers = $refers;
43 $this->description = $description;
46 public static function create(
48 ?DescriptionFactory $descriptionFactory = null,
49 ?FqsenResolver $resolver = null,
50 ?TypeContext $context = null
52 Assert::stringNotEmpty($body);
53 Assert::notNull($descriptionFactory);
54 Assert::notNull($resolver);
56 $parts = Utils::pregSplit('/\s+/Su', $body, 2);
59 self::resolveFqsen($parts[0], $resolver, $context),
60 $descriptionFactory->create($parts[1] ?? '', $context)
64 private static function resolveFqsen(string $parts, ?FqsenResolver $fqsenResolver, ?TypeContext $context) : Fqsen
66 Assert::notNull($fqsenResolver);
67 $fqsenParts = explode('::', $parts);
68 $resolved = $fqsenResolver->resolve($fqsenParts[0], $context);
70 if (!array_key_exists(1, $fqsenParts)) {
74 return new Fqsen($resolved . '::' . $fqsenParts[1]);
78 * Returns the structural element this tag refers to.
80 public function getReference() : Fqsen
86 * Returns a string representation of this tag.
88 public function __toString() : string
90 if ($this->description) {
91 $description = $this->description->render();
96 $refers = (string) $this->refers;
98 return $refers . ($description !== '' ? ($refers !== '' ? ' ' : '') . $description : '');