-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add known type casting to AnnotationToAttributeRector
- Loading branch information
1 parent
b36e264
commit 1258558
Showing
5 changed files
with
89 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PhpAttribute\Enum; | ||
|
||
final class AnnotationClassName | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const LENGTH = 'Symfony\Component\Validator\Constraints\Length'; | ||
} |
58 changes: 58 additions & 0 deletions
58
src/PhpAttribute/NodeFactory/AnnotationToAttributeKeyTypeCaster.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,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PhpAttribute\NodeFactory; | ||
|
||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PhpParser\Node\Scalar\LNumber; | ||
use PhpParser\Node\Scalar\String_; | ||
use Rector\Php80\ValueObject\AnnotationToAttribute; | ||
use Rector\PhpAttribute\Enum\AnnotationClassName; | ||
|
||
final class AnnotationToAttributeKeyTypeCaster | ||
{ | ||
/** | ||
* @var array<string, string[]> | ||
*/ | ||
private const INTEGER_KEYS_BY_CLASS = [ | ||
AnnotationClassName::LENGTH => ['min', 'max'], | ||
]; | ||
|
||
/** | ||
* @param Arg[] $args | ||
*/ | ||
public function castAttributeTypes(AnnotationToAttribute $annotationToAttribute, array $args): void | ||
{ | ||
// known type casting | ||
foreach (self::INTEGER_KEYS_BY_CLASS as $annotationClass => $integerKeys) { | ||
if ($annotationToAttribute->getAttributeClass() !== $annotationClass) { | ||
continue; | ||
} | ||
|
||
foreach ($integerKeys as $integerKey) { | ||
foreach ($args as $arg) { | ||
if (! $arg->value instanceof ArrayItem) { | ||
continue; | ||
} | ||
|
||
$arrayItem = $arg->value; | ||
if (! $arrayItem->key instanceof String_) { | ||
continue; | ||
} | ||
|
||
if ($arrayItem->key->value !== $integerKey) { | ||
continue; | ||
} | ||
|
||
// ensure type is casted to integer | ||
if ($arrayItem->value instanceof String_) { | ||
$arrayItem->value = new LNumber((int) $arrayItem->value->value); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
if (class_exists('Symfony\Component\Validator\Constraints\Length')) { | ||
return; | ||
} | ||
|
||
class Length | ||
{ | ||
} |