-
-
Notifications
You must be signed in to change notification settings - Fork 698
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodingStyle] Improve unspread array (#5120)
* decouple VendorFileDetector * decouple SpreadVariablesCollector * [CodingStyle] Improve UnSpreadOperatorRector to join argument to array
- Loading branch information
1 parent
f1d4a4a
commit 4756535
Showing
7 changed files
with
155 additions
and
135 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
rules/coding-style/src/NodeAnalyzer/SpreadVariablesCollector.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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodingStyle\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Param; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
|
||
final class SpreadVariablesCollector | ||
{ | ||
/** | ||
* @return array<int, Param> | ||
*/ | ||
public function resolveFromClassMethod(ClassMethod $classMethod): array | ||
{ | ||
$spreadParams = []; | ||
|
||
foreach ($classMethod->params as $key => $param) { | ||
// prevent race-condition removal on class method | ||
$originalParam = $param->getAttribute(AttributeKey::ORIGINAL_NODE); | ||
if (! $originalParam instanceof Param) { | ||
continue; | ||
} | ||
|
||
if (! $originalParam->variadic) { | ||
continue; | ||
} | ||
|
||
$spreadParams[$key] = $param; | ||
} | ||
|
||
return $spreadParams; | ||
} | ||
} |
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
35 changes: 35 additions & 0 deletions
35
.../Rector/ClassMethod/UnSpreadOperatorRector/Fixture/handle_multiple_items_to_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,35 @@ | ||
<?php | ||
|
||
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector\Fixture; | ||
|
||
final class HandleMultipleItemsToArray | ||
{ | ||
public function go() | ||
{ | ||
$this->run('John', 1, 2, 3, 4, 5); | ||
} | ||
|
||
public function run(string $name, ...$items) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector\Fixture; | ||
|
||
final class HandleMultipleItemsToArray | ||
{ | ||
public function go() | ||
{ | ||
$this->run('John', [1, 2, 3, 4, 5]); | ||
} | ||
|
||
public function run(string $name, array $items) | ||
{ | ||
} | ||
} | ||
|
||
?> |
10 changes: 0 additions & 10 deletions
10
...tests/Rector/ClassMethod/UnSpreadOperatorRector/Fixture/skip_typed_param_variadic.php.inc
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
...tyle/tests/Rector/ClassMethod/UnSpreadOperatorRector/Fixture/typed_param_variadic.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,25 @@ | ||
<?php | ||
|
||
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector\Fixture; | ||
|
||
final class TypedParamVariadic | ||
{ | ||
public function run(int ...$var) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\CodingStyle\Tests\Rector\ClassMethod\UnSpreadOperatorRector\Fixture; | ||
|
||
final class TypedParamVariadic | ||
{ | ||
public function run(array $var) | ||
{ | ||
} | ||
} | ||
|
||
?> |
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