Skip to content

Commit 492b884

Browse files
authored
Merge pull request #47 from PHPCSStandards/feature/pear-psr2-psr12-functioncallsignature-check-anon-classes
4.0 | PEAR/PSR2/FunctionCallSignature: support anonymous classes
2 parents 483fca2 + 4700653 commit 492b884

10 files changed

+118
-9
lines changed

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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,3 +588,24 @@ $val = namespace\functionCall(
588588
$arg,
589589
$arg2
590590
);
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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,3 +606,25 @@ $val = namespace\functionCall(
606606
$arg,
607607
$arg2
608608
);
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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ public function getErrorList()
122122
583 => 2,
123123
584 => 1,
124124
586 => 2,
125+
126+
601 => 2,
127+
602 => 2,
128+
603 => 1,
129+
604 => 1,
130+
605 => 2,
125131
];
126132

127133
}//end getErrorList()

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

Lines changed: 21 additions & 0 deletions
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

Lines changed: 23 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public function getErrorList()
7676
258 => 1,
7777
263 => 1,
7878
264 => 1,
79+
278 => 2,
80+
279 => 2,
81+
280 => 1,
82+
281 => 1,
83+
282 => 3,
7984
];
8085

8186
}//end getErrorList()

src/Standards/Squiz/Tests/Operators/ComparisonOperatorUsageUnitTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,10 @@ if (true) {}
152152
if (\true) {}
153153
for ($var1 = 10; FALSE; $var1--) {}
154154
for ($var1 = 10; \FALSE; $var1--) {}
155+
156+
$anon = new class(!$foo ? 0 : 1, ($bar == true) ? 1 : 0) {
157+
function __construct($a, $b) {}
158+
};
159+
$anon = new class($foo === false ? 0 : 1, ($bar === true) ? 1 : 0) {
160+
function __construct($a, $b) {}
161+
};

src/Standards/Squiz/Tests/Operators/ComparisonOperatorUsageUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function getErrorList()
6363
146 => 1,
6464
147 => 1,
6565
148 => 1,
66+
156 => 2,
6667
];
6768

6869
}//end getErrorList()

src/Util/Tokens.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -464,14 +464,15 @@ final class Tokens
464464
* @var array<int|string, int|string>
465465
*/
466466
public const FUNCTION_NAME_TOKENS = (self::INCLUDE_TOKENS + self::NAME_TOKENS + [
467-
T_EVAL => T_EVAL,
468-
T_EXIT => T_EXIT,
469-
T_ISSET => T_ISSET,
470-
T_UNSET => T_UNSET,
471-
T_EMPTY => T_EMPTY,
472-
T_SELF => T_SELF,
473-
T_PARENT => T_PARENT,
474-
T_STATIC => T_STATIC,
467+
T_EVAL => T_EVAL,
468+
T_EXIT => T_EXIT,
469+
T_ISSET => T_ISSET,
470+
T_UNSET => T_UNSET,
471+
T_EMPTY => T_EMPTY,
472+
T_SELF => T_SELF,
473+
T_PARENT => T_PARENT,
474+
T_STATIC => T_STATIC,
475+
T_ANON_CLASS => T_ANON_CLASS,
475476
]);
476477

477478
/**

0 commit comments

Comments
 (0)