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

namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;

final class SkipCallbackKeyedArray
{
public function run()
{
$config = [
'callback' => [$this, 'name'],
];
}

public function name()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;

final class SkipFactoryKeyedArray
{
public function run()
{
$config = [
'factory' => [$this, 'name'],
];
}

public function name()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;

use Symfony\Component\DependencyInjection\Definition;

final class SkipSetFactoryArgument
{
public function run(Definition $definition)
{
$definition->setFactory([$this, 'name']);
}

public function name()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;

final class SkipValidationGroupsKeyedArray
{
public function run()
{
$config = [
'validation_groups' => [$this, 'name'],
];
}

public function name()
{
}
}
4 changes: 4 additions & 0 deletions rules/Php81/Rector/Array_/ArrayToFirstClassCallableRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public function refactor(Node $node): StaticCall|MethodCall|null
return null;
}

if ($node->getAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE)) {
return null;
}

$scope = ScopeFetcher::fetch($node);

$arrayCallable = $this->arrayCallableMethodMatcher->match($node, $scope);
Expand Down
6 changes: 6 additions & 0 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ final class AttributeKey

public const string IS_INSIDE_SYMFONY_PHP_CLOSURE = 'is_inside_symfony_php_closure';

/**
* Array callable kept as data, not converted to first class callable,
* e.g. 'callback'/'factory' keyed array item, or Definition::setFactory() argument
*/
public const string IS_ARRAY_AS_STRING_CALLABLE = 'is_array_as_string_callable';

public const string IS_INSIDE_BYREF_FUNCTION_LIKE = 'is_inside_byref_function_like';

public const string CLASS_CONST_FETCH_NAME = 'class_const_fetch_name';
Expand Down
63 changes: 63 additions & 0 deletions src/PhpParser/NodeVisitor/ContextNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Attribute;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PostDec;
use PhpParser\Node\Expr\PostInc;
use PhpParser\Node\Expr\PreDec;
use PhpParser\Node\Expr\PreInc;
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\Scalar\String_;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Do_;
Expand Down Expand Up @@ -87,6 +91,16 @@ public function enterNode(Node $node): ?Node
return null;
}

if ($node instanceof Array_) {
$this->processArrayInSerializedCallableKey($node);
return null;
}

if ($node instanceof MethodCall) {
$this->processSetFactoryArgument($node);
return null;
}

if ($node instanceof If_ || $node instanceof Else_ || $node instanceof ElseIf_) {
$this->processContextInIf($node);
return null;
Expand Down Expand Up @@ -140,6 +154,55 @@ private function processContextInClass(Node $node): void
}
}

/**
* The array is data, not a callable to convert: 'callback'/'factory' keyed item holds
* a [$value, 'method'] pair likely to be serialized as a string.
*/
private function processArrayInSerializedCallableKey(Array_ $array): void
{
foreach ($array->items as $arrayItem) {
if (! $arrayItem instanceof ArrayItem) {
continue;
}

if (! $arrayItem->key instanceof String_) {
continue;
}

if (! in_array($arrayItem->key->value, ['callback', 'factory', 'validation_groups'], true)) {
continue;
}

if ($arrayItem->value instanceof Array_) {
$arrayItem->value->setAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE, true);
}
}
}

/**
* Symfony's Definition::setFactory() does not accept a first class callable,
* keep the [$value, 'method'] array as is.
*/
private function processSetFactoryArgument(MethodCall $methodCall): void
{
if (! $methodCall->name instanceof Identifier) {
return;
}

if ($methodCall->name->toString() !== 'setFactory') {
return;
}

$firstArg = $methodCall->args[0] ?? null;
if (! $firstArg instanceof Arg) {
return;
}

if ($firstArg->value instanceof Array_) {
$firstArg->value->setAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE, true);
}
}

private function processContextInAttribute(Attribute $attribute): void
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
Expand Down
Loading