Skip to content

Commit

Permalink
Add multi-line function argument support
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher committed Jul 18, 2024
1 parent 6d3ed6b commit d05f208
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 4 deletions.
15 changes: 15 additions & 0 deletions Alley-Interactive/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<!-- Allow checking any capability. -->
<exclude name="WordPress.WP.Capabilities.Undetermined" />
<exclude name="WordPress.WP.Capabilities.Unknown" />
<!-- Disable the PEAR-style function call signature. -->
<exclude name="PEAR.Functions.FunctionCallSignature" />
</rule>

<!-- Use the VIP Go ruleset. -->
Expand Down Expand Up @@ -95,4 +97,17 @@
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<exclude-pattern>tests/*Test.php</exclude-pattern>
</rule>

<!--
Replace PEAR.Functions.FunctionCallSignature with a sniff that supports
arguments on the same line as the function call while still requiring
spaces after the opening parenthesis and before the closing parenthesis.
-->
<rule ref="PSR2.Methods.FunctionCallSignature">
<properties>
<property name="requiredSpacesAfterOpen" value="1" />
<property name="requiredSpacesBeforeClose" value="1" />
<property name="allowMultipleArguments" value="true" />
</properties>
</rule>
</ruleset>
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ This project adheres to [Keep a CHANGELOG](https://keepachangelog.com/en/1.0.0/)
### Unreleased

- Allow PSR-4 style `ClassName.php` file names to support our migration to PSR-4 for test files.
- Remove the `PEAR.Functions.FunctionCallSignature` sniff from the ruleset and
replace it with `PSR2.Methods.FunctionCallSignature`. This change is to allow
for multi-line function calls to be formatted in a more readable way without having to insert a new line before the first argument.

### 2.0.2

Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ You can also pass arguments to the composer phpcs script, following a `--` opera
composer run phpcs -- --report=summary
```

### Extending the ruleset
### Extending the Ruleset

You can create a custom ruleset for your project that extends or customizes
these rules by creating your own `phpcs.xml` file in your project, which
Expand All @@ -68,6 +68,16 @@ references these rules, like this:
</ruleset>
```

## Testing

When contributing to this project, modifications to the ruleset should have a
corresponding test in the `tests` directory. For the most part, this takes the
form of a passing test in `tests/fixtures/pass` and a failing one in
`tests/fixtures/fail`. You can run the tests with `composer phpunit`. If you
want to run PHPCS against the test fixtures, you can run
`composer phpcs:fixtures` to ensure that what is passing/failing matches your
expectations.

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
},
"scripts": {
"phpcbf": "phpcbf --standard=Alley-Interactive tests/*Test.php",
"phpcs": "phpcs --standard=Alley-Interactive tests/*Test.php -vsn",
"phpcs:fixtures": "phpcs --standard=Alley-Interactive tests/fixtures",
"phpcs": "phpcs --standard=Alley-Interactive tests/*Test.php -sn",
"phpcs:fixtures": "phpcs -sn --standard=Alley-Interactive tests/fixtures",
"phpunit": "phpunit",
"test": [
"@phpcs",
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpcsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected function run_phpcs( string $file ): array {

$base_path = dirname( __DIR__ );
$shell = sprintf(
'%s %s %s --standard=%s --no-cache --report=json',
'%s %s "%s" --standard=%s --no-cache --report=json',
PHP_BINARY,
"{$base_path}/vendor/bin/phpcs",
$file,
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/fail/functions-empty-arguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* Functions test file.
*
* @package Alley\WP\Coding_Standards
*/

ai_method_call( );
12 changes: 12 additions & 0 deletions tests/fixtures/fail/functions-space-arguments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* Functions test file.
*
* @package Alley\WP\Coding_Standards
*/

// Ensure that there is a space between the opening parenthesis and the function arguments.
// Supports PSR2.Methods.FunctionCallSignature
ai_method_call([
'foo' => 'bar',
]);
15 changes: 15 additions & 0 deletions tests/fixtures/pass/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,18 @@
};

echo esc_html( $ai_function() );

// Allow function arguments on the same line as the function call.
// Supports PSR2.Methods.FunctionCallSignature.
ai_method_call( [
'foo' => 'bar',
] );

ai_method_call(
[
'foo' => 'bar',
]
);

ai_method_call( [ 'foo' => 'bar' ] );
ai_method_call();

0 comments on commit d05f208

Please sign in to comment.