Skip to content

Commit

Permalink
add read const fetch support
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 23, 2024
1 parent 7244227 commit fc0a974
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector\Fixture\OneToMany;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector\Source\TrainingWithIntegerIdAttributeConstantFetch;

/**
* @ORM\Entity
*/
class WithIndexByIntegerAttributeReadConstantFetch
{
/**
* @var Collection<int, string>|TrainingWithIntegerIdAttributeConstantFetch[]
*/
#[ORM\OneToMany(targetEntity: TrainingWithIntegerIdAttributeConstantFetch::class, mappedBy: 'trainer', indexBy: 'id')]
private $trainings = [];

/**
* @var Collection<int, string>|TrainingWithIntegerIdAttributeConstantFetch[]
*/
#[ORM\OneToMany(targetEntity: TrainingWithIntegerIdAttributeConstantFetch::class, mappedBy: 'trainer', indexBy: 'id2')]
private $trainings2 = [];
}

?>
-----
<?php

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector\Fixture\OneToMany;

use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector\Source\TrainingWithIntegerIdAttributeConstantFetch;

/**
* @ORM\Entity
*/
class WithIndexByIntegerAttributeReadConstantFetch
{
/**
* @var \Doctrine\Common\Collections\Collection<string, \Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector\Source\TrainingWithIntegerIdAttributeConstantFetch>
*/
#[ORM\OneToMany(targetEntity: TrainingWithIntegerIdAttributeConstantFetch::class, mappedBy: 'trainer', indexBy: 'id')]
private $trainings = [];

/**
* @var \Doctrine\Common\Collections\Collection<int, \Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector\Source\TrainingWithIntegerIdAttributeConstantFetch>
*/
#[ORM\OneToMany(targetEntity: TrainingWithIntegerIdAttributeConstantFetch::class, mappedBy: 'trainer', indexBy: 'id2')]
private $trainings2 = [];
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector\Source;

use Doctrine\ORM\Mapping as ORM;
use Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector\Source\ValueObject\DoctrineType;

final class TrainingWithIntegerIdAttributeConstantFetch
{
#[ORM\Column(name: 'id', type: DoctrineType::STRING)]
#[ORM\Id]
private string $id;

#[ORM\Column(name: 'id2', type: DoctrineType::INTEGER)]
#[ORM\Id]
private int $id2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\Tests\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector\Source\ValueObject;

class DoctrineType
{
public const INTEGER = 'integer';
public const STRING = 'string';
}
8 changes: 5 additions & 3 deletions src/TypeAnalyzer/CollectionTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\PhpParser\AstResolver;
use Rector\PhpParser\Node\Value\ValueResolver;

final readonly class CollectionTypeFactory
{
public function __construct(
private PhpDocInfoFactory $phpDocInfoFactory,
private AstResolver $astResolver
private AstResolver $astResolver,
private ValueResolver $valueResolver
) {
}

Expand Down Expand Up @@ -153,8 +155,8 @@ private function resolveKeyFromAttribute(Class_ $class, string $key): IntegerTyp
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === 'Doctrine\ORM\Mapping\Column') {
foreach ($attr->args as $arg) {
if ($arg->name instanceof Identifier && $arg->name->name === 'type' && $arg->value instanceof String_) {
$type = $arg->value->value;
if ($arg->name instanceof Identifier && $arg->name->name === 'type') {
$type = $this->valueResolver->getValue($arg->value);
return $type === 'string' ? new StringType() : new IntegerType();
}
}
Expand Down

0 comments on commit fc0a974

Please sign in to comment.