• 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

99.07
/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::METHOD_MODIFIERS;
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
            ) {
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::EMPTY_TOKENS, ($currPos + 1), null, true);
3✔
108
            if ($tokens[$nextToken]['code'] === T_NEW
3✔
109
                || isset(Tokens::NAME_TOKENS[$tokens[$nextToken]['code']]) === true
3✔
110
            ) {
111
                if ($tokens[$nextToken]['code'] === T_NEW) {
3✔
112
                    $currException = $phpcsFile->findNext(
3✔
113
                        Tokens::EMPTY_TOKENS,
3✔
114
                        ($nextToken + 1),
3✔
115
                        $stackPtrEnd,
3✔
116
                        true
3✔
117
                    );
2✔
118
                } else {
119
                    $currException = $nextToken;
3✔
120
                }
121

122
                if ($currException !== false
3✔
123
                    && isset(Tokens::NAME_TOKENS[$tokens[$currException]['code']]) === true
3✔
124
                ) {
125
                    if ($tokens[$currException]['code'] === T_NAME_RELATIVE) {
3✔
126
                        // Strip the `namespace\` prefix off the exception name
127
                        // to prevent confusing the name comparison.
128
                        $thrownExceptions[] = substr($tokens[$currException]['content'], 10);
3✔
129
                    } else {
130
                        $thrownExceptions[] = $tokens[$currException]['content'];
3✔
131
                    }
132
                }
133
            } else if ($tokens[$nextToken]['code'] === T_VARIABLE) {
3✔
134
                // Find the nearest catch block in this scope and, if the caught var
135
                // matches our re-thrown var, use the exception types being caught as
136
                // exception types that are being thrown as well.
137
                $catch = $phpcsFile->findPrevious(
3✔
138
                    T_CATCH,
3✔
139
                    $currPos,
3✔
140
                    $tokens[$stackPtr]['scope_opener'],
3✔
141
                    false,
3✔
142
                    null,
3✔
143
                    false
3✔
144
                );
2✔
145

146
                if ($catch !== false) {
3✔
147
                    $thrownVar = $phpcsFile->findPrevious(
3✔
148
                        T_VARIABLE,
3✔
149
                        ($tokens[$catch]['parenthesis_closer'] - 1),
3✔
150
                        $tokens[$catch]['parenthesis_opener']
3✔
151
                    );
2✔
152

153
                    if ($tokens[$thrownVar]['content'] === $tokens[$nextToken]['content']) {
3✔
154
                        $exceptions = explode('|', $phpcsFile->getTokensAsString(($tokens[$catch]['parenthesis_opener'] + 1), ($thrownVar - $tokens[$catch]['parenthesis_opener'] - 1)));
3✔
155
                        foreach ($exceptions as $exception) {
3✔
156
                            $thrownExceptions[] = trim($exception);
3✔
157
                        }
158
                    }
159
                }
160
            } else {
161
                ++$unknownCount;
×
162
            }//end if
163
        } while ($currPos < $stackPtrEnd && $currPos !== false);
3✔
164

165
        if ($foundThrows === false) {
3✔
166
            return;
3✔
167
        }
168

169
        // Only need one @throws tag for each type of exception thrown.
170
        $thrownExceptions = array_unique($thrownExceptions);
3✔
171

172
        $throwTags    = [];
3✔
173
        $commentStart = $tokens[$commentEnd]['comment_opener'];
3✔
174
        foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
3✔
175
            if ($tokens[$tag]['content'] !== '@throws') {
3✔
176
                continue;
3✔
177
            }
178

179
            if ($tokens[($tag + 2)]['code'] === T_DOC_COMMENT_STRING) {
3✔
180
                $exception = $tokens[($tag + 2)]['content'];
3✔
181
                $space     = strpos($exception, ' ');
3✔
182
                if ($space !== false) {
3✔
183
                    $exception = substr($exception, 0, $space);
3✔
184
                }
185

186
                $throwTags[$exception] = true;
3✔
187
            }
188
        }
189

190
        if (empty($throwTags) === true) {
3✔
191
            $error = 'Missing @throws tag in function comment';
3✔
192
            $phpcsFile->addError($error, $commentEnd, 'Missing');
3✔
193
            return;
3✔
194
        } else if (empty($thrownExceptions) === true) {
3✔
195
            // If token count is zero, it means that only variables are being
196
            // thrown, so we need at least one @throws tag (checked above).
197
            // Nothing more to do.
198
            return;
3✔
199
        }
200

201
        // Make sure @throws tag count matches thrown count.
202
        $thrownCount = (count($thrownExceptions) + $unknownCount);
3✔
203
        $tagCount    = count($throwTags);
3✔
204
        if ($thrownCount !== $tagCount) {
3✔
205
            $error = 'Expected %s @throws tag(s) in function comment; %s found';
3✔
206
            $data  = [
2✔
207
                $thrownCount,
3✔
208
                $tagCount,
3✔
209
            ];
2✔
210
            $phpcsFile->addError($error, $commentEnd, 'WrongNumber', $data);
3✔
211
            return;
3✔
212
        }
213

214
        foreach ($thrownExceptions as $throw) {
3✔
215
            if (isset($throwTags[$throw]) === true) {
3✔
216
                continue;
3✔
217
            }
218

219
            foreach ($throwTags as $tag => $ignore) {
3✔
220
                if (strrpos($tag, $throw) === (strlen($tag) - strlen($throw))) {
3✔
221
                    continue 2;
3✔
222
                }
223
            }
224

225
            $error = 'Missing @throws tag for "%s" exception';
3✔
226
            $data  = [$throw];
3✔
227
            $phpcsFile->addError($error, $commentEnd, 'Missing', $data);
3✔
228
        }
229

230
    }//end process()
1✔
231

232

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

© 2025 Coveralls, Inc