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

namespace Rector\Tests\Transform\Rector\StaticCall\StaticCallToMethodCallRector\Fixture;

use Illuminate\Support\Facades\App;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\MissingValue;
use Rector\Tests\Transform\Rector\StaticCall\StaticCallToMethodCallRector\Source\ResourceWithFinalConstruct;

class SkipWhenParentHasFinalConstruct extends ResourceWithFinalConstruct
{
public function toArray(
Request $request,
): array {
return [
'user_id' => $this->user_id ?? App::get(MissingValue::class),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Transform\Rector\StaticCall\StaticCallToMethodCallRector\Source;

class ResourceWithFinalConstruct
{
public $resource;

final public function __construct($resource)
{
$this->resource = $resource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\CodeQuality\Rector\CallLike;

use PhpParser\Node\Expr;
use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use Rector\NodeAnalyzer\CallLikeArgumentNameAdder;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function refactor(Node $node): ?Node
{
return $this->callLikeArgumentNameAdder->addNamesToArgs(
$node,
fn ($expr): bool => $this->valueResolver->isTrueOrFalse($expr),
fn (Expr $expr): bool => $this->valueResolver->isTrueOrFalse($expr),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\CodeQuality\Rector\CallLike;

use PhpParser\Node\Expr;
use PhpParser\Node;
use PhpParser\Node\Expr\CallLike;
use Rector\NodeAnalyzer\CallLikeArgumentNameAdder;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function refactor(Node $node): ?Node
{
return $this->callLikeArgumentNameAdder->addNamesToArgs(
$node,
fn ($expr): bool => $this->valueResolver->isNull($expr),
fn (Expr $expr): bool => $this->valueResolver->isNull($expr),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function matchTypeProvidingExpr(
Class_ $class,
ClassMethod $classMethod,
ObjectType $objectType,
): MethodCall | PropertyFetch | Variable {
): MethodCall | PropertyFetch | Variable | null {
$expr = $this->typeProvidingExprFromClassResolver->resolveTypeProvidingExprFromClass(
$class,
$classMethod,
Expand All @@ -51,6 +51,11 @@ public function matchTypeProvidingExpr(
return $expr;
}

// Cannot add constructor dependency when nearest parent constructor is final
if ($this->classDependencyManipulator->hasFinalParentConstructor($class)) {
return null;
}

$propertyName = $this->propertyNaming->fqnToVariableName($objectType);
$this->classDependencyManipulator->addConstructorDependency(
$class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public function refactor(Node $node): ?Node
$funcNameToMethodCallName->getNewObjectType(),
);

if ($expr === null) {
return null;
}

$hasChanged = true;

return $this->nodeFactory->createMethodCall(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ public function refactor(Node $node): ?Node
$staticCallToMethodCall->getClassObjectType(),
);

if ($expr === null) {
return null;
}

$methodName = $this->getMethodName($node, $staticCallToMethodCall);

$hasChanged = true;
Expand Down
19 changes: 10 additions & 9 deletions src/NodeAnalyzer/CallLikeArgumentNameAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\NodeAnalyzer;

use PhpParser\Node\Expr;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Identifier;
Expand All @@ -26,22 +27,22 @@ public function __construct(
* argument whose value satisfies $shouldNameArgValue. All subsequent positional
* arguments receive names too (required by PHP named-arg semantics).
*
* @param callable(\PhpParser\Node\Expr): bool $shouldNameArgValue
* @param callable(Expr):bool $shouldNameArgValue
*/
public function addNamesToArgs(CallLike $node, callable $shouldNameArgValue): ?CallLike
public function addNamesToArgs(CallLike $callLike, callable $shouldNameArgValue): ?CallLike
{
if ($this->shouldSkip($node)) {
if ($this->shouldSkip($callLike)) {
return null;
}

$reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
$reflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($callLike);
if (! $reflection instanceof FunctionReflection && ! $reflection instanceof MethodReflection) {
return null;
}

$scope = ScopeFetcher::fetch($node);
$args = $node->getArgs();
$parameters = ParametersAcceptorSelectorVariantsWrapper::select($reflection, $node, $scope)
$scope = ScopeFetcher::fetch($callLike);
$args = $callLike->getArgs();
$parameters = ParametersAcceptorSelectorVariantsWrapper::select($reflection, $callLike, $scope)
->getParameters();

$position = $this->resolveFirstPositionToName($args, $parameters, $shouldNameArgValue);
Expand Down Expand Up @@ -70,7 +71,7 @@ public function addNamesToArgs(CallLike $node, callable $shouldNameArgValue): ?C
return null;
}

return $node;
return $callLike;
}

private function shouldSkip(CallLike $callLike): bool
Expand All @@ -96,7 +97,7 @@ private function shouldSkip(CallLike $callLike): bool
/**
* @param Arg[] $args
* @param ParameterReflection[] $parameters
* @param callable(\PhpParser\Node\Expr): bool $shouldNameArgValue
* @param callable(Expr):bool $shouldNameArgValue
*/
private function resolveFirstPositionToName(array $args, array $parameters, callable $shouldNameArgValue): ?int
{
Expand Down
37 changes: 37 additions & 0 deletions src/NodeManipulator/ClassDependencyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,43 @@ public function addStmtsToConstructorIfNotThereYet(Class_ $class, array $stmts):
$classMethod->stmts = array_merge($stmts, (array) $classMethod->stmts);
}

public function hasFinalParentConstructor(Class_ $class): bool
{
if ($class->getMethod(MethodName::CONSTRUCT) instanceof ClassMethod) {
return false;
}

$classReflection = $this->reflectionResolver->resolveClassReflection($class);
if (! $classReflection instanceof ClassReflection) {
return false;
}

$ancestors = array_filter(
$classReflection->getAncestors(),
static fn (ClassReflection $ancestor): bool => $ancestor->getName() !== $classReflection->getName()
);

foreach ($ancestors as $ancestor) {
if (! $ancestor->hasNativeMethod(MethodName::CONSTRUCT)) {
continue;
}

$parentClass = $this->astResolver->resolveClassFromClassReflection($ancestor);
Copy link
Copy Markdown
Member

@samsonasik samsonasik May 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be not needed, there is $ancestor->getNativeMethod(MethodName::CONSTRUCT)->isFinalByKeyword()->yes() for it, see

https://github.com/phpstan/phpstan-src/blob/c853aa27f93ac6c73146107f709712cf6034c220/src/Reflection/Native/NativeMethodReflection.php#L158

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (! $parentClass instanceof ClassLike) {
continue;
}

$parentConstructorMethod = $parentClass->getMethod(MethodName::CONSTRUCT);
if (! $parentConstructorMethod instanceof ClassMethod) {
continue;
}

return $parentConstructorMethod->isFinal();
}

return false;
}

private function resolveConstruct(Class_ $class): ?ClassMethod
{
$constructorMethod = $class->getMethod(MethodName::CONSTRUCT);
Expand Down
Loading