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

PHPCSStandards / PHP_CodeSniffer / 15036337869

15 May 2025 04:03AM UTC coverage: 78.375% (-0.2%) from 78.556%
15036337869

Pull #856

github

web-flow
Merge 93f570b46 into f5e7943d0
Pull Request #856: [Doc] Cover all errors of PEAR ClassDeclaration

25112 of 32041 relevant lines covered (78.37%)

69.4 hits per line

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

97.27
/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

16
class VariableCommentSniff extends AbstractVariableSniff
17
{
18

19

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

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

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

68
            break;
3✔
69
        }
70

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

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

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

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

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

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

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

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

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

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

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

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

167
    }//end processMemberVar()
2✔
168

169

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

184
    }//end processVariable()
3✔
185

186

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

201
    }//end processVariableInString()
3✔
202

203

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