PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

Array destructuring in foreach - conflict between SpaceBeforeComma & NoSpaceAfterComma

Open VladaHejda opened this issue 8 months ago • 3 comments

Describe the bug

Destructuring an array for foreach - foreach ($data as [, , $value]) { - violates the Squiz.Arrays.ArrayDeclaration.SpaceBeforeComma ("Expected 0 spaces between "," and comma; 1 found") But when "fixed" - foreach ($data as [,, $value]) { - the Squiz.Arrays.ArrayDeclaration.NoSpaceAfterComma is violated ("Expected 1 space between comma and ","; 0 found") So this code cannot be written while using both sniffs together.

Code sample

class Foo
{
	/**
	 * @param list<array{int, int, int}> $data
	 */
	public function bar(array $data): int
	{
		$sum = 0;
		foreach ($data as [, , $value]) {
			$sum += $value;
		}

		return $sum;
	}
}

Versions

Operating System Windows 11
PHP version 8.3
PHP_CodeSniffer version 3.10.1
Install type Composer

VladaHejda avatar Jun 13 '24 21:06 VladaHejda