-
-
Notifications
You must be signed in to change notification settings - Fork 346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add known type casting to AnnotationToAttributeRector #6217
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on else, union with empty types is impossible, I think this can be handled by
count($unionedTypesWithoutNullType) === 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#6220