Open
Description
Describe the bug
If a closure/anonymous function is used as a callback in a function call, then indentations doesn't get fixed with Generic.WhiteSpace.ScopeIndent
sniff property exact
set to true
. The conditional statement blocks will get fixed, but the code inside them won't. Both space and tab indentation doesn't get fixed. I am aware there is multiple closed issues relating to indentation but they're slightly different issues I believe (none of the solutions fixed the issue).
Code sample
some_function(function ($arg) {
if (!$arg) {
return;
}
$arg = "Hello";
return $arg;
});
Custom ruleset
<?xml version="1.0"?>
<ruleset name="yCodeTech">
<description>yCodeTech's Coding Standards</description>
<!-- Scan all files in directory -->
<file>.</file>
<!-- Scan only PHP files -->
<arg name="extensions" value="php"/>
<!-- Ignore WordPress and Composer dependencies -->
<exclude-pattern>web/wp</exclude-pattern>
<exclude-pattern>web/app/plugins</exclude-pattern>
<exclude-pattern>web/app/mu-plugins</exclude-pattern>
<exclude-pattern>vendor/</exclude-pattern>
<exclude-pattern>web/app/uploads/</exclude-pattern>
<!-- Show colors in console -->
<arg value="-colors"/>
<!-- Show sniff codes in all reports -->
<arg value="ns"/>
<!-- Allow multiple classes in a single file ONLY for facades.php files,
ie. exclude the file from the sniff. -->
<rule ref="PSR1.Classes.ClassDeclaration.MultipleClasses">
<exclude-pattern>*facades.php</exclude-pattern>
</rule>
<!-- Use PSR-2 as a base -->
<rule ref="PSR2">
<!-- Allow tab indent -->
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
<!-- Allow php files to end without blank line -->
<exclude name="PSR2.Files.EndFileNewline"/>
<!-- Allow braces on the same line as the function declaration. -->
<exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine" />
<!-- Allow inline if statements without braces -->
<exclude name="Generic.ControlStructures.InlineControlStructure" />
<exclude name="PSR2.Classes.ClassDeclaration.OpenBraceNewLine" />
<exclude name="Squiz.ControlStructures.ControlSignature.SpaceAfterCloseBrace" />
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" />
<exclude name="PEAR.Functions.ValidDefaultValue.NotAtEnd" />
<!-- Allow same-line function call arguments, ie. prevent 1 argument per line. -->
<exclude name="PEAR.Functions.FunctionCallSignature.MultipleArguments" />
<!-- Allow content after a function opening bracket,
ie. allow arguments on same line as the opening bracket. -->
<exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket" />
<!-- Prevent function closing bracket from being on it's own line. -->
<exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine" />
</rule>
<!-- Allow same line starting brackets for classes -->
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie" />
<!-- Disallow space indent -->
<!--<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/> -->
<!-- Allow tab indents -->
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="indent" value="4"/>
<property name="tabIndent" value="false"/>
<property name="exact" value="true"/>
</properties>
</rule>
<!-- Disallow spaces after opening and closing braces -->
<rule ref="PEAR.Functions.FunctionCallSignature">
<properties>
<property name="requiredSpacesAfterOpen" value="0" />
<property name="requiredSpacesBeforeClose" value="0" />
<property name="allowMultipleArguments" value="false"/>
</properties>
</rule>
</ruleset>
To reproduce
Steps to reproduce the behavior:
- Create a file called
test.php
with the code sample above... - Run
phpcs test.php ...
- No error message outputted.
Expected behavior
The indentation should be fixed no matter its type. So above code should be fixed to:
some_function(function ($arg) {
if (!$arg) {
return;
}
$arg = "Hello";
return $arg;
});
Versions (please complete the following information)
Operating System | Windows 10 |
PHP version | 8.1.22 |
PHP_CodeSniffer version | 3.8.0 (Master) |
Standard | custom |
Install type | Composer (global) |
Please confirm:
- I have searched the issue list and am not opening a duplicate issue.
- I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
- I have verified the issue still exists in the
master
branch of PHP_CodeSniffer.