Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic.WhiteSpace.ScopeIndent: Indentation doesn't get fixed inside closures as a callback arg in a function call #3884

Open
3 tasks done
yCodeTech opened this issue Sep 10, 2023 · 0 comments

Comments

@yCodeTech
Copy link

yCodeTech commented Sep 10, 2023

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:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. 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.
@yCodeTech yCodeTech changed the title Generic.WhiteSpace.ScopeIndent Generic.WhiteSpace.ScopeIndent: Indentation doesn't get fixed inside closures as a callback arg in a function call Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant