diff --git a/.travis.yml b/.travis.yml index e9f260ed..f1c2c60b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,72 +1,86 @@ sudo: false +dist: trusty + +cache: + apt: true + language: - php php: - - 5.2 - - 5.3 - 5.4 - 5.5 - 5.6 - 7.0 - 7.1 + - nightly env: - - PHPCS_BRANCH=master - - PHPCS_BRANCH=2.8.1 + # `master` is now 3.x. + - PHPCS_BRANCH=master LINT=1 + # Lowest tagged release in the 2.x series with which WPCS is compatible. + - PHPCS_BRANCH=2.9.0 matrix: fast_finish: true include: - # Run PHPCS against WPCS. I just picked to run it against 5.5. - - php: 5.5 + # Run PHPCS against WPCS. I just picked to run it against 7.0. + - php: 7.0 env: PHPCS_BRANCH=master SNIFF=1 - # Run against PHPCS 3.0. I just picked to run it against 5.6. - - php: 5.6 - env: PHPCS_BRANCH=3.0 + addons: + apt: + packages: + - libxml2-utils + # Run against HHVM and PHP nightly. - php: hhvm sudo: required dist: trusty group: edge - env: PHPCS_BRANCH=master - - php: nightly - env: PHPCS_BRANCH=master - # Run custom integration test. I just picked to run it against 7.0. + env: PHPCS_BRANCH=master LINT=1 + + # Test PHP 5.3 only against PHPCS 2.x as PHPCS 3.x has a minimum requirement of PHP 5.4. + - php: 5.3 + env: PHPCS_BRANCH=2.9 LINT=1 + dist: precise + # Test PHP 5.3 with short_open_tags set to On (is Off by default) + - php: 5.3 + env: PHPCS_BRANCH=2.9.0 SHORT_OPEN_TAGS=true + dist: precise + # Run custom integration test. I just picked to it agains PHP 7.0. - php: 7.0 env: PHPCS_BRANCH=master INTEGRATION_TEST=1 + allow_failures: # Allow failures for unstable builds. - php: nightly - php: hhvm - - env: PHPCS_BRANCH=3.0 - -before_script: - - export PATH="$HOME/.composer/vendor/bin:$PATH" +before_install: + - export XMLLINT_INDENT=" " - export PHPCS_DIR=/tmp/phpcs - export WPCS_DIR=/tmp/wpcs - - export PHPCS_BIN=$(if [[ $PHPCS_BRANCH == 3.0 ]]; then echo $PHPCS_DIR/bin/phpcs; else echo $PHPCS_DIR/scripts/phpcs; fi) + - export PHPUNIT_DIR=/tmp/phpunit + - export PHPCS_BIN=$(if [[ $PHPCS_BRANCH == master ]]; then echo $PHPCS_DIR/bin/phpcs; else echo $PHPCS_DIR/scripts/phpcs; fi) - mkdir -p $PHPCS_DIR && git clone --depth 1 https://github.com/squizlabs/PHP_CodeSniffer.git -b $PHPCS_BRANCH $PHPCS_DIR - mkdir -p $WPCS_DIR && git clone --depth 1 https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git -b master $WPCS_DIR - - ln -s $WPCS_DIR/WordPress $PHPCS_DIR/CodeSniffer/Standards/WordPress - - ln -s $WPCS_DIR/WordPress-Core $PHPCS_DIR/CodeSniffer/Standards/WordPress-Core - - ln -s $WPCS_DIR/WordPress-Docs $PHPCS_DIR/CodeSniffer/Standards/WordPress-Docs - - ln -s $WPCS_DIR/WordPress-Extra $PHPCS_DIR/CodeSniffer/Standards/WordPress-Extra - - ln -s $WPCS_DIR/WordPress-VIP $PHPCS_DIR/CodeSniffer/Standards/WordPress-VIP - - $PHPCS_BIN --config-set installed_paths $(pwd) - - | - if [[ ${TRAVIS_PHP_VERSION:0:2} == "7." ]]; then - composer global require "phpunit/phpunit=5.7.*" - elif [[ ${TRAVIS_PHP_VERSION:0:3} != "5.2" ]]; then - composer global require "phpunit/phpunit=4.8.*" - fi; - - phpunit --version + - $PHPCS_BIN --config-set installed_paths $(pwd),$WPCS_DIR + # Download PHPUnit 5.x for builds on PHP 7, nightly and HHVM as the PHPCS + # test suite is currently not compatible with PHPUnit 6.x. + # Fixed at a very specific PHPUnit version which is also compatible with HHVM. + - if [[ ${TRAVIS_PHP_VERSION:0:2} != "5." ]]; then wget -P $PHPUNIT_DIR https://phar.phpunit.de/phpunit-5.7.17.phar && chmod +x $PHPUNIT_DIR/phpunit-5.7.17.phar; fi + # Selectively adjust the ini values for the build image to test ini value dependent sniff features. + - if [[ "$SHORT_OPEN_TAGS" == "true" ]]; then echo "short_open_tag = On" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi script: - - if find . -name "*.php" -exec php -l {} \; | grep "^[Parse error|Fatal error]"; then exit 1; fi; - - phpunit --filter WordPressVIPMinimum /tmp/phpcs/tests/AllTests.php + # Lint the PHP files against parse errors. + - if [[ "$LINT" == "1" ]]; then if find . -name "*.php" -exec php -l {} \; | grep "^[Parse error|Fatal error]"; then exit 1; fi; fi + # Run the unit tests. + - if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." && ${PHPCS_BRANCH:0:2} == "2." ]]; then phpunit --bootstrap $WPCS_DIR/Test/bootstrap.php --filter WordPressVIPMinimum $WPCS_DIR/Test/AllTests.php; fi + - if [[ ${TRAVIS_PHP_VERSION:0:2} == "5." && ${PHPCS_BRANCH:0:2} != "2." ]]; then phpunit --bootstrap $WPCS_DIR/Test/bootstrap.php --filter WordPressVIPMinimum $PHPCS_DIR/tests/AllTests.php; fi + - if [[ ${TRAVIS_PHP_VERSION:0:2} != "5." && ${PHPCS_BRANCH:0:2} == "2." ]]; then php $PHPUNIT_DIR/phpunit-5.7.17.phar --bootstrap $WPCS_DIR/Test/bootstrap.php --filter WordPressVIPMinimum $WPCS_DIR/Test/AllTests.php; fi + - if [[ ${TRAVIS_PHP_VERSION:0:2} != "5." && ${PHPCS_BRANCH:0:2} != "2." ]]; then php $PHPUNIT_DIR/phpunit-5.7.17.phar --bootstrap $WPCS_DIR/Test/bootstrap.php --filter WordPressVIPMinumum $PHPCS_DIR/tests/AllTests.php; fi # WordPress Coding Standards. # @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards # @link http://pear.php.net/package/PHP_CodeSniffer/ @@ -76,5 +90,10 @@ script: # -n flag: Do not print warnings. (shortcut for --warning-severity=0) # --standard: Use WordPress as the standard. # --extensions: Only sniff PHP files. - - if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/scripts/phpcs -p -s -n . --standard=./bin/phpcs.xml --extensions=php; fi + - if [[ "$SNIFF" == "1" ]]; then $PHPCS_BIN . --standard=./bin/phpcs.xml --runtime-set ignore_warnings_on_exit 1; fi + # Validate the xml files. + # @link http://xmlsoft.org/xmllint.html + - if [[ "$SNIFF" == "1" ]]; then xmllint --noout ./*/ruleset.xml; fi + # Check the code-style consistency of the xml files. + - if [[ "$SNIFF" == "1" ]]; then diff -B --tabsize=4 ./WordPressVIPMinimum/ruleset.xml <(xmllint --format "./WordPressVIPMinimum/ruleset.xml"); fi - if [[ "$INTEGRATION_TEST" == "1" ]]; then php ./ruleset_test.php; fi diff --git a/WordPressVIPMinimum/Sniffs/Actions/PreGetPostsSniff.php b/WordPressVIPMinimum/Sniffs/Actions/PreGetPostsSniff.php index 7563f683..790dd9f2 100644 --- a/WordPressVIPMinimum/Sniffs/Actions/PreGetPostsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Actions/PreGetPostsSniff.php @@ -5,6 +5,11 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Sniffs\Actions; + +use PHP_CodeSniffer_File as File; +use PHP_CodeSniffer_Tokens as Tokens; + /** * This sniff validates a propper usage of pre_get_posts action callback * @@ -12,7 +17,7 @@ * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPminimum_Sniffs_Actions_PreGetPostsSniff implements PHP_CodeSniffer_Sniff { +class PreGetPostsSniff implements \PHP_CodeSniffer_Sniff { /** * The tokens of the phpcsFile. @@ -34,7 +39,7 @@ class WordPressVIPminimum_Sniffs_Actions_PreGetPostsSniff implements PHP_CodeSni * @return array(int) */ public function register() { - return PHP_CodeSniffer_Tokens::$functionNameTokens; + return Tokens::$functionNameTokens; }//end register() @@ -42,13 +47,13 @@ public function register() { /** * Processes the tokens that this sniff is interested in. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. - * @param int $stackPtr The position in the stack where - * the token was found. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. + * @param int $stackPtr The position in the stack where + * the token was found. * * @return void */ - public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + public function process( File $phpcsFile, $stackPtr ) { $this->_tokens = $phpcsFile->getTokens(); @@ -62,7 +67,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { } $actionNamePtr = $this->_phpcsFile->findNext( - array_merge( PHP_CodeSniffer_Tokens::$emptyTokens, array( T_OPEN_PARENTHESIS ) ), // types. + array_merge( Tokens::$emptyTokens, array( T_OPEN_PARENTHESIS ) ), // types. $stackPtr + 1, // start. null, // end. true, // exclude. @@ -81,7 +86,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { } $callbackPtr = $this->_phpcsFile->findNext( - array_merge( PHP_CodeSniffer_Tokens::$emptyTokens, array( T_COMMA ) ), // types. + array_merge( Tokens::$emptyTokens, array( T_COMMA ) ), // types. $actionNamePtr + 1, // start. null, // end. true, // exclude. @@ -96,9 +101,9 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { if ( 'PHPCS_T_CLOSURE' === $this->_tokens[ $callbackPtr ]['code'] ) { $this->processClosure( $callbackPtr ); - } else if ( 'T_ARRAY' === $this->_tokens[ $callbackPtr ]['type'] ) { + } elseif ( 'T_ARRAY' === $this->_tokens[ $callbackPtr ]['type'] ) { $this->processArray( $callbackPtr ); - } else if ( true === in_array( $this->_tokens[ $callbackPtr ]['code'], PHP_CodeSniffer_Tokens::$stringTokens ) ) { + } elseif ( true === in_array( $this->_tokens[ $callbackPtr ]['code'], Tokens::$stringTokens, true ) ) { $this->processString( $callbackPtr ); } @@ -112,7 +117,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { private function processArray( $stackPtr ) { $previous = $this->_phpcsFile->findPrevious( - PHP_CodeSniffer_Tokens::$emptyTokens, // types + Tokens::$emptyTokens, // types $this->_tokens[ $stackPtr ]['parenthesis_closer'] - 1, // start. null, // end. true, // exclude. @@ -134,7 +139,7 @@ private function processString( $stackPtr ) { $callbackFunctionName = substr( $this->_tokens[ $stackPtr ]['content'], 1, -1 ); $callbackFunctionPtr = $this->_phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$functionNameTokens, // types. + Tokens::$functionNameTokens, // types. 0, // start. null, // end. false, // exclude. @@ -242,12 +247,12 @@ private function processFunctionBody( $stackPtr, $variableName ) { if ( $this->isEarlyMainQueryCheck( $wpQueryVarUsed ) ) { return; } - } else if ( $this->isInsideIfConditonal( $wpQueryVarUsed ) ) { + } elseif ( $this->isInsideIfConditonal( $wpQueryVarUsed ) ) { if ( ! $this->isParentConditionalCheckingMainQuery( $wpQueryVarUsed ) ) { - $this->_phpcsFile->addWarning( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection.', $wpQueryVarUsed ); + $this->_phpcsFile->addWarning( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection.', $wpQueryVarUsed, 'PreGetPosts' ); } - } else if ( $this->isWPQueryMethodCall( $wpQueryVarUsed, 'set' ) ) { - $this->_phpcsFile->addWarning( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection.', $wpQueryVarUsed ); + } elseif ( $this->isWPQueryMethodCall( $wpQueryVarUsed, 'set' ) ) { + $this->_phpcsFile->addWarning( 'Main WP_Query is being modified without $query->is_main_query() check. Needs manual inspection.', $wpQueryVarUsed, 'PreGetPosts' ); } $findStart = $wpQueryVarUsed + 1; } @@ -316,7 +321,7 @@ private function isEarlyMainQueryCheck( $stackPtr ) { } $nestedParenthesisEnd = array_shift( $this->_tokens[ $stackPtr ]['nested_parenthesis'] ); - if ( true === in_array( 'PHPCS_T_CLOSURE', $this->_tokens[ $stackPtr ]['conditions'] ) ) { + if ( true === in_array( 'PHPCS_T_CLOSURE', $this->_tokens[ $stackPtr ]['conditions'], true ) ) { $nestedParenthesisEnd = array_shift( $this->_tokens[ $stackPtr ]['nested_parenthesis'] ); } @@ -346,7 +351,7 @@ private function isEarlyMainQueryCheck( $stackPtr ) { */ private function isWPQueryMethodCall( $stackPtr, $method = null ) { $next = $this->_phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, // types. + Tokens::$emptyTokens, // types. $stackPtr + 1, // start. null, // end. true, // exclude. @@ -363,7 +368,7 @@ private function isWPQueryMethodCall( $stackPtr, $method = null ) { } $next = $this->_phpcsFile->findNext( - PHP_CodeSniffer_Tokens::$emptyTokens, // types. + Tokens::$emptyTokens, // types. $next + 1, // start. null, // end. true, // exclude. @@ -372,7 +377,7 @@ private function isWPQueryMethodCall( $stackPtr, $method = null ) { ); if ( $next && - true === in_array( $this->_tokens[ $next ]['code'], PHP_CodeSniffer_Tokens::$functionNameTokens, true ) && + true === in_array( $this->_tokens[ $next ]['code'], Tokens::$functionNameTokens, true ) && $method === $this->_tokens[ $next ]['content'] ) { return true; diff --git a/WordPressVIPMinimum/Sniffs/Cache/CacheValueOverrideSniff.php b/WordPressVIPMinimum/Sniffs/Cache/CacheValueOverrideSniff.php index c68342f4..8b6d9ab2 100644 --- a/WordPressVIPMinimum/Sniffs/Cache/CacheValueOverrideSniff.php +++ b/WordPressVIPMinimum/Sniffs/Cache/CacheValueOverrideSniff.php @@ -5,6 +5,12 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Sniffs\Cache; + +use PHP_CodeSniffer_Sniff as PHPCS_Sniff; +use PHP_CodeSniffer_File as File; +use PHP_CodeSniffer_Tokens as Tokens; + /** * This sniff enforces checking the return value of a function before passing it to anoher one. * @@ -16,7 +22,7 @@ * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPminimum_Sniffs_Cache_CacheValueOverrideSniff implements PHP_CodeSniffer_Sniff { +class CacheValueOverrideSniff implements PHPCS_Sniff { /** * Tokens of the file. @@ -31,7 +37,7 @@ class WordPressVIPminimum_Sniffs_Cache_CacheValueOverrideSniff implements PHP_Co * @return array(int) */ public function register() { - return PHP_CodeSniffer_Tokens::$functionNameTokens; + return Tokens::$functionNameTokens; }//end register() @@ -39,13 +45,13 @@ public function register() { /** * Processes the tokens that this sniff is interested in. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. - * @param int $stackPtr The position in the stack where - * the token was found. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. + * @param int $stackPtr The position in the stack where + * the token was found. * * @return void */ - public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + public function process( File $phpcsFile, $stackPtr ) { $this->_tokens = $phpcsFile->getTokens(); $tokens = $phpcsFile->getTokens(); @@ -74,24 +80,24 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { $variableName = $variableToken['content']; // Find the next non-empty token. - $openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true ); + $openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true ); // Find the closing bracket. $closeBracket = $tokens[ $openBracket ]['parenthesis_closer']; $nextVariableOccurrence = $phpcsFile->findNext( T_VARIABLE, ($closeBracket + 1), null, false, $variableName, false ); - $rightAfterNextVariableOccurence = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($nextVariableOccurrence + 1), null, true, null, true ); + $rightAfterNextVariableOccurence = $phpcsFile->findNext( Tokens::$emptyTokens, ($nextVariableOccurrence + 1), null, true, null, true ); if ( T_EQUAL !== $tokens[ $rightAfterNextVariableOccurence ]['code'] ) { // Not a value override. return; } - $valueAfterEqualSign = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($rightAfterNextVariableOccurence + 1), null, true, null, true ); + $valueAfterEqualSign = $phpcsFile->findNext( Tokens::$emptyTokens, ($rightAfterNextVariableOccurence + 1), null, true, null, true ); if ( T_FALSE === $tokens[ $valueAfterEqualSign ]['code'] ) { - $phpcsFile->addError( sprintf( 'Obtained cached value in %s is being overriden. Disabling caching?', $variableName ), $nextVariableOccurrence ); + $phpcsFile->addError( sprintf( 'Obtained cached value in %s is being overriden. Disabling caching?', $variableName ), $nextVariableOccurrence, 'CacheValueOverride' ); } } //end Process() @@ -108,12 +114,12 @@ private function isFunctionCall( $stackPtr ) { $tokens = $this->_tokens; $phpcsFile = $this->_phpcsFile; - if ( false === in_array( $tokens[ $stackPtr ]['code'], PHP_CodeSniffer_Tokens::$functionNameTokens ) ) { + if ( false === in_array( $tokens[ $stackPtr ]['code'], Tokens::$functionNameTokens, true ) ) { return false; } // Find the next non-empty token. - $openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true ); + $openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true ); if ( T_OPEN_PARENTHESIS !== $tokens[ $openBracket ]['code'] ) { // Not a function call. @@ -121,7 +127,7 @@ private function isFunctionCall( $stackPtr ) { } // Find the previous non-empty token. - $search = PHP_CodeSniffer_Tokens::$emptyTokens; + $search = Tokens::$emptyTokens; $search[] = T_BITWISE_AND; $previous = $phpcsFile->findPrevious( $search, ($stackPtr - 1), null, true ); if ( T_FUNCTION === $tokens[ $previous ]['code'] ) { @@ -145,7 +151,7 @@ private function isVariableAssignment( $stackPtr ) { $phpcsFile = $this->_phpcsFile; // Find the previous non-empty token. - $search = PHP_CodeSniffer_Tokens::$emptyTokens; + $search = Tokens::$emptyTokens; $search[] = T_BITWISE_AND; $previous = $phpcsFile->findPrevious( $search, ($stackPtr - 1), null, true ); diff --git a/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php b/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php index 9c797388..0625ef63 100644 --- a/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php +++ b/WordPressVIPMinimum/Sniffs/Classes/DeclarationCompatibilitySniff.php @@ -5,9 +5,13 @@ * @package VIPCS\WordPressVIPMinimum */ -if ( false === class_exists( 'PHP_CodeSniffer_Standards_AbstractScopeSniff', true ) ) { - $error = 'Class PHP_CodeSniffer_Standards_AbstractScopeSniff not found'; - throw new PHP_CodeSniffer_Exception( $error ); +namespace WordPressVIPMinimum\Sniffs\Classes; + +use PHP_CodeSniffer_File as File; + +// WPCS pre 0.13.1 backwardcompatibility. +if ( ! class_exists( '\PHP_CodeSniffer_Standards_AbstractScopeSniff' ) ) { + class_alias( 'PHP_CodeSniffer\Sniffs\AbstractScopeSniff', '\PHP_CodeSniffer_Standards_AbstractScopeSniff' ); } /** @@ -15,7 +19,7 @@ * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Sniffs_Classes_DeclarationCompatibilitySniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff { +class DeclarationCompatibilitySniff extends \PHP_CodeSniffer_Standards_AbstractScopeSniff { /** * The name of the class we are currently checking. @@ -104,7 +108,7 @@ class WordPressVIPMinimum_Sniffs_Classes_DeclarationCompatibilitySniff extends P ), 'args' => array( 'default' => 'array()', - ), + ), ), 'start_el' => array( 'output' => array( @@ -194,14 +198,14 @@ public function __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. + * @param \PHP_CodeSniffer\Files\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 ) { + protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currScope ) { $className = $phpcsFile->getDeclarationName( $currScope ); @@ -284,12 +288,12 @@ protected function processTokenWithinScope( PHP_CodeSniffer_File $phpcsFile, $st /** * Generates an error with nice current and parent class method notations * - * @param string $parentClassName The name of the extended (parent) class. - * @param string $methodName The name of the method currently being examined. - * @param array $currentMethodSignature The list of params and their options of the method which is being examined. - * @param array $parentMethodSignature The list of params and their options of the parent class method. - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token in the stack. + * @param string $parentClassName The name of the extended (parent) class. + * @param string $methodName The name of the method currently being examined. + * @param array $currentMethodSignature The list of params and their options of the method which is being examined. + * @param array $parentMethodSignature The list of params and their options of the parent class method. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in the stack. * * @return void */ @@ -299,7 +303,7 @@ private function addError( $parentClassName, $methodName, $currentMethodSignatur $parentSignature = sprintf( '%s::%s(%s)', $parentClassName, $methodName, implode( ', ', $this->generateParamList( $parentMethodSignature ) ) ); - $phpcsFile->addError( sprintf( 'Declaration of %s should be compatible with %s', $currentSignature, $parentSignature ), $stackPtr ); + $phpcsFile->addError( sprintf( 'Declaration of %s should be compatible with %s', $currentSignature, $parentSignature ), $stackPtr, 'DeclarationCompatibility' ); }//end addError() /** @@ -341,12 +345,12 @@ private function generateParamList( $methodSignature ) { /** * Extracts all the function names found in the given scope. * - * @param PHP_CodeSniffer_File $phpcsFile The current file being scanned. - * @param int $currScope A pointer to the start of the scope. + * @param File $phpcsFile The current file being scanned. + * @param int $currScope A pointer to the start of the scope. * * @return void */ - protected function loadFunctionNamesInScope( PHP_CodeSniffer_File $phpcsFile, $currScope ) { + protected function loadFunctionNamesInScope( File $phpcsFile, $currScope ) { $this->_functionList = array(); $tokens = $phpcsFile->getTokens(); @@ -361,4 +365,11 @@ protected function loadFunctionNamesInScope( PHP_CodeSniffer_File $phpcsFile, $c }//end loadFunctionNamesInScope() + /** + * Do nothing outside the scope. Has to be implemented accordingly to parent abstract class. + * + * @param File $phpcsFile PHPCS File. + * @param int $stackPtr Stack position. + */ + public function processTokenOutsideScope( File $phpcsFile, $stackPtr ) {} } diff --git a/WordPressVIPMinimum/Sniffs/Constants/ConstantRestrictionsSniff.php b/WordPressVIPMinimum/Sniffs/Constants/ConstantRestrictionsSniff.php index 2de9bcbb..c389b54b 100644 --- a/WordPressVIPMinimum/Sniffs/Constants/ConstantRestrictionsSniff.php +++ b/WordPressVIPMinimum/Sniffs/Constants/ConstantRestrictionsSniff.php @@ -6,12 +6,17 @@ * @link https://github.com/Automattic/VIP-Coding-Standards */ +namespace WordPressVIPMinimum\Sniffs\Constants; + +use PHP_CodeSniffer_File as File; +use PHP_CodeSniffer_Tokens as Tokens; + /** * Restricts usage of some constants. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Sniffs_Constants_ConstantRestrictionsSniff implements PHP_CodeSniffer_Sniff { +class ConstantRestrictionsSniff implements \PHP_CodeSniffer_Sniff { /** * List of restricted constant names. @@ -37,12 +42,12 @@ public function register() { /** * Process this test when one of its tokens is encoutnered * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token in the stack passed in $tokens. + * @param \PHP_CodeSniffer\Files\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 ) { + public function process( File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -58,12 +63,12 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { } if ( T_STRING === $tokens[ $stackPtr ]['code'] ) { - $phpcsFile->addWarning( sprintf( 'Code is touching the %s constant. Make sure it\'s used appropriately.', $constantName ), $stackPtr ); + $phpcsFile->addWarning( sprintf( 'Code is touching the %s constant. Make sure it\'s used appropriately.', $constantName ), $stackPtr, 'ConstantRestrictions' ); return; } // Find the previous non-empty token. - $openBracket = $phpcsFile->findPrevious( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true ); + $openBracket = $phpcsFile->findPrevious( Tokens::$emptyTokens, ($stackPtr - 1), null, true, null, true ); if ( T_OPEN_PARENTHESIS !== $tokens[ $openBracket ]['code'] ) { // Not a function call. @@ -76,7 +81,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { } // Find the previous non-empty token. - $search = PHP_CodeSniffer_Tokens::$emptyTokens; + $search = Tokens::$emptyTokens; $search[] = T_BITWISE_AND; $previous = $phpcsFile->findPrevious( $search, ($openBracket - 1), null, true ); if ( T_FUNCTION === $tokens[ $previous ]['code'] ) { @@ -84,11 +89,11 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { return; } - if ( true === in_array( $tokens[ $previous ]['code'], PHP_CodeSniffer_Tokens::$functionNameTokens, true ) ) { + if ( true === in_array( $tokens[ $previous ]['code'], Tokens::$functionNameTokens, true ) ) { if ( 'define' === $tokens[ $previous ]['content'] ) { - $phpcsFile->addError( sprintf( 'The definition of %s constant is prohibited. Please use a different name.', $constantName ), $previous ); + $phpcsFile->addError( sprintf( 'The definition of %s constant is prohibited. Please use a different name.', $constantName ), $previous, 'ConstantRestrictions' ); } else { - $phpcsFile->addWarning( sprintf( 'Code is touching the %s constant. Make sure it\'s used appropriately.', $constantName ), $previous ); + $phpcsFile->addWarning( sprintf( 'Code is touching the %s constant. Make sure it\'s used appropriately.', $constantName ), $previous, 'ConstantRestrictions' ); } } } diff --git a/WordPressVIPMinimum/Sniffs/Files/IncludingFileSniff.php b/WordPressVIPMinimum/Sniffs/Files/IncludingFileSniff.php index b963a78d..a2eee545 100644 --- a/WordPressVIPMinimum/Sniffs/Files/IncludingFileSniff.php +++ b/WordPressVIPMinimum/Sniffs/Files/IncludingFileSniff.php @@ -5,6 +5,11 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Sniffs\Files; + +use PHP_CodeSniffer_File as File; +use PHP_CodeSniffer_Tokens as Tokens; + /** * WordPressVIPMinimum_Sniffs_Files_IncludingFileSniff. * @@ -13,7 +18,7 @@ * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Sniffs_Files_IncludingFileSniff implements PHP_CodeSniffer_Sniff { +class IncludingFileSniff implements \PHP_CodeSniffer_Sniff { /** * List of function used for getting paths. @@ -65,7 +70,7 @@ class WordPressVIPMinimum_Sniffs_Files_IncludingFileSniff implements PHP_CodeSni * @return array */ public function register() { - return PHP_CodeSniffer_Tokens::$includeTokens; + return Tokens::$includeTokens; }//end register() @@ -73,20 +78,20 @@ public function 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. + * @param \PHP_CodeSniffer\Files\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 ) { + public function process( File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); - $nextToken = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true ); + $nextToken = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true, null, true ); if ( T_OPEN_PARENTHESIS === $tokens[ $nextToken ]['code'] ) { // The construct is using parenthesis, grab the next non empty token. - $nextToken = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($nextToken + 1), null, true, null, true ); + $nextToken = $phpcsFile->findNext( Tokens::$emptyTokens, ($nextToken + 1), null, true, null, true ); } if ( T_DIR === $tokens[ $nextToken ]['code'] || '__DIR__' === $tokens[ $nextToken ]['content'] ) { @@ -95,7 +100,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { } if ( T_VARIABLE === $tokens[ $nextToken ]['code'] ) { - $phpcsFile->addWarning( sprintf( 'File inclusion using variable (%s). Probably needs manual inspection.', $tokens[ $nextToken ]['content'] ), $nextToken ); + $phpcsFile->addWarning( sprintf( 'File inclusion using variable (%s). Probably needs manual inspection.', $tokens[ $nextToken ]['content'] ), $nextToken, 'IncludingFile' ); return; } @@ -119,12 +124,12 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { if ( 1 === preg_match( '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $tokens[ $nextToken ]['content'] ) ) { // The construct is using custom constant, which needs manula inspection. - $phpcsFile->addWarning( sprintf( 'File inclusion using custom constant (%s). Probably needs manual inspection.', $tokens[ $nextToken ]['content'] ), $nextToken ); + $phpcsFile->addWarning( sprintf( 'File inclusion using custom constant (%s). Probably needs manual inspection.', $tokens[ $nextToken ]['content'] ), $nextToken, 'IncludingFile' ); return; } if ( 0 === strpos( $tokens[ $nextToken ]['content'], '$' ) ) { - $phpcsFile->addWarning( sprintf( 'File inclusion using variable (%s). Probably needs manual inspection.', $tokens[ $nextToken ]['content'] ), $nextToken ); + $phpcsFile->addWarning( sprintf( 'File inclusion using variable (%s). Probably needs manual inspection.', $tokens[ $nextToken ]['content'] ), $nextToken, 'IncludingFile' ); return; } @@ -133,16 +138,16 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { return; } - $nextNextToken = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($nextToken + 1), null, true, null, true ); + $nextNextToken = $phpcsFile->findNext( Tokens::$emptyTokens, ($nextToken + 1), null, true, null, true ); if ( T_OPEN_PARENTHESIS === $tokens[ $nextNextToken ]['code'] ) { - $phpcsFile->addWarning( sprintf( 'File inclusion using custom function ( %s() ). Probably needs manual inspection.', $tokens[ $nextToken ]['content'] ), $nextToken ); + $phpcsFile->addWarning( sprintf( 'File inclusion using custom function ( %s() ). Probably needs manual inspection.', $tokens[ $nextToken ]['content'] ), $nextToken, 'IncludingFile' ); return; } - $phpcsFile->addError( 'Absolute include path must be used. Use get_template_directory, get_stylesheet_directory or plugin_dir_path.', $nextToken ); + $phpcsFile->addError( 'Absolute include path must be used. Use get_template_directory, get_stylesheet_directory or plugin_dir_path.', $nextToken, 'IncludingFile' ); return; } else { - $phpcsFile->addError( 'Absolute include path must be used. Use get_template_directory, get_stylesheet_directory or plugin_dir_path.', $nextToken ); + $phpcsFile->addError( 'Absolute include path must be used. Use get_template_directory, get_stylesheet_directory or plugin_dir_path.', $nextToken, 'IncludingFile' ); return; }// End if(). diff --git a/WordPressVIPMinimum/Sniffs/Functions/CheckReturnValueSniff.php b/WordPressVIPMinimum/Sniffs/Functions/CheckReturnValueSniff.php index de10313e..e20a401a 100644 --- a/WordPressVIPMinimum/Sniffs/Functions/CheckReturnValueSniff.php +++ b/WordPressVIPMinimum/Sniffs/Functions/CheckReturnValueSniff.php @@ -5,6 +5,11 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Sniffs\Functions; + +use PHP_CodeSniffer_File as File; +use PHP_CodeSniffer_Tokens as Tokens; + /** * This sniff enforces checking the return value of a function before passing it to another one. * @@ -14,7 +19,7 @@ * echo esc_url( wpcom_vip_get_term_link( $term ) ); * */ -class WordPressVIPminimum_Sniffs_Functions_CheckReturnValueSniff implements PHP_CodeSniffer_Sniff { +class CheckReturnValueSniff implements \PHP_CodeSniffer_Sniff { /** * Tokens of the whole file. @@ -63,7 +68,7 @@ class WordPressVIPminimum_Sniffs_Functions_CheckReturnValueSniff implements PHP_ * @return array(int) */ public function register() { - return PHP_CodeSniffer_Tokens::$functionNameTokens; + return Tokens::$functionNameTokens; }//end register() @@ -71,13 +76,13 @@ public function register() { /** * Processes the tokens that this sniff is interested in. * - * @param PHP_CodeSniffer_File $phpcsFile The file where the token was found. - * @param int $stackPtr The position in the stack where - * the token was found. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The file where the token was found. + * @param int $stackPtr The position in the stack where + * the token was found. * * @return void */ - public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { + public function process( File $phpcsFile, $stackPtr ) { $this->_tokens = $phpcsFile->getTokens(); $this->_phpcsFile = $phpcsFile; @@ -98,12 +103,12 @@ private function isFunctionCall( $stackPtr ) { $tokens = $this->_tokens; $phpcsFile = $this->_phpcsFile; - if ( false === in_array( $tokens[ $stackPtr ]['code'], PHP_CodeSniffer_Tokens::$functionNameTokens ) ) { + if ( false === in_array( $tokens[ $stackPtr ]['code'], Tokens::$functionNameTokens, true ) ) { return false; } // Find the next non-empty token. - $openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true ); + $openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true ); if ( T_OPEN_PARENTHESIS !== $tokens[ $openBracket ]['code'] ) { // Not a function call. @@ -111,7 +116,7 @@ private function isFunctionCall( $stackPtr ) { } // Find the previous non-empty token. - $search = PHP_CodeSniffer_Tokens::$emptyTokens; + $search = Tokens::$emptyTokens; $search[] = T_BITWISE_AND; $previous = $phpcsFile->findPrevious( $search, ($stackPtr - 1), null, true ); if ( T_FUNCTION === $tokens[ $previous ]['code'] ) { @@ -135,7 +140,7 @@ private function isVariableAssignment( $stackPtr ) { $phpcsFile = $this->_phpcsFile; // Find the previous non-empty token. - $search = PHP_CodeSniffer_Tokens::$emptyTokens; + $search = Tokens::$emptyTokens; $search[] = T_BITWISE_AND; $previous = $phpcsFile->findPrevious( $search, ($stackPtr - 1), null, true ); @@ -177,15 +182,15 @@ public function findDirectFunctionCalls( $stackPtr ) { } // Find the next non-empty token. - $openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true ); + $openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true ); // Find the closing bracket. $closeBracket = $tokens[ $openBracket ]['parenthesis_closer']; $startNext = $openBracket + 1; - while ( $next = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$functionNameTokens, $startNext, $closeBracket, false, null, true ) ) { + while ( $next = $phpcsFile->findNext( Tokens::$functionNameTokens, $startNext, $closeBracket, false, null, true ) ) { if ( true === in_array( $tokens[ $next ]['content'], $this->catch[ $functionName ], true ) ) { - $phpcsFile->addError( sprintf( "%s's return type must be checked before calling %s using that value", $tokens[ $next ]['content'], $functionName ), $next ); + $phpcsFile->addError( sprintf( "%s's return type must be checked before calling %s using that value", $tokens[ $next ]['content'], $functionName ), $next, 'CheckReturnValue' ); } $startNext = $next + 1; } @@ -214,7 +219,7 @@ public function findNonCheckedVariables( $stackPtr ) { $callees = array(); foreach ( $this->catch as $callee => $checkReturnArray ) { - if ( true === in_array( $functionName, $checkReturnArray ) ) { + if ( true === in_array( $functionName, $checkReturnArray, true ) ) { $isFunctionWeLookFor = true; $callees[] = $callee; } @@ -241,7 +246,7 @@ public function findNonCheckedVariables( $stackPtr ) { $variableName = $variableToken['content']; // Find the next non-empty token. - $openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true ); + $openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true ); // Find the closing bracket. $closeBracket = $tokens[ $openBracket ]['parenthesis_closer']; @@ -279,7 +284,7 @@ public function findNonCheckedVariables( $stackPtr ) { $nextVariableOccurrence = $phpcsFile->findNext( T_VARIABLE, ($closeBracket + 1), null, false, $variableName, false ); // Find previous non-empty token, which is not an open parenthesis, comma nor variable. - $search = PHP_CodeSniffer_Tokens::$emptyTokens; + $search = Tokens::$emptyTokens; $search[] = T_OPEN_PARENTHESIS; // This allows us to check for variables which are passed as second paramt of a function. Eg.: array_key_exists. $search[] = T_COMMA; @@ -290,19 +295,19 @@ public function findNonCheckedVariables( $stackPtr ) { foreach ( $callees as $callee ) { $notFunctionsCallee = array_key_exists( $callee, $this->notFunctions ) ? (array) $this->notFunctions[ $callee ] : array(); // Check whether the found token is one of the function calls (or foreach call) we are interested in. - if ( true === in_array( $tokens[ $nextFunctionCallWithVariable ]['code'], array_merge( PHP_CodeSniffer_Tokens::$functionNameTokens, $notFunctionsCallee ), true ) + if ( true === in_array( $tokens[ $nextFunctionCallWithVariable ]['code'], array_merge( Tokens::$functionNameTokens, $notFunctionsCallee ), true ) && $tokens[ $nextFunctionCallWithVariable ]['content'] === $callee ) { - $phpcsFile->addError( sprintf( 'Type of %s must be checked before calling %s using that variable', $variableName, $callee ), $nextFunctionCallWithVariable ); + $phpcsFile->addError( sprintf( 'Type of %s must be checked before calling %s using that variable', $variableName, $callee ), $nextFunctionCallWithVariable, 'CheckReturnValue' ); return; } - $search = array_merge( PHP_CodeSniffer_Tokens::$emptyTokens, array( T_EQUAL ) ); + $search = array_merge( Tokens::$emptyTokens, array( T_EQUAL ) ); $next = $phpcsFile->findNext( $search, $nextVariableOccurrence + 1, null, true, null, false ); - if ( true === in_array( $tokens[ $next ]['code'], PHP_CodeSniffer_Tokens::$functionNameTokens, true ) + if ( true === in_array( $tokens[ $next ]['code'], Tokens::$functionNameTokens, true ) && $tokens[ $next ]['content'] === $callee ) { - $phpcsFile->addError( sprintf( 'Type of %s must be checked before calling %s using that variable', $variableName, $callee ), $next ); + $phpcsFile->addError( sprintf( 'Type of %s must be checked before calling %s using that variable', $variableName, $callee ), $next, 'CheckReturnValue' ); return; } } diff --git a/WordPressVIPMinimum/Sniffs/VIP/ErrorControlSniff.php b/WordPressVIPMinimum/Sniffs/VIP/ErrorControlSniff.php index d50f004f..e2750159 100644 --- a/WordPressVIPMinimum/Sniffs/VIP/ErrorControlSniff.php +++ b/WordPressVIPMinimum/Sniffs/VIP/ErrorControlSniff.php @@ -6,12 +6,16 @@ * @link https://github.com/Automattic/VIP-Coding-Standards */ +namespace WordPressVIPMinimum\Sniffs\VIP; + +use PHP_CodeSniffer_File as File; + /** * Restricts usage of error control operators. Currently only the at sign. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Sniffs_VIP_ErrorControlSniff implements PHP_CodeSniffer_Sniff { +class ErrorControlSniff implements \PHP_CodeSniffer_Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -20,23 +24,23 @@ class WordPressVIPMinimum_Sniffs_VIP_ErrorControlSniff implements PHP_CodeSniffe */ public function register() { return array( - T_ASPERAND + T_ASPERAND, ); } /** * Process this test when one of its tokens is encoutered * - * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. - * @param int $stackPtr The position of the current token in the stack passed in $tokens. + * @param \PHP_CodeSniffer\Files\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 ) { + public function process( File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); - $phpcsFile->addError( sprintf( 'The code shouldn\'t use error control operators (%s). The call should be wrapped in appropriate checks.', $tokens[ $stackPtr ]['content'] ), $stackPtr ); + $phpcsFile->addError( sprintf( 'The code shouldn\'t use error control operators (%s). The call should be wrapped in appropriate checks.', $tokens[ $stackPtr ]['content'] ), $stackPtr, 'ErrorControl' ); } } diff --git a/WordPressVIPMinimum/Sniffs/VIP/FlushRewriteRulesSniff.php b/WordPressVIPMinimum/Sniffs/VIP/FlushRewriteRulesSniff.php index 3a73d868..ffbde47f 100644 --- a/WordPressVIPMinimum/Sniffs/VIP/FlushRewriteRulesSniff.php +++ b/WordPressVIPMinimum/Sniffs/VIP/FlushRewriteRulesSniff.php @@ -6,12 +6,17 @@ * @link https://github.com/Automattic/VIP-Coding-Standards */ +namespace WordPressVIPMinimum\Sniffs\VIP; + +use PHP_CodeSniffer_File as File; +use PHP_CodeSniffer_Tokens as Tokens; + /** * Restricts usage of rewrite rules flushing * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Sniffs_VIP_FlushRewriteRulesSniff implements PHP_CodeSniffer_Sniff { +class FlushRewriteRulesSniff implements \PHP_CodeSniffer_Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -19,18 +24,18 @@ class WordPressVIPMinimum_Sniffs_VIP_FlushRewriteRulesSniff implements PHP_CodeS * @return array */ public function register() { - return PHP_CodeSniffer_Tokens::$functionNameTokens; + return Tokens::$functionNameTokens; } /** * Process 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. + * @param \PHP_CodeSniffer\Files\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 ) { + public function process( File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -41,7 +46,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { } $previousPtr = $phpcsFile->findPrevious( - array_merge( PHP_CodeSniffer_Tokens::$emptyTokens ), // types. + array_merge( Tokens::$emptyTokens ), // types. $stackPtr - 1, // start. null, // end. true, // exclude. @@ -54,7 +59,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { } $previousPtr = $phpcsFile->findPrevious( - array_merge( PHP_CodeSniffer_Tokens::$emptyTokens ), // types. + array_merge( Tokens::$emptyTokens ), // types. $previousPtr - 1, // start. null, // end. true, // exclude. @@ -68,7 +73,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { if ( 'PHPCS_T_CLOSE_SQUARE_BRACKET' === $tokens[ $previousPtr ]['code'] ) { $previousPtr = $phpcsFile->findPrevious( - array_merge( PHP_CodeSniffer_Tokens::$emptyTokens ), // types. + array_merge( Tokens::$emptyTokens ), // types. $previousPtr - 1, // start. null, // end. true, // exclude. @@ -84,7 +89,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { return; } - $phpcsFile->addError( sprintf( '%s should not be used in any normal circumstances in the theme code.', $tokens[ $stackPtr ]['content'] ), $stackPtr ); + $phpcsFile->addError( sprintf( '%s should not be used in any normal circumstances in the theme code.', $tokens[ $stackPtr ]['content'] ), $stackPtr, 'FlushRewriteRules' ); } } diff --git a/WordPressVIPMinimum/Sniffs/VIP/RegexpCompareSniff.php b/WordPressVIPMinimum/Sniffs/VIP/RegexpCompareSniff.php index 3415015b..91db7368 100644 --- a/WordPressVIPMinimum/Sniffs/VIP/RegexpCompareSniff.php +++ b/WordPressVIPMinimum/Sniffs/VIP/RegexpCompareSniff.php @@ -5,25 +5,27 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Sniffs\VIP; + /** * Flag REGEXP and NOT REGEXP in meta compare * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Sniffs_VIP_RegexpCompareSniff extends WordPress_AbstractArrayAssignmentRestrictionsSniff { +class RegexpCompareSniff extends \WordPress\AbstractArrayAssignmentRestrictionsSniff { /** * Groups of variables to restrict. * This should be overridden in extending classes. * * Example: groups => array( - * 'wpdb' => array( - * 'type' => 'error' | 'warning', - * 'message' => 'Dont use this one please!', - * 'variables' => array( '$val', '$var' ), - * 'object_vars' => array( '$foo->bar', .. ), - * 'array_members' => array( '$foo['bar']', .. ), - * ) + * 'wpdb' => array( + * 'type' => 'error' | 'warning', + * 'message' => 'Dont use this one please!', + * 'variables' => array( '$val', '$var' ), + * 'object_vars' => array( '$foo->bar', .. ), + * 'array_members' => array( '$foo['bar']', .. ), + * ) * ) * * @return array diff --git a/WordPressVIPMinimum/Sniffs/VIP/RemoteRequestTimeoutSniff.php b/WordPressVIPMinimum/Sniffs/VIP/RemoteRequestTimeoutSniff.php index a63adfb5..4df10bd9 100644 --- a/WordPressVIPMinimum/Sniffs/VIP/RemoteRequestTimeoutSniff.php +++ b/WordPressVIPMinimum/Sniffs/VIP/RemoteRequestTimeoutSniff.php @@ -5,25 +5,27 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Sniffs\VIP; + /** * Flag REGEXP and NOT REGEXP in meta compare * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Sniffs_VIP_RemoteRequestTimeoutSniff extends WordPress_AbstractArrayAssignmentRestrictionsSniff { +class RemoteRequestTimeoutSniff extends \WordPress\AbstractArrayAssignmentRestrictionsSniff { /** * Groups of variables to restrict. * This should be overridden in extending classes. * * Example: groups => array( - * 'wpdb' => array( - * 'type' => 'error' | 'warning', - * 'message' => 'Dont use this one please!', - * 'variables' => array( '$val', '$var' ), - * 'object_vars' => array( '$foo->bar', .. ), - * 'array_members' => array( '$foo['bar']', .. ), - * ) + * 'wpdb' => array( + * 'type' => 'error' | 'warning', + * 'message' => 'Dont use this one please!', + * 'variables' => array( '$val', '$var' ), + * 'object_vars' => array( '$foo->bar', .. ), + * 'array_members' => array( '$foo['bar']', .. ), + * ) * ) * * @return array diff --git a/WordPressVIPMinimum/Sniffs/VIP/RestrictedFunctionsSniff.php b/WordPressVIPMinimum/Sniffs/VIP/RestrictedFunctionsSniff.php index 0e118687..128d3c05 100644 --- a/WordPressVIPMinimum/Sniffs/VIP/RestrictedFunctionsSniff.php +++ b/WordPressVIPMinimum/Sniffs/VIP/RestrictedFunctionsSniff.php @@ -5,12 +5,14 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Sniffs\VIP; + /** * Restricts usage of some functions in VIP context. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Sniffs_VIP_RestrictedFunctionsSniff extends WordPress_Sniffs_VIP_RestrictedFunctionsSniff { +class RestrictedFunctionsSniff extends \WordPress\Sniffs\VIP\RestrictedFunctionsSniff { /** * Groups of functions to restrict. diff --git a/WordPressVIPMinimum/Sniffs/VIP/StaticStrreplaceSniff.php b/WordPressVIPMinimum/Sniffs/VIP/StaticStrreplaceSniff.php index 303a8c76..dd26ccdc 100644 --- a/WordPressVIPMinimum/Sniffs/VIP/StaticStrreplaceSniff.php +++ b/WordPressVIPMinimum/Sniffs/VIP/StaticStrreplaceSniff.php @@ -6,12 +6,17 @@ * @link https://github.com/Automattic/VIP-Coding-Standards */ +namespace WordPressVIPMinimum\Sniffs\VIP; + +use PHP_CodeSniffer_File as File; +use PHP_CodeSniffer_Tokens as Tokens; + /** * Restricts usage of str_replace with all 3 params being static. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Sniffs_VIP_StaticStrreplaceSniff implements PHP_CodeSniffer_Sniff { +class StaticStrreplaceSniff implements \PHP_CodeSniffer_Sniff { /** * Returns an array of tokens this test wants to listen for. @@ -19,18 +24,18 @@ class WordPressVIPMinimum_Sniffs_VIP_StaticStrreplaceSniff implements PHP_CodeSn * @return array */ public function register() { - return PHP_CodeSniffer_Tokens::$functionNameTokens; + return Tokens::$functionNameTokens; } /** * Process 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. + * @param \PHP_CodeSniffer\Files\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 ) { + public function process( File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -38,7 +43,7 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { return; } - $openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true ); + $openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($stackPtr + 1), null, true ); if ( T_OPEN_PARENTHESIS !== $tokens[ $openBracket ]['code'] ) { return; @@ -46,10 +51,10 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { $next_start_ptr = $openBracket + 1; for ( $i = 0; $i < 3; $i++ ) { - $param_ptr = $phpcsFile->findNext( array_merge( PHP_CodeSniffer_Tokens::$emptyTokens, array( T_COMMA ) ), $next_start_ptr, null, true ); + $param_ptr = $phpcsFile->findNext( array_merge( Tokens::$emptyTokens, array( T_COMMA ) ), $next_start_ptr, null, true ); if ( T_ARRAY === $tokens[ $param_ptr ]['code'] ) { - $openBracket = $phpcsFile->findNext( PHP_CodeSniffer_Tokens::$emptyTokens, ($param_ptr + 1), null, true ); + $openBracket = $phpcsFile->findNext( Tokens::$emptyTokens, ($param_ptr + 1), null, true ); if ( T_OPEN_PARENTHESIS !== $tokens[ $openBracket ]['code'] ) { return; } @@ -57,19 +62,19 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { // Find the closing bracket. $closeBracket = $tokens[ $openBracket ]['parenthesis_closer']; - $array_item_ptr = $phpcsFile->findNext( array_merge( PHP_CodeSniffer_Tokens::$emptyTokens, array( T_COMMA ) ), ( $openBracket + 1 ), $closeBracket, true ); + $array_item_ptr = $phpcsFile->findNext( array_merge( Tokens::$emptyTokens, array( T_COMMA ) ), ( $openBracket + 1 ), $closeBracket, true ); while ( false !== $array_item_ptr ) { if ( T_CONSTANT_ENCAPSED_STRING !== $tokens[ $array_item_ptr ]['code'] ) { return; } - $array_item_ptr = $phpcsFile->findNext( array_merge( PHP_CodeSniffer_Tokens::$emptyTokens, array( T_COMMA ) ), ( $array_item_ptr + 1 ), $closeBracket, true ); + $array_item_ptr = $phpcsFile->findNext( array_merge( Tokens::$emptyTokens, array( T_COMMA ) ), ( $array_item_ptr + 1 ), $closeBracket, true ); } $next_start_ptr = $closeBracket + 1; continue; - } else if ( T_CONSTANT_ENCAPSED_STRING !== $tokens[ $param_ptr ]['code'] ) { + } elseif ( T_CONSTANT_ENCAPSED_STRING !== $tokens[ $param_ptr ]['code'] ) { return; } @@ -77,6 +82,6 @@ public function process( PHP_CodeSniffer_File $phpcsFile, $stackPtr ) { } - $phpcsFile->addError( sprintf( 'This code pattern is often used to run a very dangerous shell programs on your server. The code in these files needs to be reviewed, and possibly cleaned.', $tokens[ $stackPtr ]['content'] ), $stackPtr ); + $phpcsFile->addError( sprintf( 'This code pattern is often used to run a very dangerous shell programs on your server. The code in these files needs to be reviewed, and possibly cleaned.', $tokens[ $stackPtr ]['content'] ), $stackPtr, 'StaticStrreplace' ); }//end process() } diff --git a/WordPressVIPMinimum/Sniffs/Variables/VariableAnalysisHelper.php b/WordPressVIPMinimum/Sniffs/Variables/VariableAnalysisHelper.php index 06258a88..5974b284 100644 --- a/WordPressVIPMinimum/Sniffs/Variables/VariableAnalysisHelper.php +++ b/WordPressVIPMinimum/Sniffs/Variables/VariableAnalysisHelper.php @@ -13,6 +13,8 @@ * @link http://pear.php.net/package/PHP_CodeSniffer */ +namespace WordPressVIPMinimum\Sniffs\Variables; + /** * Holds details of a scope. * diff --git a/WordPressVIPMinimum/Sniffs/Variables/VariableAnalysisSniff.php b/WordPressVIPMinimum/Sniffs/Variables/VariableAnalysisSniff.php index f54be266..cdbfbcd6 100644 --- a/WordPressVIPMinimum/Sniffs/Variables/VariableAnalysisSniff.php +++ b/WordPressVIPMinimum/Sniffs/Variables/VariableAnalysisSniff.php @@ -13,6 +13,10 @@ * @link http://pear.php.net/package/PHP_CodeSniffer */ +namespace WordPressVIPMinimum\Sniffs\Variables; + +use PHP_CodeSniffer_File as File; + /** * Checks the for undefined function variables. * @@ -25,7 +29,7 @@ * @copyright 2011 Sam Graham * @link http://pear.php.net/package/PHP_CodeSniffer */ -class WordPressVIPMinimum_Sniffs_Variables_VariableAnalysisSniff implements PHP_CodeSniffer_Sniff +class VariableAnalysisSniff implements \PHP_CodeSniffer_Sniff { /** * The current phpcsFile being checked. @@ -330,13 +334,13 @@ public function 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. + * @param \PHP_CodeSniffer\Files\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) { + public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); $token = $tokens[$stackPtr]; @@ -481,19 +485,23 @@ function isVariableUndefined($varName, $stackPtr, $currScope) { } function markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $currScope) { + $this->markVariableRead($varName, $stackPtr, $currScope); if ($this->isVariableUndefined($varName, $stackPtr, $currScope) === true) { // We haven't been defined by this point. - $phpcsFile->addWarning("Variable %s is undefined.", $stackPtr, + $phpcsFile->addWarning( + "Variable %s is undefined.", + $stackPtr, 'UndefinedVariable', - array("\${$varName}")); + array("\${$varName}") + ); } return true; } function findFunctionPrototype( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -516,7 +524,7 @@ function findFunctionPrototype( } function findVariableScope( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -548,7 +556,7 @@ function findVariableScope( } function isNextThingAnAssign( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -564,7 +572,7 @@ function isNextThingAnAssign( } function findWhereAssignExecuted( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -599,7 +607,7 @@ function findWhereAssignExecuted( } function findContainingBrackets( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -613,7 +621,7 @@ function findContainingBrackets( function findFunctionCall( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -630,7 +638,7 @@ function findFunctionCall( } function findFunctionCallArguments( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -673,13 +681,12 @@ function findFunctionCallArguments( } protected function checkForFunctionPrototype( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope ) { $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; // Are we a function or closure parameter? // It would be nice to get the list of function parameters from watching for @@ -721,7 +728,9 @@ protected function checkForFunctionPrototype( // We haven't been defined by this point. $phpcsFile->addWarning("Variable %s is undefined.", $stackPtr, 'UndefinedVariable', - array("\${$varName}")); + array("\${$varName}"), + 'WordPressVIPMinimum.Variables.VariableAnalysis' + ); return true; } // $functionPtr is at the use, we need the function keyword for start of scope. @@ -738,13 +747,12 @@ protected function checkForFunctionPrototype( } protected function checkForCatchBlock( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope ) { $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; // Are we a catch block parameter? if (($openPtr = $this->findContainingBrackets($phpcsFile, $stackPtr)) === false) { @@ -773,7 +781,7 @@ protected function checkForCatchBlock( } protected function checkForThisWithinClass( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope @@ -801,13 +809,12 @@ protected function checkForThisWithinClass( } protected function checkForSuperGlobal( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope ) { $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; // Are we a superglobal variable? if (in_array($varName, array( @@ -830,7 +837,7 @@ protected function checkForSuperGlobal( } protected function checkForStaticMember( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope @@ -868,7 +875,9 @@ protected function checkForStaticMember( if ($tokens[$scopePtr]['code'] === T_CLOSURE) { $phpcsFile->addError("Use of {$err_desc}%s inside closure.", $stackPtr, $err_class, - array("\${$varName}")); + array("\${$varName}"), + 'WordPressVIPMinimum.Variables.VariableAnalysis' + ); return true; } if ($scopeCode === T_CLASS) { @@ -878,7 +887,9 @@ protected function checkForStaticMember( } $phpcsFile->addError("Use of {$err_desc}%s outside class definition.", $stackPtr, $err_class, - array("\${$varName}")); + array("\${$varName}"), + 'WordPressVIPMinimum.Variables.VariableAnalysis' + ); return true; } @@ -886,13 +897,12 @@ protected function checkForStaticMember( } protected function checkForAssignment( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope ) { $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; // Is the next non-whitespace an assignment? if (($assignPtr = $this->isNextThingAnAssign($phpcsFile, $stackPtr)) === false) { @@ -908,13 +918,12 @@ protected function checkForAssignment( } protected function checkForListAssignment( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope ) { $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; // OK, are we within a list (...) construct? if (($openPtr = $this->findContainingBrackets($phpcsFile, $stackPtr)) === false) { @@ -939,13 +948,12 @@ protected function checkForListAssignment( } protected function checkForGlobalDeclaration( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope ) { $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; // Are we a global declaration? // Search backwards for first token that isn't whitespace, comma or variable. @@ -962,13 +970,12 @@ protected function checkForGlobalDeclaration( } protected function checkForStaticDeclaration( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope ) { $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; // Are we a static declaration? // Static declarations are a bit more complicated than globals, since they @@ -1026,13 +1033,12 @@ protected function checkForStaticDeclaration( } protected function checkForForeachLoopVar( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope ) { $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; // Are we a foreach loopvar? if (($openPtr = $this->findContainingBrackets($phpcsFile, $stackPtr)) === false) { @@ -1049,13 +1055,12 @@ protected function checkForForeachLoopVar( } protected function checkForPassByReferenceFunctionCall( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope ) { $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; // Are we pass-by-reference to known pass-by-reference function? if (($functionPtr = $this->findFunctionCall($phpcsFile, $stackPtr)) === false) { @@ -1114,7 +1119,7 @@ protected function checkForPassByReferenceFunctionCall( } protected function checkForSymbolicObjectProperty( - PHP_CodeSniffer_File $phpcsFile, + File $phpcsFile, $stackPtr, $varName, $currScope @@ -1138,32 +1143,30 @@ protected function checkForSymbolicObjectProperty( /** * Called to process class member vars. * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this - * token was found. - * @param int $stackPtr The position where the token was found. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer 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, + File $phpcsFile, $stackPtr ) { - $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; // TODO: don't care for now } /** * Called to process normal member vars. * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this - * token was found. - * @param int $stackPtr The position where the token was found. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer 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, + File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); @@ -1273,15 +1276,15 @@ protected function processVariable( * Note that there may be more than one variable in the string, which will * result only in one call for the string. * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this - * token was found. - * @param int $stackPtr The position where the double quoted - * string was found. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this + * token was found. + * @param int $stackPtr The position where the double quoted + * string was found. * * @return void */ protected function processVariableInString( - PHP_CodeSniffer_File + File $phpcsFile, $stackPtr ) { @@ -1307,14 +1310,13 @@ protected function processVariableInString( } protected function processCompactArguments( - PHP_CodeSniffer_File - $phpcsFile, + File $phpcsFile, $stackPtr, $arguments, $currScope ) { + $this->phpcsFile = $phpcsFile; $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; foreach ($arguments as $argumentPtrs) { $argumentPtrs = array_values(array_filter($argumentPtrs, array( $this, 'filter_non_whitespace_tokens' ) )); @@ -1360,20 +1362,18 @@ protected function processCompactArguments( /** * Called to process variables named in a call to compact(). * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this - * token was found. - * @param int $stackPtr The position where the call to compact() - * was found. + * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this + * token was found. + * @param int $stackPtr The position where the call to compact() + * was found. * * @return void */ protected function processCompact( - PHP_CodeSniffer_File - $phpcsFile, + File $phpcsFile, $stackPtr ) { $tokens = $phpcsFile->getTokens(); - $token = $tokens[$stackPtr]; $currScope = $this->findVariableScope($phpcsFile, $stackPtr); @@ -1388,15 +1388,14 @@ protected function processCompact( * Note that although triggered by the closing curly brace of the scope, $stackPtr is * the scope conditional, not the closing curly brace. * - * @param PHP_CodeSniffer_File $phpcsFile The PHP_CodeSniffer file where this + * @param \PHP_CodeSniffer\Files\File $phpcsFile The PHP_CodeSniffer file where this * token was found. - * @param int $stackPtr The position of the scope conditional. + * @param int $stackPtr The position of the scope conditional. * * @return void */ protected function processScopeClose( - PHP_CodeSniffer_File - $phpcsFile, + File $phpcsFile, $stackPtr ) { $scopeInfo = $this->getScopeInfo($stackPtr, false); diff --git a/WordPressVIPMinimum/Tests/Actions/PreGetPostsUnitTest.php b/WordPressVIPMinimum/Tests/Actions/PreGetPostsUnitTest.php index ca0dc433..91ecc48d 100644 --- a/WordPressVIPMinimum/Tests/Actions/PreGetPostsUnitTest.php +++ b/WordPressVIPMinimum/Tests/Actions/PreGetPostsUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\Actions; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the PreGetPosts sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_Actions_PreGetPostsUnitTest extends AbstractSniffUnitTest { +class PreGetPostsUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/Cache/CacheValueOverrideUnitTest.php b/WordPressVIPMinimum/Tests/Cache/CacheValueOverrideUnitTest.php index eef1e8fc..37097e97 100644 --- a/WordPressVIPMinimum/Tests/Cache/CacheValueOverrideUnitTest.php +++ b/WordPressVIPMinimum/Tests/Cache/CacheValueOverrideUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\Cache; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the CacheValueOverride sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_Cache_CacheValueOverrideUnitTest extends AbstractSniffUnitTest { +class CacheValueOverrideUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/Classes/DeclarationCompatibilityUnitTest.php b/WordPressVIPMinimum/Tests/Classes/DeclarationCompatibilityUnitTest.php index 2d99e5b6..99026700 100644 --- a/WordPressVIPMinimum/Tests/Classes/DeclarationCompatibilityUnitTest.php +++ b/WordPressVIPMinimum/Tests/Classes/DeclarationCompatibilityUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\Classes; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the DeclarationCompatibility sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_Classes_DeclarationCompatibilityUnitTest extends AbstractSniffUnitTest { +class DeclarationCompatibilityUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/Constants/ConstantRestrictionsUnitTest.php b/WordPressVIPMinimum/Tests/Constants/ConstantRestrictionsUnitTest.php index 2ec599f4..015d9f37 100644 --- a/WordPressVIPMinimum/Tests/Constants/ConstantRestrictionsUnitTest.php +++ b/WordPressVIPMinimum/Tests/Constants/ConstantRestrictionsUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\Constants; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the ConstantRestrictions sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_Constants_ConstantRestrictionsUnitTest extends AbstractSniffUnitTest { +class ConstantRestrictionsUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/Files/IncludingFileUnitTest.php b/WordPressVIPMinimum/Tests/Files/IncludingFileUnitTest.php index 662162a5..e31be4a7 100644 --- a/WordPressVIPMinimum/Tests/Files/IncludingFileUnitTest.php +++ b/WordPressVIPMinimum/Tests/Files/IncludingFileUnitTest.php @@ -5,12 +5,15 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\Files; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; /** * Unit test class for the IncludingFile sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_Files_IncludingFileUnitTest extends AbstractSniffUnitTest { +class IncludingFileUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/Functions/CheckReturnValueUnitTest.php b/WordPressVIPMinimum/Tests/Functions/CheckReturnValueUnitTest.php index 6411342d..d06491e9 100644 --- a/WordPressVIPMinimum/Tests/Functions/CheckReturnValueUnitTest.php +++ b/WordPressVIPMinimum/Tests/Functions/CheckReturnValueUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\Functions; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the CheckReturnValue sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_Functions_CheckReturnValueUnitTest extends AbstractSniffUnitTest { +class CheckReturnValueUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/VIP/ErrorControlUnitTest.php b/WordPressVIPMinimum/Tests/VIP/ErrorControlUnitTest.php index 7749caab..2ba94c57 100644 --- a/WordPressVIPMinimum/Tests/VIP/ErrorControlUnitTest.php +++ b/WordPressVIPMinimum/Tests/VIP/ErrorControlUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\VIP; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the ErrorControl sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_VIP_ErrorControlUnitTest extends AbstractSniffUnitTest { +class ErrorControlUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/VIP/FlushRewriteRulesUnitTest.php b/WordPressVIPMinimum/Tests/VIP/FlushRewriteRulesUnitTest.php index 210e1a7c..cd71d391 100644 --- a/WordPressVIPMinimum/Tests/VIP/FlushRewriteRulesUnitTest.php +++ b/WordPressVIPMinimum/Tests/VIP/FlushRewriteRulesUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\VIP; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the FlushRewriteRules sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_VIP_FlushRewriteRulesUnitTest extends AbstractSniffUnitTest { +class FlushRewriteRulesUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/VIP/RegexpCompareUnitTest.php b/WordPressVIPMinimum/Tests/VIP/RegexpCompareUnitTest.php index ff60b22b..b4e015b2 100644 --- a/WordPressVIPMinimum/Tests/VIP/RegexpCompareUnitTest.php +++ b/WordPressVIPMinimum/Tests/VIP/RegexpCompareUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\VIP; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the RegexpCompare sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_VIP_RegexpCompareUnitTest extends AbstractSniffUnitTest { +class RegexpCompareUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/VIP/RemoteRequestTimeoutUnitTest.php b/WordPressVIPMinimum/Tests/VIP/RemoteRequestTimeoutUnitTest.php index 37ebcf1e..6139c080 100644 --- a/WordPressVIPMinimum/Tests/VIP/RemoteRequestTimeoutUnitTest.php +++ b/WordPressVIPMinimum/Tests/VIP/RemoteRequestTimeoutUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\VIP; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the RemoteRequestTimeout sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_VIP_RemoteRequestTimeoutUnitTest extends AbstractSniffUnitTest { +class RemoteRequestTimeoutUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/VIP/RestrictedFunctionsUnitTest.php b/WordPressVIPMinimum/Tests/VIP/RestrictedFunctionsUnitTest.php index 58742809..777b6029 100644 --- a/WordPressVIPMinimum/Tests/VIP/RestrictedFunctionsUnitTest.php +++ b/WordPressVIPMinimum/Tests/VIP/RestrictedFunctionsUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\VIP; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the RestrictedFunctions sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_VIP_RestrictedFunctionsUnitTest extends AbstractSniffUnitTest { +class RestrictedFunctionsUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/VIP/StaticStrreplaceUnitTest.php b/WordPressVIPMinimum/Tests/VIP/StaticStrreplaceUnitTest.php index 43b80b53..4f00379e 100644 --- a/WordPressVIPMinimum/Tests/VIP/StaticStrreplaceUnitTest.php +++ b/WordPressVIPMinimum/Tests/VIP/StaticStrreplaceUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\VIP; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the StaticStrreplace sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_VIP_StaticStrreplaceUnitTest extends AbstractSniffUnitTest { +class StaticStrreplaceUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/Tests/Variables/VariableAnalysisUnitTest.php b/WordPressVIPMinimum/Tests/Variables/VariableAnalysisUnitTest.php index 8f520ffa..ef8d0072 100644 --- a/WordPressVIPMinimum/Tests/Variables/VariableAnalysisUnitTest.php +++ b/WordPressVIPMinimum/Tests/Variables/VariableAnalysisUnitTest.php @@ -5,12 +5,16 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum\Tests\Variables; + +use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; + /** * Unit test class for the Variable Analysis sniff. * * @package VIPCS\WordPressVIPMinimum */ -class WordPressVIPMinimum_Tests_Variables_VariableAnalysisUnitTest extends AbstractSniffUnitTest { +class VariableAnalysisUnitTest extends AbstractSniffUnitTest { /** * Returns the lines where errors should occur. diff --git a/WordPressVIPMinimum/autoload.php b/WordPressVIPMinimum/autoload.php new file mode 100644 index 00000000..6dbc5052 --- /dev/null +++ b/WordPressVIPMinimum/autoload.php @@ -0,0 +1,27 @@ + $details ) { + if ( 'WordPress' === $standard ) { + require_once( $details['path'] . '/PHPCSAliases.php' ); + return; + } + } +} + +load_phpcs_helper_file(); + diff --git a/WordPressVIPMinimum/ruleset.xml b/WordPressVIPMinimum/ruleset.xml index 3c31ad15..c0e31b20 100644 --- a/WordPressVIPMinimum/ruleset.xml +++ b/WordPressVIPMinimum/ruleset.xml @@ -2,23 +2,25 @@ WordPress VIP Minimum Coding Standards + ./autoload.php + - + - - - - + + + + - - + + diff --git a/bin/phpcs.xml b/bin/phpcs.xml index 9852ad6c..346f1ec4 100644 --- a/bin/phpcs.xml +++ b/bin/phpcs.xml @@ -1,17 +1,25 @@ - The Coding standard for the WordPress Coding Standards itself. + The Coding standard for the WordPress Coding Standards itself. - - /WordPress/Sniffs/Arrays/ArrayDeclarationSniff.php + + - - - - + + /Test/AllTests.php + /Test/Standards/*.php - + + */vendor/* - \ No newline at end of file + + + + + + + + + + + diff --git a/ruleset_test.inc b/ruleset_test.inc index efd20808..4faeb2c5 100644 --- a/ruleset_test.inc +++ b/ruleset_test.inc @@ -130,4 +130,4 @@ get_children(); // Bad. Warning. Message. attachment_url_to_postid(); // Bad. Error. -str_replace( 'foo', 'bar', 'foobar' ); // Bad. Error. \ No newline at end of file +str_replace( 'foo', 'bar', 'foobar' ); // Bad. Error. diff --git a/ruleset_test.php b/ruleset_test.php index 6dc0b22b..ec3772fc 100644 --- a/ruleset_test.php +++ b/ruleset_test.php @@ -15,6 +15,8 @@ * @package VIPCS\WordPressVIPMinimum */ +namespace WordPressVIPMinimum; + // Expected values. $expected = array( 'errors' => array( @@ -119,10 +121,12 @@ public function __construct( $expected = array() ) { // Travis support. if ( false === getenv( 'PHPCS_BIN' ) ) { + // @codingStandardsIgnoreLine putenv( 'PHPCS_BIN=phpcs' ); } // Collect the PHPCS result. + // @codingStandardsIgnoreLine $output = shell_exec( '$PHPCS_BIN --standard=WordPressVIPMinimum --report=json ./ruleset_test.inc' ); $output = json_decode( $output, true ); @@ -172,7 +176,7 @@ private function check_missing_expected_values() { if ( false === isset( $this->$type[ $line ] ) ) { $this->error_warning_message( $number, $type, 0, $line ); $this->total_issues ++; - } else if ( $this->$type[ $line ] !== $number ) { + } elseif ( $this->$type[ $line ] !== $number ) { $this->error_warning_message( $number, $type, $this->$type[ $line ], $line ); $this->total_issues ++; } @@ -190,7 +194,7 @@ private function check_unexpected_values() { if ( false === isset( $expected[ $type ][ $line ] ) ) { $this->error_warning_message( 0, $type, $number, $line ); $this->total_issues ++; - } else if ( $number !== $expected[ $type ][ $line ] ) { + } elseif ( $number !== $expected[ $type ][ $line ] ) { $this->error_warning_message( $expected[ $type ][ $line ], $type, $number, $line ); $this->total_issues ++; } @@ -209,7 +213,7 @@ private function check_messages() { if ( false === isset( $this->messages[ $line ] ) ) { printf( 'Expected "%s" but found no message for line %d' . PHP_EOL, $message, $line ); // XSS OK. $this->total_issues ++; - } else if ( false === in_array( $message, $this->messages[ $line ] ) ) { + } elseif ( false === in_array( $message, $this->messages[ $line ], true ) ) { printf( 'Expected message "%s" was not found for line %d' . PHP_EOL, $message, $line ); // XSS OK. $this->total_issues ++; } @@ -218,7 +222,7 @@ private function check_messages() { foreach ( $this->messages as $line => $messages ) { foreach ( $messages as $message ) { if ( true === isset( $this->expected['messages'][ $line ] ) ) { - if ( false === in_array( $message, $this->expected['messages'][ $line ] ) ) { + if ( false === in_array( $message, $this->expected['messages'][ $line ], true ) ) { printf( 'Unexpected message "%s" was found for line %d' . PHP_EOL, $message, $line ); // XSS OK. $this->total_issues ++; }