Skip to content

Commit

Permalink
PEAR/PSR2/FunctionCallSignature: support anonymous classes
Browse files Browse the repository at this point in the history
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 .
  • Loading branch information
jrfnl committed Nov 9, 2023
1 parent 68fec14 commit d17d642
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,25 @@ content
<p><?php require get_theme_file_path(
'/theme_extra/test_block.php'
); ?></p>
<?php

// 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 {};
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,26 @@ content
<p><?php require get_theme_file_path(
'/theme_extra/test_block.php'
); ?></p>
<?php

// 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 {};
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public function getErrorList()
572 => 1,
580 => 1,
581 => 1,

594 => 2,
595 => 2,
596 => 1,
597 => 1,
598 => 2,
];

}//end getErrorList()
Expand Down
21 changes: 21 additions & 0 deletions src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/Util/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

/**
Expand Down

0 comments on commit d17d642

Please sign in to comment.