Skip to content

Commit

Permalink
[Php54] No need reprint when array is just created on LongArrayToShor…
Browse files Browse the repository at this point in the history
…tArrayRector (#6206)

* [Php54] No need reprint when array is just created on LongArrayToShortArrayRector

* [Php54] No need reprint when array is just created on LongArrayToShortArrayRector
  • Loading branch information
samsonasik authored Aug 1, 2024
1 parent ad36316 commit 1331f97
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions rules/Php54/Rector/Array_/LongArrayToShortArrayRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
// no kind attribute yet, it means just created
// no need to reprint, it already will be short array by default
if (! $node->hasAttribute(AttributeKey::KIND)) {
return null;
}

if ($node->getAttribute(AttributeKey::KIND) === Array_::KIND_SHORT) {
return null;
}
Expand Down
28 changes: 28 additions & 0 deletions tests/Issues/CountArrayLongToShort/CountArrayLongToShortTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\CountArrayLongToShort;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class CountArrayLongToShortTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::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,33 @@
<?php

namespace Rector\Tests\Issues\CountArrayLongToShort\Fixture;

final class CountToEmptyArrayCompare
{
public function run()
{
$data = [];

if (count($data) === 0) {
}
}
}

?>
-----
<?php

namespace Rector\Tests\Issues\CountArrayLongToShort\Fixture;

final class CountToEmptyArrayCompare
{
public function run()
{
$data = [];

if ($data === []) {
}
}
}

?>
13 changes: 13 additions & 0 deletions tests/Issues/CountArrayLongToShort/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\FuncCall\CountArrayToEmptyArrayComparisonRector;
use Rector\Config\RectorConfig;
use Rector\Php54\Rector\Array_\LongArrayToShortArrayRector;

return RectorConfig::configure()
->withRules([
CountArrayToEmptyArrayComparisonRector::class,
LongArrayToShortArrayRector::class,
]);

0 comments on commit 1331f97

Please sign in to comment.