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

PHPCSStandards / PHP_CodeSniffer / 15637524486

13 Jun 2025 02:54PM UTC coverage: 78.436% (+0.06%) from 78.375%
15637524486

Pull #1108

github

web-flow
Merge ce5067991 into ef0b6a62c
Pull Request #1108: Squiz/SelfMemberReference: update XML doc

25193 of 32119 relevant lines covered (78.44%)

69.39 hits per line

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

97.22
/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\AbstractVariableSniff;
14
use PHP_CodeSniffer\Util\Common;
15
use PHP_CodeSniffer\Util\Tokens;
16

17
class VariableCommentSniff extends AbstractVariableSniff
18
{
19

20

21
    /**
22
     * Called to process class member vars.
23
     *
24
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
25
     * @param int                         $stackPtr  The position of the current token
26
     *                                               in the stack passed in $tokens.
27
     *
28
     * @return void
29
     */
30
    public function processMemberVar(File $phpcsFile, $stackPtr)
3✔
31
    {
32
        $tokens  = $phpcsFile->getTokens();
3✔
33
        $ignore  = Tokens::$scopeModifiers;
3✔
34
        $ignore += [
1✔
35
            T_VAR                    => T_VAR,
3✔
36
            T_STATIC                 => T_STATIC,
3✔
37
            T_READONLY               => T_READONLY,
3✔
38
            T_FINAL                  => T_FINAL,
3✔
39
            T_WHITESPACE             => T_WHITESPACE,
3✔
40
            T_STRING                 => T_STRING,
3✔
41
            T_NS_SEPARATOR           => T_NS_SEPARATOR,
3✔
42
            T_NAMESPACE              => T_NAMESPACE,
3✔
43
            T_NULLABLE               => T_NULLABLE,
3✔
44
            T_TYPE_UNION             => T_TYPE_UNION,
3✔
45
            T_TYPE_INTERSECTION      => T_TYPE_INTERSECTION,
3✔
46
            T_TYPE_OPEN_PARENTHESIS  => T_TYPE_OPEN_PARENTHESIS,
3✔
47
            T_TYPE_CLOSE_PARENTHESIS => T_TYPE_CLOSE_PARENTHESIS,
3✔
48
            T_NULL                   => T_NULL,
3✔
49
            T_TRUE                   => T_TRUE,
3✔
50
            T_FALSE                  => T_FALSE,
3✔
51
            T_SELF                   => T_SELF,
3✔
52
            T_PARENT                 => T_PARENT,
3✔
53
        ];
1✔
54

55
        for ($commentEnd = ($stackPtr - 1); $commentEnd >= 0; $commentEnd--) {
3✔
56
            if (isset($ignore[$tokens[$commentEnd]['code']]) === true) {
3✔
57
                continue;
3✔
58
            }
59

60
            if ($tokens[$commentEnd]['code'] === T_ATTRIBUTE_END
3✔
61
                && isset($tokens[$commentEnd]['attribute_opener']) === true
3✔
62
            ) {
1✔
63
                $commentEnd = $tokens[$commentEnd]['attribute_opener'];
3✔
64
                continue;
3✔
65
            }
66

67
            break;
3✔
68
        }
69

70
        if ($commentEnd === false
2✔
71
            || ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
3✔
72
            && $tokens[$commentEnd]['code'] !== T_COMMENT)
3✔
73
        ) {
1✔
74
            $phpcsFile->addError('Missing member variable doc comment', $stackPtr, 'Missing');
3✔
75
            return;
3✔
76
        }
77

78
        if ($tokens[$commentEnd]['code'] === T_COMMENT) {
3✔
79
            $phpcsFile->addError('You must use "/**" style comments for a member variable comment', $stackPtr, 'WrongStyle');
3✔
80
            return;
3✔
81
        }
82

83
        $commentStart = $tokens[$commentEnd]['comment_opener'];
3✔
84

85
        $foundVar = null;
3✔
86
        foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
3✔
87
            if ($tokens[$tag]['content'] === '@var') {
3✔
88
                if ($foundVar !== null) {
3✔
89
                    $error = 'Only one @var tag is allowed in a member variable comment';
3✔
90
                    $phpcsFile->addError($error, $tag, 'DuplicateVar');
3✔
91
                } else {
1✔
92
                    $foundVar = $tag;
3✔
93
                }
94
            } else if ($tokens[$tag]['content'] === '@see') {
3✔
95
                // Make sure the tag isn't empty.
96
                $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $tag, $commentEnd);
3✔
97
                if ($string === false || $tokens[$string]['line'] !== $tokens[$tag]['line']) {
3✔
98
                    $error = 'Content missing for @see tag in member variable comment';
3✔
99
                    $phpcsFile->addError($error, $tag, 'EmptySees');
3✔
100
                }
1✔
101
            } else {
1✔
102
                $error = '%s tag is not allowed in member variable comment';
3✔
103
                $data  = [$tokens[$tag]['content']];
3✔
104
                $phpcsFile->addWarning($error, $tag, 'TagNotAllowed', $data);
3✔
105
            }//end if
106
        }//end foreach
1✔
107

108
        // The @var tag is the only one we require.
109
        if ($foundVar === null) {
3✔
110
            $error = 'Missing @var tag in member variable comment';
3✔
111
            $phpcsFile->addError($error, $commentEnd, 'MissingVar');
3✔
112
            return;
3✔
113
        }
114

115
        $firstTag = $tokens[$commentStart]['comment_tags'][0];
3✔
116
        if ($foundVar !== null && $tokens[$firstTag]['content'] !== '@var') {
3✔
117
            $error = 'The @var tag must be the first tag in a member variable comment';
×
118
            $phpcsFile->addError($error, $foundVar, 'VarOrder');
×
119
        }
120

121
        // Make sure the tag isn't empty and has the correct padding.
122
        $string = $phpcsFile->findNext(T_DOC_COMMENT_STRING, $foundVar, $commentEnd);
3✔
123
        if ($string === false || $tokens[$string]['line'] !== $tokens[$foundVar]['line']) {
3✔
124
            $error = 'Content missing for @var tag in member variable comment';
3✔
125
            $phpcsFile->addError($error, $foundVar, 'EmptyVar');
3✔
126
            return;
3✔
127
        }
128

129
         // Support both a var type and a description.
130
        preg_match('`^((?:\|?(?:array\([^\)]*\)|[\\\\a-z0-9\[\]]+))*)( .*)?`i', $tokens[($foundVar + 2)]['content'], $varParts);
3✔
131
        if (isset($varParts[1]) === false) {
3✔
132
            return;
×
133
        }
134

135
        $varType = $varParts[1];
3✔
136

137
        // Check var type (can be multiple, separated by '|').
138
        $typeNames      = explode('|', $varType);
3✔
139
        $suggestedNames = [];
3✔
140
        foreach ($typeNames as $typeName) {
3✔
141
            $suggestedName = Common::suggestType($typeName);
3✔
142
            if (in_array($suggestedName, $suggestedNames, true) === false) {
3✔
143
                $suggestedNames[] = $suggestedName;
3✔
144
            }
1✔
145
        }
1✔
146

147
        $suggestedType = implode('|', $suggestedNames);
3✔
148
        if ($varType !== $suggestedType) {
3✔
149
            $error = 'Expected "%s" but found "%s" for @var tag in member variable comment';
3✔
150
            $data  = [
1✔
151
                $suggestedType,
3✔
152
                $varType,
3✔
153
            ];
2✔
154
            $fix   = $phpcsFile->addFixableError($error, $foundVar, 'IncorrectVarType', $data);
3✔
155
            if ($fix === true) {
3✔
156
                $replacement = $suggestedType;
3✔
157
                if (empty($varParts[2]) === false) {
3✔
158
                    $replacement .= $varParts[2];
3✔
159
                }
1✔
160

161
                $phpcsFile->fixer->replaceToken(($foundVar + 2), $replacement);
3✔
162
                unset($replacement);
3✔
163
            }
1✔
164
        }
1✔
165

166
    }//end processMemberVar()
2✔
167

168

169
    /**
170
     * Called to process a normal variable.
171
     *
172
     * Not required for this sniff.
173
     *
174
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this token was found.
175
     * @param int                         $stackPtr  The position where the double quoted
176
     *                                               string was found.
177
     *
178
     * @return void
179
     */
180
    protected function processVariable(File $phpcsFile, $stackPtr)
3✔
181
    {
182

183
    }//end processVariable()
3✔
184

185

186
    /**
187
     * Called to process variables found in double quoted strings.
188
     *
189
     * Not required for this sniff.
190
     *
191
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this token was found.
192
     * @param int                         $stackPtr  The position where the double quoted
193
     *                                               string was found.
194
     *
195
     * @return void
196
     */
197
    protected function processVariableInString(File $phpcsFile, $stackPtr)
3✔
198
    {
199

200
    }//end processVariableInString()
3✔
201

202

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