Skip to content

Commit 2415d9d

Browse files
fallback to var float (#7601)
* fallback to var * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 31f46dd commit 2415d9d

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromJMSSerializerAttributeTypeRector\Fixture;
4+
5+
use JMS\Serializer\Annotation\Type;
6+
7+
final class StringVarFloat
8+
{
9+
/**
10+
* @var float
11+
*/
12+
#[Type('string')]
13+
private $price;
14+
}
15+
16+
?>
17+
-----
18+
<?php
19+
20+
namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromJMSSerializerAttributeTypeRector\Fixture;
21+
22+
use JMS\Serializer\Annotation\Type;
23+
24+
final class StringVarFloat
25+
{
26+
#[Type('string')]
27+
private ?float $price = null;
28+
}
29+
30+
?>

rules/TypeDeclaration/Rector/Class_/TypedPropertyFromJMSSerializerAttributeTypeRector.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
use PhpParser\Node\Stmt\Class_;
1414
use PhpParser\Node\Stmt\Property;
1515
use PHPStan\Reflection\ClassReflection;
16+
use PHPStan\Type\FloatType;
1617
use PHPStan\Type\MixedType;
1718
use PHPStan\Type\ObjectType;
1819
use PHPStan\Type\StringType;
19-
use PHPStan\Type\Type;
2020
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
2121
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
2222
use Rector\DeadCode\PhpDoc\TagRemover\VarTagRemover;
@@ -97,8 +97,6 @@ public function provideMinPhpVersion(): int
9797
*/
9898
public function refactor(Node $node): ?Node
9999
{
100-
$hasChanged = false;
101-
102100
if (! $this->hasAtLeastOneUntypedPropertyUsingJmsAttribute($node)) {
103101
return null;
104102
}
@@ -108,6 +106,8 @@ public function refactor(Node $node): ?Node
108106
return null;
109107
}
110108

109+
$hasChanged = false;
110+
111111
foreach ($node->getProperties() as $property) {
112112
if ($this->shouldSkipProperty($property, $classReflection)) {
113113
continue;
@@ -180,13 +180,19 @@ private function createTypeNode(string $typeValue, Property $property): ?Node
180180
{
181181
if ($typeValue === 'float') {
182182
$propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($property);
183-
if ($propertyPhpDocInfo instanceof PhpDocInfo) {
184-
// fallback to string, as most likely string representation of float
185-
if ($propertyPhpDocInfo->getVarType() instanceof StringType) {
186-
$this->varTagRemover->removeVarTag($property);
183+
// fallback to string, as most likely string representation of float
184+
if ($propertyPhpDocInfo instanceof PhpDocInfo && $propertyPhpDocInfo->getVarType() instanceof StringType) {
185+
$this->varTagRemover->removeVarTag($property);
186+
return new Identifier('string');
187+
}
188+
}
187189

188-
return new Identifier('string');
189-
}
190+
if ($typeValue === 'string') {
191+
$propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($property);
192+
// fallback to string, as most likely string representation of float
193+
if ($propertyPhpDocInfo instanceof PhpDocInfo && $propertyPhpDocInfo->getVarType() instanceof FloatType) {
194+
$this->varTagRemover->removeVarTag($property);
195+
return new Identifier('float');
190196
}
191197
}
192198

0 commit comments

Comments
 (0)