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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector\Fixture;

class SampleChild extends CancelableHandler
{
public function getMessage()
{
return $this->configFactory->get('');
}
}

class CancelableHandler extends \NotAutoloaded\EmailHandler
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\CodeQuality\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
Expand All @@ -15,6 +14,7 @@
use Rector\NodeAnalyzer\ClassAnalyzer;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ClassReflectionAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -30,6 +30,7 @@ public function __construct(
private readonly ClassAnalyzer $classAnalyzer,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly MissingPropertiesResolver $missingPropertiesResolver,
private readonly ClassReflectionAnalyzer $classReflectionAnalyzer,
) {
}

Expand Down Expand Up @@ -140,9 +141,25 @@ private function shouldSkipClass(Class_ $class): bool
return true;
}

return $class->extends instanceof FullyQualified && ! $this->reflectionProvider->hasClass(
$class->extends->toString()
);
// any not autoloaded ancestor may already declare the property, so we cannot safely add it
return $this->hasNotAutoloadedAncestor($classReflection);
}

private function hasNotAutoloadedAncestor(ClassReflection $classReflection): bool
{
$currentClassReflection = $classReflection;

while ($currentClassReflection instanceof ClassReflection) {
$parentClassName = $this->classReflectionAnalyzer->resolveParentClassName($currentClassReflection);

if ($parentClassName !== null && ! $this->reflectionProvider->hasClass($parentClassName)) {
return true;
}

$currentClassReflection = $currentClassReflection->getParentClass();
}

return false;
}

private function matchClassReflection(Class_ $class): ?ClassReflection
Expand Down
Loading