coding-standards
coding-standards copied to clipboard
PHPCS not enforcing space after parenthesis when args span multiple lines
The following code is NOT flagged as an error, but I would expect it to be. Note missing space after the first (
.
get_extended_template_part('atoms/heading', '4', [
'heading_three' => 'This is a heading 3',
] );
However it is correctly enforced if the args are on a single line. e.g.
get_extended_template_part('atoms/heading', '4', [ 'heading_three' => 'This is a heading 3' ] );
In addition PHPCBF fixes this to the following, which I don't think is quite correct - certainly not enforced by PHPCS
get_extended_template_part(
'atoms/heading', '2', [
'heading_two' => 'This is a heading 2',
]
);
Digging... There are a couple of related issues. Seems a combination of the fact that WPCS isn't enforcing proper multiline function calls, and that these checks are ignroed for PHPCS only and not PHPCBF.
We ran into this issue on the a different project too and ended up ignoring this sniff for PHPCBF.
<exclude phpcbf-only="true" name="PEAR.Functions.FunctionCallSignature"/>
Needs refresh to see whether this is still an issue.
I can confirm that the original issue is still present.
Using the most recent version of the HM Coding Standards, this does get flagged, as expected.
To reproduce, execute the following in some empty folder:
-
echo '{}' > composer.json
-
composer require humanmade/coding-standards
-
echo -e "<?php\nprint('foo', [\n\t42,\n], 'bar');\n" > code.php
-
./vendor/bin/phpcs --standard=HM code.php
This results in the following output:
----------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------
1 | ERROR | [ ] Missing file doc comment
2 | ERROR | [ ] PHP syntax error: syntax error, unexpected ','
2 | ERROR | [x] Expected 1 space after open parenthesis; 0 found
4 | ERROR | [x] Expected 1 space before close parenthesis; 0 found
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Both the missing space right after the open parenthesis and the one right before the closing parenthesis get flagged.
Also, running phpcbf
, results in the expected code:
<?php
-print('foo', [
+print( 'foo', [
42,
-], 'bar');
+], 'bar' );
Can you confirm this, @mattheu? Can we close?