Skip to content

Commit 1505104

Browse files
committed
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 .
1 parent bf3346a commit 1505104

8 files changed

+103
-1
lines changed

src/Standards/PEAR/Sniffs/Functions/FunctionCallSignatureSniff.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ public function process(File $phpcsFile, $stackPtr)
110110

111111
$closeBracket = $tokens[$openBracket]['parenthesis_closer'];
112112

113-
if (($stackPtr + 1) !== $openBracket) {
113+
if ($tokens[$stackPtr]['code'] !== T_ANON_CLASS
114+
&& ($stackPtr + 1) !== $openBracket
115+
) {
114116
// Checking this: $value = my_function[*](...).
115117
$error = 'Space before opening parenthesis of function call prohibited';
116118
$fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpaceBeforeOpenBracket');

src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc

+22
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,25 @@ content
587587
'my_file.php'
588588
); ?>
589589
<?php endif; ?>
590+
<?php
591+
592+
// Anonymous object instantiations are treated the same as a normal call.
593+
$anon = new class() {};
594+
$anon = new class($foo, true) {};
595+
$anon = new class(
596+
$foo,
597+
true,
598+
10
599+
) {};
600+
601+
$anon = new class( ) {};
602+
$anon = new class( $foo, true ) {};
603+
$anon = new class($foo,
604+
605+
true, 10) {};
606+
607+
// ... though do not enforce no space between the class keyword and the open parenthesis.
608+
$anon = new class () {};
609+
610+
// And anonymous object instantiations without parentheses are ignored.
611+
$anon = new class {};

src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.inc.fixed

+23
Original file line numberDiff line numberDiff line change
@@ -605,3 +605,26 @@ content
605605
'my_file.php'
606606
); ?>
607607
<?php endif; ?>
608+
<?php
609+
610+
// Anonymous object instantiations are treated the same as a normal call.
611+
$anon = new class() {};
612+
$anon = new class($foo, true) {};
613+
$anon = new class(
614+
$foo,
615+
true,
616+
10
617+
) {};
618+
619+
$anon = new class() {};
620+
$anon = new class($foo, true) {};
621+
$anon = new class(
622+
$foo,
623+
true, 10
624+
) {};
625+
626+
// ... though do not enforce no space between the class keyword and the open parenthesis.
627+
$anon = new class () {};
628+
629+
// And anonymous object instantiations without parentheses are ignored.
630+
$anon = new class {};

src/Standards/PEAR/Tests/Functions/FunctionCallSignatureUnitTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ public function getErrorList()
117117
581 => 1,
118118
586 => 1,
119119
587 => 1,
120+
601 => 2,
121+
602 => 2,
122+
603 => 1,
123+
604 => 1,
124+
605 => 2,
120125
];
121126

122127
}//end getErrorList()

src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc

+21
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,24 @@ array_fill_keys(
265265
), value: true,
266266
);
267267
// phpcs:set PSR2.Methods.FunctionCallSignature allowMultipleArguments false
268+
269+
// Anonymous object instantiations are treated the same as a normal call.
270+
$anon = new class() {};
271+
$anon = new class($foo, true) {};
272+
$anon = new class(
273+
$foo,
274+
true,
275+
10
276+
) {};
277+
278+
$anon = new class( ) {};
279+
$anon = new class( $foo, true ) {};
280+
$anon = new class($foo,
281+
282+
true, 10) {};
283+
284+
// ... though do not enforce no space between the class keyword and the open parenthesis.
285+
$anon = new class () {};
286+
287+
// And anonymous object instantiations without parentheses are ignored.
288+
$anon = new class {};

src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.inc.fixed

+23
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,26 @@ array_fill_keys(
281281
), value: true,
282282
);
283283
// phpcs:set PSR2.Methods.FunctionCallSignature allowMultipleArguments false
284+
285+
// Anonymous object instantiations are treated the same as a normal call.
286+
$anon = new class() {};
287+
$anon = new class($foo, true) {};
288+
$anon = new class(
289+
$foo,
290+
true,
291+
10
292+
) {};
293+
294+
$anon = new class() {};
295+
$anon = new class($foo, true) {};
296+
$anon = new class(
297+
$foo,
298+
true,
299+
10
300+
) {};
301+
302+
// ... though do not enforce no space between the class keyword and the open parenthesis.
303+
$anon = new class () {};
304+
305+
// And anonymous object instantiations without parentheses are ignored.
306+
$anon = new class {};

src/Standards/PSR2/Tests/Methods/FunctionCallSignatureUnitTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public function getErrorList()
7171
258 => 1,
7272
263 => 1,
7373
264 => 1,
74+
278 => 2,
75+
279 => 2,
76+
280 => 1,
77+
281 => 1,
78+
282 => 3,
7479
];
7580

7681
}//end getErrorList()

src/Util/Tokens.php

+1
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ final class Tokens
632632
T_SELF => T_SELF,
633633
T_PARENT => T_PARENT,
634634
T_STATIC => T_STATIC,
635+
T_ANON_CLASS => T_ANON_CLASS,
635636
];
636637

637638
/**

0 commit comments

Comments
 (0)