/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Commenting/TodoSniff.php |
---|
New file |
0,0 → 1,88 |
<?php |
/** |
* Generic_Sniffs_Commenting_TodoSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: TodoSniff.php,v 1.3 2008/12/02 02:38:34 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Commenting_TodoSniff. |
* |
* Warns about TODO comments. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Commenting_TodoSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* A list of tokenizers this sniff supports. |
* |
* @var array |
*/ |
public $supportedTokenizers = array( |
'PHP', |
'JS', |
); |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return PHP_CodeSniffer_Tokens::$commentTokens; |
}//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(); |
$content = $tokens[$stackPtr]['content']; |
$matches = Array(); |
if (preg_match('|[^a-z]+todo[^a-z]+(.*)|i', $content, $matches) !== 0) { |
// Clear whitespace and some common characters not required at |
// the end of a to-do message to make the warning more informative. |
$todoMessage = trim($matches[1]); |
$todoMessage = trim($todoMessage, '[]().'); |
$error = 'Comment refers to a TODO task'; |
if ($todoMessage !== '') { |
$error .= " \"$todoMessage\""; |
} |
$phpcsFile->addWarning($error, $stackPtr); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Files/LineEndingsSniff.php |
---|
New file |
0,0 → 1,99 |
<?php |
/** |
* Generic_Sniffs_Files_LineEndingsSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: LineEndingsSniff.php,v 1.4 2008/10/28 04:42:37 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Files_LineEndingsSniff. |
* |
* Checks that end of line characters are correct. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Files_LineEndingsSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* A list of tokenizers this sniff supports. |
* |
* @var array |
*/ |
public $supportedTokenizers = array( |
'PHP', |
'JS', |
'CSS', |
); |
/** |
* The valid EOL character. |
* |
* @var string |
*/ |
protected $eolChar = "\n"; |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array(T_OPEN_TAG); |
}//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) |
{ |
// We are only interested if this is the first open tag. |
if ($stackPtr !== 0) { |
if ($phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)) !== false) { |
return; |
} |
} |
if ($phpcsFile->eolChar !== $this->eolChar) { |
$expected = $this->eolChar; |
$expected = str_replace("\n", '\n', $expected); |
$expected = str_replace("\r", '\r', $expected); |
$found = $phpcsFile->eolChar; |
$found = str_replace("\n", '\n', $found); |
$found = str_replace("\r", '\r', $found); |
$error = "End of line character is invalid; expected \"$expected\" but found \"$found\""; |
$phpcsFile->addError($error, $stackPtr); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Files/LineLengthSniff.php |
---|
New file |
0,0 → 1,134 |
<?php |
/** |
* Generic_Sniffs_Files_LineLengthSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: LineLengthSniff.php,v 1.16 2008/06/27 01:58:38 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Files_LineLengthSniff. |
* |
* Checks all lines in the file, and throws warnings if they are over 80 |
* characters in length and errors if they are over 100. Both these |
* figures can be changed by extending this sniff in your own standard. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Files_LineLengthSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* The limit that the length of a line should not exceed. |
* |
* @var int |
*/ |
protected $lineLimit = 80; |
/** |
* The limit that the length of a line must not exceed. |
* |
* Set to zero (0) to disable. |
* |
* @var int |
*/ |
protected $absoluteLineLimit = 100; |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array(T_OPEN_TAG); |
}//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(); |
// Make sure this is the first open tag. |
$previousOpenTag = $phpcsFile->findPrevious(array(T_OPEN_TAG), ($stackPtr - 1)); |
if ($previousOpenTag !== false) { |
return; |
} |
$tokenCount = 0; |
$currentLineContent = ''; |
$currentLine = 1; |
for (; $tokenCount < $phpcsFile->numTokens; $tokenCount++) { |
if ($tokens[$tokenCount]['line'] === $currentLine) { |
$currentLineContent .= $tokens[$tokenCount]['content']; |
} else { |
$currentLineContent = trim($currentLineContent, $phpcsFile->eolChar); |
$this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent); |
$currentLineContent = $tokens[$tokenCount]['content']; |
$currentLine++; |
} |
} |
$this->checkLineLength($phpcsFile, ($tokenCount - 1), $currentLineContent); |
}//end process() |
/** |
* Checks if a line is too long. |
* |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
* @param int $stackPtr The token at the end of the line. |
* @param string $lineContent The content of the line. |
* |
* @return void |
*/ |
protected function checkLineLength(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $lineContent) |
{ |
// If the content is a CVS or SVN id in a version tag, or it is |
// a license tag with a name and URL, there is nothing the |
// developer can do to shorten the line, so don't throw errors. |
if (preg_match('|@version[^\$]+\$Id|', $lineContent) === 0 && preg_match('|@license|', $lineContent) === 0) { |
$lineLength = strlen($lineContent); |
if ($this->absoluteLineLimit > 0 && $lineLength > $this->absoluteLineLimit) { |
$error = 'Line exceeds maximum limit of '.$this->absoluteLineLimit." characters; contains $lineLength characters"; |
$phpcsFile->addError($error, $stackPtr); |
} else if ($lineLength > $this->lineLimit) { |
$warning = 'Line exceeds '.$this->lineLimit." characters; contains $lineLength characters"; |
$phpcsFile->addWarning($warning, $stackPtr); |
} |
} |
}//end checkLineLength() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/ControlStructures/InlineControlStructureSniff.php |
---|
New file |
0,0 → 1,120 |
<?php |
/** |
* Generic_Sniffs_ControlStructures_InlineControlStructureSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: InlineControlStructureSniff.php,v 1.1 2008/05/01 00:49:31 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_ControlStructures_InlineControlStructureSniff. |
* |
* Verifies that inline control statements are not present. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_ControlStructures_InlineControlStructureSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* A list of tokenizers this sniff supports. |
* |
* @var array |
*/ |
public $supportedTokenizers = array( |
'PHP', |
'JS', |
); |
/** |
* If true, an error will be thrown; otherwise a warning. |
* |
* @var bool |
*/ |
protected $error = true; |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array( |
T_IF, |
T_ELSE, |
T_FOREACH, |
T_WHILE, |
T_DO, |
T_SWITCH, |
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(); |
if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
// Ignore the ELSE in ELSE IF. We'll process the IF part later. |
if (($tokens[$stackPtr]['code'] === T_ELSE) && ($tokens[($stackPtr + 2)]['code'] === T_IF)) { |
return; |
} |
if ($tokens[$stackPtr]['code'] === T_WHILE) { |
// This could be from a DO WHILE, which doesn't have an opening brace. |
$lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) { |
$brace = $tokens[$lastContent]; |
if (isset($brace['scope_condition']) === true) { |
$condition = $tokens[$brace['scope_condition']]; |
if ($condition['code'] === T_DO) { |
return; |
} |
} |
} |
} |
// This is a control structure without an opening brace, |
// so it is an inline statement. |
if ($this->error === true) { |
$phpcsFile->addError('Inline control structures are not allowed', $stackPtr); |
} else { |
$phpcsFile->addWarning('Inline control structures are discouraged', $stackPtr); |
} |
return; |
}//end if |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Metrics/CyclomaticComplexitySniff.php |
---|
New file |
0,0 → 1,124 |
<?php |
/** |
* Checks the cyclomatic complexity (McCabe) for functions. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: CyclomaticComplexitySniff.php,v 1.1 2007/07/30 04:55:35 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Checks the cyclomatic complexity (McCabe) for functions. |
* |
* The cyclomatic complexity (also called McCabe code metrics) |
* indicates the complexity within a function by counting |
* the different paths the function includes. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Johann-Peter Hartmann <hartmann@mayflower.de> |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @copyright 2007 Mayflower GmbH |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Metrics_CyclomaticComplexitySniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* A complexity higher than this value will throw a warning. |
* |
* @var int |
*/ |
protected $complexity = 10; |
/** |
* A complexity higer than this value will throw an error. |
* |
* @var int |
*/ |
protected $absoluteComplexity = 20; |
/** |
* 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) |
{ |
$this->currentFile = $phpcsFile; |
$tokens = $phpcsFile->getTokens(); |
// Ignore abstract methods. |
if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
return; |
} |
// Detect start and end of this function definition. |
$start = $tokens[$stackPtr]['scope_opener']; |
$end = $tokens[$stackPtr]['scope_closer']; |
// Predicate nodes for PHP. |
$find = array( |
'T_CASE', |
'T_DEFAULT', |
'T_CATCH', |
'T_IF', |
'T_FOR', |
'T_FOREACH', |
'T_WHILE', |
'T_DO', |
'T_ELSEIF', |
); |
$complexity = 1; |
// Iterate from start to end and count predicate nodes. |
for ($i = ($start + 1); $i < $end; $i++) { |
if (in_array($tokens[$i]['type'], $find) === true) { |
$complexity++; |
} |
} |
if ($complexity > $this->absoluteComplexity) { |
$error = "Function's cyclomatic complexity ($complexity) exceeds allowed maximum of ".$this->absoluteComplexity; |
$phpcsFile->addError($error, $stackPtr); |
} else if ($complexity > $this->complexity) { |
$warning = "Function's cyclomatic complexity ($complexity) exceeds ".$this->complexity.'; consider refactoring the function'; |
$phpcsFile->addWarning($warning, $stackPtr); |
} |
return; |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Metrics/NestingLevelSniff.php |
---|
New file |
0,0 → 1,107 |
<?php |
/** |
* Checks the nesting level for methods. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: NestingLevelSniff.php,v 1.1 2007/07/30 04:55:35 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Checks the nesting level for methods. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Johann-Peter Hartmann <hartmann@mayflower.de> |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @copyright 2007 Mayflower GmbH |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Metrics_NestingLevelSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* A nesting level than this value will throw a warning. |
* |
* @var int |
*/ |
protected $nestingLevel = 5; |
/** |
* A nesting level than this value will throw an error. |
* |
* @var int |
*/ |
protected $absoluteNestingLevel = 10; |
/** |
* 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(); |
// Ignore abstract methods. |
if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
return; |
} |
// Detect start and end of this function definition. |
$start = $tokens[$stackPtr]['scope_opener']; |
$end = $tokens[$stackPtr]['scope_closer']; |
$nestingLevel = 0; |
// Find the maximum nesting level of any token in the function. |
for ($i = ($start + 1); $i < $end; $i++) { |
$level = $tokens[$i]['level']; |
if ($nestingLevel < $level) { |
$nestingLevel = $level; |
} |
} |
// We subtract the nesting level of the function itself. |
$nestingLevel = ($nestingLevel - $tokens[$stackPtr]['level'] - 1); |
if ($nestingLevel > $this->absoluteNestingLevel) { |
$error = "Function's nesting level ($nestingLevel) exceeds allowed maximum of ".$this->absoluteNestingLevel; |
$phpcsFile->addError($error, $stackPtr); |
} else if ($nestingLevel > $this->nestingLevel) { |
$warning = "Function's nesting level ($nestingLevel) exceeds ".$this->nestingLevel.'; consider refactoring the function'; |
$phpcsFile->addWarning($warning, $stackPtr); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/VersionControl/SubversionPropertiesSniff.php |
---|
New file |
0,0 → 1,169 |
<?php |
/** |
* Generic_Sniffs_VersionControl_SubversionPropertiesSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Jack Bates <ms419@freezone.co.uk> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: SubversionPropertiesSniff.php,v 1.1 2009/01/05 01:07:47 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_VersionControl_SubversionPropertiesSniff. |
* |
* Tests that the correct Subversion properties are set. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Jack Bates <ms419@freezone.co.uk> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_VersionControl_SubversionPropertiesSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* The Subversion properties that should be set. |
* |
* @var array |
*/ |
protected $properties = array( |
'svn:keywords' => 'Author Id Revision', |
'svn:eol-style' => 'native', |
); |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array( |
T_OPEN_TAG, |
); |
}//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(); |
// Make sure this is the first PHP open tag so we don't process the |
// same file twice. |
$prevOpenTag = $phpcsFile->findPrevious(T_OPEN_TAG, ($stackPtr - 1)); |
if ($prevOpenTag !== false) { |
return; |
} |
$path = $phpcsFile->getFileName(); |
$properties = $this->properties($path); |
foreach (($properties + $this->properties) as $key => $value) { |
if (isset($properties[$key]) === true |
&& isset($this->properties[$key]) === false |
) { |
$error = 'Unexpected Subversion property "'.$key.'" = "'.$properties[$key].'"'; |
$phpcsFile->addError($error, $stackPtr); |
continue; |
} |
if (isset($properties[$key]) === false |
&& isset($this->properties[$key]) === true |
) { |
$error = 'Missing Subversion property "'.$key.'" = "'.$this->properties[$key].'"'; |
$phpcsFile->addError($error, $stackPtr); |
continue; |
} |
if ($properties[$key] !== $this->properties[$key]) { |
$error = 'Subversion property "'.$key.'" = "'.$properties[$key].'" does not match "'.$this->properties[$key].'"'; |
$phpcsFile->addError($error, $stackPtr); |
} |
} |
}//end process() |
/** |
* Returns the Subversion properties which are actually set on a path. |
* |
* @param string $path The path to return Subversion properties on. |
* |
* @return array |
* @throws PHP_CodeSniffer_Exception If Subversion properties file could |
* not be opened. |
*/ |
protected function properties($path) |
{ |
$properties = array(); |
$paths = array(); |
$paths[] = dirname($path).'/.svn/props/'.basename($path).'.svn-work'; |
$paths[] = dirname($path).'/.svn/prop-base/'.basename($path).'.svn-base'; |
foreach ($paths as $path) { |
if (true === file_exists($path)) { |
if (false === $handle = fopen($path, 'r')) { |
$error = 'Error opening file; could not get Subversion properties'; |
throw new PHP_CodeSniffer_Exception($error); |
} |
while (!feof($handle)) { |
// Read a key length line. Might be END, though. |
$buffer = fgets($handle); |
// Check for the end of the hash. |
if ("END\n" === $buffer) { |
break; |
} |
// Now read that much into a buffer. |
$key = fread($handle, substr($buffer, 2)); |
// Suck up extra newline after key data. |
fgetc($handle); |
// Read a value length line. |
$buffer = fgets($handle); |
// Now read that much into a buffer. |
$value = fread($handle, substr($buffer, 2)); |
// Suck up extra newline after value data. |
fgetc($handle); |
$properties[$key] = $value; |
}//end while |
fclose($handle); |
}//end if |
}//end foreach |
return $properties; |
}//end properties() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/PHP/LowerCaseConstantSniff.php |
---|
New file |
0,0 → 1,84 |
<?php |
/** |
* Generic_Sniffs_PHP_LowerCaseConstantSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: LowerCaseConstantSniff.php,v 1.8 2008/02/18 00:01:06 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_PHP_LowerCaseConstantSniff. |
* |
* Checks that all uses of true, false and null are lowerrcase. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_PHP_LowerCaseConstantSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* A list of tokenizers this sniff supports. |
* |
* @var array |
*/ |
public $supportedTokenizers = array( |
'PHP', |
'JS', |
); |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array( |
T_TRUE, |
T_FALSE, |
T_NULL, |
); |
}//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(); |
$keyword = $tokens[$stackPtr]['content']; |
if (strtolower($keyword) !== $keyword) { |
$error = 'TRUE, FALSE and NULL must be lowercase; expected "'.strtolower($keyword).'" but found "'.$keyword.'"'; |
$phpcsFile->addError($error, $stackPtr); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/PHP/NoSilencedErrorsSniff.php |
---|
New file |
0,0 → 1,82 |
<?php |
/** |
* Generic_Sniffs_PHP_NoSilencedErrorsSniff |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Andy Brockhurst <abrock@yahoo-inc.com> |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: NoSilencedErrorsSniff.php,v 1.1 2008/12/03 04:42:07 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_PHP_NoSilencedErrorsSniff. |
* |
* Throws an error or warning when any code prefixed with an asperand is encountered. |
* |
* <code> |
* if (@in_array($array, $needle)) |
* { |
* doSomething(); |
* } |
* </code> |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Andy Brockhurst <abrock@yahoo-inc.com> |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_PHP_NoSilencedErrorsSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* If true, an error will be thrown; otherwise a warning. |
* |
* @var bool |
*/ |
protected $error = false; |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array(T_ASPERAND); |
}//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) |
{ |
$error = 'Silencing errors is '; |
if ($this->error === true) { |
$error .= 'forbidden'; |
$phpcsFile->addError($error, $stackPtr); |
} else { |
$error .= 'discouraged'; |
$phpcsFile->addWarning($error, $stackPtr); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/PHP/UpperCaseConstantSniff.php |
---|
New file |
0,0 → 1,75 |
<?php |
/** |
* Generic_Sniffs_PHP_UpperCaseConstantSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: UpperCaseConstantSniff.php,v 1.8 2008/02/18 00:01:06 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_PHP_UpperCaseConstantSniff. |
* |
* Checks that all uses of TRUE, FALSE and NULL are uppercase. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_PHP_UpperCaseConstantSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array( |
T_TRUE, |
T_FALSE, |
T_NULL, |
); |
}//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(); |
$keyword = $tokens[$stackPtr]['content']; |
if (strtoupper($keyword) !== $keyword) { |
$error = 'TRUE, FALSE and NULL must be uppercase; expected "'.strtoupper($keyword).'" but found "'.$keyword.'"'; |
$phpcsFile->addError($error, $stackPtr); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php |
---|
New file |
0,0 → 1,115 |
<?php |
/** |
* Generic_Sniffs_PHP_ForbiddenFunctionsSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: ForbiddenFunctionsSniff.php,v 1.7 2008/08/19 06:35:37 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_PHP_ForbiddenFunctionsSniff. |
* |
* Discourages the use of alias functions that are kept in PHP for compatibility |
* with older versions. Can be used to forbid the use of any function. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_PHP_ForbiddenFunctionsSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* A list of forbidden functions with their alternatives. |
* |
* The value is NULL if no alternative exists. IE, the |
* function should just not be used. |
* |
* @var array(string => string|null) |
*/ |
protected $forbiddenFunctions = array( |
'sizeof' => 'count', |
'delete' => 'unset', |
); |
/** |
* If true, an error will be thrown; otherwise a warning. |
* |
* @var bool |
*/ |
protected $error = true; |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array(T_STRING); |
}//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(); |
$prevToken = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
if (in_array($tokens[$prevToken]['code'], array(T_DOUBLE_COLON, T_OBJECT_OPERATOR, T_FUNCTION)) === true) { |
// Not a call to a PHP function. |
return; |
} |
$function = strtolower($tokens[$stackPtr]['content']); |
if (in_array($function, array_keys($this->forbiddenFunctions)) === false) { |
return; |
} |
$error = "The use of function $function() is "; |
if ($this->error === true) { |
$error .= 'forbidden'; |
} else { |
$error .= 'discouraged'; |
} |
if ($this->forbiddenFunctions[$function] !== null) { |
$error .= '; use '.$this->forbiddenFunctions[$function].'() instead'; |
} |
if ($this->error === 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/PHP/DisallowShortOpenTagSniff.php |
---|
New file |
0,0 → 1,91 |
<?php |
/** |
* Generic_Sniffs_PHP_DisallowShortOpenTagSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: DisallowShortOpenTagSniff.php,v 1.8 2008/12/03 04:42:41 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_PHP_DisallowShortOpenTagSniff. |
* |
* Makes sure that shorthand PHP open tags are not used. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_PHP_DisallowShortOpenTagSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array( |
T_OPEN_TAG, |
T_OPEN_TAG_WITH_ECHO, |
); |
}//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) |
{ |
// If short open tags are off, then any short open tags will be converted |
// to inline_html tags so we can just ignore them. |
// If its on, then we want to ban the use of them. |
$option = ini_get('short_open_tag'); |
// Ini_get returns a string "0" if short open tags is off. |
if ($option === '0') { |
return; |
} |
$tokens = $phpcsFile->getTokens(); |
$openTag = $tokens[$stackPtr]; |
if ($openTag['content'] === '<?') { |
$error = 'Short PHP opening tag used. Found "'.$openTag['content'].'" Expected "<?php".'; |
$phpcsFile->addError($error, $stackPtr); |
} |
if ($openTag['code'] === T_OPEN_TAG_WITH_ECHO) { |
$nextVar = $tokens[$phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true)]; |
$error = 'Short PHP opening tag used with echo. Found "'; |
$error .= $openTag['content'].' '.$nextVar['content'].' ..." but expected "<?php echo '.$nextVar['content'].' ...".'; |
$phpcsFile->addError($error, $stackPtr); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceBsdAllmanSniff.php |
---|
New file |
0,0 → 1,122 |
<?php |
/** |
* Generic_Sniffs_Methods_OpeningMethodBraceBsdAllmanSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: OpeningFunctionBraceBsdAllmanSniff.php,v 1.8 2008/05/05 03:59:12 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff. |
* |
* Checks that the opening brace of a function is on the line after the |
* function declaration. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Functions_OpeningFunctionBraceBsdAllmanSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* Registers the tokens that this sniff wants to listen for. |
* |
* @return void |
*/ |
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(); |
if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
return; |
} |
$openingBrace = $tokens[$stackPtr]['scope_opener']; |
// The end of the function occurs at the end of the argument list. Its |
// like this because some people like to break long function declarations |
// over multiple lines. |
$functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line']; |
$braceLine = $tokens[$openingBrace]['line']; |
$lineDifference = ($braceLine - $functionLine); |
if ($lineDifference === 0) { |
$error = 'Opening brace should be on a new line'; |
$phpcsFile->addError($error, $openingBrace); |
return; |
} |
if ($lineDifference > 1) { |
$ender = 'line'; |
if (($lineDifference - 1) !== 1) { |
$ender .= 's'; |
} |
$error = 'Opening brace should be on the line after the declaration; found '.($lineDifference - 1).' blank '.$ender; |
$phpcsFile->addError($error, $openingBrace); |
return; |
} |
// We need to actually find the first piece of content on this line, |
// as if this is a method with tokens before it (public, static etc) |
// or an if with an else before it, then we need to start the scope |
// checking from there, rather than the current token. |
$lineStart = $stackPtr; |
while (($lineStart = $phpcsFile->findPrevious(array(T_WHITESPACE), ($lineStart - 1), null, false)) !== false) { |
if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) { |
break; |
} |
} |
// We found a new line, now go forward and find the first non-whitespace |
// token. |
$lineStart = $phpcsFile->findNext(array(T_WHITESPACE), $lineStart, null, true); |
// The opening brace is on the correct line, now it needs to be |
// checked to be correctly indented. |
$startColumn = $tokens[$lineStart]['column']; |
$braceIndent = $tokens[$openingBrace]['column']; |
if ($braceIndent !== $startColumn) { |
$error = 'Opening brace indented incorrectly; expected '.($startColumn - 1).' spaces, found '.($braceIndent - 1); |
$phpcsFile->addError($error, $openingBrace); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Functions/OpeningFunctionBraceKernighanRitchieSniff.php |
---|
New file |
0,0 → 1,108 |
<?php |
/** |
* Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: OpeningFunctionBraceKernighanRitchieSniff.php,v 1.5 2008/05/05 03:59:12 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff. |
* |
* Checks that the opening brace of a function is on the same line |
* as the function declaration. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Functions_OpeningFunctionBraceKernighanRitchieSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* Registers the tokens that this sniff wants to listen for. |
* |
* @return void |
*/ |
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(); |
if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
return; |
} |
$openingBrace = $tokens[$stackPtr]['scope_opener']; |
// The end of the function occurs at the end of the argument list. Its |
// like this because some people like to break long function declarations |
// over multiple lines. |
$functionLine = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['line']; |
$braceLine = $tokens[$openingBrace]['line']; |
$lineDifference = ($braceLine - $functionLine); |
if ($lineDifference > 0) { |
$error = 'Opening brace should be on the same line as the declaration'; |
$phpcsFile->addError($error, $openingBrace); |
return; |
} |
// Checks that the closing parenthesis and the opening brace are |
// separated by a whitespace character. |
$closerColumn = $tokens[$tokens[$stackPtr]['parenthesis_closer']]['column']; |
$braceColumn = $tokens[$openingBrace]['column']; |
$columnDifference = ($braceColumn - $closerColumn); |
if ($columnDifference !== 2) { |
$error = 'Expected 1 space between the closing parenthesis and the opening brace; found '.($columnDifference - 1).'.'; |
$phpcsFile->addError($error, $openingBrace); |
return; |
} |
// Check that a tab was not used instead of a space. |
$spaceTokenPtr = ($tokens[$stackPtr]['parenthesis_closer'] + 1); |
$spaceContent = $tokens[$spaceTokenPtr]['content']; |
if ($spaceContent !== ' ') { |
$error = 'Expected a single space character between closing parenthesis and opening brace; found "'.$spaceContent.'".'; |
$phpcsFile->addError($error, $openingBrace); |
return; |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Strings/UnnecessaryStringConcatSniff.php |
---|
New file |
0,0 → 1,112 |
<?php |
/** |
* Generic_Sniffs_Strings_UnnecessaryStringConcatSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: UnnecessaryStringConcatSniff.php,v 1.1 2008/12/05 04:39:00 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Strings_UnnecessaryStringConcatSniff. |
* |
* Checks that two strings are not concatenated together; suggests |
* using one string instead. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Strings_UnnecessaryStringConcatSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* A list of tokenizers this sniff supports. |
* |
* @var array |
*/ |
public $supportedTokenizers = array( |
'PHP', |
'JS', |
); |
/** |
* If true, an error will be thrown; otherwise a warning. |
* |
* @var bool |
*/ |
protected $error = true; |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array( |
T_STRING_CONCAT, |
T_PLUS, |
); |
}//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) |
{ |
// Work out which type of file this is for. |
$tokens = $phpcsFile->getTokens(); |
if ($tokens[$stackPtr]['code'] === T_STRING_CONCAT) { |
if ($phpcsFile->tokenizerType === 'JS') { |
return; |
} |
} else { |
if ($phpcsFile->tokenizerType === 'PHP') { |
return; |
} |
} |
$prev = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
$next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
if ($prev === false || $next === false) { |
return; |
} |
$stringTokens = PHP_CodeSniffer_Tokens::$stringTokens; |
if (in_array($tokens[$prev]['code'], $stringTokens) === true |
&& in_array($tokens[$next]['code'], $stringTokens) === true |
) { |
$error = 'String concat is not required here; use a single string instead'; |
if ($this->error === 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/Formatting/NoSpaceAfterCastSniff.php |
---|
New file |
0,0 → 1,70 |
<?php |
/** |
* Generic_Sniffs_Formatting_NoSpaceAfterCastSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: NoSpaceAfterCastSniff.php,v 1.2 2007/07/23 01:47:52 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Formatting_NoSpaceAfterCastSniff. |
* |
* Ensures there is no space after cast tokens. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Formatting_NoSpaceAfterCastSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return PHP_CodeSniffer_Tokens::$castTokens; |
}//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(); |
if ($tokens[($stackPtr + 1)]['code'] === T_WHITESPACE) { |
$error = 'A cast statement must not be followed by a space'; |
$phpcsFile->addError($error, $stackPtr); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Formatting/SpaceAfterCastSniff.php |
---|
New file |
0,0 → 1,76 |
<?php |
/** |
* Generic_Sniffs_Formatting_SpaceAfterCastSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: SpaceAfterCastSniff.php,v 1.2 2007/07/23 01:47:52 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Formatting_SpaceAfterCastSniff. |
* |
* Ensures there is a single space after cast tokens. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Formatting_SpaceAfterCastSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return PHP_CodeSniffer_Tokens::$castTokens; |
}//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(); |
if ($tokens[($stackPtr + 1)]['code'] !== T_WHITESPACE) { |
$error = 'A cast statement must be followed by a single space'; |
$phpcsFile->addError($error, $stackPtr); |
return; |
} |
if ($tokens[($stackPtr + 1)]['content'] !== ' ') { |
$error = 'A cast statement must be followed by a single space'; |
$phpcsFile->addError($error, $stackPtr); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Formatting/MultipleStatementAlignmentSniff.php |
---|
New file |
0,0 → 1,287 |
<?php |
/** |
* Generic_Sniffs_Formatting_MultipleStatementAlignmentSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: MultipleStatementAlignmentSniff.php,v 1.23 2008/12/02 02:38:34 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Formatting_MultipleStatementAlignmentSniff. |
* |
* Checks alignment of assignments. If there are multiple adjacent assignments, |
* it will check that the equals signs of each assignment are aligned. It will |
* display a warning to advise that the signs should be aligned. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Formatting_MultipleStatementAlignmentSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* A list of tokenizers this sniff supports. |
* |
* @var array |
*/ |
public $supportedTokenizers = array( |
'PHP', |
'JS', |
); |
/** |
* If true, an error will be thrown; otherwise a warning. |
* |
* @var bool |
*/ |
protected $error = false; |
/** |
* The maximum amount of padding before the alignment is ignored. |
* |
* If the amount of padding required to align this assignment with the |
* surrounding assignments exceeds this number, the assignment will be |
* ignored and no errors or warnings will be thrown. |
* |
* @var int |
*/ |
protected $maxPadding = 1000; |
/** |
* If true, multi-line assignments are not checked. |
* |
* @var int |
*/ |
protected $ignoreMultiLine = false; |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return PHP_CodeSniffer_Tokens::$assignmentTokens; |
}//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(); |
// Ignore assignments used in a condition, like an IF or FOR. |
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $start => $end) { |
if (isset($tokens[$start]['parenthesis_owner']) === true) { |
return; |
} |
} |
} |
/* |
By this stage, it is known that there is an assignment on this line. |
We only want to process the block once we reach the last assignment, |
so we need to determine if there are more to follow. |
*/ |
// The assignment may span over multiple lines, so look for the |
// end of the assignment so we can check assignment blocks correctly. |
$lineEnd = $phpcsFile->findNext(T_SEMICOLON, ($stackPtr + 1)); |
$nextAssign = $phpcsFile->findNext( |
PHP_CodeSniffer_Tokens::$assignmentTokens, |
($lineEnd + 1) |
); |
if ($nextAssign !== false) { |
$isAssign = true; |
if ($tokens[$nextAssign]['line'] === ($tokens[$lineEnd]['line'] + 1)) { |
// Assignment may be in the same block as this one. Just make sure |
// it is not used in a condition, like an IF or FOR. |
if (isset($tokens[$nextAssign]['nested_parenthesis']) === true) { |
foreach ($tokens[$nextAssign]['nested_parenthesis'] as $start => $end) { |
if (isset($tokens[$start]['parenthesis_owner']) === true) { |
// Not an assignment. |
$isAssign = false; |
break; |
} |
} |
} |
if ($isAssign === true) { |
return; |
} |
} |
} |
// Getting here means that this is the last in a block of statements. |
$assignments = array(); |
$assignments[] = $stackPtr; |
$prevAssignment = $stackPtr; |
$lastLine = $tokens[$stackPtr]['line']; |
while (($prevAssignment = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$assignmentTokens, ($prevAssignment - 1))) !== false) { |
// The assignment's end token must be on the line directly |
// above the current one to be in the same assignment block. |
$lineEnd = $phpcsFile->findNext(T_SEMICOLON, ($prevAssignment + 1)); |
// And the end token must actually belong to this assignment. |
$nextOpener = $phpcsFile->findNext( |
PHP_CodeSniffer_Tokens::$scopeOpeners, |
($prevAssignment + 1) |
); |
if ($nextOpener !== false && $nextOpener < $lineEnd) { |
break; |
} |
if ($tokens[$lineEnd]['line'] !== ($lastLine - 1)) { |
break; |
} |
// Make sure it is not assigned inside a condition (eg. IF, FOR). |
if (isset($tokens[$prevAssignment]['nested_parenthesis']) === true) { |
foreach ($tokens[$prevAssignment]['nested_parenthesis'] as $start => $end) { |
if (isset($tokens[$start]['parenthesis_owner']) === true) { |
break(2); |
} |
} |
} |
$assignments[] = $prevAssignment; |
$lastLine = $tokens[$prevAssignment]['line']; |
}//end while |
$assignmentData = array(); |
$maxAssignmentLength = 0; |
$maxVariableLength = 0; |
foreach ($assignments as $assignment) { |
$prev = $phpcsFile->findPrevious( |
PHP_CodeSniffer_Tokens::$emptyTokens, |
($assignment - 1), |
null, |
true |
); |
$endColumn = $tokens[($prev + 1)]['column']; |
if ($maxVariableLength < $endColumn) { |
$maxVariableLength = $endColumn; |
} |
if ($maxAssignmentLength < strlen($tokens[$assignment]['content'])) { |
$maxAssignmentLength = strlen($tokens[$assignment]['content']); |
} |
$assignmentData[$assignment] |
= array( |
'variable_length' => $endColumn, |
'assignment_length' => strlen($tokens[$assignment]['content']), |
); |
}//end foreach |
foreach ($assignmentData as $assignment => $data) { |
if ($data['assignment_length'] === $maxAssignmentLength) { |
if ($data['variable_length'] === $maxVariableLength) { |
// The assignment is the longest possible, so the column that |
// everything has to align to is based on it. |
$column = ($maxVariableLength + 1); |
break; |
} else { |
// The assignment token is the longest out of all of the |
// assignments, but the variable name is not, so the column |
// the start at can go back more to cover the space |
// between the variable name and the assigment operator. |
$column = ($maxVariableLength - ($maxAssignmentLength - 1) + 1); |
} |
} |
} |
// Determine the actual position that each equals sign should be in. |
foreach ($assignments as $assignment) { |
// Actual column takes into account the length of the assignment operator. |
$actualColumn = ($column + $maxAssignmentLength - strlen($tokens[$assignment]['content'])); |
if ($tokens[$assignment]['column'] !== $actualColumn) { |
$prev = $phpcsFile->findPrevious( |
PHP_CodeSniffer_Tokens::$emptyTokens, |
($assignment - 1), |
null, |
true |
); |
$expected = ($actualColumn - $tokens[($prev + 1)]['column']); |
if ($tokens[$assignment]['line'] !== $tokens[$prev]['line']) { |
// Instead of working out how many spaces there are |
// across new lines, the error message becomes more |
// generic below. |
$found = null; |
} else { |
$found = ($tokens[$assignment]['column'] - $tokens[($prev + 1)]['column']); |
} |
// If the expected number of spaces for alignment exceeds the |
// maxPadding rule, we can ignore this assignment. |
if ($expected > $this->maxPadding) { |
continue; |
} |
// Skip multi-line assignments if required. |
if ($found === null && $this->ignoreMultiLine === true) { |
continue; |
} |
$expected .= ($expected === 1) ? ' space' : ' spaces'; |
if ($found === null) { |
$found = 'a new line'; |
} else { |
$found .= ($found === 1) ? ' space' : ' spaces'; |
} |
if (count($assignments) === 1) { |
$error = "Equals sign not aligned correctly; expected $expected but found $found"; |
} else { |
$error = "Equals sign not aligned with surrounding assignments; expected $expected but found $found"; |
} |
if ($this->error === true) { |
$phpcsFile->addError($error, $assignment); |
} else { |
$phpcsFile->addWarning($error, $assignment); |
} |
}//end if |
}//end foreach |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Formatting/DisallowMultipleStatementsSniff.php |
---|
New file |
0,0 → 1,84 |
<?php |
/** |
* Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: DisallowMultipleStatementsSniff.php,v 1.1 2008/06/24 06:37:54 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff. |
* |
* Ensures each statement is on a line by itself. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Formatting_DisallowMultipleStatementsSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array(T_SEMICOLON); |
}//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(); |
$prev = $phpcsFile->findPrevious(T_SEMICOLON, ($stackPtr - 1)); |
if ($prev === false) { |
return; |
} |
// Ignore multiple statements in a FOR condition. |
if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
foreach ($tokens[$stackPtr]['nested_parenthesis'] as $bracket) { |
$owner = $tokens[$bracket]['parenthesis_owner']; |
if ($tokens[$owner]['code'] === T_FOR) { |
return; |
} |
} |
} |
if ($tokens[$prev]['line'] === $tokens[$stackPtr]['line']) { |
$error = 'Each PHP statement must be on a line by itself'; |
$phpcsFile->addError($error, $stackPtr); |
return; |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/DisallowTabIndentSniff.php |
---|
New file |
0,0 → 1,87 |
<?php |
/** |
* Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: DisallowTabIndentSniff.php,v 1.3 2008/10/28 04:43:57 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff. |
* |
* Throws errors if tabs are used for indentation. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* A list of tokenizers this sniff supports. |
* |
* @var array |
*/ |
public $supportedTokenizers = array( |
'PHP', |
'JS', |
'CSS', |
); |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array(T_WHITESPACE); |
}//end register() |
/** |
* Processes this test, when one of its tokens is encountered. |
* |
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
* @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(); |
// Make sure this is whitespace used for indentation. |
$line = $tokens[$stackPtr]['line']; |
if ($stackPtr > 0 && $tokens[($stackPtr - 1)]['line'] === $line) { |
return; |
} |
if (strpos($tokens[$stackPtr]['content'], "\t") !== false) { |
$error = 'Spaces must be used to indent lines; tabs are not allowed'; |
$phpcsFile->addError($error, $stackPtr); |
} |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/WhiteSpace/ScopeIndentSniff.php |
---|
New file |
0,0 → 1,311 |
<?php |
/** |
* Generic_Sniffs_Whitespace_ScopeIndentSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: ScopeIndentSniff.php,v 1.12 2008/12/02 02:38:34 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_Whitespace_ScopeIndentSniff. |
* |
* Checks that control structures are structured correctly, and their content |
* is indented correctly. This sniff will throw errors if tabs are used |
* for indentation rather than spaces. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_WhiteSpace_ScopeIndentSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* The number of spaces code should be indented. |
* |
* @var int |
*/ |
protected $indent = 4; |
/** |
* Does the indent need to be exactly right. |
* |
* If TRUE, indent needs to be exactly $ident spaces. If FALSE, |
* indent needs to be at least $ident spaces (but can be more). |
* |
* @var bool |
*/ |
protected $exact = false; |
/** |
* Any scope openers that should not cause an indent. |
* |
* @var array(int) |
*/ |
protected $nonIndentingScopes = array(); |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return PHP_CodeSniffer_Tokens::$scopeOpeners; |
}//end register() |
/** |
* Processes this test, when one of its tokens is encountered. |
* |
* @param PHP_CodeSniffer_File $phpcsFile All the tokens found in the document. |
* @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(); |
// If this is an inline condition (ie. there is no scope opener), then |
// return, as this is not a new scope. |
if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
return; |
} |
if ($tokens[$stackPtr]['code'] === T_ELSE) { |
$next = $phpcsFile->findNext( |
PHP_CodeSniffer_Tokens::$emptyTokens, |
($stackPtr + 1), |
null, |
true |
); |
// We will handle the T_IF token in another call to process. |
if ($tokens[$next]['code'] === T_IF) { |
return; |
} |
} |
// Find the first token on this line. |
$firstToken = $stackPtr; |
for ($i = $stackPtr; $i >= 0; $i--) { |
// Record the first code token on the line. |
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$emptyTokens) === false) { |
$firstToken = $i; |
} |
// It's the start of the line, so we've found our first php token. |
if ($tokens[$i]['column'] === 1) { |
break; |
} |
} |
// Based on the conditions that surround this token, determine the |
// indent that we expect this current content to be. |
$expectedIndent = $this->calculateExpectedIndent($tokens, $firstToken); |
if ($tokens[$firstToken]['column'] !== $expectedIndent) { |
$error = 'Line indented incorrectly; expected '; |
$error .= ($expectedIndent - 1).' spaces, found '; |
$error .= ($tokens[$firstToken]['column'] - 1); |
$phpcsFile->addError($error, $stackPtr); |
} |
$scopeOpener = $tokens[$stackPtr]['scope_opener']; |
$scopeCloser = $tokens[$stackPtr]['scope_closer']; |
// Some scopes are expected not to have indents. |
if (in_array($tokens[$firstToken]['code'], $this->nonIndentingScopes) === false) { |
$indent = ($expectedIndent + $this->indent); |
} else { |
$indent = $expectedIndent; |
} |
$newline = false; |
$commentOpen = false; |
$inHereDoc = false; |
// Only loop over the content beween the opening and closing brace, not |
// the braces themselves. |
for ($i = ($scopeOpener + 1); $i < $scopeCloser; $i++) { |
// If this token is another scope, skip it as it will be handled by |
// another call to this sniff. |
if (in_array($tokens[$i]['code'], PHP_CodeSniffer_Tokens::$scopeOpeners) === true) { |
if (isset($tokens[$i]['scope_opener']) === true) { |
$i = $tokens[$i]['scope_closer']; |
} else { |
// If this token does not have a scope_opener indice, then |
// it's probably an inline scope, so let's skip to the next |
// semicolon. Inline scopes include inline if's, abstract |
// methods etc. |
$nextToken = $phpcsFile->findNext(T_SEMICOLON, $i, $scopeCloser); |
if ($nextToken !== false) { |
$i = $nextToken; |
} |
} |
continue; |
} |
// If this is a HEREDOC then we need to ignore it as the |
// whitespace before the contents within the HEREDOC are |
// considered part of the content. |
if ($tokens[$i]['code'] === T_START_HEREDOC) { |
$inHereDoc = true; |
continue; |
} else if ($inHereDoc === true) { |
if ($tokens[$i]['code'] === T_END_HEREDOC) { |
$inHereDoc = false; |
} |
continue; |
} |
if ($tokens[$i]['column'] === 1) { |
// We started a newline. |
$newline = true; |
} |
if ($newline === true && $tokens[$i]['code'] !== T_WHITESPACE) { |
// If we started a newline and we find a token that is not |
// whitespace, then this must be the first token on the line that |
// must be indented. |
$newline = false; |
$firstToken = $i; |
$column = $tokens[$firstToken]['column']; |
// Special case for non-PHP code. |
if ($tokens[$firstToken]['code'] === T_INLINE_HTML) { |
$trimmedContentLength |
= strlen(ltrim($tokens[$firstToken]['content'])); |
if ($trimmedContentLength === 0) { |
continue; |
} |
$contentLength = strlen($tokens[$firstToken]['content']); |
$column = ($contentLength - $trimmedContentLength + 1); |
} |
// Check to see if this constant string spans multiple lines. |
// If so, then make sure that the strings on lines other than the |
// first line are indented appropriately, based on their whitespace. |
if (in_array($tokens[$firstToken]['code'], PHP_CodeSniffer_Tokens::$stringTokens) === true) { |
if (in_array($tokens[($firstToken - 1)]['code'], PHP_CodeSniffer_Tokens::$stringTokens) === true) { |
// If we find a string that directly follows another string |
// then its just a string that spans multiple lines, so we |
// don't need to check for indenting. |
continue; |
} |
} |
// This is a special condition for T_DOC_COMMENT and C-style |
// comments, which contain whitespace between each line. |
$comments = array( |
T_COMMENT, |
T_DOC_COMMENT |
); |
if (in_array($tokens[$firstToken]['code'], $comments) === true) { |
$content = trim($tokens[$firstToken]['content']); |
if (preg_match('|^/\*|', $content) !== 0) { |
// Check to see if the end of the comment is on the same line |
// as the start of the comment. If it is, then we don't |
// have to worry about opening a comment. |
if (preg_match('|\*/$|', $content) === 0) { |
// We don't have to calculate the column for the |
// start of the comment as there is a whitespace |
// token before it. |
$commentOpen = true; |
} |
} else if ($commentOpen === true) { |
if ($content === '') { |
// We are in a comment, but this line has nothing on it |
// so let's skip it. |
continue; |
} |
$contentLength = strlen($tokens[$firstToken]['content']); |
$trimmedContentLength |
= strlen(ltrim($tokens[$firstToken]['content'])); |
$column = ($contentLength - $trimmedContentLength + 1); |
if (preg_match('|\*/$|', $content) !== 0) { |
$commentOpen = false; |
} |
}//end if |
}//end if |
// The token at the start of the line, needs to have its' column |
// greater than the relative indent we set above. If it is less, |
// an error should be shown. |
if ($column !== $indent) { |
if ($this->exact === true || $column < $indent) { |
$error = 'Line indented incorrectly; expected '; |
if ($this->exact === false) { |
$error .= 'at least '; |
} |
$error .= ($indent - 1).' spaces, found '; |
$error .= ($column - 1); |
$phpcsFile->addError($error, $firstToken); |
} |
} |
}//end if |
}//end for |
}//end process() |
/** |
* Calculates the expected indent of a token. |
* |
* @param array $tokens The stack of tokens for this file. |
* @param int $stackPtr The position of the token to get indent for. |
* |
* @return int |
*/ |
protected function calculateExpectedIndent(array $tokens, $stackPtr) |
{ |
$conditionStack = array(); |
// Empty conditions array (top level structure). |
if (empty($tokens[$stackPtr]['conditions']) === true) { |
return 1; |
} |
$tokenConditions = $tokens[$stackPtr]['conditions']; |
foreach ($tokenConditions as $id => $condition) { |
// If it's an indenting scope ie. it's not in our array of |
// scopes that don't indent, add it to our condition stack. |
if (in_array($condition, $this->nonIndentingScopes) === false) { |
$conditionStack[$id] = $condition; |
} |
} |
return ((count($conditionStack) * $this->indent) + 1); |
}//end calculateExpectedIndent() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/Classes/DuplicateClassNameSniff.php |
---|
New file |
0,0 → 1,74 |
<?php |
/** |
* Reports errors if the same class or interface name is used in multiple files. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: DuplicateClassNameSniff.php,v 1.1 2008/07/25 04:24:10 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Reports errors if the same class or interface name is used in multiple files. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_Classes_DuplicateClassNameSniff implements PHP_CodeSniffer_MultiFileSniff |
{ |
/** |
* Called once per script run to allow for processing of this sniff. |
* |
* @param array(PHP_CodeSniffer_File) $files The PHP_CodeSniffer files processed |
* during the script run. |
* |
* @return void |
*/ |
public function process(array $files) |
{ |
$foundClasses = array(); |
foreach ($files as $phpcsFile) { |
$tokens = $phpcsFile->getTokens(); |
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), 0); |
while ($stackPtr !== false) { |
$nameToken = $phpcsFile->findNext(T_STRING, $stackPtr); |
$name = $tokens[$nameToken]['content']; |
$compareName = strtolower($name); |
if (isset($foundClasses[$compareName]) === true) { |
$type = strtolower($tokens[$stackPtr]['content']); |
$file = $foundClasses[$compareName]['file']; |
$line = $foundClasses[$compareName]['line']; |
$error = "Duplicate $type name \"$name\" found; first defined in $file on line $line"; |
$phpcsFile->addWarning($error, $stackPtr); |
} else { |
$foundClasses[$compareName] = array( |
'file' => $phpcsFile->getFilename(), |
'line' => $tokens[$stackPtr]['line'], |
); |
} |
$stackPtr = $phpcsFile->findNext(array(T_CLASS, T_INTERFACE), ($stackPtr + 1)); |
}//end while |
}//end foreach |
}//end process() |
}//end class |
?> |
/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 |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/NamingConventions/UpperCaseConstantNameSniff.php |
---|
New file |
0,0 → 1,151 |
<?php |
/** |
* Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: UpperCaseConstantNameSniff.php,v 1.10 2008/05/07 23:13:49 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
/** |
* Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff. |
* |
* Ensures that constant names are all uppercase. |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Marc McIntyre <mmcintyre@squiz.net> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_NamingConventions_UpperCaseConstantNameSniff implements PHP_CodeSniffer_Sniff |
{ |
/** |
* Returns an array of tokens this test wants to listen for. |
* |
* @return array |
*/ |
public function register() |
{ |
return array(T_STRING); |
}//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(); |
$constName = $tokens[$stackPtr]['content']; |
// If this token is in a heredoc, ignore it. |
if ($phpcsFile->hasCondition($stackPtr, T_START_HEREDOC) === true) { |
return; |
} |
// If the next non-whitespace token after this token |
// is not an opening parenthesis then it is not a function call. |
$openBracket = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true); |
if ($tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { |
$functionKeyword = $phpcsFile->findPrevious(array(T_WHITESPACE, T_COMMA, T_COMMENT, T_STRING), ($stackPtr - 1), null, true); |
$declarations = array( |
T_FUNCTION, |
T_CLASS, |
T_INTERFACE, |
T_IMPLEMENTS, |
T_EXTENDS, |
T_INSTANCEOF, |
T_NEW, |
); |
if (in_array($tokens[$functionKeyword]['code'], $declarations) === true) { |
// This is just a declaration; no constants here. |
return; |
} |
if ($tokens[$functionKeyword]['code'] === T_CONST) { |
// This is a class constant. |
if (strtoupper($constName) !== $constName) { |
$error = 'Class constants must be uppercase; expected '.strtoupper($constName)." but found $constName"; |
$phpcsFile->addError($error, $stackPtr); |
} |
return; |
} |
// Is this a class name? |
$nextPtr = $phpcsFile->findNext(array(T_WHITESPACE), ($stackPtr + 1), null, true); |
if ($tokens[$nextPtr]['code'] === T_DOUBLE_COLON) { |
return; |
} |
// Is this a type hint? |
if ($tokens[$nextPtr]['code'] === T_VARIABLE) { |
return; |
} else if ($phpcsFile->isReference($nextPtr) === true) { |
return; |
} |
// Is this a member var name? |
$prevPtr = $phpcsFile->findPrevious(array(T_WHITESPACE), ($stackPtr - 1), null, true); |
if ($tokens[$prevPtr]['code'] === T_OBJECT_OPERATOR) { |
return; |
} |
// Is this an instance of declare() |
$prevPtr = $phpcsFile->findPrevious(array(T_WHITESPACE, T_OPEN_PARENTHESIS), ($stackPtr - 1), null, true); |
if ($tokens[$prevPtr]['code'] === T_DECLARE) { |
return; |
} |
// This is a real constant. |
if (strtoupper($constName) !== $constName) { |
$error = 'Constants must be uppercase; expected '.strtoupper($constName)." but found $constName"; |
$phpcsFile->addError($error, $stackPtr); |
} |
} else if (strtolower($constName) === 'define' || strtolower($constName) === 'constant') { |
/* |
This may be a "define" or "constant" function call. |
*/ |
// The next non-whitespace token must be the constant name. |
$constPtr = $phpcsFile->findNext(array(T_WHITESPACE), ($openBracket + 1), null, true); |
if ($tokens[$constPtr]['code'] !== T_CONSTANT_ENCAPSED_STRING) { |
return; |
} |
$constName = $tokens[$constPtr]['content']; |
if (strtoupper($constName) !== $constName) { |
$error = 'Constants must be uppercase; expected '.strtoupper($constName)." but found $constName"; |
$phpcsFile->addError($error, $stackPtr); |
} |
}//end if |
}//end process() |
}//end class |
?> |
/trunk/tests/PHP_CodeSniffer-1.2.0RC1/CodeSniffer/Standards/Generic/Sniffs/NamingConventions/ConstructorNameSniff.php |
---|
New file |
0,0 → 1,102 |
<?php |
/** |
* Generic_Sniffs_NamingConventions_ConstructorNameSniff. |
* |
* PHP version 5 |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Greg Sherwood <gsherwood@squiz.net> |
* @author Leif Wickland <lwickland@rightnow.com> |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600) |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version CVS: $Id: ConstructorNameSniff.php,v 1.2 2009/02/24 04:43:44 squiz Exp $ |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
if (class_exists('PHP_CodeSniffer_Standards_AbstractScopeSniff', true) === false) { |
$error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found'; |
throw new PHP_CodeSniffer_Exception($error); |
} |
/** |
* Generic_Sniffs_NamingConventions_ConstructorNameSniff. |
* |
* Favor PHP 5 constructor syntax, which uses "function __construct()". |
* Avoid PHP 4 constructor syntax, which uses "function ClassName()". |
* |
* @category PHP |
* @package PHP_CodeSniffer |
* @author Leif Wickland <lwickland@rightnow.com> |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence |
* @version Release: 1.2.0RC1 |
* @link http://pear.php.net/package/PHP_CodeSniffer |
*/ |
class Generic_Sniffs_NamingConventions_ConstructorNameSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff |
{ |
/** |
* Constructs the test with the tokens it wishes to listen for. |
* |
* @return void |
*/ |
public function __construct() |
{ |
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true); |
}//end __construct() |
/** |
* Processes this test when one of its tokens is encountered. |
* |
* @param PHP_CodeSniffer_File $phpcsFile The current file being scanned. |
* @param int $stackPtr The position of the current token |
* in the stack passed in $tokens. |
* @param int $currScope A pointer to the start of the scope. |
* |
* @return void |
*/ |
protected function processTokenWithinScope( |
PHP_CodeSniffer_File $phpcsFile, |
$stackPtr, |
$currScope |
) { |
$className = $phpcsFile->getDeclarationName($currScope); |
$methodName = $phpcsFile->getDeclarationName($stackPtr); |
if (strcasecmp($methodName, $className) === 0) { |
$error = 'PHP4 style constructors are not allowed; use "__construct()" instead'; |
$phpcsFile->addError($error, $stackPtr); |
} else if (strcasecmp($methodName, '__construct') !== 0) { |
// Not a constructor. |
return; |
} |
$tokens = $phpcsFile->getTokens(); |
$parentClassName = $phpcsFile->findExtendedClassName($currScope); |
if ($parentClassName === false) { |
return; |
} |
$endFunctionIndex = $tokens[$stackPtr]['scope_closer']; |
$startIndex = $stackPtr; |
while ($doubleColonIndex = $phpcsFile->findNext(array(T_DOUBLE_COLON), $startIndex, $endFunctionIndex)) { |
if ($tokens[($doubleColonIndex + 1)]['code'] === T_STRING |
&& $tokens[($doubleColonIndex + 1)]['content'] === $parentClassName |
) { |
$error = 'PHP4 style calls to parent constructors are not allowed; use "parent::__construct()" instead'; |
$phpcsFile->addError($error, ($doubleColonIndex + 1)); |
} |
$startIndex = ($doubleColonIndex + 1); |
} |
}//end processTokenWithinScope() |
}//end class |
?> |