• 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

98.53
/src/Standards/Squiz/Sniffs/Commenting/FunctionCommentThrowTagSniff.php
1
<?php
2
/**
3
 * Verifies that a @throws tag exists for each exception type a function throws.
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\Sniff;
14
use PHP_CodeSniffer\Util\Tokens;
15

16
class FunctionCommentThrowTagSniff implements Sniff
17
{
18

19

20
    /**
21
     * Returns an array of tokens this test wants to listen for.
22
     *
23
     * @return array<int|string>
24
     */
25
    public function register()
3✔
26
    {
27
        return [T_FUNCTION];
3✔
28

29
    }//end register()
30

31

32
    /**
33
     * Processes this test, when one of its tokens is encountered.
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 process(File $phpcsFile, $stackPtr)
3✔
42
    {
43
        $tokens = $phpcsFile->getTokens();
3✔
44

45
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
3✔
46
            // Abstract or incomplete.
47
            return;
3✔
48
        }
49

50
        $ignore = Tokens::$methodPrefixes;
3✔
51
        $ignore[T_WHITESPACE] = T_WHITESPACE;
3✔
52

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

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

65
            break;
3✔
66
        }
67

68
        if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
3✔
69
            // Function doesn't have a doc comment or is using the wrong type of comment.
70
            return;
3✔
71
        }
72

73
        $stackPtrEnd = $tokens[$stackPtr]['scope_closer'];
3✔
74

75
        // Find all the exception type tokens within the current scope.
76
        $thrownExceptions = [];
3✔
77
        $currPos          = $stackPtr;
3✔
78
        $foundThrows      = false;
3✔
79
        $unknownCount     = 0;
3✔
80
        do {
81
            $currPos = $phpcsFile->findNext([T_THROW, T_ANON_CLASS, T_CLOSURE], ($currPos + 1), $stackPtrEnd);
3✔
82
            if ($currPos === false) {
3✔
83
                break;
3✔
84
            }
85

86
            if ($tokens[$currPos]['code'] !== T_THROW) {
3✔
87
                $currPos = $tokens[$currPos]['scope_closer'];
3✔
88
                continue;
3✔
89
            }
90

91
            $foundThrows = true;
3✔
92

93
            /*
94
                If we can't find a NEW, we are probably throwing
95
                a variable or calling a method.
96

97
                If we're throwing a variable, and it's the same variable as the
98
                exception container from the nearest 'catch' block, we take that exception
99
                as it is likely to be a re-throw.
100

101
                If we can't find a matching catch block, or the variable name
102
                is different, it's probably a different variable, so we ignore it,
103
                but they still need to provide at least one @throws tag, even through we
104
                don't know the exception class.
105
            */
106

107
            $nextToken = $phpcsFile->findNext(Tokens::$emptyTokens, ($currPos + 1), null, true);
3✔
108
            if ($tokens[$nextToken]['code'] === T_NEW
3✔
109
                || $tokens[$nextToken]['code'] === T_NS_SEPARATOR
3✔
110
                || $tokens[$nextToken]['code'] === T_STRING
3✔
111
            ) {
1✔
112
                if ($tokens[$nextToken]['code'] === T_NEW) {
3✔
113
                    $currException = $phpcsFile->findNext(
3✔
114
                        [
1✔
115
                            T_NS_SEPARATOR,
3✔
116
                            T_STRING,
3✔
117
                        ],
2✔
118
                        $currPos,
3✔
119
                        $stackPtrEnd,
3✔
120
                        false,
3✔
121
                        null,
3✔
122
                        true
2✔
123
                    );
2✔
124
                } else {
1✔
125
                    $currException = $nextToken;
3✔
126
                }
127

128
                if ($currException !== false) {
3✔
129
                    $endException = $phpcsFile->findNext(
3✔
130
                        [
1✔
131
                            T_NS_SEPARATOR,
3✔
132
                            T_STRING,
3✔
133
                        ],
2✔
134
                        ($currException + 1),
3✔
135
                        $stackPtrEnd,
3✔
136
                        true,
3✔
137
                        null,
3✔
138
                        true
2✔
139
                    );
2✔
140

141
                    if ($endException === false) {
3✔
142
                        $thrownExceptions[] = $tokens[$currException]['content'];
×
143
                    } else {
144
                        $thrownExceptions[] = $phpcsFile->getTokensAsString($currException, ($endException - $currException));
3✔
145
                    }
146
                }//end if
1✔
147
            } else if ($tokens[$nextToken]['code'] === T_VARIABLE) {
3✔
148
                // Find the nearest catch block in this scope and, if the caught var
149
                // matches our re-thrown var, use the exception types being caught as
150
                // exception types that are being thrown as well.
151
                $catch = $phpcsFile->findPrevious(
3✔
152
                    T_CATCH,
3✔
153
                    $currPos,
3✔
154
                    $tokens[$stackPtr]['scope_opener'],
3✔
155
                    false,
3✔
156
                    null,
3✔
157
                    false
2✔
158
                );
2✔
159

160
                if ($catch !== false) {
3✔
161
                    $thrownVar = $phpcsFile->findPrevious(
3✔
162
                        T_VARIABLE,
3✔
163
                        ($tokens[$catch]['parenthesis_closer'] - 1),
3✔
164
                        $tokens[$catch]['parenthesis_opener']
3✔
165
                    );
2✔
166

167
                    if ($tokens[$thrownVar]['content'] === $tokens[$nextToken]['content']) {
3✔
168
                        $exceptions = explode('|', $phpcsFile->getTokensAsString(($tokens[$catch]['parenthesis_opener'] + 1), ($thrownVar - $tokens[$catch]['parenthesis_opener'] - 1)));
3✔
169
                        foreach ($exceptions as $exception) {
3✔
170
                            $thrownExceptions[] = trim($exception);
3✔
171
                        }
1✔
172
                    }
1✔
173
                }
1✔
174
            } else {
1✔
175
                ++$unknownCount;
×
176
            }//end if
177
        } while ($currPos < $stackPtrEnd && $currPos !== false);
3✔
178

179
        if ($foundThrows === false) {
3✔
180
            return;
3✔
181
        }
182

183
        // Only need one @throws tag for each type of exception thrown.
184
        $thrownExceptions = array_unique($thrownExceptions);
3✔
185

186
        $throwTags    = [];
3✔
187
        $commentStart = $tokens[$commentEnd]['comment_opener'];
3✔
188
        foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
3✔
189
            if ($tokens[$tag]['content'] !== '@throws') {
3✔
190
                continue;
3✔
191
            }
192

193
            if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
3✔
194
                $exception = $tokens[($tag + 2)]['content'];
3✔
195
                $space     = strpos($exception, ' ');
3✔
196
                if ($space !== false) {
3✔
197
                    $exception = substr($exception, 0, $space);
3✔
198
                }
1✔
199

200
                $throwTags[$exception] = true;
3✔
201
            }
1✔
202
        }
1✔
203

204
        if (empty($throwTags) === true) {
3✔
205
            $error = 'Missing @throws tag in function comment';
3✔
206
            $phpcsFile->addError($error, $commentEnd, 'Missing');
3✔
207
            return;
3✔
208
        } else if (empty($thrownExceptions) === true) {
3✔
209
            // If token count is zero, it means that only variables are being
210
            // thrown, so we need at least one @throws tag (checked above).
211
            // Nothing more to do.
212
            return;
3✔
213
        }
214

215
        // Make sure @throws tag count matches thrown count.
216
        $thrownCount = (count($thrownExceptions) + $unknownCount);
3✔
217
        $tagCount    = count($throwTags);
3✔
218
        if ($thrownCount !== $tagCount) {
3✔
219
            $error = 'Expected %s @throws tag(s) in function comment; %s found';
3✔
220
            $data  = [
1✔
221
                $thrownCount,
3✔
222
                $tagCount,
3✔
223
            ];
2✔
224
            $phpcsFile->addError($error, $commentEnd, 'WrongNumber', $data);
3✔
225
            return;
3✔
226
        }
227

228
        foreach ($thrownExceptions as $throw) {
3✔
229
            if (isset($throwTags[$throw]) === true) {
3✔
230
                continue;
3✔
231
            }
232

233
            foreach ($throwTags as $tag => $ignore) {
3✔
234
                if (strrpos($tag, $throw) === (strlen($tag) - strlen($throw))) {
3✔
235
                    continue 2;
3✔
236
                }
237
            }
1✔
238

239
            $error = 'Missing @throws tag for "%s" exception';
3✔
240
            $data  = [$throw];
3✔
241
            $phpcsFile->addError($error, $commentEnd, 'Missing', $data);
3✔
242
        }
1✔
243

244
    }//end process()
2✔
245

246

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