Skip to content

Commit

Permalink
Move code-quality rules to own category namespae
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 13, 2023
1 parent aaca989 commit ea390bf
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 12 deletions.
31 changes: 30 additions & 1 deletion config/sets/doctrine-code-quality.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,43 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Rector\Class_\InitializeDefaultEntityCollectionRector;
use Rector\Doctrine\CodeQuality\Rector\Class_\InitializeDefaultEntityCollectionRector;
use Rector\Doctrine\CodeQuality\Rector\ClassMethod\MakeEntitySetterNullabilityInSyncWithPropertyRector;
use Rector\Doctrine\CodeQuality\Rector\Property\ChangeBigIntEntityPropertyToIntTypeRector;
use Rector\Doctrine\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector;
use Rector\Doctrine\CodeQuality\Rector\Property\MakeEntityDateTimePropertyDateTimeInterfaceRector;
use Rector\Doctrine\Rector\Class_\MoveCurrentDateTimeDefaultInEntityToConstructorRector;
use Rector\Doctrine\Rector\Class_\RemoveEmptyTableAttributeRector;
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
use Rector\Doctrine\Rector\ClassMethod\MakeEntitySetterNullabilityInSyncWithPropertyRector;
use Rector\Doctrine\Rector\Property\ChangeBigIntEntityPropertyToIntTypeRector;
use Rector\Doctrine\Rector\Property\CorrectDefaultTypesOnEntityPropertyRector;
use Rector\Doctrine\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector;
use Rector\Doctrine\Rector\Property\MakeEntityDateTimePropertyDateTimeInterfaceRector;
=======
use Rector\Doctrine\Rector\Class_\RemoveRedundantDefaultClassAnnotationValuesRector;
use Rector\Doctrine\CodeQuality\Rector\Property\ChangeBigIntEntityPropertyToIntTypeRector;
use Rector\Doctrine\Rector\Property\CorrectDefaultTypesOnEntityPropertyRector;
<<<<<<< HEAD
use Rector\Doctrine\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector;
use Rector\Doctrine\Rector\Property\RemoveRedundantDefaultPropertyAnnotationValuesRector;
>>>>>>> 1cee996 (Move code-quality rules to own category namespae)
=======
<<<<<<< HEAD
use Rector\Doctrine\CodeQuality\Rector\Property\ChangeBigIntEntityPropertyToIntTypeRector;
use Rector\Doctrine\Rector\Property\CorrectDefaultTypesOnEntityPropertyRector;
use Rector\Doctrine\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector;
>>>>>>> 18a567c (fixup! Move code-quality rules to own category namespae)
=======
use Rector\Doctrine\Rector\Property\CorrectDefaultTypesOnEntityPropertyRector;
>>>>>>> ae59947 (fixup! fixup! Move code-quality rules to own category namespae)
=======
use Rector\Doctrine\CodeQuality\Rector\Property\ImproveDoctrineCollectionDocTypeInEntityRector;
use Rector\Doctrine\CodeQuality\Rector\Property\RemoveRedundantDefaultPropertyAnnotationValuesRector;
>>>>>>> 0ed519d (Move code-quality rules to own category namespae)
>>>>>>> 819d28f (Move code-quality rules to own category namespae)
use Rector\Doctrine\Rector\Property\TypedPropertyFromColumnTypeRector;
use Rector\Doctrine\Rector\Property\TypedPropertyFromDoctrineCollectionRector;
use Rector\Doctrine\Rector\Property\TypedPropertyFromToManyRelationTypeRector;
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ parameters:
- config
- src
- tests
- rules
- rules-tests

scanDirectories:
- stubs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Doctrine\Rector\ClassMethod;
namespace Rector\Doctrine\CodeQuality\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\ComplexType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Doctrine\Rector\Class_;
namespace Rector\Doctrine\CodeQuality\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Doctrine\Rector\Property;
namespace Rector\Doctrine\CodeQuality\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Doctrine\Rector\Property;
namespace Rector\Doctrine\CodeQuality\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Expr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Doctrine\Rector\Property;
namespace Rector\Doctrine\CodeQuality\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php

declare(strict_types=1);

namespace Rector\Doctrine\CodeQuality\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Core\Rector\AbstractRector;
use Rector\Doctrine\NodeAnalyzer\AttributeCleaner;
use Rector\Doctrine\NodeAnalyzer\AttributeFinder;
use Rector\Doctrine\NodeManipulator\DoctrineItemDefaultValueManipulator;
use Rector\Doctrine\ValueObject\ArgName;
use Rector\Doctrine\ValueObject\DefaultAnnotationArgValue;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Doctrine\Tests\Rector\Property\RemoveRedundantDefaultPropertyAnnotationValuesRector\RemoveRedundantDefaultPropertyAnnotationValuesRectorTest
*
* @changelog https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/basic-mapping.html#property-mapping
*/
final class RemoveRedundantDefaultPropertyAnnotationValuesRector extends AbstractRector
{
/**
* @var DefaultAnnotationArgValue[]
*/
private array $defaultAnnotationArgValues = [];

public function __construct(
private readonly DoctrineItemDefaultValueManipulator $doctrineItemDefaultValueManipulator,
private readonly AttributeFinder $attributeFinder,
private readonly AttributeCleaner $attributeCleaner,
) {
$this->defaultAnnotationArgValues = [
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\Column', 'nullable', false),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\Column', 'unique', false),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\Column', 'precision', 0),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\Column', 'scale', 0),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\GeneratedValue', 'strategy', 'AUTO'),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\JoinColumn', 'unique', false),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\JoinColumn', 'nullable', true),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\JoinColumn', 'referencedColumnName', 'id'),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\ManyToMany', ArgName::ORPHAN_REMOVAL, false),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\ManyToMany', ArgName::FETCH, ArgName::LAZY),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\ManyToOne', ArgName::FETCH, ArgName::LAZY),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\OneToMany', ArgName::ORPHAN_REMOVAL, false),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\OneToMany', ArgName::FETCH, ArgName::LAZY),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\OneToOne', ArgName::ORPHAN_REMOVAL, false),
new DefaultAnnotationArgValue('Doctrine\ORM\Mapping\OneToOne', ArgName::FETCH, ArgName::LAZY),
];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Removes redundant default values from Doctrine ORM annotations/attributes properties',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class SomeClass
{
/**
* @ORM\ManyToOne(targetEntity=Training::class)
* @ORM\JoinColumn(name="training", unique=false)
*/
private $training;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class SomeClass
{
/**
* @ORM\ManyToOne(targetEntity=Training::class)
* @ORM\JoinColumn(name="training")
*/
private $training;
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Property::class];
}

/**
* @param Property $node
*/
public function refactor(Node $node): ?Node
{
$hasPhpDocInfoChanged = $this->clearPropertyAnnotations($node);
$hasAttributeChanged = false;

foreach ($this->defaultAnnotationArgValues as $defaultAnnotationArgValue) {
$argExpr = $this->attributeFinder->findAttributeByClassArgByName(
$node,
$defaultAnnotationArgValue->getAnnotationClass(),
$defaultAnnotationArgValue->getArgName()
);

if (! $argExpr instanceof Expr) {
continue;
}

if (! $this->valueResolver->isValue($argExpr, $defaultAnnotationArgValue->getDefaultValue())) {
continue;
}

$this->attributeCleaner->clearAttributeAndArgName(
$node,
$defaultAnnotationArgValue->getAnnotationClass(),
$defaultAnnotationArgValue->getArgName()
);

$hasAttributeChanged = true;
}

if ($hasPhpDocInfoChanged || $hasAttributeChanged) {
return $node;
}

return null;
}

private function clearPropertyAnnotations(Property $property): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($property);
if (! $phpDocInfo instanceof PhpDocInfo) {
return false;
}

$hasPropertyChanged = false;

foreach ($this->defaultAnnotationArgValues as $defaultAnnotationArgValue) {
$hasAnnotationChanged = $this->clearAnnotation($phpDocInfo, $defaultAnnotationArgValue);
if ($hasAnnotationChanged) {
$hasPropertyChanged = true;
}
}

return $hasPropertyChanged;
}

private function clearAnnotation(
PhpDocInfo $phpDocInfo,
DefaultAnnotationArgValue $defaultAnnotationArgValue
): bool {
$doctrineAnnotationTagValueNode = $phpDocInfo->getByAnnotationClass(
$defaultAnnotationArgValue->getAnnotationClass()
);
if (! $doctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) {
return false;
}

return $this->doctrineItemDefaultValueManipulator->clearDoctrineAnnotationTagValueNode(
$phpDocInfo,
$doctrineAnnotationTagValueNode,
$defaultAnnotationArgValue->getArgName(),
$defaultAnnotationArgValue->getDefaultValue(),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Rector\Config\RectorConfig;

use Rector\Doctrine\Rector\ClassMethod\MakeEntitySetterNullabilityInSyncWithPropertyRector;
use Rector\Doctrine\CodeQuality\Rector\ClassMethod\MakeEntitySetterNullabilityInSyncWithPropertyRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Rector\Config\RectorConfig;

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

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Rector\Config\RectorConfig;

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

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Rector\Config\RectorConfig;

use Rector\Doctrine\Rector\Property\ChangeBigIntEntityPropertyToIntTypeRector;
use Rector\Doctrine\CodeQuality\Rector\Property\ChangeBigIntEntityPropertyToIntTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Rector\Config\RectorConfig;

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

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Rector\Config\RectorConfig;

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

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Doctrine\CodeQuality\Rector\Property\RemoveRedundantDefaultPropertyAnnotationValuesRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(RemoveRedundantDefaultPropertyAnnotationValuesRector::class);
};

0 comments on commit ea390bf

Please sign in to comment.