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

PHPCSStandards / PHP_CodeSniffer / 17482034907

05 Sep 2025 02:34AM UTC coverage: 78.507% (-0.7%) from 79.184%
17482034907

Pull #1213

github

web-flow
Merge def8714f5 into f1c9394f1
Pull Request #1213: :fire: Hot Fix: remove FileList tests

25309 of 32238 relevant lines covered (78.51%)

73.81 hits per line

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

89.74
/src/Standards/Squiz/Sniffs/Scope/StaticThisUsageSniff.php
1
<?php
2
/**
3
 * Checks for usage of $this in static methods, which will cause runtime errors.
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\Scope;
11

12
use PHP_CodeSniffer\Files\File;
13
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
14
use PHP_CodeSniffer\Util\Tokens;
15

16
class StaticThisUsageSniff extends AbstractScopeSniff
17
{
18

19

20
    /**
21
     * Constructs the test with the tokens it wishes to listen for.
22
     */
23
    public function __construct()
3✔
24
    {
25
        parent::__construct([T_CLASS, T_TRAIT, T_ENUM, T_ANON_CLASS], [T_FUNCTION]);
3✔
26

27
    }//end __construct()
2✔
28

29

30
    /**
31
     * Processes this test, when one of its tokens is encountered.
32
     *
33
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
34
     * @param int                         $stackPtr  The position of the current token in the
35
     *                                               stack passed in $tokens.
36
     * @param int                         $currScope A pointer to the start of the scope.
37
     *
38
     * @return void
39
     */
40
    public function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
3✔
41
    {
42
        $tokens = $phpcsFile->getTokens();
3✔
43

44
        // Determine if this is a function which needs to be examined.
45
        $conditions = $tokens[$stackPtr]['conditions'];
3✔
46
        end($conditions);
3✔
47
        $deepestScope = key($conditions);
3✔
48
        if ($deepestScope !== $currScope) {
3✔
49
            return;
3✔
50
        }
51

52
        // Ignore abstract functions.
53
        if (isset($tokens[$stackPtr]['scope_closer']) === false) {
3✔
54
            return;
×
55
        }
56

57
        $next = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
3✔
58
        if ($next === false || $tokens[$next]['code'] !== T_STRING) {
3✔
59
            // Not a function declaration, or incomplete.
60
            return;
×
61
        }
62

63
        $methodProps = $phpcsFile->getMethodProperties($stackPtr);
3✔
64
        if ($methodProps['is_static'] === false) {
3✔
65
            return;
3✔
66
        }
67

68
        $next = $stackPtr;
3✔
69
        $end  = $tokens[$stackPtr]['scope_closer'];
3✔
70

71
        $this->checkThisUsage($phpcsFile, $next, $end);
3✔
72

73
    }//end processTokenWithinScope()
2✔
74

75

76
    /**
77
     * Check for $this variable usage between $next and $end tokens.
78
     *
79
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The current file being scanned.
80
     * @param int                         $next      The position of the next token to check.
81
     * @param int                         $end       The position of the last token to check.
82
     *
83
     * @return void
84
     */
85
    private function checkThisUsage(File $phpcsFile, $next, $end)
3✔
86
    {
87
        $tokens = $phpcsFile->getTokens();
3✔
88

89
        do {
90
            $next = $phpcsFile->findNext([T_VARIABLE, T_ANON_CLASS], ($next + 1), $end);
3✔
91
            if ($next === false) {
3✔
92
                continue;
3✔
93
            }
94

95
            if ($tokens[$next]['code'] === T_ANON_CLASS) {
3✔
96
                $this->checkThisUsage($phpcsFile, $next, $tokens[$next]['scope_opener']);
3✔
97
                $next = $tokens[$next]['scope_closer'];
3✔
98
                continue;
3✔
99
            }
100

101
            if ($tokens[$next]['content'] !== '$this') {
3✔
102
                continue;
3✔
103
            }
104

105
            $error = 'Usage of "$this" in static methods will cause runtime errors';
3✔
106
            $phpcsFile->addError($error, $next, 'Found');
3✔
107
        } while ($next !== false);
3✔
108

109
    }//end checkThisUsage()
2✔
110

111

112
    /**
113
     * Processes a token that is found within the scope that this test is
114
     * listening to.
115
     *
116
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
117
     * @param int                         $stackPtr  The position in the stack where this
118
     *                                               token was found.
119
     *
120
     * @return void
121
     */
122
    protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
×
123
    {
124

125
    }//end processTokenOutsideScope()
×
126

127

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