Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.0 | PEAR/PSR2/FunctionCallSignature: support anonymous classes #47

Open
wants to merge 2 commits into
base: 4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -587,3 +587,25 @@ content
'my_file.php'
); ?>
<?php endif; ?>
<?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 @@ -605,3 +605,26 @@ content
'my_file.php'
); ?>
<?php endif; ?>
<?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 @@ -117,6 +117,11 @@ public function getErrorList()
581 => 1,
586 => 1,
587 => 1,
601 => 2,
602 => 2,
603 => 1,
604 => 1,
605 => 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
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,10 @@ if (empty($argTags > 0)) {
}

myFunction($var1 === true ? "" : "foobar");

$anon = new class(!$foo ? 0 : 1, ($bar == true) ? 1 : 0) {
function __construct($a, $b) {}
};
$anon = new class($foo === false ? 0 : 1, ($bar === true) ? 1 : 0) {
function __construct($a, $b) {}
};
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function getErrorList()
127 => 1,
131 => 1,
135 => 1,
140 => 2,
];

}//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