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..ea144b9aeab 100644 --- a/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php +++ b/rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php @@ -13,10 +13,10 @@ 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; -use PHPStan\Type\Type; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover; @@ -97,8 +97,6 @@ public function provideMinPhpVersion(): int */ public function refactor(Node $node): ?Node { - $hasChanged = false; - if (! $this->hasAtLeastOneUntypedPropertyUsingJmsAttribute($node)) { return null; } @@ -108,6 +106,8 @@ public function refactor(Node $node): ?Node return null; } + $hasChanged = false; + foreach ($node->getProperties() as $property) { if ($this->shouldSkipProperty($property, $classReflection)) { continue; @@ -180,13 +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); + // 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'); + } + } - return new Identifier('string'); - } + if ($typeValue === 'string') { + $propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($property); + // 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'); } }