cvs: pear /PHP_CodeSniffer package.xml /PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope MemberVarScopeSniff.php MethodScopeSniff.php
StaticThisUsageSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope MemberVarScopeUnitTest.inc MemberVarScopeUnitTest.php
MethodScopeUnitTest.inc MethodScopeUnitTest.php StaticThisUsageUnitTest.inc StaticThisUsageUnitTest.php
| From: | Greg Sherwood | Date: | Thu, 28 Dec 2006 00:25:43 +0000 |
| Subject: | cvs: pear /PHP_CodeSniffer package.xml /PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope MemberVarScopeSniff.php MethodScopeSniff.php StaticThisUsageSniff.php /PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope MemberVarScopeUnitTest.inc MemberVarScopeUnitTest.php MethodScopeUnitTest.inc MethodScopeUnitTest.php StaticThisUsageUnitTest.inc StaticThisUsageUnitTest.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-43859@lists.php.net to get a copy of this message | ||
squiz Thu Dec 28 00:25:43 2006 UTC
Added files:
/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope
MemberVarScopeSniff.php
MethodScopeSniff.php
StaticThisUsageSniff.php
/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope
MemberVarScopeUnitTest.inc
MemberVarScopeUnitTest.php
MethodScopeUnitTest.inc
MethodScopeUnitTest.php
StaticThisUsageUnitTest.inc
StaticThisUsageUnitTest.php
Modified files:
/pear/PHP_CodeSniffer package.xml
Log:
Added scope sniffs to Squiz coding standard
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/package.xml?r1=1.18&r2=1.19&diff_format=u Index: pear/PHP_CodeSniffer/package.xml diff -u pear/PHP_CodeSniffer/package.xml:1.18 pear/PHP_CodeSniffer/package.xml:1.19 --- pear/PHP_CodeSniffer/package.xml:1.18 Wed Dec 27 23:49:08 2006 +++ pear/PHP_CodeSniffer/package.xml Thu Dec 28 00:25:43 2006 @@ -488,6 +488,17 @@ <replace type="package-info" from="@package_version@" to="version"/> </file> </dir> + <dir name="Scope"> + <file name="MemberVarScopeSniff.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="MethodScopeSniff.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="StaticThisUsageSniff.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + </dir> <dir name="Strings"> <file name="ConcatenationSpacingSniff.php"> <replace type="package-info" from="@package_version@" to="version"/> @@ -629,6 +640,20 @@ <replace type="package-info" from="@package_version@" to="version"/> </file> </dir> + <dir name="Scope"> + <file name="MemberVarScopeUnitTest.inc"/> + <file name="MemberVarScopeUnitTest.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="MethodScopeUnitTest.inc"/> + <file name="MethodScopeUnitTest.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="StaticThisUsageUnitTest.inc"/> + <file name="StaticThisUsageUnitTest.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + </dir> <dir name="Strings"> <file name="ConcatenationSpacingUnitTest.inc"/> <file name="ConcatenationSpacingUnitTest.php"> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php <?php /** * Verifies that class members have scope modifiers. * * 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: MemberVarScopeSniff.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Standards/AbstractVariableSniff.php'; require_once 'PHP/CodeSniffer/Tokens.php'; /** * Verifies that class members have scope modifiers. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Sniffs_Scope_MemberVarScopeSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff { /** * Processes the function tokens within the class. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr); if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) { $var = $tokens[$stackPtr]['content']; $error = "Scope modifier not specified for member variable \"$var\""; $phpcsFile->addError($error, $stackPtr); } }//end processMemberVar() /** * Processes normal variables. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { // We don't care about normal variables. return; }//end processVariable() /** * Processes variables in double quoted strings. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { // We don't care about normal variables. return; }//end processVariableInString() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php <?php /** * Verifies that class members have scope modifiers. * * 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: MethodScopeSniff.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Standards/AbstractScopeSniff.php'; /** * Verifies that class members have scope modifiers. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Sniffs_Scope_MethodScopeSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff { /** * Constructs a Squiz_Sniffs_Scope_MethodScopeSniff. */ public function __construct() { parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION)); }//end __construct() /** * Processes the function tokens within the class. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * @param int $currScope The current scope opener token. * * @return void */ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $tokens = $phpcsFile->getTokens(); $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr); if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) { $name = $phpcsFile->findNext(T_STRING, $stackPtr + 1); $name = $tokens[$name]['content']; $error = "No scope modifier specified for function \"$name\""; $phpcsFile->addError($error, $stackPtr); } }//end processTokenWithinScope() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/StaticThisUsageSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/StaticThisUsageSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/StaticThisUsageSniff.php <?php /** * Squiz_Sniffs_Scope_StaticThisUsageSniff. * * 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: StaticThisUsageSniff.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Standards/AbstractScopeSniff.php'; /** * Squiz_Sniffs_Scope_StaticThisUsageSniff. * * Checks for usage of "$this" in static methods, which will cause * runtime errors. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Sniffs_Scope_StaticThisUsageSniff 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), array(T_FUNCTION)); }//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 */ public function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $tokens = $phpcsFile->getTokens(); $function = $tokens[$stackPtr + 2]; if ($function['code'] !== T_STRING) { return; } $functionName = $function['content']; $classOpener = $tokens[$currScope]['scope_condition']; $className = $tokens[$classOpener + 2]['content']; $methodProps = $phpcsFile->getMethodProperties($stackPtr); if ($methodProps['is_static'] === true) { $thisUsage = $stackPtr; while (($thisUsage = $phpcsFile->findNext(array(T_VARIABLE), $thisUsage + 1, $tokens[$stackPtr]['scope_closer'], false, '$this')) !== false) { if ($thisUsage === false) { return; } $error = 'Usage of "$this" in static methods will cause runtime errors'; $phpcsFile->addError($error, $thisUsage); } } }//end processTokenWithinScope() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.inc <?php class MyClass { private $var1 = null; protected $var2 = null; public $var3 = null; $var4 = null; } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.php <?php /** * Unit test class for the MemberVarScope sniff. * * 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: MemberVarScopeUnitTest.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the MemberVarScope sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Tests_Scope_MemberVarScopeUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 7 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc <?php function func1() {} class MyClass { function func1() {} public function func1() {} private function func1() {} protected function func1() {} } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.php <?php /** * Unit test class for the MethodScope sniff. * * 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: MethodScopeUnitTest.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the MethodScope sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Tests_Scope_MethodScopeUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 6 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.inc <?php class MyClass { public static function func1() { $value = 'hello'; $newValue = array($this->func2()); $result = $this->getValue($value); return $this->setValue($result); } public function func1() { $value = 'hello'; $newValue = array($this->func2()); $result = $this->getValue($value); return $this->setValue($result); } function func1() { $value = 'hello'; $newValue = array($this->func2()); $result = $this->getValue($value); return $this->setValue($result); } } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.php <?php /** * Unit test class for the StaticThisUsage sniff. * * 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: StaticThisUsageUnitTest.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the StaticThisUsage sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Tests_Scope_StaticThisUsageUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 7 => 1, 8 => 1, 9 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?>
http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/package.xml?r1=1.18&r2=1.19&diff_format=u Index: pear/PHP_CodeSniffer/package.xml diff -u pear/PHP_CodeSniffer/package.xml:1.18 pear/PHP_CodeSniffer/package.xml:1.19 --- pear/PHP_CodeSniffer/package.xml:1.18 Wed Dec 27 23:49:08 2006 +++ pear/PHP_CodeSniffer/package.xml Thu Dec 28 00:25:43 2006 @@ -488,6 +488,17 @@ <replace type="package-info" from="@package_version@" to="version"/> </file> </dir> + <dir name="Scope"> + <file name="MemberVarScopeSniff.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="MethodScopeSniff.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="StaticThisUsageSniff.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + </dir> <dir name="Strings"> <file name="ConcatenationSpacingSniff.php"> <replace type="package-info" from="@package_version@" to="version"/> @@ -629,6 +640,20 @@ <replace type="package-info" from="@package_version@" to="version"/> </file> </dir> + <dir name="Scope"> + <file name="MemberVarScopeUnitTest.inc"/> + <file name="MemberVarScopeUnitTest.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="MethodScopeUnitTest.inc"/> + <file name="MethodScopeUnitTest.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + <file name="StaticThisUsageUnitTest.inc"/> + <file name="StaticThisUsageUnitTest.php"> + <replace type="package-info" from="@package_version@" to="version"/> + </file> + </dir> <dir name="Strings"> <file name="ConcatenationSpacingUnitTest.inc"/> <file name="ConcatenationSpacingUnitTest.php"> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MemberVarScopeSniff.php <?php /** * Verifies that class members have scope modifiers. * * 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: MemberVarScopeSniff.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Standards/AbstractVariableSniff.php'; require_once 'PHP/CodeSniffer/Tokens.php'; /** * Verifies that class members have scope modifiers. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Sniffs_Scope_MemberVarScopeSniff extends PHP_CodeSniffer_Standards_AbstractVariableSniff { /** * Processes the function tokens within the class. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr); if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) { $var = $tokens[$stackPtr]['content']; $error = "Scope modifier not specified for member variable \"$var\""; $phpcsFile->addError($error, $stackPtr); } }//end processMemberVar() /** * Processes normal variables. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { // We don't care about normal variables. return; }//end processVariable() /** * Processes variables in double quoted strings. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * * @return void */ protected function processVariableInString(PHP_CodeSniffer_File $phpcsFile, $stackPtr) { // We don't care about normal variables. return; }//end processVariableInString() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/MethodScopeSniff.php <?php /** * Verifies that class members have scope modifiers. * * 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: MethodScopeSniff.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Standards/AbstractScopeSniff.php'; /** * Verifies that class members have scope modifiers. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Sniffs_Scope_MethodScopeSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff { /** * Constructs a Squiz_Sniffs_Scope_MethodScopeSniff. */ public function __construct() { parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION)); }//end __construct() /** * Processes the function tokens within the class. * * @param PHP_CodeSniffer_File $phpcsFile The file where this token was found. * @param int $stackPtr The position where the token was found. * @param int $currScope The current scope opener token. * * @return void */ protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $tokens = $phpcsFile->getTokens(); $modifier = $phpcsFile->findPrevious(PHP_CodeSniffer_Tokens::$scopeModifiers, $stackPtr); if (($modifier === false) || ($tokens[$modifier]['line'] !== $tokens[$stackPtr]['line'])) { $name = $phpcsFile->findNext(T_STRING, $stackPtr + 1); $name = $tokens[$name]['content']; $error = "No scope modifier specified for function \"$name\""; $phpcsFile->addError($error, $stackPtr); } }//end processTokenWithinScope() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/StaticThisUsageSniff.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/StaticThisUsageSniff.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Sniffs/Scope/StaticThisUsageSniff.php <?php /** * Squiz_Sniffs_Scope_StaticThisUsageSniff. * * 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: StaticThisUsageSniff.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ require_once 'PHP/CodeSniffer/Standards/AbstractScopeSniff.php'; /** * Squiz_Sniffs_Scope_StaticThisUsageSniff. * * Checks for usage of "$this" in static methods, which will cause * runtime errors. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Sniffs_Scope_StaticThisUsageSniff 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), array(T_FUNCTION)); }//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 */ public function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) { $tokens = $phpcsFile->getTokens(); $function = $tokens[$stackPtr + 2]; if ($function['code'] !== T_STRING) { return; } $functionName = $function['content']; $classOpener = $tokens[$currScope]['scope_condition']; $className = $tokens[$classOpener + 2]['content']; $methodProps = $phpcsFile->getMethodProperties($stackPtr); if ($methodProps['is_static'] === true) { $thisUsage = $stackPtr; while (($thisUsage = $phpcsFile->findNext(array(T_VARIABLE), $thisUsage + 1, $tokens[$stackPtr]['scope_closer'], false, '$this')) !== false) { if ($thisUsage === false) { return; } $error = 'Usage of "$this" in static methods will cause runtime errors'; $phpcsFile->addError($error, $thisUsage); } } }//end processTokenWithinScope() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.inc <?php class MyClass { private $var1 = null; protected $var2 = null; public $var3 = null; $var4 = null; } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MemberVarScopeUnitTest.php <?php /** * Unit test class for the MemberVarScope sniff. * * 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: MemberVarScopeUnitTest.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the MemberVarScope sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Tests_Scope_MemberVarScopeUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 7 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.inc <?php function func1() {} class MyClass { function func1() {} public function func1() {} private function func1() {} protected function func1() {} } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/MethodScopeUnitTest.php <?php /** * Unit test class for the MethodScope sniff. * * 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: MethodScopeUnitTest.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the MethodScope sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Tests_Scope_MethodScopeUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 6 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.inc?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.inc +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.inc <?php class MyClass { public static function func1() { $value = 'hello'; $newValue = array($this->func2()); $result = $this->getValue($value); return $this->setValue($result); } public function func1() { $value = 'hello'; $newValue = array($this->func2()); $result = $this->getValue($value); return $this->setValue($result); } function func1() { $value = 'hello'; $newValue = array($this->func2()); $result = $this->getValue($value); return $this->setValue($result); } } ?> http://cvs.php.net/viewvc.cgi/pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.php?view=markup&rev=1.1 Index: pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.php +++ pear/PHP_CodeSniffer/CodeSniffer/Standards/Squiz/Tests/Scope/StaticThisUsageUnitTest.php <?php /** * Unit test class for the StaticThisUsage sniff. * * 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: StaticThisUsageUnitTest.php,v 1.1 2006/12/28 00:25:43 squiz Exp $ * @link http://pear.php.net/package/PHP_CodeSniffer */ /** * Unit test class for the StaticThisUsage sniff. * * A sniff unit test checks a .inc file for expected violations of a single * coding standard. Expected errors and warnings are stored in this class. * * @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: @package_version@ * @link http://pear.php.net/package/PHP_CodeSniffer */ class Squiz_Tests_Scope_StaticThisUsageUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. * * The key of the array should represent the line number and the value * should represent the number of errors that should occur on that line. * * @return array(int => int) */ public function getErrorList() { return array( 7 => 1, 8 => 1, 9 => 1, ); }//end getErrorList() /** * Returns the lines where warnings should occur. * * The key of the array should represent the line number and the value * should represent the number of warnings that should occur on that line. * * @return array(int => int) */ public function getWarningList() { return array(); }//end getWarningList() }//end class ?>