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,37 @@
<?php

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

use Rector\Tests\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector\Source\AbstractParentWithFilledProperty;

final class OwnPropertyWithParentConstruct extends AbstractParentWithFilledProperty
{
private array $ownTypes;

public function __construct()
{
parent::__construct();

$this->ownTypes = ['refresh_token'];
}
}

?>
-----
<?php

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

use Rector\Tests\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector\Source\AbstractParentWithFilledProperty;

final class OwnPropertyWithParentConstruct extends AbstractParentWithFilledProperty
{
private array $ownTypes = ['refresh_token'];

public function __construct()
{
parent::__construct();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

use Rector\Tests\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector\Source\AbstractParentWithFilledProperty;

final class SkipPropertyFilledInParentConstructor extends AbstractParentWithFilledProperty
{
protected array $allowedGrantTypes;

public function __construct()
{
parent::__construct();

$this->allowedGrantTypes = ['refresh_token'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector\Source;

abstract class AbstractParentWithFilledProperty
{
protected array $allowedGrantTypes = [];

public function __construct()
{
$this->allowedGrantTypes[] = 'authorization_code';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\ValueObject\MethodName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -30,7 +33,8 @@ final class InlineConstructorDefaultToPropertyRector extends AbstractRector
public function __construct(
private readonly ExprAnalyzer $exprAnalyzer,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly PropertyFetchFinder $propertyFetchFinder
private readonly PropertyFetchFinder $propertyFetchFinder,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -89,6 +93,8 @@ public function refactor(Node $node): ?Node
return null;
}

$hasParentConstructCall = false;

foreach ($constructClassMethod->stmts as $key => $stmt) {
// code that is possibly breaking flow
if ($stmt instanceof If_) {
Expand All @@ -99,6 +105,11 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->isParentConstructCall($stmt->expr)) {
$hasParentConstructCall = true;
continue;
}

if (! $stmt->expr instanceof Assign) {
continue;
}
Expand All @@ -115,6 +126,11 @@ public function refactor(Node $node): ?Node
continue;
}

// parent constructor may set the very same property, keep assign order as is
if ($hasParentConstructCall && $this->isPropertyDefinedInParentClass($node, $propertyName)) {
continue;
}

$hasPropertyChanged = $this->refactorProperty(
$node,
$propertyName,
Expand All @@ -135,6 +151,30 @@ public function refactor(Node $node): ?Node
return $node;
}

private function isParentConstructCall(Expr $expr): bool
{
if (! $expr instanceof StaticCall) {
return false;
}

if (! $this->isName($expr->class, 'parent')) {
return false;
}

return $this->isName($expr->name, MethodName::CONSTRUCT);
}

private function isPropertyDefinedInParentClass(Class_ $class, string $propertyName): bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($class);
if (! $classReflection instanceof ClassReflection) {
// unknown parent, keep it safe
return true;
}

return array_any($classReflection->getParents(), fn(ClassReflection $parentClassReflection) => $parentClassReflection->getNativeReflection()->hasProperty($propertyName));
}

private function matchAssignedLocalPropertyName(Assign $assign): ?string
{
if (! $assign->var instanceof PropertyFetch) {
Expand Down
Loading