Skip to content

Commit

Permalink
[Privatization] Skip Entity and Embeddable Attribute Doctrine\ORM\Map…
Browse files Browse the repository at this point in the history
…ping on FinalizeClassesWithoutChildrenRector (#155)

Co-authored-by: Sebastian Schreiber <me@schreibersebastian.de>
Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
  • Loading branch information
3 people committed Jun 5, 2021
1 parent 8151fd8 commit 4e71338
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector\Fixture;

use Doctrine\ORM\Mapping\Embeddable;

#[Embeddable]
class People
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector\Fixture;

use Doctrine\ORM\Mapping\Entity;

#[Entity]
class People
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Privatization\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\Rector\AbstractRector;
Expand All @@ -16,6 +17,14 @@
*/
final class FinalizeClassesWithoutChildrenRector extends AbstractRector
{
/**
* @var string[]
*/
private const DOCTRINE_ORM_MAPPING_ANNOTATION = [
'Doctrine\ORM\Mapping\Entity',
'Doctrine\ORM\Mapping\Embeddable',
];

public function __construct(
private ClassAnalyzer $classAnalyzer
) {
Expand Down Expand Up @@ -82,16 +91,38 @@ public function refactor(Node $node): ?Node
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
if ($phpDocInfo->hasByAnnotationClasses(['Doctrine\ORM\Mapping\Entity', 'Doctrine\ORM\Mapping\Embeddable'])) {
if ($phpDocInfo->hasByAnnotationClasses(self::DOCTRINE_ORM_MAPPING_ANNOTATION)) {
return null;
}

if ($this->nodeRepository->hasClassChildren($node)) {
return null;
}

if ($this->hasEntityOrEmbeddableAttr($node)) {
return null;
}

$this->visibilityManipulator->makeFinal($node);

return $node;
}

private function hasEntityOrEmbeddableAttr(Class_ $class): bool
{
foreach ($class->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attribute) {
if (! $attribute->name instanceof FullyQualified) {
continue;
}

$className = $this->nodeNameResolver->getName($attribute->name);
if (in_array($className, self::DOCTRINE_ORM_MAPPING_ANNOTATION, true)) {
return true;
}
}
}

return false;
}
}
5 changes: 2 additions & 3 deletions src/Console/Output/ShowOutputFormatterCollector.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php
declare(strict_types=1);

declare(strict_types=1);

namespace Rector\Core\Console\Output;


use Rector\ListReporting\Contract\Output\ShowOutputFormatterInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

Expand Down Expand Up @@ -33,7 +32,7 @@ public function getByName(string $name): ShowOutputFormatterInterface
}

/**
* @return string[]
* @return int[]|string[]
*/
public function getNames(): array
{
Expand Down

0 comments on commit 4e71338

Please sign in to comment.