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

PHPCSStandards / PHP_CodeSniffer / 18114150721

30 Sep 2025 12:00AM UTC coverage: 78.601% (-0.6%) from 79.178%
18114150721

push

github

web-flow
Merge pull request #1284 from PHPCSStandards/dependabot/github_actions/3.x/action-runners-f175f89d6c

GH Actions: Bump actions/cache from 4.2.4 to 4.3.0 in the action-runners group

25341 of 32240 relevant lines covered (78.6%)

74.7 hits per line

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

91.89
/src/Standards/MySource/Sniffs/Channels/UnusedSystemSniff.php
1
<?php
2
/**
3
 * Ensures that systems and asset types are used if they are included.
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/HEAD/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\DeprecatedSniff;
15
use PHP_CodeSniffer\Sniffs\Sniff;
16
use PHP_CodeSniffer\Files\File;
17

18
class UnusedSystemSniff implements Sniff, DeprecatedSniff
19
{
20

21

22
    /**
23
     * Returns an array of tokens this test wants to listen for.
24
     *
25
     * @return array<int|string>
26
     */
27
    public function register()
3✔
28
    {
29
        return [T_DOUBLE_COLON];
3✔
30

31
    }//end register()
32

33

34
    /**
35
     * Processes this sniff, when one of its tokens is encountered.
36
     *
37
     * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
38
     * @param int                         $stackPtr  The position of the current token in
39
     *                                               the stack passed in $tokens.
40
     *
41
     * @return void
42
     */
43
    public function process(File $phpcsFile, $stackPtr)
3✔
44
    {
45
        $tokens = $phpcsFile->getTokens();
3✔
46

47
        // Check if this is a call to includeSystem, includeAsset or includeWidget.
48
        $methodName = strtolower($tokens[($stackPtr + 1)]['content']);
3✔
49
        if ($methodName === 'includesystem'
2✔
50
            || $methodName === 'includeasset'
3✔
51
            || $methodName === 'includewidget'
3✔
52
        ) {
1✔
53
            $systemName = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 3), null, true);
3✔
54
            if ($systemName === false || $tokens[$systemName]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
3✔
55
                // Must be using a variable instead of a specific system name.
56
                // We can't accurately check that.
57
                return;
3✔
58
            }
59

60
            $systemName = trim($tokens[$systemName]['content'], " '");
3✔
61
        } else {
1✔
62
            return;
3✔
63
        }
64

65
        if ($methodName === 'includeasset') {
3✔
66
            $systemName .= 'assettype';
3✔
67
        } else if ($methodName === 'includewidget') {
3✔
68
            $systemName .= 'widgettype';
3✔
69
        }
1✔
70

71
        $systemName = strtolower($systemName);
3✔
72

73
        // Now check if this system is used anywhere in this scope.
74
        $level = $tokens[$stackPtr]['level'];
3✔
75
        for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
3✔
76
            if ($tokens[$i]['level'] < $level) {
3✔
77
                // We have gone out of scope.
78
                // If the original include was inside an IF statement that
79
                // is checking if the system exists, check the outer scope
80
                // as well.
81
                if ($tokens[$stackPtr]['level'] === $level) {
3✔
82
                    // We are still in the base level, so this is the first
83
                    // time we have got here.
84
                    $conditions = array_keys($tokens[$stackPtr]['conditions']);
3✔
85
                    if (empty($conditions) === false) {
3✔
86
                        $cond = array_pop($conditions);
3✔
87
                        if ($tokens[$cond]['code'] === T_IF) {
3✔
88
                            $i = $tokens[$cond]['scope_closer'];
3✔
89
                            $level--;
3✔
90
                            continue;
3✔
91
                        }
92
                    }
1✔
93
                }
1✔
94

95
                break;
3✔
96
            }//end if
97

98
            if ($tokens[$i]['code'] !== T_DOUBLE_COLON
3✔
99
                && $tokens[$i]['code'] !== T_EXTENDS
3✔
100
                && $tokens[$i]['code'] !== T_IMPLEMENTS
3✔
101
            ) {
1✔
102
                continue;
3✔
103
            }
104

105
            switch ($tokens[$i]['code']) {
3✔
106
            case T_DOUBLE_COLON:
3✔
107
                $usedName = strtolower($tokens[($i - 1)]['content']);
3✔
108
                if ($usedName === $systemName) {
3✔
109
                    // The included system was used, so it is fine.
110
                    return;
3✔
111
                }
112
                break;
3✔
113
            case T_EXTENDS:
3✔
114
                $classNameToken = $phpcsFile->findNext(T_STRING, ($i + 1));
3✔
115
                $className      = strtolower($tokens[$classNameToken]['content']);
3✔
116
                if ($className === $systemName) {
3✔
117
                    // The included system was used, so it is fine.
118
                    return;
3✔
119
                }
120
                break;
3✔
121
            case T_IMPLEMENTS:
3✔
122
                $endImplements = $phpcsFile->findNext([T_EXTENDS, T_OPEN_CURLY_BRACKET], ($i + 1));
3✔
123
                for ($x = ($i + 1); $x < $endImplements; $x++) {
3✔
124
                    if ($tokens[$x]['code'] === T_STRING) {
3✔
125
                        $className = strtolower($tokens[$x]['content']);
3✔
126
                        if ($className === $systemName) {
3✔
127
                            // The included system was used, so it is fine.
128
                            return;
3✔
129
                        }
130
                    }
1✔
131
                }
1✔
132
                break;
3✔
133
            }//end switch
1✔
134
        }//end for
1✔
135

136
        // If we get to here, the system was not use.
137
        $error = 'Included system "%s" is never used';
3✔
138
        $data  = [$systemName];
3✔
139
        $phpcsFile->addError($error, $stackPtr, 'Found', $data);
3✔
140

141
    }//end process()
2✔
142

143

144
    /**
145
     * Provide the version number in which the sniff was deprecated.
146
     *
147
     * @return string
148
     */
149
    public function getDeprecationVersion()
×
150
    {
151
        return 'v3.9.0';
×
152

153
    }//end getDeprecationVersion()
154

155

156
    /**
157
     * Provide the version number in which the sniff will be removed.
158
     *
159
     * @return string
160
     */
161
    public function getRemovalVersion()
×
162
    {
163
        return 'v4.0.0';
×
164

165
    }//end getRemovalVersion()
166

167

168
    /**
169
     * Provide a custom message to display with the deprecation.
170
     *
171
     * @return string
172
     */
173
    public function getDeprecationMessage()
×
174
    {
175
        return 'The MySource standard will be removed completely in v4.0.0.';
×
176

177
    }//end getDeprecationMessage()
178

179

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