Skip to content

Generic.WhiteSpace.ScopeIndent false positive with multiline yield from #3808

Closed
PHPCSStandards/PHP_CodeSniffer
#647
@Daimona

Description

@Daimona

Describe the bug
Generic.WhiteSpace.ScopeIndent reports multiline yield from statements as being incorrectly indented even if the indentation is correct. I've only tested this with tab indentation.

Code sample

<?php

function test() {
	yield
		from [ 3, 4 ];
}

Custom ruleset

<?xml version="1.0"?>
<ruleset name="My Custom Standard">
	<arg name="tab-width" value="4" />
	<rule ref="Generic.WhiteSpace.ScopeIndent">
		<properties>
			<property name="tabIndent" value="true" />
		</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. See error message displayed
--------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------------------------------------------
 5 | ERROR | [x] Line indented incorrectly; expected at least 1 tabs, found 2 spaces
   |       |     (Generic.WhiteSpace.ScopeIndent.Incorrect)
--------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------------------------------------------------------

On top of that, if you try to autofix the file it will enter an "endless" loop and not do anything:

$ vendor/bin/phpcbf -vvv test.php

...
=> Fixing file: 1/1 violations remaining
---START FILE CONTENT---
1|<?php
2|
3|function test() {
4|	yield
5|		from [ 3, 4 ];
6|}
7|
8|
--- END FILE CONTENT ---
	Generic.WhiteSpace.ScopeIndent:1537 replaced token 12 (T_YIELD_FROM on line 5) "\t\tfrom" => "\tfrom"
        => Fixing file: 1/1 violations remaining [made 1 pass]...               
	* fixed 1 violations, starting loop 2 *
---START FILE CONTENT---
1|<?php
2|
3|function test() {
4|	yield
5|	from [ 3, 4 ];
6|}
7|
8|
--- END FILE CONTENT ---
	Generic.WhiteSpace.ScopeIndent:1537 replaced token 12 (T_YIELD_FROM on line 5) "\tfrom" => "\tfrom"
        => Fixing file: 1/1 violations remaining [made 2 passes]...             
	* fixed 1 violations, starting loop 3 *
---START FILE CONTENT---
1|<?php
2|
3|function test() {
4|	yield
5|	from [ 3, 4 ];
6|}
7|
8|
--- END FILE CONTENT ---
	**** Generic.WhiteSpace.ScopeIndent:1537 has possible conflict with another sniff on loop 1; caused by the following change ****
	**** replaced token 12 (T_YIELD_FROM on line 5) "\tfrom" => "\tfrom" ****
	**** ignoring all changes until next loop ****
        => Fixing file: 0/1 violations remaining [made 3 passes]...             
	* fixed 0 violations, starting loop 4 *
---START FILE CONTENT---
1|<?php
2|
3|function test() {
4|	yield
5|	from [ 3, 4 ];
6|}
7|
8|
--- END FILE CONTENT ---
...

Expected behavior
PHPCS should not report any error. Whether separating yield and from is a good idea is beyond the scope of this bug report. However, there's nothing wrong in the code above and the error message is also confusing ("found 2 spaces").

Versions (please complete the following information):

  • OS: Ubuntu 22.04.2
  • PHP: 8.2.4
  • PHPCS: 3.7.2
  • Standard: Custom

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions