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
7 changes: 4 additions & 3 deletions packages/DeadCode/src/Analyzer/SetterOnlyMethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ public function provideSetterOnlyPropertiesAndMethodsByType(): array
// 1. setter only properties by class
$assignOnlyPrivatePropertyNames = $this->classManipulator->getAssignOnlyPrivatePropertyNames($class);

// filter out ManyToOne entities
$manyToOnePropertyNames = $this->doctrineEntityManipulator->resolveManyToOnePropertyNames($class);
$assignOnlyPrivatePropertyNames = array_diff($assignOnlyPrivatePropertyNames, $manyToOnePropertyNames);
// filter out ManyToOne/OneToMany entities
$relationPropertyNames = $this->doctrineEntityManipulator->resolveRelationPropertyNames($class);
$assignOnlyPrivatePropertyNames = array_diff($assignOnlyPrivatePropertyNames, $relationPropertyNames);

if ($assignOnlyPrivatePropertyNames) {
$this->propertiesAndMethodsToRemoveByType[$type]['properties'] = $assignOnlyPrivatePropertyNames;
}
Expand Down
16 changes: 14 additions & 2 deletions packages/DeadCode/src/Doctrine/DoctrineEntityManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Nette\Utils\Strings;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
Expand Down Expand Up @@ -183,7 +184,7 @@ public function isNullableRelation(Property $property): bool
/**
* @return string[]
*/
public function resolveManyToOnePropertyNames(Class_ $class): array
public function resolveRelationPropertyNames(Class_ $class): array
{
$manyToOnePropertyNames = [];

Expand All @@ -192,7 +193,7 @@ public function resolveManyToOnePropertyNames(Class_ $class): array
continue;
}

if (! $this->docBlockManipulator->hasTag($stmt, self::MANY_TO_ONE_ANNOTATION)) {
if (! $this->isRelationProperty($stmt)) {
continue;
}

Expand All @@ -201,4 +202,15 @@ public function resolveManyToOnePropertyNames(Class_ $class): array

return $manyToOnePropertyNames;
}

private function isRelationProperty(Node $node): bool
{
foreach (self::RELATION_ANNOTATIONS as $relationAnnotation) {
if ($this->docBlockManipulator->hasTag($node, $relationAnnotation)) {
return true;
}
}

return false;
}
}