Downgrade PHP7.4 array_merge without arguments (#4377) (#4440)

This commit is contained in:
Raffael Comi 2020-10-18 16:37:02 +02:00 committed by GitHub
parent 63615f4197
commit 055040f8e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 261 additions and 2 deletions

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
use Rector\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector;
use Rector\DowngradePhp74\Rector\ArrowFunction\ArrowFunctionToAnonymousFunctionRector;
use Rector\DowngradePhp74\Rector\Coalesce\DowngradeNullCoalescingOperatorRector;
use Rector\DowngradePhp74\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector;
use Rector\DowngradePhp74\Rector\FuncCall\DowngradeStripTagsCallWithArrayRector;
use Rector\DowngradePhp74\Rector\LNumber\DowngradeNumericLiteralSeparatorRector;
use Rector\DowngradePhp74\Rector\Property\DowngradeTypedPropertyRector;
@ -18,4 +19,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(DowngradeNumericLiteralSeparatorRector::class);
$services->set(DowngradeStripTagsCallWithArrayRector::class);
$services->set(DowngradeArraySpreadRector::class);
$services->set(DowngradeArrayMergeCallWithoutArgumentsRector::class);
};

View File

@ -1,4 +1,4 @@
# All 592 Rectors Overview
# All 593 Rectors Overview
- [Projects](#projects)
---
@ -19,7 +19,7 @@
- [DowngradePhp71](#downgradephp71) (3)
- [DowngradePhp72](#downgradephp72) (2)
- [DowngradePhp73](#downgradephp73) (1)
- [DowngradePhp74](#downgradephp74) (6)
- [DowngradePhp74](#downgradephp74) (7)
- [DowngradePhp80](#downgradephp80) (6)
- [DynamicTypeAnalysis](#dynamictypeanalysis) (3)
- [FileSystemRector](#filesystemrector) (1)
@ -5072,6 +5072,29 @@ Replace arrow functions with anonymous functions
<br><br>
### `DowngradeArrayMergeCallWithoutArgumentsRector`
- class: [`Rector\DowngradePhp74\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector`](/rules/downgrade-php74/src/Rector/FuncCall/DowngradeArrayMergeCallWithoutArgumentsRector.php)
- [test fixtures](/rules/downgrade-php74/tests/Rector/FuncCall/DowngradeArrayMergeCallWithoutArgumentsRector/Fixture)
Add missing param to `array_merge` and `array_merge_recursive`
```diff
class SomeClass
{
- public function run()
+ public function run()
{
- array_merge();
- array_merge_recursive();
+ array_merge([]);
+ array_merge_recursive([]);
}
}
```
<br><br>
### `DowngradeArraySpreadRector`
- class: [`Rector\DowngradePhp74\Rector\Array_\DowngradeArraySpreadRector`](/rules/downgrade-php74/src/Rector/Array_/DowngradeArraySpreadRector.php)

View File

@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp74\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\FuncCall;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
/**
* @see \Rector\DowngradePhp74\Tests\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector\DowngradeArrayMergeCallWithoutArgumentsRectorTest
*/
final class DowngradeArrayMergeCallWithoutArgumentsRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Add missing param to `array_merge` and `array_merge_recursive`', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
array_merge();
array_merge_recursive();
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
array_merge([]);
array_merge_recursive([]);
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->shouldRefactor($node)) {
return null;
}
$node->args = [new Arg(new Array_())];
return $node;
}
private function shouldRefactor(FuncCall $funcCall): bool
{
if (! $this->isNames($funcCall, ['array_merge', 'array_merge_recursive'])) {
return false;
}
// If param is provided, do nothing
if ($funcCall->args !== []) {
return false;
}
return true;
}
}

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp74\Tests\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector;
use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\DowngradePhp74\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DowngradeArrayMergeCallWithoutArgumentsRectorTest extends AbstractRectorTestCase
{
/**
* @requires PHP 7.4
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return DowngradeArrayMergeCallWithoutArgumentsRector::class;
}
protected function getPhpVersion(): string
{
return PhpVersionFeature::BEFORE_STRIP_TAGS_WITH_ARRAY;
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\DowngradePhp74\Tests\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector\Fixture;
class ArrayMergeClass
{
public function run()
{
array_merge();
}
}
?>
-----
<?php
namespace Rector\DowngradePhp74\Tests\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector\Fixture;
class ArrayMergeClass
{
public function run()
{
array_merge([]);
}
}
?>

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\DowngradePhp74\Tests\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector\Fixture;
class ArrayMergeRecursiveClass
{
public function run()
{
array_merge_recursive();
}
}
?>
-----
<?php
namespace Rector\DowngradePhp74\Tests\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector\Fixture;
class ArrayMergeRecursiveClass
{
public function run()
{
array_merge_recursive([]);
}
}
?>

View File

@ -0,0 +1,15 @@
<?php
namespace Rector\DowngradePhp74\Tests\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector\Fixture;
class SkipArrayClass
{
public function run()
{
// Array: do no change
array_merge([]);
array_merge_recursive([]);
}
}
?>

View File

@ -0,0 +1,22 @@
<?php
namespace Rector\DowngradePhp74\Tests\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector\Fixture;
class SkipMultipleArgumentsClass
{
public function run()
{
$array = [];
// Multiple Arguments: do no change
array_merge([], []);
array_merge($array, []);
array_merge([], $array);
array_merge($array, $array);
array_merge_recursive([], []);
array_merge_recursive($array, []);
array_merge_recursive([], $array);
array_merge_recursive($array, $array);
}
}
?>

View File

@ -0,0 +1,16 @@
<?php
namespace Rector\DowngradePhp74\Tests\Rector\FuncCall\DowngradeArrayMergeCallWithoutArgumentsRector\Fixture;
class SkipVariableClass
{
public function run()
{
$array = [];
// Variable: do no change
array_merge($array);
array_merge_recursive($array);
}
}
?>

View File

@ -226,4 +226,9 @@ final class PhpVersionFeature
* @var string
*/
public const IS_ITERABLE = '7.1';
/**
* @var string
*/
public const BEFORE_ARRAY_MERGE_WITHOUT_ARGUMENTS = '7.3';
}