-
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHP 8.0] Include keys in annotation to attribute transformation (#1766)
Co-authored-by: Morgan NEUTE <morgan.neute@niji.fr> Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
0a63fe4
commit f374b9f
Showing
42 changed files
with
423 additions
and
128 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PhpAttribute; | ||
|
||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Scalar\String_; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class AttributeArrayNameInliner | ||
{ | ||
/** | ||
* @param Array_|Arg[] $array | ||
* @return Arg[] | ||
*/ | ||
public function inlineArrayToArgs(Array_|array $array): array | ||
{ | ||
if (is_array($array)) { | ||
return $this->inlineArray($array); | ||
} | ||
|
||
return $this->inlineArrayNode($array); | ||
} | ||
|
||
/** | ||
* @return Arg[] | ||
*/ | ||
private function inlineArrayNode(Array_ $array): array | ||
{ | ||
$args = []; | ||
|
||
foreach ($array->items as $arrayItem) { | ||
if (! $arrayItem instanceof ArrayItem) { | ||
continue; | ||
} | ||
|
||
$key = $arrayItem->key; | ||
if ($key instanceof String_) { | ||
$args[] = new Arg($arrayItem->value, false, false, [], new Identifier($key->value)); | ||
} else { | ||
$args[] = new Arg($arrayItem->value); | ||
} | ||
} | ||
|
||
return $args; | ||
} | ||
|
||
/** | ||
* @param Arg[] $args | ||
* @return Arg[] | ||
*/ | ||
private function inlineArray(array $args): array | ||
{ | ||
Assert::allIsAOf($args, Arg::class); | ||
|
||
$newArgs = []; | ||
|
||
foreach ($args as $arg) { | ||
// matching top root array key | ||
if ($arg->value instanceof ArrayItem) { | ||
$arrayItem = $arg->value; | ||
if ($arrayItem->key instanceof String_) { | ||
$arrayItemString = $arrayItem->key; | ||
$newArgs[] = new Arg($arrayItem->value, false, false, [], new Identifier($arrayItemString->value)); | ||
} | ||
} | ||
} | ||
|
||
if ($newArgs !== []) { | ||
return $newArgs; | ||
} | ||
|
||
return $args; | ||
} | ||
} |
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
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
Oops, something went wrong.