-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated Rector to commit 147c96190e99909c71c2d920a83ae96cc9b3bd57
rectorphp/rector-src@147c961 Add known type casting to AnnotationToAttributeRector (#6217)
- Loading branch information
1 parent
9005492
commit cfe590f
Showing
6 changed files
with
116 additions
and
7 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
96 changes: 96 additions & 0 deletions
96
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,96 @@ | ||
<?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 RectorPrefix202408\Webmozart\Assert\Assert; | ||
final class AnnotationToAttributeIntegerValueCaster | ||
{ | ||
/** | ||
* @readonly | ||
* @var \PHPStan\Reflection\ReflectionProvider | ||
*/ | ||
private $reflectionProvider; | ||
public function __construct(ReflectionProvider $reflectionProvider) | ||
{ | ||
$this->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
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