| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Ensures that systems, asset types and libs are included before they are used.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer_MySource
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
11 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
12 |
* @version CVS: $Id: UnusedSystemSniff.php 252280 2008-02-05 00:35:03Z squiz $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Ensures that systems and asset types are used if they are included.
|
|
|
18 |
*
|
|
|
19 |
* @category PHP
|
|
|
20 |
* @package PHP_CodeSniffer_MySource
|
|
|
21 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
22 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
23 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
24 |
* @version Release: 1.2.1
|
|
|
25 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
26 |
*/
|
|
|
27 |
class MySource_Sniffs_Channels_UnusedSystemSniff implements PHP_CodeSniffer_Sniff
|
|
|
28 |
{
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Returns an array of tokens this test wants to listen for.
|
|
|
32 |
*
|
|
|
33 |
* @return array
|
|
|
34 |
*/
|
|
|
35 |
public function register()
|
|
|
36 |
{
|
|
|
37 |
return array(T_DOUBLE_COLON);
|
|
|
38 |
|
|
|
39 |
}//end register()
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Processes this sniff, when one of its tokens is encountered.
|
|
|
44 |
*
|
|
|
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.
|
|
|
48 |
*
|
|
|
49 |
* @return void
|
|
|
50 |
*/
|
|
|
51 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
52 |
{
|
|
|
53 |
$tokens = $phpcsFile->getTokens();
|
|
|
54 |
|
|
|
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.
|
|
|
62 |
return;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
$systemName = trim($tokens[$systemName]['content'], " '");
|
|
|
66 |
} else {
|
|
|
67 |
return;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
if ($methodName === 'includeasset') {
|
|
|
71 |
$systemName .= 'assettype';
|
|
|
72 |
} else if ($methodName === 'includewidget') {
|
|
|
73 |
$systemName .= 'widgettype';
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
$systemName = strtolower($systemName);
|
|
|
77 |
|
|
|
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 |
break;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
$validTokens = array(T_DOUBLE_COLON, T_EXTENDS, T_IMPLEMENTS);
|
|
|
87 |
if (in_array($tokens[$i]['code'], $validTokens) === false) {
|
|
|
88 |
continue;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
switch ($tokens[$i]['code']) {
|
|
|
92 |
case T_DOUBLE_COLON:
|
|
|
93 |
$usedName = strtolower($tokens[($i - 1)]['content']);
|
|
|
94 |
if ($usedName === $systemName) {
|
|
|
95 |
// The included system was used, so it is fine.
|
|
|
96 |
return;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
break;
|
|
|
100 |
case T_EXTENDS:
|
|
|
101 |
$classNameToken = $phpcsFile->findNext(T_STRING, ($i + 1));
|
|
|
102 |
$className = strtolower($tokens[$classNameToken]['content']);
|
|
|
103 |
if ($className === $systemName) {
|
|
|
104 |
// The included system was used, so it is fine.
|
|
|
105 |
return;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
break;
|
|
|
109 |
case T_IMPLEMENTS:
|
|
|
110 |
$endImplements = $phpcsFile->findNext(array(T_EXTENDS, T_OPEN_CURLY_BRACKET), ($i + 1));
|
|
|
111 |
for ($x = ($i + 1); $x < $endImplements; $x++) {
|
|
|
112 |
if ($tokens[$x]['code'] === T_STRING) {
|
|
|
113 |
$className = strtolower($tokens[$x]['content']);
|
|
|
114 |
if ($className === $systemName) {
|
|
|
115 |
// The included system was used, so it is fine.
|
|
|
116 |
return;
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
break;
|
|
|
122 |
}//end switch
|
|
|
123 |
}//end for
|
|
|
124 |
|
|
|
125 |
// If we get to here, the system was not use.
|
|
|
126 |
$error = "Included system \"$systemName\" is never used";
|
|
|
127 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
128 |
|
|
|
129 |
}//end process()
|
|
|
130 |
|
|
|
131 |
|
|
|
132 |
}//end class
|
|
|
133 |
|
|
|
134 |
?>
|