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

PHPCSStandards / PHP_CodeSniffer / 15253296250

26 May 2025 11:55AM UTC coverage: 78.632% (+0.3%) from 78.375%
15253296250

Pull #1105

github

web-flow
Merge d9441d98f into caf806050
Pull Request #1105: Skip tests when 'git' command is not available

19665 of 25009 relevant lines covered (78.63%)

88.67 hits per line

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

94.95
/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::OO_SCOPE_TOKENS, [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

45
        $ignore  = Tokens::SCOPE_MODIFIERS;
3✔
46
        $ignore += Tokens::NAME_TOKENS;
3✔
47
        $ignore += [
2✔
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_NULLABLE               => T_NULLABLE,
3✔
54
            T_TYPE_UNION             => T_TYPE_UNION,
3✔
55
            T_TYPE_INTERSECTION      => T_TYPE_INTERSECTION,
3✔
56
            T_TYPE_OPEN_PARENTHESIS  => T_TYPE_OPEN_PARENTHESIS,
3✔
57
            T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS,
3✔
58
            T_NULL                   => T_NULL,
3✔
59
            T_TRUE                   => T_TRUE,
3✔
60
            T_FALSE                  => T_FALSE,
3✔
61
            T_SELF                   => T_SELF,
3✔
62
            T_PARENT                 => T_PARENT,
3✔
63
        ];
2✔
64

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

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

77
            break;
3✔
78
        }
79

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

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

93
        $commentStart = $tokens[$commentEnd]['comment_opener'];
3✔
94

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

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

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

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

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

146
        $varType = $varParts[1];
3✔
147

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

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

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

177
    }//end processMemberVar()
1✔
178

179

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

194
    }//end processVariable()
3✔
195

196

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

211
    }//end processVariableInString()
×
212

213

214
}//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