diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1b1df80..2044501 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -147,8 +147,8 @@ jobs: - name: 'Composer: adjust dependencies' run: | - # Remove dev dependencies which are not compatible with all supported PHP versions. - composer remove --dev --no-update sirbrillig/phpcs-import-detection phpstan/phpstan + # Remove dev dependencies which are not compatible with all supported PHP/PHPCS versions. + composer remove --dev --no-update phpcsstandards/phpcsdevcs sirbrillig/phpcs-import-detection phpstan/phpstan composer require --no-update squizlabs/php_codesniffer:"${{ matrix.phpcs_version }}" - name: Install Composer dependencies diff --git a/Tests/BaseTestCase.php b/Tests/BaseTestCase.php index 19f2d84..f8b56cf 100644 --- a/Tests/BaseTestCase.php +++ b/Tests/BaseTestCase.php @@ -1,4 +1,5 @@ getScopeInfo($currScope); - return ( $scopeInfo && isset($scopeInfo->variables[$varName]) ) ? $scopeInfo->variables[$varName] : null; + return ($scopeInfo && isset($scopeInfo->variables[$varName])) ? $scopeInfo->variables[$varName] : null; } /** @@ -838,8 +838,7 @@ protected function processVariableAsThisWithinClass(File $phpcsFile, $stackPtr, */ protected function processVariableAsSuperGlobal($varName) { - // Are we a superglobal variable? - if (in_array($varName, [ + $superglobals = [ 'GLOBALS', '_SERVER', '_GET', @@ -854,11 +853,9 @@ protected function processVariableAsSuperGlobal($varName) 'php_errormsg', 'http_response_header', 'HTTP_RAW_POST_DATA', - ])) { - return true; - } - - return false; + ]; + // Are we a superglobal variable? + return (in_array($varName, $superglobals, true)); } /** @@ -1140,22 +1137,21 @@ protected function processVariableAsStaticDeclaration(File $phpcsFile, $stackPtr // class constant T_STRING T_DOUBLE_COLON T_STRING // Search backwards for first token that isn't whitespace, comma, variable, // equals, or on the list of assignable constant values above. - $staticPtr = $phpcsFile->findPrevious( - [ - T_WHITESPACE, T_VARIABLE, T_COMMA, T_EQUAL, - T_MINUS, T_LNUMBER, T_DNUMBER, - T_CONSTANT_ENCAPSED_STRING, - T_STRING, - T_DOUBLE_COLON, - T_START_HEREDOC, T_HEREDOC, T_END_HEREDOC, - T_START_NOWDOC, T_NOWDOC, T_END_NOWDOC, - ], - $stackPtr - 1, - null, - true, - null, - true - ); + $find = [ + T_WHITESPACE => T_WHITESPACE, + T_VARIABLE => T_VARIABLE, + T_COMMA => T_COMMA, + T_EQUAL => T_EQUAL, + T_MINUS => T_MINUS, + T_LNUMBER => T_LNUMBER, + T_DNUMBER => T_DNUMBER, + T_CONSTANT_ENCAPSED_STRING => T_CONSTANT_ENCAPSED_STRING, + T_STRING => T_STRING, + T_DOUBLE_COLON => T_DOUBLE_COLON, + ]; + $find += Tokens::$heredocTokens; + + $staticPtr = $phpcsFile->findPrevious($find, $stackPtr - 1, null, true, null, true); if (($staticPtr === false) || ($tokens[$staticPtr]['code'] !== T_STATIC)) { return false; } diff --git a/composer.json b/composer.json index 7172f63..f91cc7f 100644 --- a/composer.json +++ b/composer.json @@ -49,6 +49,7 @@ "require-dev": { "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.5 || ^7.0 || ^8.0 || ^9.0", "sirbrillig/phpcs-import-detection": "^1.1", + "phpcsstandards/phpcsdevcs": "^1.1", "phpstan/phpstan": "^1.7", "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0" } diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 46ba582..4f0cd1b 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -1,7 +1,15 @@ - ./VariableAnalysis/ - ./Tests/ + + + + . + ./Tests/VariableAnalysisSniff/fixtures/ ./vendor/ @@ -17,21 +25,83 @@ + - - + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + - - + + + 5 + + + + + + + + + + + + + + + + + + + + +