Subversion Repositories Applications.framework

Compare Revisions

Ignore whitespace Rev 4 → Rev 5

/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/EmptyStatementSniff.php
New file
0,0 → 1,129
<?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
* @version CVS: $Id: EmptyStatementSniff.php,v 1.1 2008/02/06 02:38:36 squiz Exp $
* @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.2.0RC1
* @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.
*
* @type array<boolean>
* @var array(integer=>boolean) $_tokens
*/
private $_tokens = 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->_tokens);
 
}//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 = sprintf('Empty %s statement detected', strtoupper($name));
if ($this->_tokens[$token['code']] === true) {
$phpcsFile->addError($error, $stackPtr);
} else {
$phpcsFile->addWarning($error, $stackPtr);
}
}
 
}//end process()
 
 
}//end class
 
?>
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/UnnecessaryFinalModifierSniff.php
New file
0,0 → 1,99
<?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
* @version CVS: $Id: UnnecessaryFinalModifierSniff.php,v 1.1 2008/02/06 02:38:36 squiz Exp $
* @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.2.0RC1
* @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);
}
}
 
}//end process()
 
 
}//end class
 
?>
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/ForLoopShouldBeWhileLoopSniff.php
New file
0,0 → 1,101
<?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
* @version CVS: $Id: ForLoopShouldBeWhileLoopSniff.php,v 1.1 2008/02/06 02:38:36 squiz Exp $
* @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.2.0RC1
* @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);
}
 
}//end process()
 
 
}//end class
 
?>
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/UnconditionalIfStatementSniff.php
New file
0,0 → 1,107
<?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
* @version CVS: $Id: UnconditionalIfStatementSniff.php,v 1.1 2008/02/06 02:38:36 squiz Exp $
* @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.2.0RC1
* @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);
}
 
}//end process()
 
 
}//end class
 
?>
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/UnusedFunctionParameterSniff.php
New file
0,0 → 1,141
<?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 Squiz Pty Ltd (ABN 77 084 670 600)
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version CVS: $Id: UnusedFunctionParameterSniff.php,v 1.1 2008/02/06 02:38:36 squiz Exp $
* @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.2.0RC1
* @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] === T_SEMICOLON) {
return;
}
 
$tmp = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($tmp + 1), null, true);
 
// There is a return <token>.
if ($tmp !== false && $tokens[$tmp] === 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) {
// Tokenize double quote string.
$strTokens = token_get_all(sprintf('<?php %s;?>', $token['content']));
 
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 '.$paramName.' is never used';
$phpcsFile->addWarning($error, $position);
}
}
 
}//end process()
 
 
}//end class
 
?>
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php
New file
0,0 → 1,181
<?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
* @version CVS: $Id: UselessOverridingMethodSniff.php,v 1.2 2008/10/01 06:08:49 squiz Exp $
* @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.2.0RC1
* @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);
}
 
}//end process()
 
 
}//end class
 
?>
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/ForLoopWithTestFunctionCallSniff.php
New file
0,0 → 1,114
<?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
* @version CVS: $Id: ForLoopWithTestFunctionCallSniff.php,v 1.1 2008/02/06 02:38:36 squiz Exp $
* @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.2.0RC1
* @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);
break;
}
}//end for
 
}//end process()
 
 
}//end class
 
?>
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/JumbledIncrementerSniff.php
New file
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
* @version CVS: $Id: JumbledIncrementerSniff.php,v 1.1 2008/02/06 02:38:36 squiz Exp $
* @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.2.0RC1
* @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 = sprintf('Loop incrementor (%s) jumbling with inner loop', join(', ', $diff));
$phpcsFile->addWarning($error, $stackPtr);
}
}
 
}//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
 
?>