Skip to content

Commit

Permalink
Skip date time unless entity (#256)
Browse files Browse the repository at this point in the history
* Skip date time unless entity

* [rector] Rector fixes

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user authored Jan 31, 2024
1 parent b419ce7 commit f6b9b1d
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Class_\InitializeDefaultEntityCollectionRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Class_\MoveCurrentDateTimeDefaultInEntityToConstructorRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Class_\RemoveEmptyTableAttributeRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Property\CorrectDefaultTypesOnEntityPropertyRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

use DateTime;

class SkipNoEntity
{
/**
* @var DateTime|null
*/
protected $bornAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Property\MakeEntityDateTimePropertyDateTimeInterfaceRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromColumnTypeRector;

use Rector\ValueObject\PhpVersionFeature;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromDoctrineCollectionRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromToManyRelationTypeRector;

use Rector\ValueObject\PhpVersionFeature;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Property\TypedPropertyFromToOneRelationTypeRector;

use Rector\ValueObject\PhpVersionFeature;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\Dbal40\Rector\MethodCall\ChangeCompositeExpressionAddMultipleWithWithRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\Orm28\Rector\MethodCall\IterateToToIterableRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down
2 changes: 1 addition & 1 deletion rules/CodeQuality/Enum/ToManyMappings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class ToManyMappings
/**
* @var class-string[]
*/
public const TO_MANY_CLASSES = ['Doctrine\\ORM\\Mapping\\OneToMany', 'Doctrine\\ORM\\Mapping\\ManyToMany'];
final public const TO_MANY_CLASSES = ['Doctrine\\ORM\\Mapping\\OneToMany', 'Doctrine\\ORM\\Mapping\\ManyToMany'];
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
namespace Rector\Doctrine\CodeQuality\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Doctrine\NodeAnalyzer\DoctrineEntityDetector;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockClassRenamer;
use Rector\NodeTypeResolver\ValueObject\OldToNewType;
use Rector\Rector\AbstractRector;
Expand All @@ -30,6 +32,7 @@ public function __construct(
private readonly DocBlockClassRenamer $docBlockClassRenamer,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly DoctrineEntityDetector $doctrineEntityDetector,
) {
}

Expand Down Expand Up @@ -89,31 +92,44 @@ public function setBornAt(DateTimeInterface $bornAt)
*/
public function getNodeTypes(): array
{
return [Property::class];
return [Class_::class];
}

/**
* @param Property $node
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (! $phpDocInfo instanceof PhpDocInfo) {
if (! $this->doctrineEntityDetector->detect($node)) {
return null;
}

$varType = $phpDocInfo->getVarType();
if ($varType instanceof UnionType) {
$varType = TypeCombinator::removeNull($varType);
}
$hasChanged = false;

if (! $varType->equals(new ObjectType('DateTime'))) {
return null;
foreach ($node->getProperties() as $property) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($property);
if (! $phpDocInfo instanceof PhpDocInfo) {
continue;
}

$varType = $phpDocInfo->getVarType();
if ($varType instanceof UnionType) {
$varType = TypeCombinator::removeNull($varType);
}

if (! $varType->equals(new ObjectType('DateTime'))) {
continue;
}

$this->changePropertyType($property, 'DateTime', 'DateTimeInterface');
$hasChanged = true;
}

$this->changePropertyType($node, 'DateTime', 'DateTimeInterface');
if ($hasChanged) {
return $node;
}

return $node;
return null;
}

private function changePropertyType(Property $property, string $oldClass, string $newClass): void
Expand Down
38 changes: 38 additions & 0 deletions src/NodeAnalyzer/DoctrineEntityDetector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\NodeAnalyzer;

use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ReflectionProvider;
use Rector\NodeAnalyzer\DoctrineEntityAnalyzer;

final class DoctrineEntityDetector
{
public function __construct(
private readonly DoctrineEntityAnalyzer $doctrineEntityAnalyzer,
private readonly ReflectionProvider $reflectionProvider,
) {
}

public function detect(Class_ $class): bool
{
// A. check annotations
if ($this->doctrineEntityAnalyzer->hasClassAnnotation($class)) {
return true;
}

if (! $class->namespacedName instanceof Name) {
return false;
}

$className = $class->namespacedName->toString();

// B. check attributes
$classReflection = $this->reflectionProvider->getClass($className);

return $this->doctrineEntityAnalyzer->hasClassReflectionAttribute($classReflection);
}
}
1 change: 0 additions & 1 deletion tests/Set/DoctrineBUNDLE210Set/config/configured_set.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\Set\DoctrineSetList;

return static function (RectorConfig $rectorConfig): void {
Expand Down
1 change: 0 additions & 1 deletion tests/Set/DoctrineDBAL211Set/config/configured_set.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\Set\DoctrineSetList;

return static function (RectorConfig $rectorConfig): void {
Expand Down
1 change: 0 additions & 1 deletion tests/Set/DoctrineORM29Set/config/configured_set.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\Set\DoctrineSetList;
use Rector\ValueObject\PhpVersionFeature;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\Set\DoctrineSetList;
use Rector\ValueObject\PhpVersionFeature;

Expand Down

0 comments on commit f6b9b1d

Please sign in to comment.