Completed
Pull Request — master (#130)
by Greg
04:26
created

DocblockTag   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 143
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A isTag() 0 4 1
A splitTagAndContent() 0 5 1
A __construct() 0 5 1
A appendContent() 0 4 1
A getTag() 0 4 1
A getContent() 0 4 1
A __toString() 0 4 1
A hasVariable() 0 6 2
A hasVariableAndDescription() 0 5 1
A hasTypeVariableAndDescription() 0 5 1
A hasWordAndDescription() 0 5 1
1
<?php
2
namespace Consolidation\AnnotatedCommand\Parser\Internal;
3
4
/**
5
 * Hold the tag definition for one tag in a DocBlock.
6
 *
7
 * The tag can be sliced into the following forms:
8
 * - "@tag content"
9
 * - "@tag word description"
10
 * - "@tag $variable description"
11
 * - "@tag word $variable description"
12
 */
13
class DocblockTag
14
{
15
    /** @var string Name of the tag */
16
    protected $tag;
17
18
    /** @var string|null Contents of the tag. */
19
    protected $content;
20
21
    const TAG_REGEX = '@(?P<tag>[^ \t]+)[ \t]*';
22
    const VARIABLE_REGEX = '\\$(?P<variable>[^ \t]+)[ \t]*';
23
    const VARIABLE_OR_WORD_REGEX = '\\$?(?P<variable>[^ \t]+)[ \t]*';
24
    const TYPE_REGEX = '(?P<type>[^ \t]+)[ \t]*';
25
    const WORD_REGEX = '(?P<word>[^ \t]+)[ \t]*';
26
    const DESCRIPTION_REGEX = '(?P<description>.*)';
27
    const IS_TAG_REGEX = '/^[* \t]*@/';
28
29
    /**
30
     * Check if the provided string begins with a tag
31
     * @param string $subject
32
     * @return bool
33
     */
34
    public static function isTag($subject)
35
    {
36
        return preg_match(self::IS_TAG_REGEX, $subject);
37
    }
38
39
    /**
40
     * Use a regular expression to separate the tag from the content.
41
     *
42
     * @param string $subject
43
     * @param string[] &$matches Sets $matches['tag'] and $matches['description']
44
     * @return bool
45
     */
46
    public static function splitTagAndContent($subject, &$matches)
47
    {
48
        $regex = '/' . self::TAG_REGEX . self::DESCRIPTION_REGEX . '/';
49
        return preg_match($regex, $subject, $matches);
50
    }
51
52
    /**
53
     * DockblockTag constructor
54
     */
55
    public function __construct($tag, $content = null)
56
    {
57
        $this->tag = $tag;
58
        $this->content = $content;
59
    }
60
61
    /**
62
     * Add more content onto a tag during parsing.
63
     */
64
    public function appendContent($line)
65
    {
66
        $this->content .= "\n$line";
67
    }
68
69
    /**
70
     * Return the tag - e.g. "@foo description" returns 'foo'
71
     *
72
     * @return string
73
     */
74
    public function getTag()
75
    {
76
        return $this->tag;
77
    }
78
79
    /**
80
     * Return the content portion of the tag - e.g. "@foo bar baz boz" returns
81
     * "bar baz boz"
82
     *
83
     * @return string
84
     */
85
    public function getContent()
86
    {
87
        return $this->content;
88
    }
89
90
    /**
91
     * Convert tag back into a string.
92
     */
93
    public function __toString()
94
    {
95
        return '@' . $this->getTag() . ' ' . $this->getContent();
96
    }
97
98
    /**
99
     * Determine if tag is one of:
100
     * - "@tag variable description"
101
     * - "@tag $variable description"
102
     * - "@tag type $variable description"
103
     *
104
     * @param string $subject
0 ignored issues
show
Bug introduced by
There is no parameter named $subject. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
105
     * @param string[] &$matches Sets $matches['variable'] and
106
     *   $matches['description']; might set $matches['type'].
107
     * @return bool
108
     */
109
    public function hasVariable(&$matches)
110
    {
111
        return
112
            $this->hasTypeVariableAndDescription($matches) ||
113
            $this->hasVariableAndDescription($matches);
114
    }
115
116
    /**
117
     * Determine if tag is "@tag $variable description"
118
     * @param string $subject
0 ignored issues
show
Bug introduced by
There is no parameter named $subject. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
119
     * @param string[] &$matches Sets $matches['variable'] and
120
     *   $matches['description']
121
     * @return bool
122
     */
123
    public function hasVariableAndDescription(&$matches)
124
    {
125
        $regex = '/' . self::VARIABLE_OR_WORD_REGEX . self::DESCRIPTION_REGEX . '/';
126
        return preg_match($regex, $this->getContent(), $matches);
127
    }
128
129
    /**
130
     * Determine if tag is "@tag type $variable description"
131
     *
132
     * @param string $subject
0 ignored issues
show
Bug introduced by
There is no parameter named $subject. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
133
     * @param string[] &$matches Sets $matches['variable'],
134
     *   $matches['description'] and $matches['type'].
135
     * @return bool
136
     */
137
    public function hasTypeVariableAndDescription(&$matches)
138
    {
139
        $regex = '/' . self::TYPE_REGEX . self::VARIABLE_REGEX . self::DESCRIPTION_REGEX . '/';
140
        return preg_match($regex, $this->getContent(), $matches);
141
    }
142
143
    /**
144
     * Determine if tag is "@tag word description"
145
     * @param string $subject
0 ignored issues
show
Bug introduced by
There is no parameter named $subject. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
146
     * @param string[] &$matches Sets $matches['word'] and
147
     *   $matches['description']
148
     * @return bool
149
     */
150
    public function hasWordAndDescription(&$matches)
151
    {
152
        $regex = '/' . self::WORD_REGEX . self::DESCRIPTION_REGEX . '/';
153
        return preg_match($regex, $this->getContent(), $matches);
154
    }
155
}
156