• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPCSStandards / PHP_CodeSniffer / 14476842118

15 Apr 2025 06:34PM UTC coverage: 77.611% (-0.003%) from 77.614%
14476842118

push

github

web-flow
Merge pull request #374 from PHPCSStandards/feature/variable-sniffs-minor-performance-tweak

4.0 | Variable sniffs: minor performance tweak

15 of 15 new or added lines in 5 files covered. (100.0%)

4 existing lines in 2 files now uncovered.

19409 of 25008 relevant lines covered (77.61%)

78.42 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.1
/src/Standards/Squiz/Sniffs/Commenting/VariableCommentSniff.php
1
<?php
2
/**
3
 * Parses and verifies the variable doc comment.
4
 *
5
 * @author    Greg Sherwood <gsherwood@squiz.net>
6
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
7
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8
 */
9

10
namespace PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
14
use PHP_CodeSniffer\Sniffs\AbstractVariableSniff;
15
use PHP_CodeSniffer\Util\Common;
16
use PHP_CodeSniffer\Util\Tokens;
17

18
class VariableCommentSniff extends AbstractVariableSniff
19
{
20

21

22
    /**
23
     * Only listen to variables within OO scopes.
24
     */
25
    public function __construct()
3✔
26
    {
27
        AbstractScopeSniff::__construct(Tokens::$ooScopeTokens, [T_VARIABLE], false);
3✔
28

29
    }//end __construct()
1✔
30

31

32
    /**
33
     * Called to process class member vars.
34
     *
35
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
36
     * @param int                         $stackPtr  The position of the current token
37
     *                                               in the stack passed in $tokens.
38
     *
39
     * @return void
40
     */
41
    public function processMemberVar(File $phpcsFile, $stackPtr)
3✔
42
    {
43
        $tokens = $phpcsFile->getTokens();
3✔
44
        $ignore = [
2✔
45
            T_PUBLIC                 => T_PUBLIC,
3✔
46
            T_PRIVATE                => T_PRIVATE,
3✔
47
            T_PROTECTED              => T_PROTECTED,
3✔
48
            T_VAR                    => T_VAR,
3✔
49
            T_STATIC                 => T_STATIC,
3✔
50
            T_READONLY               => T_READONLY,
3✔
51
            T_FINAL                  => T_FINAL,
3✔
52
            T_WHITESPACE             => T_WHITESPACE,
3✔
53
            T_STRING                 => T_STRING,
3✔
54
            T_NS_SEPARATOR           => T_NS_SEPARATOR,
3✔
55
            T_NAMESPACE              => T_NAMESPACE,
3✔
56
            T_NULLABLE               => T_NULLABLE,
3✔
57
            T_TYPE_UNION             => T_TYPE_UNION,
3✔
58
            T_TYPE_INTERSECTION      => T_TYPE_INTERSECTION,
3✔
59
            T_TYPE_OPEN_PARENTHESIS  => T_TYPE_OPEN_PARENTHESIS,
3✔
60
            T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS,
3✔
61
            T_NULL                   => T_NULL,
3✔
62
            T_TRUE                   => T_TRUE,
3✔
63
            T_FALSE                  => T_FALSE,
3✔
64
            T_SELF                   => T_SELF,
3✔
65
            T_PARENT                 => T_PARENT,
3✔
66
        ];
2✔
67

68
        for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
3✔
69
            if (isset($ignore[$tokens[$commentEnd]['code']]) === true) {
3✔
70
                continue;
3✔
71
            }
72

73
            if ($tokens[$commentEnd]['code'] === T_ATTRIBUTE_END
3✔
74
                && isset($tokens[$commentEnd]['attribute_opener']) === true
3✔
75
            ) {
76
                $commentEnd = $tokens[$commentEnd]['attribute_opener'];
3✔
77
                continue;
3✔
78
            }
79

80
            break;
3✔
81
        }
82

83
        if ($commentEnd === false
3✔
84
            || ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
3✔
85
            && $tokens[$commentEnd]['code'] !== T_COMMENT)
3✔
86
        ) {
87
            $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
3✔
88
            return;
3✔
89
        }
90

91
        if ($tokens[$commentEnd]['code'] === T_COMMENT) {
3✔
92
            $phpcsFile->addError('You must use "/**" style comments for a member variable comment', $stackPtr, 'WrongStyle');
3✔
93
            return;
3✔
94
        }
95

96
        $commentStart = $tokens[$commentEnd]['comment_opener'];
3✔
97

98
        $foundVar = null;
3✔
99
        foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
3✔
100
            if ($tokens[$tag]['content'] === '@var') {
3✔
101
                if ($foundVar !== null) {
3✔
102
                    $error = 'Only one @var tag is allowed in a member variable comment';
3✔
103
                    $phpcsFile->addError($error, $tag, 'DuplicateVar');
3✔
104
                } else {
105
                    $foundVar = $tag;
3✔
106
                }
107
            } else if ($tokens[$tag]['content'] === '@see') {
3✔
108
                // Make sure the tag isn't empty.
109
                $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
3✔
110
                if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
3✔
111
                    $error = 'Content missing for @see tag in member variable comment';
3✔
112
                    $phpcsFile->addError($error, $tag, 'EmptySees');
3✔
113
                }
114
            } else {
115
                $error = '%s tag is not allowed in member variable comment';
3✔
116
                $data  = [$tokens[$tag]['content']];
3✔
117
                $phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data);
3✔
118
            }//end if
119
        }//end foreach
120

121
        // The @var tag is the only one we require.
122
        if ($foundVar === null) {
3✔
123
            $error = 'Missing @var tag in member variable comment';
3✔
124
            $phpcsFile->addError($error, $commentEnd, 'MissingVar');
3✔
125
            return;
3✔
126
        }
127

128
        $firstTag = $tokens[$commentStart]['comment_tags'][0];
3✔
129
        if ($foundVar !== null && $tokens[$firstTag]['content'] !== '@var') {
3✔
130
            $error = 'The @var tag must be the first tag in a member variable comment';
×
131
            $phpcsFile->addError($error, $foundVar, 'VarOrder');
×
132
        }
133

134
        // Make sure the tag isn't empty and has the correct padding.
135
        $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd);
3✔
136
        if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) {
3✔
137
            $error = 'Content missing for @var tag in member variable comment';
3✔
138
            $phpcsFile->addError($error, $foundVar, 'EmptyVar');
3✔
139
            return;
3✔
140
        }
141

142
         // Support both a var type and a description.
143
        preg_match('`^((?:\|?(?:array\([^\)]*\)|[\\\\a-z0-9\[\]]+))*)( .*)?`i', $tokens[($foundVar + 2)]['content'], $varParts);
3✔
144
        if (isset($varParts[1]) === false) {
3✔
145
            return;
×
146
        }
147

148
        $varType = $varParts[1];
3✔
149

150
        // Check var type (can be multiple, separated by '|').
151
        $typeNames      = explode('|', $varType);
3✔
152
        $suggestedNames = [];
3✔
153
        foreach ($typeNames as $typeName) {
3✔
154
            $suggestedName = Common::suggestType($typeName);
3✔
155
            if (in_array($suggestedName, $suggestedNames, true) === false) {
3✔
156
                $suggestedNames[] = $suggestedName;
3✔
157
            }
158
        }
159

160
        $suggestedType = implode('|', $suggestedNames);
3✔
161
        if ($varType !== $suggestedType) {
3✔
162
            $error = 'Expected "%s" but found "%s" for @var tag in member variable comment';
3✔
163
            $data  = [
2✔
164
                $suggestedType,
3✔
165
                $varType,
3✔
166
            ];
2✔
167
            $fix   = $phpcsFile->addFixableError($error, $foundVar, 'IncorrectVarType', $data);
3✔
168
            if ($fix === true) {
3✔
169
                $replacement = $suggestedType;
3✔
170
                if (empty($varParts[2]) === false) {
3✔
171
                    $replacement .= $varParts[2];
3✔
172
                }
173

174
                $phpcsFile->fixer->replaceToken(($foundVar + 2), $replacement);
3✔
175
                unset($replacement);
3✔
176
            }
177
        }
178

179
    }//end processMemberVar()
1✔
180

181

182
    /**
183
     * Called to process a normal variable.
184
     *
185
     * Not required for this sniff.
186
     *
187
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this token was found.
188
     * @param int                         $stackPtr  The position where the double quoted
189
     *                                               string was found.
190
     *
191
     * @return void
192
     */
193
    protected function processVariable(File $phpcsFile, $stackPtr)
3✔
194
    {
195

196
    }//end processVariable()
3✔
197

198

199
    /**
200
     * Called to process variables found in double quoted strings.
201
     *
202
     * Not required for this sniff.
203
     *
204
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this token was found.
205
     * @param int                         $stackPtr  The position where the double quoted
206
     *                                               string was found.
207
     *
208
     * @return void
209
     */
UNCOV
210
    protected function processVariableInString(File $phpcsFile, $stackPtr)
×
211
    {
212

UNCOV
213
    }//end processVariableInString()
×
214

215

216
}//end class
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc