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
|
<?php
/**
* PEAR_Sniffs_Files_IncludingFileSniff.
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@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
*/
/**
* PEAR_Sniffs_Files_IncludingFileSniff.
*
* Checks that the include_once is used in conditional situations, and
* require_once is used elsewhere. Also checks that brackets do not surround
* the file being included.
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@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 PEAR_Sniffs_Files_IncludingFileSniff implements PHP_CodeSniffer_Sniff
{
/**
* Conditions that should use include_once
*
* @var array(int)
*/
private static $_conditions = array(
T_IF,
T_ELSE,
T_ELSEIF,
T_SWITCH,
);
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
*/
public function register()
{
return array(
T_INCLUDE_ONCE,
T_REQUIRE_ONCE,
T_REQUIRE,
T_INCLUDE,
);
}//end register()
/**
* Processes this test, 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();
$nextToken = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true);
if ($tokens[$nextToken]['code'] === T_OPEN_PARENTHESIS) {
$error = '"%s" is a statement not a function; no parentheses are required';
$data = array($tokens[$stackPtr]['content']);
$phpcsFile->addError($error, $stackPtr, 'BracketsNotRequired', $data);
}
$inCondition = (count($tokens[$stackPtr]['conditions']) !== 0) ? true : false;
// Check to see if this including statement is within the parenthesis
// of a condition. If that's the case then we need to process it as being
// within a condition, as they are checking the return value.
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) {
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $left => $right) {
if (isset($tokens[$left]['parenthesis_owner']) === true) {
$inCondition = true;
}
}
}
// Check to see if they are assigning the return value of this
// including call. If they are then they are probably checking it, so
// it's conditional.
$previous = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if (in_array($tokens[$previous]['code'], PHP_CodeSniffer_Tokens::$assignmentTokens) === true) {
// The have assigned the return value to it, so its conditional.
$inCondition = true;
}
$tokenCode = $tokens[$stackPtr]['code'];
if ($inCondition === true) {
// We are inside a conditional statement. We need an include_once.
if ($tokenCode === T_REQUIRE_ONCE) {
$error = 'File is being conditionally included; ';
$error .= 'use "include_once" instead';
$phpcsFile->addError($error, $stackPtr, 'UseIncludeOnce');
} else if ($tokenCode === T_REQUIRE) {
$error = 'File is being conditionally included; ';
$error .= 'use "include" instead';
$phpcsFile->addError($error, $stackPtr, 'UseInclude');
}
} else {
// We are unconditionally including, we need a require_once.
if ($tokenCode === T_INCLUDE_ONCE) {
$error = 'File is being unconditionally included; ';
$error .= 'use "require_once" instead';
$phpcsFile->addError($error, $stackPtr, 'UseRequireOnce');
} else if ($tokenCode === T_INCLUDE) {
$error = 'File is being unconditionally included; ';
$error .= 'use "require" instead';
$phpcsFile->addError($error, $stackPtr, 'UseRequire');
}
}//end if
}//end process()
}//end class
?>
|