Skip to content

Commit

Permalink
[Php54] Add ArrayToShortArrayRector (#2615)
Browse files Browse the repository at this point in the history
* [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
edsrzf committed Jul 3, 2022
1 parent 25accb2 commit 0660b21
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 2 deletions.
23 changes: 21 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 411 Rules Overview
# 412 Rules Overview

<br>

Expand Down Expand Up @@ -32,7 +32,7 @@

- [Php53](#php53) (3)

- [Php54](#php54) (2)
- [Php54](#php54) (3)

- [Php55](#php55) (5)

Expand Down Expand Up @@ -4592,6 +4592,25 @@ Use ?: instead of ?, where useful

## Php54

### ArrayToShortArrayRector

Array to short array

- class: [`Rector\Php54\Rector\Array_\ArrayToShortArrayRector`](../rules/Php54/Rector/Array_/ArrayToShortArrayRector.php)

```diff
class SomeClass
{
public function run()
{
- return array();
+ return [];
}
}
```

<br>

### RemoveReferenceFromCallRector

Remove & from function and method calls
Expand Down
3 changes: 3 additions & 0 deletions config/set/php54.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php54\Rector\Array_\ArrayToShortArrayRector;
use Rector\Php54\Rector\Break_\RemoveZeroBreakContinueRector;
use Rector\Php54\Rector\FuncCall\RemoveReferenceFromCallRector;
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ArrayToShortArrayRector::class);

$rectorConfig
->ruleWithConfiguration(RenameFunctionRector::class, [
'mysqli_param_count' => 'mysqli_stmt_param_count',
Expand Down
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';
}
}
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];
}

?>
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 [];
}

?>
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);
};
77 changes: 77 additions & 0 deletions rules/Php54/Rector/Array_/ArrayToShortArrayRector.php
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;
}
}
5 changes: 5 additions & 0 deletions src/ValueObject/PhpVersionFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ final class PhpVersionFeature
*/
public const NO_REFERENCE_IN_ARG = PhpVersion::PHP_54;

/**
* @var int
*/
public const SHORT_ARRAY = PhpVersion::PHP_54;

/**
* @var int
*/
Expand Down

0 comments on commit 0660b21

Please sign in to comment.