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,31 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Property\TypedPropertyFromColumnTypeRector\Fixture;

use Doctrine\ORM\Mapping as ORM;

class DecimalDefaultValue
{
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=false)
*/
private $totalShippingCostDec = 0;
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Property\TypedPropertyFromColumnTypeRector\Fixture;

use Doctrine\ORM\Mapping as ORM;

class DecimalDefaultValue
{
/**
* @ORM\Column(type="decimal", precision=10, scale=2, nullable=false)
*/
private string $totalShippingCostDec = '0';
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
namespace Rector\Doctrine\CodeQuality\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Scalar\Float_;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -112,6 +116,9 @@ public function refactor(Node $node): Property|null

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

// Doctrine "decimal"/"bigint" map to string; keep a numeric default valid as string, eg: 0 to '0'
$this->refactorNumericDefaultToString($node, $propertyType);

if ($propertyType instanceof UnionType) {
$this->propertyTypeDecorator->decoratePropertyUnionType($propertyType, $typeNode, $node, $phpDocInfo);
return $node;
Expand All @@ -121,6 +128,22 @@ public function refactor(Node $node): Property|null
return $node;
}

private function refactorNumericDefaultToString(Property $property, Type $propertyType): void
{
$bareType = TypeCombinator::removeNull($propertyType);
if (! $bareType instanceof StringType) {
return;
}

$propertyItem = $property->props[0];
$default = $propertyItem->default;
if (! $default instanceof Int_ && ! $default instanceof Float_) {
return;
}

$propertyItem->default = new String_((string) $default->value);
}

private function hasUntypedParentProperty(ClassReflection $classReflection, Property $property): bool
{
$propertyName = $this->getName($property);
Expand Down
Loading