Skip to content
Closed
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 @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\Closure\ClosureReturnTypeFromAssertInstanceOfRector;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ClosureReturnTypeFromAssertInstanceOfRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Closure\ClosureReturnTypeFromAssertInstanceOfRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ClosureReturnTypeFromAssertInstanceOfRector\Fixture;

use PHPUnit\Framework\TestCase;

Expand All @@ -20,7 +20,7 @@ final class AssertInstanceOfNarrowedReturn extends TestCase
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Closure\ClosureReturnTypeFromAssertInstanceOfRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ClosureReturnTypeFromAssertInstanceOfRector\Fixture;

use PHPUnit\Framework\TestCase;

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

namespace Rector\Tests\TypeDeclaration\Rector\Closure\ClosureReturnTypeFromAssertInstanceOfRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ClosureReturnTypeFromAssertInstanceOfRector\Fixture;

use PHPUnit\Framework\TestCase;

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

namespace Rector\Tests\TypeDeclaration\Rector\Closure\ClosureReturnTypeFromAssertInstanceOfRector\Fixture;
namespace Rector\Tests\TypeDeclaration\Rector\Class_\ClosureReturnTypeFromAssertInstanceOfRector\Fixture;

final class SkipOutsideTestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\Closure\ClosureReturnTypeFromAssertInstanceOfRector;
use Rector\TypeDeclaration\Rector\Class_\ClosureReturnTypeFromAssertInstanceOfRector;
use Rector\ValueObject\PhpVersion;

return static function (RectorConfig $rectorConfig): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

declare(strict_types=1);

namespace Rector\TypeDeclaration\Rector\Closure;
namespace Rector\TypeDeclaration\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\NeverType;
use Rector\Enum\ClassName;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand All @@ -24,7 +23,7 @@
use Rector\VersionBonding\Contract\MinPhpVersionInterface;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\Closure\ClosureReturnTypeFromAssertInstanceOfRector\ClosureReturnTypeFromAssertInstanceOfRectorTest
* @see \Rector\Tests\TypeDeclaration\Rector\Class_\ClosureReturnTypeFromAssertInstanceOfRector\ClosureReturnTypeFromAssertInstanceOfRectorTest
*/
final class ClosureReturnTypeFromAssertInstanceOfRector extends AbstractRector implements MinPhpVersionInterface
{
Expand Down Expand Up @@ -82,44 +81,60 @@ public function test()
*/
public function getNodeTypes(): array
{
return [Closure::class];
return [Class_::class];
}

/**
* @param Closure $node
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
// type is already set
if ($node->returnType instanceof Node) {
$classReflection = $this->fetchClassLikeReflection($node);
if (! $classReflection instanceof ClassReflection) {
return null;
}

// only handle closures narrowed by assertInstanceOf(); plain returns are handled by ClosureReturnTypeRector
if (! $this->hasAssertInstanceOf($node)) {
// is phpunit test case?
if (! $classReflection->is(ClassName::TEST_CASE_CLASS)) {
return null;
}

$scope = ScopeFetcher::fetch($node);
if (! $this->isInsideTestCaseClass($scope)) {
return null;
}
$hasChanged = false;

$closureReturnType = $this->returnTypeInferer->inferFunctionLike($node);
/** @var Closure[] $closures */
$closures = $this->betterNodeFinder->findInstancesOf($node, [Closure::class]);
foreach ($closures as $closure) {
// type is already set
if ($closure->returnType instanceof Node) {
continue;
}

// handled by other rules
if ($closureReturnType instanceof NeverType) {
return null;
}
// only handle closures narrowed by assertInstanceOf(); plain returns are handled by ClosureReturnTypeRector
if (! $this->hasAssertInstanceOf($closure)) {
continue;
}

$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($closureReturnType, TypeKind::RETURN);
if (! $returnTypeNode instanceof Node) {
return null;
$closureReturnType = $this->returnTypeInferer->inferFunctionLike($closure);

// handled by other rules
if ($closureReturnType instanceof NeverType) {
continue;
}

$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($closureReturnType, TypeKind::RETURN);
if (! $returnTypeNode instanceof Node) {
continue;
}

$closure->returnType = $returnTypeNode;
$hasChanged = true;
}

$node->returnType = $returnTypeNode;
if ($hasChanged) {
return $node;
}

return $node;
return null;
}

public function provideMinPhpVersion(): int
Expand All @@ -135,15 +150,4 @@ private function hasAssertInstanceOf(Closure $closure): bool
&& $this->isName($node->name, 'assertInstanceOf')
);
}

private function isInsideTestCaseClass(Scope $scope): bool
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return false;
}

// is phpunit test case?
return $classReflection->is(ClassName::TEST_CASE_CLASS);
}
}
2 changes: 1 addition & 1 deletion src/Config/Level/TypeDeclarationLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\TypeDeclaration\Rector\ArrowFunction\AddArrowFunctionReturnTypeRector;
use Rector\TypeDeclaration\Rector\Class_\AddTestsVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\Class_\ChildDoctrineRepositoryClassTypeRector;
use Rector\TypeDeclaration\Rector\Class_\ClosureReturnTypeFromAssertInstanceOfRector;
use Rector\TypeDeclaration\Rector\Class_\MergeDateTimePropertyTypeDeclarationRector;
use Rector\TypeDeclaration\Rector\Class_\ObjectTypedPropertyFromJMSSerializerAttributeTypeRector;
use Rector\TypeDeclaration\Rector\Class_\PropertyTypeFromStrictSetterGetterRector;
Expand Down Expand Up @@ -56,7 +57,6 @@
use Rector\TypeDeclaration\Rector\ClassMethod\StringReturnTypeFromStrictStringReturnsRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureNeverReturnTypeRector;
use Rector\TypeDeclaration\Rector\Closure\AddClosureVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\Closure\ClosureReturnTypeFromAssertInstanceOfRector;
use Rector\TypeDeclaration\Rector\Closure\ClosureReturnTypeRector;
use Rector\TypeDeclaration\Rector\FuncCall\AddArrayAnyAllClosureParamTypeRector;
use Rector\TypeDeclaration\Rector\FuncCall\AddArrayFunctionClosureParamTypeRector;
Expand Down
Loading