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
Expand Up @@ -4,11 +4,11 @@ namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixtu

use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;

final class StaticWithOtherObjectConstFetch
final class ReturnArrayCallableUnion
{
public function provideListenerPriority(): array
public function provideListener(): array|callable
{
return [SomeExternalObject::class, 'sleepStatic'];
return [SomeExternalObject::class, 'sleepStatic'];
}
}

Expand All @@ -20,11 +20,11 @@ namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixtu

use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;

final class StaticWithOtherObjectConstFetch
final class ReturnArrayCallableUnion
{
public function provideListenerPriority(): array
public function provideListener(): array|callable
{
return SomeExternalObject::sleepStatic(...);
return SomeExternalObject::sleepStatic(...);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

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

use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;

final class ReturnInUntypedMethod
{
public function provideListener()
{
return [SomeExternalObject::class, 'sleepStatic'];
}

public function provideNestedClosure(): array
{
return [
'listener' => function () {
return [SomeExternalObject::class, 'sleepStatic'];
},
];
}
}

?>
-----
<?php

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

use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;

final class ReturnInUntypedMethod
{
public function provideListener()
{
return SomeExternalObject::sleepStatic(...);
}

public function provideNestedClosure(): array
{
return [
'listener' => function () {
return SomeExternalObject::sleepStatic(...);
},
];
}
}

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

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

use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;

final class SkipReturnArrayTypes
{
public function provideIterable(): iterable
{
return [SomeExternalObject::class, 'sleepStatic'];
}

public function provideNullable(): ?array
{
return [SomeExternalObject::class, 'sleepStatic'];
}

public function provideUnion(): array|string
{
return [SomeExternalObject::class, 'sleepStatic'];
}

public function provideClosures()
{
$closure = function (): array {
return [SomeExternalObject::class, 'sleepStatic'];
};

$arrowFunction = fn (): array => [SomeExternalObject::class, 'sleepStatic'];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

use Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Source\SomeExternalObject;

final class SkipStaticWithOtherObjectConstFetchReturnedAsArray
{
public function provideListenerPriority(): array
{
return [SomeExternalObject::class, 'sleepStatic'];
}
}

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

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

if ($node->getAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE)) {
return null;
}
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 @@ -175,6 +175,12 @@ final class AttributeKey
*/
public const string IS_ARG_NOT_ACCEPTING_CLOSURE = 'is_arg_not_accepting_closure';

/**
* The array is returned from a function-like whose native return type does not accept a Closure,
* e.g. a "return [$this, 'method'];" in a method returning "array"
*/
public const string IS_RETURN_NOT_ACCEPTING_CLOSURE = 'is_return_not_accepting_closure';

/**
* Closure or arrow function with rebound $this, e.g. (fn () => ...)->call($object)
*/
Expand Down
61 changes: 61 additions & 0 deletions src/PhpParser/NodeVisitor/ContextNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Attribute;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Isset_;
Expand All @@ -23,7 +25,9 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Break_;
Expand All @@ -36,10 +40,12 @@
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\Node\Stmt\Unset_;
use PhpParser\Node\Stmt\While_;
use PhpParser\Node\UnionType;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -61,6 +67,13 @@ final class ContextNodeVisitor extends NodeVisitorAbstract implements Decorating
*/
private array $outerLoopAndTryDepths = [];

/**
* Whether the enclosing function-like declares a native return type not accepting a Closure
*
* @var bool[]
*/
private array $returnsArrayStack = [];

/**
* @param Node[] $nodes
*/
Expand All @@ -70,6 +83,7 @@ public function beforeTraverse(array $nodes): null
$this->tryDepth = 0;
$this->attributeDepth = 0;
$this->outerLoopAndTryDepths = [];
$this->returnsArrayStack = [];

return null;
}
Expand All @@ -84,6 +98,15 @@ public function enterNode(Node $node): ?Node
return null;
}

if ($node instanceof FunctionLike) {
$this->processReturnTypeArray($node);
}

if ($node instanceof Return_ && $node->expr instanceof Array_ && end($this->returnsArrayStack)) {
$node->expr->setAttribute(AttributeKey::IS_RETURN_NOT_ACCEPTING_CLOSURE, true);
return null;
}

if ($node instanceof ArrayDimFetch) {
$this->processInsideArrayDimFetch($node);
return null;
Expand Down Expand Up @@ -181,6 +204,10 @@ public function leaveNode(Node $node): null
return null;
}

if ($node instanceof FunctionLike) {
array_pop($this->returnsArrayStack);
}

if ($node instanceof Class_ || $node instanceof FunctionLike) {
[$this->loopDepth, $this->tryDepth] = array_pop($this->outerLoopAndTryDepths) ?? [0, 0];
}
Expand Down Expand Up @@ -219,6 +246,40 @@ private function decorateEnclosingContext(Node $node): void
}
}

private function processReturnTypeArray(FunctionLike $functionLike): void
{
$returnsArray = $this->isArrayNotAcceptingClosureType($functionLike->getReturnType());
$this->returnsArrayStack[] = $returnsArray;

if ($returnsArray && $functionLike instanceof ArrowFunction && $functionLike->expr instanceof Array_) {
$functionLike->expr->setAttribute(AttributeKey::IS_RETURN_NOT_ACCEPTING_CLOSURE, true);
}
}

private function isArrayNotAcceptingClosureType(Identifier|Name|ComplexType|null $type): bool
{
if ($type instanceof NullableType) {
$type = $type->type;
}

$types = $type instanceof UnionType ? $type->types : [$type];

$typeNames = [];
foreach ($types as $singleType) {
if (! $singleType instanceof Identifier && ! $singleType instanceof Name) {
return false;
}

$typeNames[] = strtolower($singleType->toString());
}

if (array_intersect($typeNames, ['callable', 'closure', 'mixed', 'object']) !== []) {
return false;
}

return array_intersect($typeNames, ['array', 'iterable']) !== [];
}

private function processInsideArrayDimFetch(ArrayDimFetch $arrayDimFetch): void
{
if ($arrayDimFetch->var instanceof PropertyFetch || $arrayDimFetch->var instanceof StaticPropertyFetch) {
Expand Down
Loading