From 09729f6825d01fde3ac1a5618f47171aa15ef925 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 28 Oct 2025 20:21:46 +0100 Subject: [PATCH 1/2] fallback to var --- .../Fixture/string_var_float.php.inc | 30 +++++++++++++++++++ ...tyFromJMSSerializerAttributeTypeRector.php | 17 +++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector/Fixture/string_var_float.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector/Fixture/string_var_float.php.inc b/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector/Fixture/string_var_float.php.inc new file mode 100644 index 00000000000..aae9b2feee9 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector/Fixture/string_var_float.php.inc @@ -0,0 +1,30 @@ + +----- + diff --git a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php index 8bb276b09d8..e7cc0c54f37 100644 --- a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php +++ b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php @@ -13,6 +13,7 @@ use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Property; use PHPStan\Reflection\ClassReflection; +use PHPStan\Type\FloatType; use PHPStan\Type\MixedType; use PHPStan\Type\ObjectType; use PHPStan\Type\StringType; @@ -97,8 +98,6 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $hasChanged = false; - if (! $this->hasAtLeastOneUntypedPropertyUsingJmsAttribute($node)) { return null; } @@ -108,6 +107,8 @@ public function refactor(Node $node): ?Node return null; } + $hasChanged = false; + foreach ($node->getProperties() as $property) { if ($this->shouldSkipProperty($property, $classReflection)) { continue; @@ -190,6 +191,18 @@ private function createTypeNode(string $typeValue, Property $property): ?Node } } + if ($typeValue === 'string') { + $propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($property); + if ($propertyPhpDocInfo instanceof PhpDocInfo) { + // fallback to string, as most likely string representation of float + if ($propertyPhpDocInfo->getVarType() instanceof FloatType) { + $this->varTagRemover->removeVarTag($property); + + return new Identifier('float'); + } + } + } + $type = $this->scalarStringToTypeMapper->mapScalarStringToType($typeValue); if ($type instanceof MixedType) { // fallback to object type From f5558c013a93d94b483782dd221429b45133f850 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Tue, 28 Oct 2025 19:24:17 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- ...tyFromJMSSerializerAttributeTypeRector.php | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php index e7cc0c54f37..ea144b9aeab 100644 --- a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php +++ b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php @@ -17,7 +17,6 @@ use PHPStan\Type\MixedType; use PHPStan\Type\ObjectType; use PHPStan\Type\StringType; -use PHPStan\Type\Type; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover; @@ -181,25 +180,19 @@ private function createTypeNode(string $typeValue, Property $property): ?Node { if ($typeValue === 'float') { $propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($property); - if ($propertyPhpDocInfo instanceof PhpDocInfo) { - // fallback to string, as most likely string representation of float - if ($propertyPhpDocInfo->getVarType() instanceof StringType) { - $this->varTagRemover->removeVarTag($property); - - return new Identifier('string'); - } + // fallback to string, as most likely string representation of float + if ($propertyPhpDocInfo instanceof PhpDocInfo && $propertyPhpDocInfo->getVarType() instanceof StringType) { + $this->varTagRemover->removeVarTag($property); + return new Identifier('string'); } } if ($typeValue === 'string') { $propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($property); - if ($propertyPhpDocInfo instanceof PhpDocInfo) { - // fallback to string, as most likely string representation of float - if ($propertyPhpDocInfo->getVarType() instanceof FloatType) { - $this->varTagRemover->removeVarTag($property); - - return new Identifier('float'); - } + // fallback to string, as most likely string representation of float + if ($propertyPhpDocInfo instanceof PhpDocInfo && $propertyPhpDocInfo->getVarType() instanceof FloatType) { + $this->varTagRemover->removeVarTag($property); + return new Identifier('float'); } }