-
-
Notifications
You must be signed in to change notification settings - Fork 347
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 (#6217)
* add casting fixture * Add known type casting to AnnotationToAttributeRector * make generic * add validation file with union string or int
- Loading branch information
1 parent
0f09b7a
commit 147c961
Showing
13 changed files
with
274 additions
and
13 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...tionToAttributeRector/Fixture/Symfony/Validator/validation_cast_integer_parameter.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,36 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Symfony; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
final class ValidationCastIntegerParameter | ||
{ | ||
/** | ||
* @Assert\Length( | ||
* min="100", | ||
* max="255" | ||
* ) | ||
*/ | ||
public function action() | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Symfony; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
final class ValidationCastIntegerParameter | ||
{ | ||
#[Assert\Length(min: 100, max: 255)] | ||
public function action() | ||
{ | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...ctor/Class_/AnnotationToAttributeRector/Fixture/Symfony/Validator/validation_file.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,33 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Symfony; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
final class ValidationFile | ||
{ | ||
/** | ||
* @Assert\File(maxSize="100") | ||
*/ | ||
public function action() | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Symfony; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
final class ValidationFile | ||
{ | ||
#[Assert\File(maxSize: 100)] | ||
public function action() | ||
{ | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...AnnotationToAttributeRector/Fixture/Symfony/Validator/validation_file_with_string.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,33 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Symfony; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
final class ValidationFileWithString | ||
{ | ||
/** | ||
* @Assert\File(maxSize="5555K") | ||
*/ | ||
public function action() | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture\Symfony; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
final class ValidationFileWithString | ||
{ | ||
#[Assert\File(maxSize: '5555K')] | ||
public function action() | ||
{ | ||
} | ||
} | ||
|
||
?> |
File renamed without changes.
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
113 changes: 113 additions & 0 deletions
113
src/PhpAttribute/NodeFactory/AnnotationToAttributeIntegerValueCaster.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,113 @@ | ||
<?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 PHPStan\Reflection\ClassReflection; | ||
use PHPStan\Reflection\ParameterReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\UnionType; | ||
use Rector\Php80\ValueObject\AnnotationToAttribute; | ||
use Webmozart\Assert\Assert; | ||
|
||
final readonly class AnnotationToAttributeIntegerValueCaster | ||
{ | ||
public function __construct( | ||
private ReflectionProvider $reflectionProvider, | ||
) { | ||
} | ||
|
||
/** | ||
* @param Arg[] $args | ||
*/ | ||
public function castAttributeTypes(AnnotationToAttribute $annotationToAttribute, array $args): void | ||
{ | ||
Assert::allIsInstanceOf($args, Arg::class); | ||
|
||
if (! $this->reflectionProvider->hasClass($annotationToAttribute->getAttributeClass())) { | ||
return; | ||
} | ||
|
||
$attributeClassReflection = $this->reflectionProvider->getClass($annotationToAttribute->getAttributeClass()); | ||
if (! $attributeClassReflection->hasConstructor()) { | ||
return; | ||
} | ||
|
||
$parameterReflections = $this->resolveConstructorParameterReflections($attributeClassReflection); | ||
|
||
foreach ($parameterReflections as $parameterReflection) { | ||
foreach ($args as $arg) { | ||
if (! $arg->value instanceof ArrayItem) { | ||
continue; | ||
} | ||
|
||
$arrayItem = $arg->value; | ||
if (! $arrayItem->key instanceof String_) { | ||
continue; | ||
} | ||
|
||
$keyString = $arrayItem->key; | ||
if ($keyString->value !== $parameterReflection->getName()) { | ||
continue; | ||
} | ||
|
||
// ensure type is casted to integer | ||
if (! $arrayItem->value instanceof String_) { | ||
continue; | ||
} | ||
|
||
if (! $this->containsInteger($parameterReflection->getType())) { | ||
continue; | ||
} | ||
|
||
$valueString = $arrayItem->value; | ||
if (! is_numeric($valueString->value)) { | ||
continue; | ||
} | ||
|
||
$arrayItem->value = new LNumber((int) $valueString->value); | ||
} | ||
} | ||
} | ||
|
||
private function containsInteger(Type $type): bool | ||
{ | ||
if ($type instanceof IntegerType) { | ||
return true; | ||
} | ||
|
||
if (! $type instanceof UnionType) { | ||
return false; | ||
} | ||
|
||
foreach ($type->getTypes() as $unionedType) { | ||
if ($unionedType instanceof IntegerType) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @return ParameterReflection[] | ||
*/ | ||
private function resolveConstructorParameterReflections(ClassReflection $classReflection): array | ||
{ | ||
$extendedMethodReflection = $classReflection->getConstructor(); | ||
|
||
$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle( | ||
$extendedMethodReflection->getVariants() | ||
); | ||
|
||
return $parametersAcceptorWithPhpDocs->getParameters(); | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
if (class_exists('Symfony\Component\Validator\Constraints\File')) { | ||
return; | ||
} | ||
|
||
// @see https://github.com/symfony/validator/blob/94e7465b1271ba024bd96a424da037e3390184a5/Constraints/File.php | ||
|
||
class File | ||
{ | ||
public function __construct( | ||
int|string|null $maxSize = null | ||
) { | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
if (class_exists('Symfony\Component\Validator\Constraints\Length')) { | ||
return; | ||
} | ||
|
||
// @see https://github.com/symfony/validator/blob/94e7465b1271ba024bd96a424da037e3390184a5/Constraints/Length.php | ||
|
||
class Length | ||
{ | ||
public function __construct( | ||
?int $min, | ||
?int $max | ||
) { | ||
} | ||
} |
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