1<?php
2/**
3 * This file is part of phpDocumentor.
4 *
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
7 *
8 * @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
9 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10 * @link      http://phpdoc.org
11 */
12
13namespace phpDocumentor\Reflection\DocBlock\Tags;
14
15use phpDocumentor\Reflection\DocBlock\Description;
16use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
17use phpDocumentor\Reflection\Fqsen;
18use phpDocumentor\Reflection\FqsenResolver;
19use phpDocumentor\Reflection\Types\Context as TypeContext;
20use Webmozart\Assert\Assert;
21
22/**
23 * Reflection class for a @covers tag in a Docblock.
24 */
25final class Covers extends BaseTag implements Factory\StaticMethod
26{
27    protected $name = 'covers';
28
29    /** @var Fqsen */
30    private $refers = null;
31
32    /**
33     * Initializes this tag.
34     *
35     * @param Fqsen $refers
36     * @param Description $description
37     */
38    public function __construct(Fqsen $refers, Description $description = null)
39    {
40        $this->refers = $refers;
41        $this->description = $description;
42    }
43
44    /**
45     * {@inheritdoc}
46     */
47    public static function create(
48        $body,
49        DescriptionFactory $descriptionFactory = null,
50        FqsenResolver $resolver = null,
51        TypeContext $context = null
52    ) {
53        Assert::string($body);
54        Assert::notEmpty($body);
55
56        $parts = preg_split('/\s+/Su', $body, 2);
57
58        return new static(
59            $resolver->resolve($parts[0], $context),
60            $descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context)
61        );
62    }
63
64    /**
65     * Returns the structural element this tag refers to.
66     *
67     * @return Fqsen
68     */
69    public function getReference()
70    {
71        return $this->refers;
72    }
73
74    /**
75     * Returns a string representation of this tag.
76     *
77     * @return string
78     */
79    public function __toString()
80    {
81        return $this->refers . ($this->description ? ' ' . $this->description->render() : '');
82    }
83}
84