Skip to content

Commit 04df9bf

Browse files
authored
split of RemoveNullNamedArgOnNullDefaultParamRector to handle only named args (#8014)
* split of RemoveNullNamedArgOnNullDefaultParamRector to handle only named args * make RemoveNullArgOnNullDefaultParamRector skip named args * add fixture to handle even different order
1 parent 68472f0 commit 04df9bf

14 files changed

Lines changed: 307 additions & 75 deletions

File tree

.github/workflows/code_analysis.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ name: Code Analysis
22

33
on:
44
pull_request: null
5-
push: null
5+
push:
6+
branches:
7+
- "main"
68

79
env:
810
# see https://github.com/composer/composer/issues/9368#issuecomment-718112361

config/set/named-args.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
use Rector\CodeQuality\Rector\CallLike\AddNameToNullArgumentRector;
88
use Rector\CodeQuality\Rector\FuncCall\SortCallLikeNamedArgsRector;
99
use Rector\Config\RectorConfig;
10-
use Rector\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector;
10+
use Rector\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector;
1111
use Rector\NetteUtils\Rector\StaticCall\UtilsJsonStaticCallNamedArgRector;
1212

1313
return static function (RectorConfig $rectorConfig): void {
1414
$rectorConfig->rules([
1515
AddNameToNullArgumentRector::class,
1616
AddNameToBooleanArgumentRector::class,
17-
RemoveNullArgOnNullDefaultParamRector::class,
17+
RemoveNullNamedArgOnNullDefaultParamRector::class,
1818
SortCallLikeNamedArgsRector::class,
1919
SortAttributeNamedArgsRector::class,
2020
UtilsJsonStaticCallNamedArgRector::class,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullArgOnNullDefaultParamRector\Fixture;
6+
7+
function skipNamedArgs(?string $someClass = null)
8+
{
9+
}
10+
11+
skipNamedArgs(someClass: null);

rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Fixture/skip_named_argument_position_not_match.php.inc

Lines changed: 0 additions & 15 deletions
This file was deleted.

rules-tests/DeadCode/Rector/MethodCall/RemoveNullArgOnNullDefaultParamRector/Fixture/with_named_argument_position_match.php.inc

Lines changed: 0 additions & 35 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Fixture;
6+
7+
use Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Source\SomeExternalClass;
8+
9+
final class HandleNamedArgumentPositionNotMatch
10+
{
11+
public function get()
12+
{
13+
SomeExternalClass::withMiddleNotNullDefault(item: null);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
declare(strict_types=1);
22+
23+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Fixture;
24+
25+
use Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Source\SomeExternalClass;
26+
27+
final class HandleNamedArgumentPositionNotMatch
28+
{
29+
public function get()
30+
{
31+
SomeExternalClass::withMiddleNotNullDefault();
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Fixture;
6+
7+
use Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Source\SomeExternalClass;
8+
9+
final class WithNamedArgumentPositionMatch
10+
{
11+
public function get()
12+
{
13+
$object = new SomeExternalClass(name: 'John', id: null);
14+
15+
$secondObject = new SomeExternalClass(id: null, name: 'John');
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
declare(strict_types=1);
24+
25+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Fixture;
26+
27+
use Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Source\SomeExternalClass;
28+
29+
final class WithNamedArgumentPositionMatch
30+
{
31+
public function get()
32+
{
33+
$object = new SomeExternalClass(name: 'John');
34+
35+
$secondObject = new SomeExternalClass(name: 'John');
36+
}
37+
}
38+
39+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveNullNamedArgOnNullDefaultParamRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector\Source;
4+
5+
final class SomeExternalClass
6+
{
7+
public function __construct(string $name, ?int $id = null)
8+
{
9+
}
10+
11+
public static function withMiddleNotNullDefault(?int $id = null, int $data = 1, ?string $name = null, ?string $item = null)
12+
{
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([RemoveNullNamedArgOnNullDefaultParamRector::class]);

0 commit comments

Comments
 (0)