coding-standards icon indicating copy to clipboard operation
coding-standards copied to clipboard

PHPCS not enforcing space after parenthesis when args span multiple lines

Open mattheu opened this issue 6 years ago • 4 comments

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' ] );

mattheu avatar Aug 13 '18 11:08 mattheu

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

mattheu avatar Aug 13 '18 13:08 mattheu

Needs refresh to see whether this is still an issue.

kadamwhite avatar May 19 '20 15:05 kadamwhite

I can confirm that the original issue is still present.

mikeselander avatar May 20 '20 19:05 mikeselander

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?

tfrommen avatar Mar 02 '21 19:03 tfrommen