3
* Ensures that systems, asset types and libs are included before they are used.
8
* @package PHP_CodeSniffer_MySource
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* Ensures that systems and asset types are used if they are included.
19
* @package PHP_CodeSniffer_MySource
20
* @author Greg Sherwood <gsherwood@squiz.net>
21
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
22
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
23
* @version Release: 1.5.3
24
* @link http://pear.php.net/package/PHP_CodeSniffer
26
class MySource_Sniffs_Channels_UnusedSystemSniff implements PHP_CodeSniffer_Sniff
31
* Returns an array of tokens this test wants to listen for.
35
public function register()
37
return array(T_DOUBLE_COLON);
43
* Processes this sniff, when one of its tokens is encountered.
45
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
46
* @param int $stackPtr The position of the current token in
47
* the stack passed in $tokens.
51
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
53
$tokens = $phpcsFile->getTokens();
55
// Check if this is a call to includeSystem, includeAsset or includeWidget.
56
$methodName = strtolower($tokens[($stackPtr + 1)]['content']);
57
if (in_array($methodName, array('includesystem', 'includeasset', 'includewidget')) === true) {
58
$systemName = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 3), null, true);
59
if ($systemName === false || $tokens[$systemName]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
60
// Must be using a variable instead of a specific system name.
61
// We can't accurately check that.
65
$systemName = trim($tokens[$systemName]['content'], " '");
70
if ($methodName === 'includeasset') {
71
$systemName .= 'assettype';
72
} else if ($methodName === 'includewidget') {
73
$systemName .= 'widgettype';
76
$systemName = strtolower($systemName);
78
// Now check if this system is used anywhere in this scope.
79
$level = $tokens[$stackPtr]['level'];
80
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
81
if ($tokens[$i]['level'] < $level) {
82
// We have gone out of scope.
83
// If the original include was inside an IF statement that
84
// is checking if the system exists, check the outer scope
86
if ($tokens[$stackPtr]['level'] === $level) {
87
// We are still in the base level, so this is the first
88
// time we have got here.
89
$conditions = array_keys($tokens[$stackPtr]['conditions']);
90
if (empty($conditions) === false) {
91
$cond = array_pop($conditions);
92
if ($tokens[$cond]['code'] === T_IF) {
93
$i = $tokens[$cond]['scope_closer'];
103
$validTokens = array(
109
if (in_array($tokens[$i]['code'], $validTokens) === false) {
113
switch ($tokens[$i]['code']) {
115
$usedName = strtolower($tokens[($i - 1)]['content']);
116
if ($usedName === $systemName) {
117
// The included system was used, so it is fine.
123
$classNameToken = $phpcsFile->findNext(T_STRING, ($i + 1));
124
$className = strtolower($tokens[$classNameToken]['content']);
125
if ($className === $systemName) {
126
// The included system was used, so it is fine.
132
$endImplements = $phpcsFile->findNext(array(T_EXTENDS, T_OPEN_CURLY_BRACKET), ($i + 1));
133
for ($x = ($i + 1); $x < $endImplements; $x++) {
134
if ($tokens[$x]['code'] === T_STRING) {
135
$className = strtolower($tokens[$x]['content']);
136
if ($className === $systemName) {
137
// The included system was used, so it is fine.
147
// If we get to here, the system was not use.
148
$error = 'Included system "%s" is never used';
149
$data = array($systemName);
150
$phpcsFile->addError($error, $stackPtr, 'Found', $data);