1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
<?php
/**
* Ensures that systems, asset types and libs are included before they are used.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer_MySource
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
/**
* Ensures that systems and asset types are used if they are included.
*
* @category PHP
* @package PHP_CodeSniffer_MySource
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
* @version Release: 1.3.4
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class MySource_Sniffs_Channels_UnusedSystemSniff implements PHP_CodeSniffer_Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(T_DOUBLE_COLON);
}//end register()
/**
* Processes this sniff, when one of its tokens is encountered.
*
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in
* the stack passed in $tokens.
*
* @return void
*/
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
// Check if this is a call to includeSystem, includeAsset or includeWidget.
$methodName = strtolower($tokens[($stackPtr + 1)]['content']);
if (in_array($methodName, array('includesystem', 'includeasset', 'includewidget')) === true) {
$systemName = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 3), null, true);
if ($systemName === false || $tokens[$systemName]['code'] !== T_CONSTANT_ENCAPSED_STRING) {
// Must be using a variable instead of a specific system name.
// We can't accurately check that.
return;
}
$systemName = trim($tokens[$systemName]['content'], " '");
} else {
return;
}
if ($methodName === 'includeasset') {
$systemName .= 'assettype';
} else if ($methodName === 'includewidget') {
$systemName .= 'widgettype';
}
$systemName = strtolower($systemName);
// Now check if this system is used anywhere in this scope.
$level = $tokens[$stackPtr]['level'];
for ($i = ($stackPtr + 1); $i < $phpcsFile->numTokens; $i++) {
if ($tokens[$i]['level'] < $level) {
// We have gone out of scope.
// If the original include was inside an IF statement that
// is checking if the system exists, check the outer scope
// as well.
if ($tokens[$stackPtr]['level'] === $level) {
// We are still in the base level, so this is the first
// time we have got here.
$conditions = array_keys($tokens[$stackPtr]['conditions']);
if (empty($conditions) === false) {
$cond = array_pop($conditions);
if ($tokens[$cond]['code'] === T_IF) {
$i = $tokens[$cond]['scope_closer'];
$level--;
continue;
}
}
}
break;
}//end if
$validTokens = array(
T_DOUBLE_COLON,
T_EXTENDS,
T_IMPLEMENTS,
);
if (in_array($tokens[$i]['code'], $validTokens) === false) {
continue;
}
switch ($tokens[$i]['code']) {
case T_DOUBLE_COLON:
$usedName = strtolower($tokens[($i - 1)]['content']);
if ($usedName === $systemName) {
// The included system was used, so it is fine.
return;
}
break;
case T_EXTENDS:
$classNameToken = $phpcsFile->findNext(T_STRING, ($i + 1));
$className = strtolower($tokens[$classNameToken]['content']);
if ($className === $systemName) {
// The included system was used, so it is fine.
return;
}
break;
case T_IMPLEMENTS:
$endImplements = $phpcsFile->findNext(array(T_EXTENDS, T_OPEN_CURLY_BRACKET), ($i + 1));
for ($x = ($i + 1); $x < $endImplements; $x++) {
if ($tokens[$x]['code'] === T_STRING) {
$className = strtolower($tokens[$x]['content']);
if ($className === $systemName) {
// The included system was used, so it is fine.
return;
}
}
}
break;
}//end switch
}//end for
// If we get to here, the system was not use.
$error = 'Included system "%s" is never used';
$data = array($systemName);
$phpcsFile->addError($error, $stackPtr, 'Found', $data);
}//end process()
}//end class
?>
|