Skip to content

Support multiline foreach in Squiz.ControlStructures.ForEachLoopDeclaration #3907

@Daimona

Description

@Daimona

The Squiz.ControlStructures.ForEachLoopDeclaration sniff fails to account for multiline foreach loops when checking the space around the as keyword. If you simply enable the sniff and run it on this code (with tabWidth = 4):

function myTest() {
	foreach ( $array
		as $el ) {

	}
}

it says:

 202 | ERROR   | [x] Expected 1 space before "as"; 8 found
     |         |     (Squiz.ControlStructures.ForEachLoopDeclaration.SpacingBeforeAs)

And similarly, for this code:

function myTest() {
	foreach ( $array as
		$el ) {

	}
}
 202 | ERROR   | [x] Expected 1 space after "as"; 0 found
     |         |     (Squiz.ControlStructures.ForEachLoopDeclaration.SpacingAfterAs)

Regardless of where the as keyword is placed, there is no way to have a multiline foreach assignment with this sniff enabled. This is especially problematic if any side of as is particularly long (e.g., a function call, or an array destructuring expression).

The Squiz.ControlStructures.ForLoopDeclaration sniff provides the ignoreNewlines option to ignore multi-line for conditions. It would be nice to have something similar here.

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