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

PHPCSStandards / PHP_CodeSniffer / 17483947696

05 Sep 2025 04:45AM UTC coverage: 78.507% (-0.7%) from 79.184%
17483947696

push

github

web-flow
Merge pull request #1213 from PHPCSStandards/feature/remove-filelist-tests

: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.1
/src/Standards/MySource/Sniffs/Channels/IncludeSystemSniff.php
1
<?php
2
/**
3
 * Ensures that systems, asset types and libs are included before they are used.
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
 * @deprecated 3.9.0
10
 */
11

12
namespace PHP_CodeSniffer\Standards\MySource\Sniffs\Channels;
13

14
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
15
use PHP_CodeSniffer\Sniffs\DeprecatedSniff;
16
use PHP_CodeSniffer\Files\File;
17
use PHP_CodeSniffer\Util\Tokens;
18

19
class IncludeSystemSniff extends AbstractScopeSniff implements DeprecatedSniff
20
{
21

22
    /**
23
     * A list of classes that don't need to be included.
24
     *
25
     * @var array<string, bool>
26
     */
27
    private $ignore = [
28
        'self'                      => true,
29
        'static'                    => true,
30
        'parent'                    => true,
31
        'channels'                  => true,
32
        'basesystem'                => true,
33
        'dal'                       => true,
34
        'init'                      => true,
35
        'pdo'                       => true,
36
        'util'                      => true,
37
        'ziparchive'                => true,
38
        'phpunit_framework_assert'  => true,
39
        'abstractmysourceunittest'  => true,
40
        'abstractdatacleanunittest' => true,
41
        'exception'                 => true,
42
        'abstractwidgetwidgettype'  => true,
43
        'domdocument'               => true,
44
    ];
45

46

47
    /**
48
     * Constructs an AbstractScopeSniff.
49
     */
50
    public function __construct()
3✔
51
    {
52
        parent::__construct([T_FUNCTION], [T_DOUBLE_COLON, T_EXTENDS], true);
3✔
53

54
    }//end __construct()
2✔
55

56

57
    /**
58
     * Processes the function tokens within the class.
59
     *
60
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
61
     * @param integer                     $stackPtr  The position where the token was found.
62
     * @param integer                     $currScope The current scope opener token.
63
     *
64
     * @return void
65
     */
66
    protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope)
3✔
67
    {
68
        $tokens = $phpcsFile->getTokens();
3✔
69

70
        // Determine the name of the class that the static function
71
        // is being called on.
72
        $classNameToken = $phpcsFile->findPrevious(
3✔
73
            T_WHITESPACE,
3✔
74
            ($stackPtr - 1),
3✔
75
            null,
3✔
76
            true
2✔
77
        );
2✔
78

79
        // Don't process class names represented by variables as this can be
80
        // an inexact science.
81
        if ($tokens[$classNameToken]['code'] === T_VARIABLE) {
3✔
82
            return;
×
83
        }
84

85
        $className = $tokens[$classNameToken]['content'];
3✔
86
        if (isset($this->ignore[strtolower($className)]) === true) {
3✔
87
            return;
3✔
88
        }
89

90
        $includedClasses = [];
3✔
91

92
        $fileName = strtolower($phpcsFile->getFilename());
3✔
93
        $matches  = [];
3✔
94
        if (preg_match('|/systems/(.*)/([^/]+)?actions.inc$|', $fileName, $matches) !== 0) {
3✔
95
            // This is an actions file, which means we don't
96
            // have to include the system in which it exists.
97
            $includedClasses[$matches[2]] = true;
×
98

99
            // Or a system it implements.
100
            $class      = $phpcsFile->getCondition($stackPtr, T_CLASS);
×
101
            $implements = $phpcsFile->findNext(T_IMPLEMENTS, $class, ($class + 10));
×
102
            if ($implements !== false) {
×
103
                $implementsClass     = $phpcsFile->findNext(T_STRING, $implements);
×
104
                $implementsClassName = strtolower($tokens[$implementsClass]['content']);
×
105
                if (substr($implementsClassName, -7) === 'actions') {
×
106
                    $includedClasses[substr($implementsClassName, 0, -7)] = true;
×
107
                }
108
            }
109
        }
110

111
        // Go searching for includeSystem and includeAsset calls within this
112
        // function, or the inclusion of .inc files, which
113
        // would be library files.
114
        for ($i = ($currScope + 1); $i < $stackPtr; $i++) {
3✔
115
            $name = $this->getIncludedClassFromToken($phpcsFile, $tokens, $i);
3✔
116
            if ($name !== false) {
3✔
117
                $includedClasses[$name] = true;
3✔
118
                // Special case for Widgets cause they are, well, special.
119
            } else if (strtolower($tokens[$i]['content']) === 'includewidget') {
3✔
120
                $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
3✔
121
                $typeName = trim($tokens[$typeName]['content'], " '");
3✔
122
                $includedClasses[strtolower($typeName).'widgettype'] = true;
3✔
123
            }
1✔
124
        }
1✔
125

126
        // Now go searching for includeSystem, includeAsset or require/include
127
        // calls outside our scope. If we are in a class, look outside the
128
        // class. If we are not, look outside the function.
129
        $condPtr = $currScope;
3✔
130
        if ($phpcsFile->hasCondition($stackPtr, T_CLASS) === true) {
3✔
131
            foreach ($tokens[$stackPtr]['conditions'] as $condPtr => $condType) {
3✔
132
                if ($condType === T_CLASS) {
3✔
133
                    break;
3✔
134
                }
135
            }
1✔
136
        }
1✔
137

138
        for ($i = 0; $i < $condPtr; $i++) {
3✔
139
            // Skip other scopes.
140
            if (isset($tokens[$i]['scope_closer']) === true) {
3✔
141
                $i = $tokens[$i]['scope_closer'];
3✔
142
                continue;
3✔
143
            }
144

145
            $name = $this->getIncludedClassFromToken($phpcsFile, $tokens, $i);
3✔
146
            if ($name !== false) {
3✔
147
                $includedClasses[$name] = true;
3✔
148
            }
1✔
149
        }
1✔
150

151
        // If we are in a testing class, we might have also included
152
        // some systems and classes in our setUp() method.
153
        $setupFunction = null;
3✔
154
        if ($phpcsFile->hasCondition($stackPtr, T_CLASS) === true) {
3✔
155
            foreach ($tokens[$stackPtr]['conditions'] as $condPtr => $condType) {
3✔
156
                if ($condType === T_CLASS) {
3✔
157
                    // Is this is a testing class?
158
                    $name = $phpcsFile->findNext(T_STRING, $condPtr);
3✔
159
                    $name = $tokens[$name]['content'];
3✔
160
                    if (substr($name, -8) === 'UnitTest') {
3✔
161
                        // Look for a method called setUp().
162
                        $end      = $tokens[$condPtr]['scope_closer'];
3✔
163
                        $function = $phpcsFile->findNext(T_FUNCTION, ($condPtr + 1), $end);
3✔
164
                        while ($function !== false) {
3✔
165
                            $name = $phpcsFile->findNext(T_STRING, $function);
3✔
166
                            if ($tokens[$name]['content'] === 'setUp') {
3✔
167
                                $setupFunction = $function;
3✔
168
                                break;
3✔
169
                            }
170

171
                            $function = $phpcsFile->findNext(T_FUNCTION, ($function + 1), $end);
×
172
                        }
173
                    }
1✔
174
                }
1✔
175
            }//end foreach
1✔
176
        }//end if
1✔
177

178
        if ($setupFunction !== null) {
3✔
179
            $start = ($tokens[$setupFunction]['scope_opener'] + 1);
3✔
180
            $end   = $tokens[$setupFunction]['scope_closer'];
3✔
181
            for ($i = $start; $i < $end; $i++) {
3✔
182
                $name = $this->getIncludedClassFromToken($phpcsFile, $tokens, $i);
3✔
183
                if ($name !== false) {
3✔
184
                    $includedClasses[$name] = true;
3✔
185
                }
1✔
186
            }
1✔
187
        }//end if
1✔
188

189
        if (isset($includedClasses[strtolower($className)]) === false) {
3✔
190
            $error = 'Static method called on non-included class or system "%s"; include system with Channels::includeSystem() or include class with require_once';
3✔
191
            $data  = [$className];
3✔
192
            $phpcsFile->addError($error, $stackPtr, 'NotIncludedCall', $data);
3✔
193
        }
1✔
194

195
    }//end processTokenWithinScope()
2✔
196

197

198
    /**
199
     * Processes a token within the scope that this test is listening to.
200
     *
201
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found.
202
     * @param int                         $stackPtr  The position in the stack where
203
     *                                               this token was found.
204
     *
205
     * @return void
206
     */
207
    protected function processTokenOutsideScope(File $phpcsFile, $stackPtr)
3✔
208
    {
209
        $tokens = $phpcsFile->getTokens();
3✔
210

211
        if ($tokens[$stackPtr]['code'] === T_EXTENDS) {
3✔
212
            // Find the class name.
213
            $classNameToken = $phpcsFile->findNext(T_STRING, ($stackPtr + 1));
3✔
214
            $className      = $tokens[$classNameToken]['content'];
3✔
215
        } else {
1✔
216
            // Determine the name of the class that the static function
217
            // is being called on. But don't process class names represented by
218
            // variables as this can be an inexact science.
219
            $classNameToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true);
3✔
220
            if ($tokens[$classNameToken]['code'] === T_VARIABLE) {
3✔
221
                return;
3✔
222
            }
223

224
            $className = $tokens[$classNameToken]['content'];
3✔
225
        }
226

227
        // Some systems are always available.
228
        if (isset($this->ignore[strtolower($className)]) === true) {
3✔
229
            return;
3✔
230
        }
231

232
        $includedClasses = [];
3✔
233

234
        $fileName = strtolower($phpcsFile->getFilename());
3✔
235
        $matches  = [];
3✔
236
        if (preg_match('|/systems/([^/]+)/([^/]+)?actions.inc$|', $fileName, $matches) !== 0) {
3✔
237
            // This is an actions file, which means we don't
238
            // have to include the system in which it exists
239
            // We know the system from the path.
240
            $includedClasses[$matches[1]] = true;
×
241
        }
242

243
        // Go searching for includeSystem, includeAsset or require/include
244
        // calls outside our scope.
245
        for ($i = 0; $i < $stackPtr; $i++) {
3✔
246
            // Skip classes and functions as will we never get
247
            // into their scopes when including this file, although
248
            // we have a chance of getting into IF, WHILE etc.
249
            if (($tokens[$i]['code'] === T_CLASS
3✔
250
                || $tokens[$i]['code'] === T_INTERFACE
3✔
251
                || $tokens[$i]['code'] === T_FUNCTION)
3✔
252
                && isset($tokens[$i]['scope_closer']) === true
3✔
253
            ) {
1✔
254
                $i = $tokens[$i]['scope_closer'];
3✔
255
                continue;
3✔
256
            }
257

258
            $name = $this->getIncludedClassFromToken($phpcsFile, $tokens, $i);
3✔
259
            if ($name !== false) {
3✔
260
                $includedClasses[$name] = true;
3✔
261
                // Special case for Widgets cause they are, well, special.
262
            } else if (strtolower($tokens[$i]['content']) === 'includewidget') {
3✔
263
                $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($i + 1));
3✔
264
                $typeName = trim($tokens[$typeName]['content'], " '");
3✔
265
                $includedClasses[strtolower($typeName).'widgettype'] = true;
3✔
266
            }
1✔
267
        }//end for
1✔
268

269
        if (isset($includedClasses[strtolower($className)]) === false) {
3✔
270
            if ($tokens[$stackPtr]['code'] === T_EXTENDS) {
3✔
271
                $error = 'Class extends non-included class or system "%s"; include system with Channels::includeSystem() or include class with require_once';
3✔
272
                $data  = [$className];
3✔
273
                $phpcsFile->addError($error, $stackPtr, 'NotIncludedExtends', $data);
3✔
274
            } else {
1✔
275
                $error = 'Static method called on non-included class or system "%s"; include system with Channels::includeSystem() or include class with require_once';
3✔
276
                $data  = [$className];
3✔
277
                $phpcsFile->addError($error, $stackPtr, 'NotIncludedCall', $data);
3✔
278
            }
279
        }
1✔
280

281
    }//end processTokenOutsideScope()
2✔
282

283

284
    /**
285
     * Determines the included class name from given token.
286
     *
287
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
288
     * @param array                       $tokens    The array of file tokens.
289
     * @param int                         $stackPtr  The position in the tokens array of the
290
     *                                               potentially included class.
291
     *
292
     * @return string|false
293
     */
294
    protected function getIncludedClassFromToken(File $phpcsFile, array $tokens, $stackPtr)
3✔
295
    {
296
        if (strtolower($tokens[$stackPtr]['content']) === 'includesystem') {
3✔
297
            $systemName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 1));
3✔
298
            $systemName = trim($tokens[$systemName]['content'], " '");
3✔
299
            return strtolower($systemName);
3✔
300
        } else if (strtolower($tokens[$stackPtr]['content']) === 'includeasset') {
3✔
301
            $typeName = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 1));
3✔
302
            $typeName = trim($tokens[$typeName]['content'], " '");
3✔
303
            return strtolower($typeName).'assettype';
3✔
304
        } else if (isset(Tokens::$includeTokens[$tokens[$stackPtr]['code']]) === true) {
3✔
305
            $filePath = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, ($stackPtr + 1));
3✔
306
            $filePath = $tokens[$filePath]['content'];
3✔
307
            $filePath = trim($filePath, " '");
3✔
308
            $filePath = basename($filePath, '.inc');
3✔
309
            return strtolower($filePath);
3✔
310
        }
311

312
        return false;
3✔
313

314
    }//end getIncludedClassFromToken()
315

316

317
    /**
318
     * Provide the version number in which the sniff was deprecated.
319
     *
320
     * @return string
321
     */
322
    public function getDeprecationVersion()
×
323
    {
324
        return 'v3.9.0';
×
325

326
    }//end getDeprecationVersion()
327

328

329
    /**
330
     * Provide the version number in which the sniff will be removed.
331
     *
332
     * @return string
333
     */
334
    public function getRemovalVersion()
×
335
    {
336
        return 'v4.0.0';
×
337

338
    }//end getRemovalVersion()
339

340

341
    /**
342
     * Provide a custom message to display with the deprecation.
343
     *
344
     * @return string
345
     */
346
    public function getDeprecationMessage()
×
347
    {
348
        return 'The MySource standard will be removed completely in v4.0.0.';
×
349

350
    }//end getDeprecationMessage()
351

352

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