Skip to content

Commit

Permalink
implement array unpacking PHP 7.4 feature (see #331)
Browse files Browse the repository at this point in the history
  • Loading branch information
llaville committed Jan 15, 2022
1 parent 695d097 commit 006494e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/Application/Sniffs/Arrays/ArrayUnpackingSyntaxSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);
/**
* This file is part of the PHP_CompatInfo package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bartlett\CompatInfo\Application\Sniffs\Arrays;

use Bartlett\CompatInfo\Application\Sniffs\SniffAbstract;

use PhpParser\Node;

use Generator;

/**
* Array unpacking support :
* - for numeric-keyed arrays (since PHP 7.4)
*
* @author Laurent Laville
* @since Release 6.2.0
*
* @link https://wiki.php.net/rfc/spread_operator_for_array
* @link https://www.php.net/manual/en/language.types.array.php#language.types.array.unpacking
* @see tests/Sniffs/ArrayUnpackingSyntaxSniffTest.php
*/
final class ArrayUnpackingSyntaxSniff extends SniffAbstract
{
// Rules identifiers for SARIF report
private const CA74 = 'CA7402';

/**
* {@inheritDoc}
*/
public function getRules(): Generator
{
yield self::CA74 => [
'name' => $this->getShortClass(),
'fullDescription' => "Array unpacking support is available since PHP 7.4.0",
'helpUri' => '%baseHelpUri%/01_Components/03_Sniffs/Features/#php-74',
];
}

/**
* {@inheritDoc}
*/
public function enterNode(Node $node)
{
if (!$node instanceof Node\Expr\ArrayItem) {
return null;
}
if (!$node->unpack) {
return null;
}
if (!$parent = $node->getAttribute($this->attributeParentKeyStore)) {
return null;
}

// Array unpacking support
$phpMin = '7.4.0';
$ruleId = self::CA74;

$this->updateNodeElementVersion($parent, $this->attributeKeyStore, ['php.min' => $phpMin]);
$this->updateNodeElementRule($parent, $this->attributeKeyStore, $ruleId);
return null;
}
}
58 changes: 58 additions & 0 deletions tests/Sniffs/ArrayUnpackingSyntaxSniffTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);
/**
* This file is part of the PHP_CompatInfo package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Bartlett\CompatInfo\Tests\Sniffs;

use Exception;

/**
* Array unpacking support :
* - for numeric-keyed arrays (since PHP 7.4)
*
* @author Laurent Laville
* @since Release 6.2.0
*
* @link https://wiki.php.net/rfc/spread_operator_for_array
* @link https://www.php.net/manual/en/language.types.array.php#language.types.array.unpacking
*/
final class ArrayUnpackingSyntaxSniffTest extends SniffTestCase
{
/**
* {@inheritDoc}
*/
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

self::$fixtures .= 'arrays' . DIRECTORY_SEPARATOR;
}

/**
* Feature test for Array unpacking support for numeric-keyed arrays
*
* @link https://wiki.php.net/rfc/spread_operator_for_array
* @group feature
* @return void
* @throws Exception
*/
public function testArrayUnpackingSupportForNumericKeyed()
{
$dataSource = 'unpacking_support_array_numeric_keyed.php';
$metrics = $this->executeAnalysis($dataSource);
$versions = $metrics[self::$analyserId]['versions'];

$this->assertEquals(
'7.4.0',
$versions['php.min']
);

$this->assertEquals(
'',
$versions['php.max']
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
// @link https://wiki.php.net/rfc/spread_operator_for_array

$arr1 = [1, 2, 3];
$arr2 = [...$arr1];

var_dump($arr2); //[1, 2, 3]

0 comments on commit 006494e

Please sign in to comment.