-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement array unpacking PHP 7.4 feature (see #331)
- Loading branch information
Showing
3 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/Application/Sniffs/Arrays/ArrayUnpackingSyntaxSniff.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/sniffs/arrays/unpacking_support_array_numeric_keyed.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |