-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Php54] Add ArrayToShortArrayRector (#2615)
* [Php54] Add ArrayToShortArrayRector * Use kind attribute to determine short array * Add fixture for skipping already-short arrays * Simplify fixture for unchanged code
- Loading branch information
Showing
8 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
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
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
33 changes: 33 additions & 0 deletions
33
rules-tests/Php54/Rector/Array_/ArrayToShortArrayRector/ArrayToShortArrayRectorTest.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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Php54\Rector\Array_\ArrayToShortArrayRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ArrayToShortArrayRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
rules-tests/Php54/Rector/Array_/ArrayToShortArrayRector/Fixture/fixture.php.inc
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,41 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php54\Rector\Array_\ArrayToShortArrayTest\Fixture; | ||
|
||
function emptyArray() | ||
{ | ||
return array(); | ||
} | ||
|
||
function singleElementArray() | ||
{ | ||
return array(1); | ||
} | ||
|
||
function manyElementArray() | ||
{ | ||
return array(1, 'abc', true); | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php54\Rector\Array_\ArrayToShortArrayTest\Fixture; | ||
|
||
function emptyArray() | ||
{ | ||
return []; | ||
} | ||
|
||
function singleElementArray() | ||
{ | ||
return [1]; | ||
} | ||
|
||
function manyElementArray() | ||
{ | ||
return [1, 'abc', true]; | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...ests/Php54/Rector/Array_/ArrayToShortArrayRector/Fixture/skip_already_short_array.php.inc
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,10 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php54\Rector\Array_\ArrayToShortArrayTest\Fixture; | ||
|
||
function emptyArray() | ||
{ | ||
return []; | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
rules-tests/Php54/Rector/Array_/ArrayToShortArrayRector/config/configured_rule.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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Php54\Rector\Array_\ArrayToShortArrayRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ArrayToShortArrayRector::class); | ||
}; |
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,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php54\Rector\Array_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Array_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\PhpVersionFeature; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\VersionBonding\Contract\MinPhpVersionInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\Php54\Rector\Array_\ArrayToShortArrayRector\ArrayToShortArrayRectorTest | ||
*/ | ||
final class ArrayToShortArrayRector extends AbstractRector implements MinPhpVersionInterface | ||
{ | ||
public function provideMinPhpVersion(): int | ||
{ | ||
return PhpVersionFeature::SHORT_ARRAY; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Array to short array', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
return array(); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
return []; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Array_::class]; | ||
} | ||
|
||
/** | ||
* @param Array_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->getAttribute(AttributeKey::KIND) === Array_::KIND_SHORT) { | ||
return null; | ||
} | ||
|
||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null); | ||
$node->setAttribute(AttributeKey::KIND, Array_::KIND_SHORT); | ||
return $node; | ||
} | ||
} |
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