Skip to content

Allow foreach ($array as list($key, $values)) #928

@BR0kEN-

Description

@BR0kEN-

At the moment the list language construction cannot be used directly in foreach (after as statement) which is rather a bug in https://github.com/PHPCompatibility/PHPCompatibility than an issue with PHP incompatibility, since the construction is available as of PHP 5.5 (see https://www.php.net/manual/en/migration55.new-features.php#migration55.new-features.foreach-list).

test.php:

<?php

$a = [
  [1, 2],
  [1, 2],
];

foreach ($a as list($key, $value)) {
  assert($key === 1);
  assert($value === 2);
}

foreach ($a as [$key, $value]) {
  assert($key === 1);
  assert($value === 2);
}
./vendor/bin/phpcs ./test.php --standard=PHPCompatibility -s

FILE: test.php
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 8 | ERROR | Function name, class name, namespace name or constant name can not be reserved keyword 'list' (since version all)
   |       | (PHPCompatibility.Keywords.ForbiddenNames.listFound)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 151ms; Memory: 10MB

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions