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

PHPCSStandards / PHP_CodeSniffer / 17137405110

21 Aug 2025 07:48PM UTC coverage: 78.884% (-0.03%) from 78.912%
17137405110

Pull #1175

github

web-flow
Merge 4fed0943c into 038abb652
Pull Request #1175: [4.0] Fix exit code when phpcbf is processing STDIN

0 of 23 new or added lines in 2 files covered. (0.0%)

90 existing lines in 6 files now uncovered.

19848 of 25161 relevant lines covered (78.88%)

89.71 hits per line

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

95.0
/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_ABSTRACT               => T_ABSTRACT,
3✔
53
            T_WHITESPACE             => T_WHITESPACE,
3✔
54
            T_NULLABLE               => T_NULLABLE,
3✔
55
            T_TYPE_UNION             => T_TYPE_UNION,
3✔
56
            T_TYPE_INTERSECTION      => T_TYPE_INTERSECTION,
3✔
57
            T_TYPE_OPEN_PARENTHESIS  => T_TYPE_OPEN_PARENTHESIS,
3✔
58
            T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS,
3✔
59
            T_NULL                   => T_NULL,
3✔
60
            T_TRUE                   => T_TRUE,
3✔
61
            T_FALSE                  => T_FALSE,
3✔
62
            T_SELF                   => T_SELF,
3✔
63
            T_PARENT                 => T_PARENT,
3✔
64
        ];
2✔
65

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

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

78
            break;
3✔
79
        }
80

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

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

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

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

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

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

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

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

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

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

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

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

178
    }//end processMemberVar()
1✔
179

180

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

195
    }//end processVariable()
3✔
196

197

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

UNCOV
212
    }//end processVariableInString()
×
213

214

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