Initial Commit
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* This sniff class detected empty statement.
|
||||
*
|
||||
* This sniff implements the common algorithm for empty statement body detection.
|
||||
* A body is considered as empty if it is completely empty or it only contains
|
||||
* whitespace characters and|or comments.
|
||||
*
|
||||
* <code>
|
||||
* stmt {
|
||||
* // foo
|
||||
* }
|
||||
* stmt (conditions) {
|
||||
* // foo
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* Statements covered by this sniff are <b>catch</b>, <b>do</b>, <b>else</b>,
|
||||
* <b>elsif</b>, <b>for</b>, <b>foreach<b>, <b>if</b>, <b>switch</b>, <b>try</b>
|
||||
* and <b>while</b>.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @version Release: 1.3.3
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class Generic_Sniffs_CodeAnalysis_EmptyStatementSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
/**
|
||||
* List of block tokens that this sniff covers.
|
||||
*
|
||||
* The key of this hash identifies the required token while the boolean
|
||||
* value says mark an error or mark a warning.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $checkedTokens = array(
|
||||
T_CATCH => true,
|
||||
T_DO => false,
|
||||
T_ELSE => false,
|
||||
T_ELSEIF => false,
|
||||
T_FOR => false,
|
||||
T_FOREACH => false,
|
||||
T_IF => false,
|
||||
T_SWITCH => false,
|
||||
T_TRY => false,
|
||||
T_WHILE => false,
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* Registers the tokens that this sniff wants to listen for.
|
||||
*
|
||||
* @return array(integer)
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array_keys($this->checkedTokens);
|
||||
|
||||
}//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();
|
||||
$token = $tokens[$stackPtr];
|
||||
|
||||
// Skip for-statements without body.
|
||||
if (isset($token['scope_opener']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$next = ++$token['scope_opener'];
|
||||
$end = --$token['scope_closer'];
|
||||
|
||||
$emptyBody = true;
|
||||
for (; $next <= $end; ++$next) {
|
||||
if (in_array($tokens[$next]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
|
||||
$emptyBody = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($emptyBody === true) {
|
||||
// Get token identifier.
|
||||
$name = $phpcsFile->getTokensAsString($stackPtr, 1);
|
||||
$error = 'Empty %s statement detected';
|
||||
$data = array(strtoupper($name));
|
||||
if ($this->checkedTokens[$token['code']] === true) {
|
||||
$phpcsFile->addError($error, $stackPtr, 'NotAllowed', $data);
|
||||
} else {
|
||||
$phpcsFile->addWarning($error, $stackPtr, 'NotAllowedWarning', $data);
|
||||
}
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Detects for-loops that can be simplified to a while-loop.
|
||||
*
|
||||
* This rule is based on the PMD rule catalog. Detects for-loops that can be
|
||||
* simplified as a while-loop.
|
||||
*
|
||||
* <code>
|
||||
* class Foo
|
||||
* {
|
||||
* public function bar($x)
|
||||
* {
|
||||
* for (;true;) true; // No Init or Update part, may as well be: while (true)
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @version Release: 1.3.3
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class Generic_Sniffs_CodeAnalysis_ForLoopShouldBeWhileLoopSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Registers the tokens that this sniff wants to listen for.
|
||||
*
|
||||
* @return array(integer)
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_FOR);
|
||||
|
||||
}//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();
|
||||
$token = $tokens[$stackPtr];
|
||||
|
||||
// Skip invalid statement.
|
||||
if (isset($token['parenthesis_opener']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$next = ++$token['parenthesis_opener'];
|
||||
$end = --$token['parenthesis_closer'];
|
||||
|
||||
$parts = array(0, 0, 0);
|
||||
$index = 0;
|
||||
|
||||
for (; $next <= $end; ++$next) {
|
||||
$code = $tokens[$next]['code'];
|
||||
if ($code === T_SEMICOLON) {
|
||||
++$index;
|
||||
} else if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
|
||||
++$parts[$index];
|
||||
}
|
||||
}
|
||||
|
||||
if ($parts[0] === 0 && $parts[2] === 0 && $parts[1] > 0) {
|
||||
$error = 'This FOR loop can be simplified to a WHILE loop';
|
||||
$phpcsFile->addWarning($error, $stackPtr, 'CanSimplify');
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Detects for-loops that use a function call in the test expression.
|
||||
*
|
||||
* This rule is based on the PMD rule catalog. Detects for-loops that use a
|
||||
* function call in the test expression.
|
||||
*
|
||||
* <code>
|
||||
* class Foo
|
||||
* {
|
||||
* public function bar($x)
|
||||
* {
|
||||
* $a = array(1, 2, 3, 4);
|
||||
* for ($i = 0; $i < count($a); $i++) {
|
||||
* $a[$i] *= $i;
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @version Release: 1.3.3
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class Generic_Sniffs_CodeAnalysis_ForLoopWithTestFunctionCallSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Registers the tokens that this sniff wants to listen for.
|
||||
*
|
||||
* @return array(integer)
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_FOR);
|
||||
|
||||
}//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();
|
||||
$token = $tokens[$stackPtr];
|
||||
|
||||
// Skip invalid statement.
|
||||
if (isset($token['parenthesis_opener']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$next = ++$token['parenthesis_opener'];
|
||||
$end = --$token['parenthesis_closer'];
|
||||
|
||||
$position = 0;
|
||||
|
||||
for (; $next <= $end; ++$next) {
|
||||
$code = $tokens[$next]['code'];
|
||||
if ($code === T_SEMICOLON) {
|
||||
++$position;
|
||||
}
|
||||
|
||||
if ($position < 1) {
|
||||
continue;
|
||||
} else if ($position > 1) {
|
||||
break;
|
||||
} else if ($code !== T_VARIABLE && $code !== T_STRING) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find next non empty token, if it is a open curly brace we have a
|
||||
// function call.
|
||||
$index = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
|
||||
|
||||
if ($tokens[$index]['code'] === T_OPEN_PARENTHESIS) {
|
||||
$error = 'Avoid function calls in a FOR loop test part';
|
||||
$phpcsFile->addWarning($error, $stackPtr, 'NotAllowed');
|
||||
break;
|
||||
}
|
||||
}//end for
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Detects incrementer jumbling in for loops.
|
||||
*
|
||||
* This rule is based on the PMD rule catalog. The jumbling incrementer sniff
|
||||
* detects the usage of one and the same incrementer into an outer and an inner
|
||||
* loop. Even it is intended this is confusing code.
|
||||
*
|
||||
* <code>
|
||||
* class Foo
|
||||
* {
|
||||
* public function bar($x)
|
||||
* {
|
||||
* for ($i = 0; $i < 10; $i++)
|
||||
* {
|
||||
* for ($k = 0; $k < 20; $i++)
|
||||
* {
|
||||
* echo 'Hello';
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @version Release: 1.3.3
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class Generic_Sniffs_CodeAnalysis_JumbledIncrementerSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Registers the tokens that this sniff wants to listen for.
|
||||
*
|
||||
* @return array(integer)
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_FOR);
|
||||
|
||||
}//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();
|
||||
$token = $tokens[$stackPtr];
|
||||
|
||||
// Skip for-loop without body.
|
||||
if (isset($token['scope_opener']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find incrementors for outer loop.
|
||||
$outer = $this->findIncrementers($tokens, $token);
|
||||
|
||||
// Skip if empty.
|
||||
if (count($outer) === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find nested for loops.
|
||||
$start = ++$token['scope_opener'];
|
||||
$end = --$token['scope_closer'];
|
||||
|
||||
for (; $start <= $end; ++$start) {
|
||||
if ($tokens[$start]['code'] !== T_FOR) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$inner = $this->findIncrementers($tokens, $tokens[$start]);
|
||||
$diff = array_intersect($outer, $inner);
|
||||
|
||||
if (count($diff) !== 0) {
|
||||
$error = 'Loop incrementor (%s) jumbling with inner loop';
|
||||
$data = array(join(', ', $diff));
|
||||
$phpcsFile->addWarning($error, $stackPtr, 'Found', $data);
|
||||
}
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
/**
|
||||
* Get all used variables in the incrementer part of a for statement.
|
||||
*
|
||||
* @param array(integer=>array) $tokens Array with all code sniffer tokens.
|
||||
* @param array(string=>mixed) $token Current for loop token
|
||||
*
|
||||
* @return array(string) List of all found incrementer variables.
|
||||
*/
|
||||
protected function findIncrementers(array $tokens, array $token)
|
||||
{
|
||||
// Skip invalid statement.
|
||||
if (isset($token['parenthesis_opener']) === false) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$start = ++$token['parenthesis_opener'];
|
||||
$end = --$token['parenthesis_closer'];
|
||||
|
||||
$incrementers = array();
|
||||
$semicolons = 0;
|
||||
for ($next = $start; $next <= $end; ++$next) {
|
||||
$code = $tokens[$next]['code'];
|
||||
if ($code === T_SEMICOLON) {
|
||||
++$semicolons;
|
||||
} else if ($semicolons === 2 && $code === T_VARIABLE) {
|
||||
$incrementers[] = $tokens[$next]['content'];
|
||||
}
|
||||
}
|
||||
|
||||
return $incrementers;
|
||||
|
||||
}//end findIncrementers()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Detects unconditional if- and elseif-statements.
|
||||
*
|
||||
* This rule is based on the PMD rule catalog. The Unconditional If Statment
|
||||
* sniff detects statement conditions that are only set to one of the constant
|
||||
* values <b>true</b> or <b>false</b>
|
||||
*
|
||||
* <code>
|
||||
* class Foo
|
||||
* {
|
||||
* public function close()
|
||||
* {
|
||||
* if (true)
|
||||
* {
|
||||
* // ...
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @version Release: 1.3.3
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class Generic_Sniffs_CodeAnalysis_UnconditionalIfStatementSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Registers the tokens that this sniff wants to listen for.
|
||||
*
|
||||
* @return array(integer)
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(
|
||||
T_IF,
|
||||
T_ELSEIF,
|
||||
);
|
||||
|
||||
}//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();
|
||||
$token = $tokens[$stackPtr];
|
||||
|
||||
// Skip for-loop without body.
|
||||
if (isset($token['parenthesis_opener']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$next = ++$token['parenthesis_opener'];
|
||||
$end = --$token['parenthesis_closer'];
|
||||
|
||||
$goodCondition = false;
|
||||
for (; $next <= $end; ++$next) {
|
||||
$code = $tokens[$next]['code'];
|
||||
|
||||
if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
|
||||
continue;
|
||||
} else if ($code !== T_TRUE && $code !== T_FALSE) {
|
||||
$goodCondition = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($goodCondition === false) {
|
||||
$error = 'Avoid IF statements that are always true or false';
|
||||
$phpcsFile->addWarning($error, $stackPtr, 'Found');
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Detects unnecessary final modifiers inside of final classes.
|
||||
*
|
||||
* This rule is based on the PMD rule catalog. The Unnecessary Final Modifier
|
||||
* sniff detects the use of the final modifier inside of a final class which
|
||||
* is unnecessary.
|
||||
*
|
||||
* <code>
|
||||
* final class Foo
|
||||
* {
|
||||
* public final function bar()
|
||||
* {
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @version Release: 1.3.3
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class Generic_Sniffs_CodeAnalysis_UnnecessaryFinalModifierSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Registers the tokens that this sniff wants to listen for.
|
||||
*
|
||||
* @return array(integer)
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_CLASS);
|
||||
|
||||
}//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();
|
||||
$token = $tokens[$stackPtr];
|
||||
|
||||
// Skip for-statements without body.
|
||||
if (isset($token['scope_opener']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch previous token.
|
||||
$prev = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true);
|
||||
|
||||
// Skip for non final class.
|
||||
if ($prev === false || $tokens[$prev]['code'] !== T_FINAL) {
|
||||
return;
|
||||
}
|
||||
|
||||
$next = ++$token['scope_opener'];
|
||||
$end = --$token['scope_closer'];
|
||||
|
||||
for (; $next <= $end; ++$next) {
|
||||
if ($tokens[$next]['code'] === T_FINAL) {
|
||||
$error = 'Unnecessary FINAL modifier in FINAL class';
|
||||
$phpcsFile->addWarning($error, $next, 'Found');
|
||||
}
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2006-2011 Squiz Pty Ltd (ABN 77 084 670 600)
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Checks the for unused function parameters.
|
||||
*
|
||||
* This sniff checks that all function parameters are used in the function body.
|
||||
* One exception is made for empty function bodies or function bodies that only
|
||||
* contain comments. This could be usefull for the classes that implement an
|
||||
* interface that defines multiple methods but the implementation only needs some
|
||||
* of them.
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @version Release: 1.3.3
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class Generic_Sniffs_CodeAnalysis_UnusedFunctionParameterSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of tokens this test wants to listen for.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_FUNCTION);
|
||||
|
||||
}//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();
|
||||
$token = $tokens[$stackPtr];
|
||||
|
||||
// Skip broken function declarations.
|
||||
if (isset($token['scope_opener']) === false || isset($token['parenthesis_opener']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
$params = array();
|
||||
foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
|
||||
$params[$param['name']] = $stackPtr;
|
||||
}
|
||||
|
||||
$next = ++$token['scope_opener'];
|
||||
$end = --$token['scope_closer'];
|
||||
|
||||
$emptyBody = true;
|
||||
|
||||
for (; $next <= $end; ++$next) {
|
||||
$token = $tokens[$next];
|
||||
$code = $token['code'];
|
||||
|
||||
// Ingorable tokens.
|
||||
if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
|
||||
continue;
|
||||
} else if ($code === T_THROW && $emptyBody === true) {
|
||||
// Throw statement and an empty body indicate an interface method.
|
||||
return;
|
||||
} else if ($code === T_RETURN && $emptyBody === true) {
|
||||
// Return statement and an empty body indicate an interface method.
|
||||
$tmp = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
|
||||
if ($tmp === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// There is a return.
|
||||
if ($tokens[$tmp]['code'] === T_SEMICOLON) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tmp = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($tmp + 1), null, true);
|
||||
|
||||
// There is a return <token>.
|
||||
if ($tmp !== false && $tokens[$tmp]['code'] === T_SEMICOLON) {
|
||||
return;
|
||||
}
|
||||
}//end if
|
||||
|
||||
$emptyBody = false;
|
||||
|
||||
if ($code === T_VARIABLE && isset($params[$token['content']]) === true) {
|
||||
unset($params[$token['content']]);
|
||||
} else if ($code === T_DOUBLE_QUOTED_STRING || $code === T_HEREDOC) {
|
||||
// Tokenize strings that can contain variables.
|
||||
// Make sure the string is re-joined if it occurs over multiple lines.
|
||||
$string = $token['content'];
|
||||
for ($i = ($next + 1); $i <= $end; $i++) {
|
||||
if ($tokens[$i]['code'] === $code) {
|
||||
$string .= $tokens[$i]['content'];
|
||||
$next++;
|
||||
}
|
||||
}
|
||||
|
||||
$strTokens = token_get_all(sprintf('<?php %s;?>', $string));
|
||||
|
||||
foreach ($strTokens as $tok) {
|
||||
if (is_array($tok) === false || $tok[0] !== T_VARIABLE ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($params[$tok[1]]) === true) {
|
||||
unset($params[$tok[1]]);
|
||||
}
|
||||
}
|
||||
}//end if
|
||||
}//end for
|
||||
|
||||
if ($emptyBody === false && count($params) > 0) {
|
||||
foreach ($params as $paramName => $position) {
|
||||
$error = 'The method parameter %s is never used';
|
||||
$data = array($paramName);
|
||||
$phpcsFile->addWarning($error, $position, 'Found', $data);
|
||||
}
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Greg Sherwood <gsherwood@squiz.net>
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
|
||||
/**
|
||||
* Detects unnecessary overriden methods that simply call their parent.
|
||||
*
|
||||
* This rule is based on the PMD rule catalog. The Useless Overriding Method
|
||||
* sniff detects the use of methods that only call their parent classes's method
|
||||
* with the same name and arguments. These methods are not required.
|
||||
*
|
||||
* <code>
|
||||
* class FooBar {
|
||||
* public function __construct($a, $b) {
|
||||
* parent::__construct($a, $b);
|
||||
* }
|
||||
* }
|
||||
* </code>
|
||||
*
|
||||
* @category PHP
|
||||
* @package PHP_CodeSniffer
|
||||
* @author Manuel Pichler <mapi@manuel-pichler.de>
|
||||
* @copyright 2007-2008 Manuel Pichler. All rights reserved.
|
||||
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
|
||||
* @version Release: 1.3.3
|
||||
* @link http://pear.php.net/package/PHP_CodeSniffer
|
||||
*/
|
||||
class Generic_Sniffs_CodeAnalysis_UselessOverridingMethodSniff implements PHP_CodeSniffer_Sniff
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* Registers the tokens that this sniff wants to listen for.
|
||||
*
|
||||
* @return array(integer)
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
return array(T_FUNCTION);
|
||||
|
||||
}//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();
|
||||
$token = $tokens[$stackPtr];
|
||||
|
||||
// Skip function without body.
|
||||
if (isset($token['scope_opener']) === false) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get function name.
|
||||
$methodName = $phpcsFile->getDeclarationName($stackPtr);
|
||||
|
||||
// Get all parameters from method signature.
|
||||
$signature = array();
|
||||
foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
|
||||
$signature[] = $param['name'];
|
||||
}
|
||||
|
||||
$next = ++$token['scope_opener'];
|
||||
$end = --$token['scope_closer'];
|
||||
|
||||
for (; $next <= $end; ++$next) {
|
||||
$code = $tokens[$next]['code'];
|
||||
|
||||
if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
|
||||
continue;
|
||||
} else if ($code === T_RETURN) {
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// Any token except 'parent' indicates correct code.
|
||||
if ($tokens[$next]['code'] !== T_PARENT) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find next non empty token index, should be double colon.
|
||||
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
|
||||
|
||||
// Skip for invalid code.
|
||||
if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find next non empty token index, should be the function name.
|
||||
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
|
||||
|
||||
// Skip for invalid code or other method.
|
||||
if ($next === false || $tokens[$next]['content'] !== $methodName) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find next non empty token index, should be the open parenthesis.
|
||||
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
|
||||
|
||||
// Skip for invalid code.
|
||||
if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
|
||||
return;
|
||||
}
|
||||
|
||||
$validParameterTypes = array(
|
||||
T_VARIABLE,
|
||||
T_LNUMBER,
|
||||
T_CONSTANT_ENCAPSED_STRING,
|
||||
);
|
||||
|
||||
$parameters = array('');
|
||||
$parenthesisCount = 1;
|
||||
$count = count($tokens);
|
||||
for (++$next; $next < $count; ++$next) {
|
||||
$code = $tokens[$next]['code'];
|
||||
|
||||
if ($code === T_OPEN_PARENTHESIS) {
|
||||
++$parenthesisCount;
|
||||
} else if ($code === T_CLOSE_PARENTHESIS) {
|
||||
--$parenthesisCount;
|
||||
} else if ($parenthesisCount === 1 && $code === T_COMMA) {
|
||||
$parameters[] = '';
|
||||
} else if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
|
||||
$parameters[(count($parameters) - 1)] .= $tokens[$next]['content'];
|
||||
}
|
||||
|
||||
if ($parenthesisCount === 0) {
|
||||
break;
|
||||
}
|
||||
}//end for
|
||||
|
||||
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
|
||||
if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check rest of the scope.
|
||||
for (++$next; $next <= $end; ++$next) {
|
||||
$code = $tokens[$next]['code'];
|
||||
// Skip for any other content.
|
||||
if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$parameters = array_map('trim', $parameters);
|
||||
$parameters = array_filter($parameters);
|
||||
|
||||
if (count($parameters) === count($signature) && $parameters === $signature) {
|
||||
$phpcsFile->addWarning('Useless method overriding detected', $stackPtr, 'Found');
|
||||
}
|
||||
|
||||
}//end process()
|
||||
|
||||
|
||||
}//end class
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user