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

PHPCSStandards / PHP_CodeSniffer / 9101918600

15 May 2024 07:59PM UTC coverage: 73.231% (-0.1%) from 73.363%
9101918600

push

github

jrfnl
Generic/SpaceAfterNot: improve code coverage

17793 of 24297 relevant lines covered (73.23%)

69.17 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, $stackPtr, $currScope)
15
 *     {
16
 *         $className = $phpcsFile->getDeclarationName($currScope);
17
 *         echo 'encountered a method within class '.$className;
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
        $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
    }//end __construct()
98

99

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

114
    }//end register()
115

116

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

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

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

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

151
    }//end process()
152

153

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

172

173
    /**
174
     * Processes a token that is found outside the scope that this test is
175
     * listening to.
176
     *
177
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
178
     * @param int                         $stackPtr  The position in the stack where this
179
     *                                               token was found.
180
     *
181
     * @return void|int Optionally returns a stack pointer. The sniff will not be
182
     *                  called again on the current file until the returned stack
183
     *                  pointer is reached. Return `$phpcsFile->numTokens` to skip
184
     *                  the rest of the file.
185
     */
186
    abstract protected function processTokenOutsideScope(File $phpcsFile, $stackPtr);
187

188

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