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

PHPCSStandards / PHP_CodeSniffer / 17663621563

12 Sep 2025 03:25AM UTC coverage: 78.786%. Remained the same
17663621563

push

github

web-flow
Merge pull request #1243 from PHPCSStandards/phpcs-4.x/feature/155-normalize-some-code-style-rules-5

CS: normalize code style rules [5]

294 of 308 new or added lines in 191 files covered. (95.45%)

2354 existing lines in 130 files now uncovered.

19732 of 25045 relevant lines covered (78.79%)

96.47 hits per line

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

0.0
/src/Sniffs/AbstractScopeSniff.php
1
<?php
2
/**
3
 * Allows tests that extend this class to listen for tokens within a particular scope.
4
 *
5
 * Below is a test that listens to methods that exist only within classes:
6
 * <code>
7
 * class ClassScopeTest extends PHP_CodeSniffer_Standards_AbstractScopeSniff
8
 * {
9
 *     public function __construct()
10
 *     {
11
 *         parent::__construct(array(T_CLASS), array(T_FUNCTION));
12
 *     }
13
 *
14
 *     protected function processTokenWithinScope(\PHP_CodeSniffer\Files\File $phpcsFile, int $stackPtr, int $currScope)
15
 *     {
16
 *         $className = $phpcsFile->getDeclarationName($currScope);
17
 *         $phpcsFile->addWarning('encountered a method within class '.$className, $stackPtr, 'MethodFound');
18
 *     }
19
 * }
20
 * </code>
21
 *
22
 * @author    Greg Sherwood <gsherwood@squiz.net>
23
 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
24
 * @license   https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
25
 */
26

27
namespace PHP_CodeSniffer\Sniffs;
28

29
use PHP_CodeSniffer\Exceptions\RuntimeException;
30
use PHP_CodeSniffer\Files\File;
31

32
abstract class AbstractScopeSniff implements Sniff
33
{
34

35
    /**
36
     * The token types that this test wishes to listen to within the scope.
37
     *
38
     * @var array
39
     */
40
    private $tokens = [];
41

42
    /**
43
     * The type of scope opener tokens that this test wishes to listen to.
44
     *
45
     * @var array<int|string>
46
     */
47
    private $scopeTokens = [];
48

49
    /**
50
     * True if this test should fire on tokens outside of the scope.
51
     *
52
     * @var boolean
53
     */
54
    private $listenOutside = false;
55

56

57
    /**
58
     * Constructs a new AbstractScopeTest.
59
     *
60
     * @param array   $scopeTokens   The type of scope the test wishes to listen to.
61
     * @param array   $tokens        The tokens that the test wishes to listen to
62
     *                               within the scope.
63
     * @param boolean $listenOutside If true this test will also alert the
64
     *                               extending class when a token is found outside
65
     *                               the scope, by calling the
66
     *                               processTokenOutsideScope method.
67
     *
68
     * @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified tokens arrays are empty
69
     *                                                      or invalid.
70
     */
71
    public function __construct(
×
72
        array $scopeTokens,
73
        array $tokens,
74
        bool $listenOutside = false
75
    ) {
76
        if (empty($scopeTokens) === true) {
×
77
            $error = 'The scope tokens list cannot be empty';
×
78
            throw new RuntimeException($error);
×
79
        }
80

81
        if (empty($tokens) === true) {
×
82
            $error = 'The tokens list cannot be empty';
×
83
            throw new RuntimeException($error);
×
84
        }
85

86
        $invalidScopeTokens = array_intersect($scopeTokens, $tokens);
×
87
        if (empty($invalidScopeTokens) === false) {
×
88
            $invalid = implode(', ', $invalidScopeTokens);
×
89
            $error   = "Scope tokens [$invalid] can't be in the tokens array";
×
90
            throw new RuntimeException($error);
×
91
        }
92

93
        $this->listenOutside = $listenOutside;
×
94
        $this->scopeTokens   = array_flip($scopeTokens);
×
95
        $this->tokens        = $tokens;
×
96
    }
97

98

99
    /**
100
     * The method that is called to register the tokens this test wishes to
101
     * listen to.
102
     *
103
     * DO NOT OVERRIDE THIS METHOD. Use the constructor of this class to register
104
     * for the desired tokens and scope.
105
     *
106
     * @return array<int|string>
107
     * @see    __constructor()
108
     */
UNCOV
109
    final public function register()
×
110
    {
UNCOV
111
        return $this->tokens;
×
112
    }
113

114

115
    /**
116
     * Processes the tokens that this test is listening for.
117
     *
118
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
119
     * @param int                         $stackPtr  The position in the stack where this
120
     *                                               token was found.
121
     *
122
     * @return void|int Optionally returns a stack pointer. The sniff will not be
123
     *                  called again on the current file until the returned stack
124
     *                  pointer is reached. Return `$phpcsFile->numTokens` to skip
125
     *                  the rest of the file.
126
     * @see    processTokenWithinScope()
127
     */
UNCOV
128
    final public function process(File $phpcsFile, int $stackPtr)
×
129
    {
130
        $tokens = $phpcsFile->getTokens();
×
131

132
        $foundScope = false;
×
UNCOV
133
        $skipTokens = [];
×
134
        foreach ($tokens[$stackPtr]['conditions'] as $scope => $code) {
×
135
            if (isset($this->scopeTokens[$code]) === true) {
×
136
                $skipTokens[] = $this->processTokenWithinScope($phpcsFile, $stackPtr, $scope);
×
137
                $foundScope   = true;
×
138
            }
139
        }
140

UNCOV
141
        if ($this->listenOutside === true && $foundScope === false) {
×
UNCOV
142
            $skipTokens[] = $this->processTokenOutsideScope($phpcsFile, $stackPtr);
×
143
        }
144

UNCOV
145
        if (empty($skipTokens) === false) {
×
UNCOV
146
            return min($skipTokens);
×
147
        }
148
    }
149

150

151
    /**
152
     * Processes a token that is found within the scope that this test is
153
     * listening to.
154
     *
155
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
156
     * @param int                         $stackPtr  The position in the stack where this
157
     *                                               token was found.
158
     * @param int                         $currScope The position in the tokens array that
159
     *                                               opened the scope that this test is
160
     *                                               listening for.
161
     *
162
     * @return void|int Optionally returns a stack pointer. The sniff will not be
163
     *                  called again on the current file until the returned stack
164
     *                  pointer is reached. Return `$phpcsFile->numTokens` to skip
165
     *                  the rest of the file.
166
     */
167
    abstract protected function processTokenWithinScope(File $phpcsFile, int $stackPtr, int $currScope);
168

169

170
    /**
171
     * Processes a token that is found outside the scope that this test is
172
     * listening to.
173
     *
174
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
175
     * @param int                         $stackPtr  The position in the stack where this
176
     *                                               token was found.
177
     *
178
     * @return void|int Optionally returns a stack pointer. The sniff will not be
179
     *                  called again on the current file until the returned stack
180
     *                  pointer is reached. Return `$phpcsFile->numTokens` to skip
181
     *                  the rest of the file.
182
     */
183
    abstract protected function processTokenOutsideScope(File $phpcsFile, int $stackPtr);
184
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc