From e0ff181229a67b988e789e3699fb8e19ef014c88 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 23 Jul 2022 15:20:32 +0200 Subject: [PATCH] PEAR/PSR2/FunctionCallSignature: support anonymous classes The function call spacing for anonymous class instantiations was so far not checked by these or any other PHPCS native sniffs. In my opinion, object instantiations of anonymous classes should be treated the same an object instantiations of non-anonymous classes. The `PEAR.Functions.FunctionCallSignature` and the `PSR2.Methods.FunctionCallSignature` sniffs check the object instantiation spacing for non-anonymous classes, so seem like the logical place to also check the spacing for anonymous class object instantiations. To add this support, the `T_ANON_CLASS` token has been added to the `Tokens::$functionNameTokens` array. Notes: * As PSR12 does not specify the spacing between the `class` keyword and the open parenthesis (or rather is unclear about it), I am explicitly excluding anonymous classes from the "space before open parenthesis" check. Related: squizlabs/PHP_CodeSniffer 3200 * I have verified all other uses of the `Tokens::$functionNameTokens` array within PHPCS. - The `Generic.WhiteSpace.ArbitraryParenthesesSpacing` sniff is not affected by the change and already contains a test to verify this. - The `Squiz.Operators.ComparisonOperatorUsage` sniff also is not affected by the change. I have added tests to confirm this in a separate commit. * Obviously external standards using the token array _may_ be affected by the change, but a scan of the most popular external standards showed me that the token array is rarely used and when it is used, is mostly used incorrectly. The only sniff using the array, which looks to be using it correctly and which may be affected, is the `WebImpressCodingStandard.WhiteSpace.ScopeIndent` sniff. Whether this is positive or negative is up to michalbundyra to determine. Includes unit tests for both the `PEAR.Functions.FunctionCallSignature` and the `PSR2.Methods.FunctionCallSignature` sniffs . --- .../Functions/FunctionCallSignatureSniff.php | 4 +++- .../FunctionCallSignatureUnitTest.inc | 22 ++++++++++++++++++ .../FunctionCallSignatureUnitTest.inc.fixed | 23 +++++++++++++++++++ .../FunctionCallSignatureUnitTest.php | 5 ++++ .../Methods/FunctionCallSignatureUnitTest.inc | 21 +++++++++++++++++ .../FunctionCallSignatureUnitTest.inc.fixed | 23 +++++++++++++++++++ .../Methods/FunctionCallSignatureUnitTest.php | 5 ++++ src/Util/Tokens.php | 1 + 8 files changed, 103 insertions(+), 1 deletion(-) diff --git a/src/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php b/src/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php index 323dc3b306..b98186b146 100644 --- a/src/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php +++ b/src/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php @@ -110,7 +110,9 @@ public function process(File $phpcsFile, $stackPtr) $closeBracket = $tokens[$openBracket]['parenthesis_closer']; - if (($stackPtr + 1) !== $openBracket) { + if ($tokens[$stackPtr]['code'] !== T_ANON_CLASS + && ($stackPtr + 1) !== $openBracket + ) { // Checking this: $value = my_function[*](...). $error = 'Space before opening parenthesis of function call prohibited'; $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeOpenBracket'); diff --git a/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc b/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc index 8e35a96c8b..5c7823ce7c 100644 --- a/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc +++ b/src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc @@ -587,3 +587,25 @@ content 'my_file.php' ); ?> + + 1, 586 => 1, 587 => 1, + 601 => 2, + 602 => 2, + 603 => 1, + 604 => 1, + 605 => 2, ]; }//end getErrorList() diff --git a/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc b/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc index 1ca477d054..57ed98c301 100644 --- a/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc +++ b/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc @@ -265,3 +265,24 @@ array_fill_keys( ), value: true, ); // phpcs:set PSR2.Methods.FunctionCallSignature allowMultipleArguments false + +// Anonymous object instantiations are treated the same as a normal call. +$anon = new class() {}; +$anon = new class($foo, true) {}; +$anon = new class( + $foo, + true, + 10 +) {}; + +$anon = new class( ) {}; +$anon = new class( $foo, true ) {}; +$anon = new class($foo, + + true, 10) {}; + +// ... though do not enforce no space between the class keyword and the open parenthesis. +$anon = new class () {}; + +// And anonymous object instantiations without parentheses are ignored. +$anon = new class {}; diff --git a/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc.fixed b/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc.fixed index dc383ed2a7..ef4630874b 100644 --- a/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc.fixed +++ b/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc.fixed @@ -281,3 +281,26 @@ array_fill_keys( ), value: true, ); // phpcs:set PSR2.Methods.FunctionCallSignature allowMultipleArguments false + +// Anonymous object instantiations are treated the same as a normal call. +$anon = new class() {}; +$anon = new class($foo, true) {}; +$anon = new class( + $foo, + true, + 10 +) {}; + +$anon = new class() {}; +$anon = new class($foo, true) {}; +$anon = new class( + $foo, + true, + 10 +) {}; + +// ... though do not enforce no space between the class keyword and the open parenthesis. +$anon = new class () {}; + +// And anonymous object instantiations without parentheses are ignored. +$anon = new class {}; diff --git a/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.php b/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.php index 7f6ce1f959..ee133d22bd 100644 --- a/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.php +++ b/src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.php @@ -71,6 +71,11 @@ public function getErrorList() 258 => 1, 263 => 1, 264 => 1, + 278 => 2, + 279 => 2, + 280 => 1, + 281 => 1, + 282 => 3, ]; }//end getErrorList() diff --git a/src/Util/Tokens.php b/src/Util/Tokens.php index 4f0fb5ae86..fe544f96f8 100644 --- a/src/Util/Tokens.php +++ b/src/Util/Tokens.php @@ -632,6 +632,7 @@ final class Tokens T_SELF => T_SELF, T_PARENT => T_PARENT, T_STATIC => T_STATIC, + T_ANON_CLASS => T_ANON_CLASS, ]; /**