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\Types\Context as TypeContext;
18use Webmozart\Assert\Assert;
19
20/**
21 * Reflection class for a {@}deprecated tag in a Docblock.
22 */
23final class Deprecated extends BaseTag implements Factory\StaticMethod
24{
25    protected $name = 'deprecated';
26
27    /**
28     * PCRE regular expression matching a version vector.
29     * Assumes the "x" modifier.
30     */
31    const REGEX_VECTOR = '(?:
32        # Normal release vectors.
33        \d\S*
34        |
35        # VCS version vectors. Per PHPCS, they are expected to
36        # follow the form of the VCS name, followed by ":", followed
37        # by the version vector itself.
38        # By convention, popular VCSes like CVS, SVN and GIT use "$"
39        # around the actual version vector.
40        [^\s\:]+\:\s*\$[^\$]+\$
41    )';
42
43    /** @var string The version vector. */
44    private $version = '';
45
46    public function __construct($version = null, Description $description = null)
47    {
48        Assert::nullOrStringNotEmpty($version);
49
50        $this->version = $version;
51        $this->description = $description;
52    }
53
54    /**
55     * @return static
56     */
57    public static function create($body, DescriptionFactory $descriptionFactory = null, TypeContext $context = null)
58    {
59        Assert::nullOrString($body);
60        if (empty($body)) {
61            return new static();
62        }
63
64        $matches = [];
65        if (!preg_match('/^(' . self::REGEX_VECTOR . ')\s*(.+)?$/sux', $body, $matches)) {
66            return new static(
67                null,
68                null !== $descriptionFactory ? $descriptionFactory->create($body, $context) : null
69            );
70        }
71
72        return new static(
73            $matches[1],
74            $descriptionFactory->create(isset($matches[2]) ? $matches[2] : '', $context)
75        );
76    }
77
78    /**
79     * Gets the version section of the tag.
80     *
81     * @return string
82     */
83    public function getVersion()
84    {
85        return $this->version;
86    }
87
88    /**
89     * Returns a string representation for this tag.
90     *
91     * @return string
92     */
93    public function __toString()
94    {
95        return $this->version . ($this->description ? ' ' . $this->description->render() : '');
96    }
97}
98