From 5d341921629ecff9399710969d8455c38b46e87d Mon Sep 17 00:00:00 2001 From: jrfnl Date: Thu, 3 Apr 2025 00:36:38 +0200 Subject: [PATCH] Generic.Functions.CallTimePassByReference: remove sniff Fixes 921 Related to 6 --- .../CallTimePassByReferenceStandard.xml | 31 --- .../CallTimePassByReferenceSniff.php | 180 ------------------ .../CallTimePassByReferenceUnitTest.1.inc | 66 ------- .../CallTimePassByReferenceUnitTest.2.inc | 7 - .../CallTimePassByReferenceUnitTest.3.inc | 7 - .../CallTimePassByReferenceUnitTest.php | 75 -------- 6 files changed, 366 deletions(-) delete mode 100644 src/Standards/Generic/Docs/Functions/CallTimePassByReferenceStandard.xml delete mode 100644 src/Standards/Generic/Sniffs/Functions/CallTimePassByReferenceSniff.php delete mode 100644 src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.1.inc delete mode 100644 src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.2.inc delete mode 100644 src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.3.inc delete mode 100644 src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.php diff --git a/src/Standards/Generic/Docs/Functions/CallTimePassByReferenceStandard.xml b/src/Standards/Generic/Docs/Functions/CallTimePassByReferenceStandard.xml deleted file mode 100644 index 738998d432..0000000000 --- a/src/Standards/Generic/Docs/Functions/CallTimePassByReferenceStandard.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - &$bar) -{ - $bar++; -} - -$baz = 1; -foo($baz); - ]]> - - - &$baz); - ]]> - - - diff --git a/src/Standards/Generic/Sniffs/Functions/CallTimePassByReferenceSniff.php b/src/Standards/Generic/Sniffs/Functions/CallTimePassByReferenceSniff.php deleted file mode 100644 index 2547e238d5..0000000000 --- a/src/Standards/Generic/Sniffs/Functions/CallTimePassByReferenceSniff.php +++ /dev/null @@ -1,180 +0,0 @@ - - * @copyright 2009-2014 Florian Grandel - * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence - * - * @deprecated 3.12.1 - */ - -namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Functions; - -use PHP_CodeSniffer\Files\File; -use PHP_CodeSniffer\Sniffs\DeprecatedSniff; -use PHP_CodeSniffer\Sniffs\Sniff; -use PHP_CodeSniffer\Util\Tokens; - -class CallTimePassByReferenceSniff implements Sniff, DeprecatedSniff -{ - - - /** - * Returns an array of tokens this test wants to listen for. - * - * @return array - */ - public function register() - { - return [ - T_STRING, - T_VARIABLE, - T_ANON_CLASS, - T_PARENT, - T_SELF, - T_STATIC, - ]; - - }//end register() - - - /** - * Processes this test, when one of its tokens is encountered. - * - * @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(File $phpcsFile, $stackPtr) - { - $tokens = $phpcsFile->getTokens(); - - $findTokens = Tokens::$emptyTokens; - $findTokens[] = T_BITWISE_AND; - - $prev = $phpcsFile->findPrevious($findTokens, ($stackPtr - 1), null, true); - - // Skip tokens that are the names of functions - // within their definitions. For example: function myFunction... - // "myFunction" is T_STRING but we should skip because it is not a - // function or method *call*. - $prevCode = $tokens[$prev]['code']; - if ($prevCode === T_FUNCTION) { - return; - } - - // If the next non-whitespace token after the function or method call - // is not an opening parenthesis then it cant really be a *call*. - $functionName = $stackPtr; - $openBracket = $phpcsFile->findNext( - Tokens::$emptyTokens, - ($functionName + 1), - null, - true - ); - - if ($openBracket === false || $tokens[$openBracket]['code'] !== T_OPEN_PARENTHESIS) { - return; - } - - if (isset($tokens[$openBracket]['parenthesis_closer']) === false) { - return; - } - - $closeBracket = $tokens[$openBracket]['parenthesis_closer']; - - $nextSeparator = $openBracket; - $find = [ - T_VARIABLE, - T_OPEN_SHORT_ARRAY, - ]; - - while (($nextSeparator = $phpcsFile->findNext($find, ($nextSeparator + 1), $closeBracket)) !== false) { - if ($tokens[$nextSeparator]['code'] === T_OPEN_SHORT_ARRAY) { - $nextSeparator = $tokens[$nextSeparator]['bracket_closer']; - continue; - } - - // Make sure the variable belongs directly to this function call - // and is not inside a nested function call or array. - $brackets = $tokens[$nextSeparator]['nested_parenthesis']; - $lastBracket = array_pop($brackets); - if ($lastBracket !== $closeBracket) { - continue; - } - - $tokenBefore = $phpcsFile->findPrevious( - Tokens::$emptyTokens, - ($nextSeparator - 1), - null, - true - ); - - if ($tokens[$tokenBefore]['code'] === T_BITWISE_AND) { - if ($phpcsFile->isReference($tokenBefore) === false) { - continue; - } - - // We also want to ignore references used in assignment - // operations passed as function arguments, but isReference() - // sees them as valid references (which they are). - $tokenBefore = $phpcsFile->findPrevious( - Tokens::$emptyTokens, - ($tokenBefore - 1), - null, - true - ); - - if (isset(Tokens::$assignmentTokens[$tokens[$tokenBefore]['code']]) === true) { - continue; - } - - // T_BITWISE_AND represents a pass-by-reference. - $error = 'Call-time pass-by-reference calls are prohibited'; - $phpcsFile->addError($error, $tokenBefore, 'NotAllowed'); - }//end if - }//end while - - }//end process() - - - /** - * Provide the version number in which the sniff was deprecated. - * - * @return string - */ - public function getDeprecationVersion() - { - return 'v3.12.1'; - - }//end getDeprecationVersion() - - - /** - * Provide the version number in which the sniff will be removed. - * - * @return string - */ - public function getRemovalVersion() - { - return 'v4.0.0'; - - }//end getRemovalVersion() - - - /** - * Provide a custom message to display with the deprecation. - * - * @return string - */ - public function getDeprecationMessage() - { - return ''; - - }//end getDeprecationMessage() - - -}//end class diff --git a/src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.1.inc b/src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.1.inc deleted file mode 100644 index 9360680ce7..0000000000 --- a/src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.1.inc +++ /dev/null @@ -1,66 +0,0 @@ -myfunc(&$myvar); -$this->myfunc($myvar); - -myclass::myfunc(&$myvar); -myclass::myfunc($myvar); - -while(testfunc($var1, &$var2, $var3, &$var4) === false) { -} - -sprintf("0%o", 0777 & $p); - -$foo(&$myvar); - -if (is_array($foo = &$this->bar())) { -} - -Hooks::run( 'SecondaryDataUpdates', [ $title, $old, $recursive, $parserOutput, &$updates ] ); - -$foo = Bar(&$fooBar); - -myfunc($myvar&$myvar); -myfunc($myvar[0]&$myvar); - -myfunc(myclass::MY_CONST&$myvar); -myfunc(MY_CONST&$myvar); - -efg( true == &$b ); -efg( true === &$b ); - -foo($a, bar(&$b)); -foo($a, array(&$b)); - -enum Foo {} -interface Foo {} -trait Foo {} - -$instance = new $var($a); -$instance = new MyClass($a); -$instance = new $var(&$a); -$instance = new MyClass(&$a); - -$anon = new class($a) {}; -$anon = new class(&$a) {}; - -class Foo extends Bar { - function myMethod() { - $a = new static($var); - $b = new self($var); - $c = new parent($var); - - $d = new static(&$var); - $e = new self(&$var); - $f = new parent(&$var); - } -} diff --git a/src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.2.inc b/src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.2.inc deleted file mode 100644 index 769c5d61ba..0000000000 --- a/src/Standards/Generic/Tests/Functions/CallTimePassByReferenceUnitTest.2.inc +++ /dev/null @@ -1,7 +0,0 @@ - - * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600) - * @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence - */ - -namespace PHP_CodeSniffer\Standards\Generic\Tests\Functions; - -use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest; - -/** - * Unit test class for the CallTimePassByReference sniff. - * - * @covers \PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\CallTimePassByReferenceSniff - */ -final class CallTimePassByReferenceUnitTest 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. - * - * @param string $testFile The name of the test file to process. - * - * @return array - */ - public function getErrorList($testFile='CallTimePassByReferenceUnitTest.1.inc') - { - switch ($testFile) { - case 'CallTimePassByReferenceUnitTest.1.inc': - return [ - 9 => 1, - 12 => 1, - 15 => 1, - 18 => 2, - 23 => 1, - 30 => 1, - 41 => 1, - 50 => 1, - 51 => 1, - 54 => 1, - 62 => 1, - 63 => 1, - 64 => 1, - ]; - - default: - return []; - }//end switch - - }//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 - */ - public function getWarningList() - { - return []; - - }//end getWarningList() - - -}//end class