squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.63k stars 1.48k forks source link

Squiz/FunctionDeclarationArgumentSpacing: make the reference operator spacing configurable #3900

Closed darrenedale closed 9 months ago

darrenedale commented 9 months ago

Description

Enables the amount of whitespace between the reference operator and the argument name in function declarations to be defined by a property in XML config. The existing sniff fixes this at 0 spaces, meaning function declarations with by-reference arguments must look like this:

function do_something(string &$withThis): void;

My coding style follows PSR12, with a few modifications, one of which is to have a space between the reference operator and the argument name:

function do_something(string & $withThis): void;

Since the PSR12 standard imports Standards\Squizz\Sniffs\Functions\FunctionDeclarationArgumentSpacingSniff, and since at present this sniff has a strict requirement of 0 spaces between the reference operator and the argument name, it's not possible to base my standard off PSR12 and make this customisation. This PR resolves that by making the amount of space between the reference operator and the argument name configurable in the XML file, thus:

<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing">
    <properties>
        <property name="requiredSpacesAfterReferenceOperator" value="1"/>
    </properties>
</rule>

The feature has been implemented similarly to other properties that are configurable in this sniff. Notably, the property controlling the required number of spaces after the reference operator defaults to 0, ensuring that where no customisation is present for this property for this sniff in the configuration file, the sniff continues to behave as it does currently. Therefore existing workflows using this sniff will be unaffected.

Suggested changelog entry

Allow the required spacing between the reference operator and the argument name in function declarations to be customised in configuration files for the Squizz standard's FunctionDeclarationArgumentSpacing sniff.

Related issues/external references

Fixes #

Types of changes

PR checklist

* I've checked the test and it doesn't look like it's possible to test the sniff with alternate configurations. I'm happy to write one if I've missed how this is done.

jrfnl commented 9 months ago

@darrenedale I've never seen anyone else ask for this and it goes against industry standards like PSR12, so I'm not sure this feature should be accepted without validation that this is something that more than one person intends to use.

I'm leaving this PR open for now to see if more people are interested in this feature.

Please be aware that as things are, the PR is not in a mergable state as there are no tests covering the new feature included.

darrenedale commented 9 months ago

OK, I'll find a different solution.