Skip to content

Commit

Permalink
Merge pull request #41 from alleyinteractive/feature/allow-array-same…
Browse files Browse the repository at this point in the history
…-line-function

Proposal: Add support for function arguments to be passed in the same line as the function
  • Loading branch information
srtfisher authored Jul 19, 2024
2 parents c024f52 + e552adf commit 242b93f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 5 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 @@ -96,6 +98,19 @@
<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>

<!-- Allow snakeCase for DOMDocument/DOMElement properties -->
<rule ref="WordPress.NamingConventions.ValidVariableName">
<properties>
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ This project adheres to [Keep a CHANGELOG](https://keepachangelog.com/en/1.0.0/)

This project adheres to [Keep a CHANGELOG](https://keepachangelog.com/en/1.0.0/).

### Unreleased
### 2.1.0

- 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.
- Allow camelCase'd DOMDocument/DOMElement/etc. property names to not be flagged by `WordPress.NamingConventions.ValidVariableName`.

### 2.0.2
Expand Down
13 changes: 12 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,17 @@ 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. For failing fixtures in `tests/fixtures/fail`, we recommend
keeping the files smaller and focused on the specific sniff being tested.

## 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',
]);
23 changes: 23 additions & 0 deletions tests/fixtures/pass/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,26 @@
};

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',
'another' => 'test',
'depth' => [
'one' => 'two',
],
] );

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

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

0 comments on commit 242b93f

Please sign in to comment.