Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromJMSSerializerAttributeTypeRector\Fixture;

use JMS\Serializer\Annotation\Type;

final class StringVarFloat
{
/**
* @var float
*/
#[Type('string')]
private $price;
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromJMSSerializerAttributeTypeRector\Fixture;

use JMS\Serializer\Annotation\Type;

final class StringVarFloat
{
#[Type('string')]
private ?float $price = null;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -97,8 +97,6 @@ public function provideMinPhpVersion(): int
*/
public function refactor(Node $node): ?Node
{
$hasChanged = false;

if (! $this->hasAtLeastOneUntypedPropertyUsingJmsAttribute($node)) {
return null;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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');
}
}

Expand Down