Skip to content
Draft
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
4 changes: 4 additions & 0 deletions src/NodeCollector/NodeAnalyzer/ArrayCallableMethodMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public function match(
return null;
}

if ($array->getAttribute(AttributeKey::IS_ARG_VALUE_FACTORY_SERVICECONFIGURATOR) === true) {
return null;
}

$values = $this->valueResolver->getValue($array);
$className = $callerType->getClassName();
$secondItemValue = $items[1]->value;
Expand Down
2 changes: 2 additions & 0 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,6 @@ final class AttributeKey
public const string HAS_CLOSURE_WITH_VARIADIC_ARGS = 'has_closure_with_variadic_args';

public const string IS_IN_TRY_BLOCK = 'is_in_try_block';

public const string IS_ARG_VALUE_FACTORY_SERVICECONFIGURATOR = 'is_arg_value_factory_serviceconfigurator';
}
55 changes: 54 additions & 1 deletion src/PhpParser/NodeVisitor/ContextNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\PostDec;
Expand All @@ -19,6 +20,7 @@
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Break_;
Expand All @@ -36,15 +38,20 @@
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\NodeTraverser\SimpleNodeTraverser;
use Rector\Reflection\ReflectionResolver;

final class ContextNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
{
public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly ReflectionResolver $reflectionResolver
) {
}

Expand Down Expand Up @@ -92,6 +99,52 @@ public function enterNode(Node $node): ?Node
return null;
}

if ($node instanceof CallLike && ! $node->isFirstClassCallable()) {
$functionReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
if (! $functionReflection instanceof MethodReflection) {
return null;
}

if ($functionReflection->getDeclaringClass()->getName() !== 'Symfony\Component\DependencyInjection\Loader\Configurator\ServiceConfigurator') {
return null;
}

if ($functionReflection->getName() !== 'factory') {
return null;
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
$functionReflection,
$node,
$scope
);

$args = $node->getArgs();
foreach ($parametersAcceptor->getParameters() as $key => $parameterReflection) {
if ($parameterReflection->getName() !== 'factory') {
continue;
}

// based on name
foreach ($args as $arg) {
if ($arg->name instanceof Identifier && 'factory' === $arg->name->toString()) {
$arg->value->setAttribute(AttributeKey::IS_ARG_VALUE_FACTORY_SERVICECONFIGURATOR, true);
continue 2;
}
}

// based on key
if (isset($args[$key])) {
$args[$key]->value->setAttribute(AttributeKey::IS_ARG_VALUE_FACTORY_SERVICECONFIGURATOR, true);
}
}
}

if ($node instanceof Arg) {
$node->value->setAttribute(AttributeKey::IS_ARG_VALUE, true);
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;
use Symfony\Component\ExpressionLanguage\Expression;

if (class_exists('Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\ContainerConfigurator')) {
return;
}

class ContainerConfigurator
{
public function services(): ServiceConfigurator
{
return new ServiceConfigurator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator;
use Symfony\Component\ExpressionLanguage\Expression;

if (class_exists('Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\ServiceConfigurator')) {
return;
}

class ServiceConfigurator
{
function factory(string|array|ReferenceConfigurator|Expression $factory): static
{
return $this;
}
}