Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel 12 set with Bind Closures without Abstract rule #304

Merged
merged 7 commits into from
Feb 28, 2025
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
14 changes: 14 additions & 0 deletions config/sets/laravel120.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector;

// see https://laravel.com/docs/12.x/upgrade
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');

// https://github.com/laravel/framework/pull/54628
$rectorConfig->rule(ContainerBindConcreteWithClosureOnlyRector::class);
};
11 changes: 11 additions & 0 deletions config/sets/level/up-to-laravel-120.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelLevelSetList;
use RectorLaravel\Set\LaravelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([LaravelSetList::LARAVEL_120, LaravelLevelSetList::UP_TO_LARAVEL_110]);
};
107 changes: 107 additions & 0 deletions src/Rector/MethodCall/ContainerBindConcreteWithClosureOnlyRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace RectorLaravel\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Const_;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class ContainerBindConcreteWithClosureOnlyRector extends AbstractRector
{
public function __construct(
private readonly ReturnTypeInferer $returnTypeInferer,
private readonly StaticTypeMapper $staticTypeMapper,
) {}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Drop the specified abstract class from the bind method and replace it with a closure that returns the abstract class.',
[
new CodeSample(
<<<'CODE_SAMPLE'
$this->app->bind(SomeClass::class, function (): SomeClass {
return new SomeClass();
});
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$this->app->bind(function (): SomeClass {
return new SomeClass();
});
CODE_SAMPLE
),
]
);
}

public function getNodeTypes(): array
{
return [MethodCall::class];
}

/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?MethodCall
{
if (! $this->isNames($node->name, ['bind', 'singleton', 'bindIf', 'singletonIf'])) {
return null;
}

if (! $this->isObjectType($node->var, new ObjectType('Illuminate\Contracts\Container\Container'))) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

if (count($node->getArgs()) < 2) {
return null;
}

$type = $this->getType($node->getArgs()[0]->value);
$classString = $node->getArgs()[0]->value;
$concreteNode = $node->getArgs()[1]->value;

if (! $concreteNode instanceof Closure) {
return null;
}
$abstractFromConcrete = $this->returnTypeInferer->inferFunctionLike($concreteNode);

if ($classString instanceof Const_
&& $this->isName($classString, 'class')) {
return null;
}

$abstractObjectType = $type->getClassStringObjectType();

if ($abstractFromConcrete->isSuperTypeOf($abstractObjectType)->no()) {
return null;
}

// set the concrete's return type of the closure to from what's determined in PHPStan
$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($abstractObjectType, TypeKind::RETURN);
if (! $returnTypeNode instanceof Node) {
return null;
}

$concreteNode->returnType = $returnTypeNode;

$args = $node->getArgs();

$node->args = array_splice($args, 1);

return $node;
}
}
2 changes: 2 additions & 0 deletions src/Set/LaravelLevelSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ final class LaravelLevelSetList
final public const string UP_TO_LARAVEL_100 = __DIR__ . '/../../config/sets/level/up-to-laravel-100.php';

final public const string UP_TO_LARAVEL_110 = __DIR__ . '/../../config/sets/level/up-to-laravel-110.php';

final public const string UP_TO_LARAVEL_120 = __DIR__ . '/../../config/sets/level/up-to-laravel-120.php';
}
2 changes: 2 additions & 0 deletions src/Set/LaravelSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ final class LaravelSetList

final public const string LARAVEL_110 = __DIR__ . '/../../config/sets/laravel110.php';

final public const string LARAVEL_120 = __DIR__ . '/../../config/sets/laravel120.php';

final public const string LARAVEL_ARRAYACCESS_TO_METHOD_CALL = __DIR__ . '/../../config/sets/laravel-arrayaccess-to-method-call.php';

final public const string LARAVEL_ARRAY_STR_FUNCTION_TO_STATIC_CALL = __DIR__ . '/../../config/sets/laravel-array-str-functions-to-static-call.php';
Expand Down
1 change: 1 addition & 0 deletions src/Set/LaravelSetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class LaravelSetProvider implements SetProviderInterface
* @var string[]
*/
private const array LARAVEL_POST_FIVE = [
LaravelSetList::LARAVEL_120,
LaravelSetList::LARAVEL_110,
LaravelSetList::LARAVEL_100,
LaravelSetList::LARAVEL_90,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ContainerBindConcreteWithClosureOnlyRectorTest extends AbstractRectorTestCase
{
public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

/**
* @test
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Fixture;

use Illuminate\Contracts\Container\Container;
use RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Source\SomeClass;
use RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Source\SomeInterface;

/** @var Container $app */
$app->bind(SomeInterface::class, function () {
return new SomeClass();
});

$app->bind(SomeInterface::class, function (): SomeClass {
return new SomeClass();
});

$app->singleton(SomeInterface::class, function () {
return new SomeClass();
});

?>
-----
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Fixture;

use Illuminate\Contracts\Container\Container;
use RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Source\SomeClass;
use RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Source\SomeInterface;

/** @var Container $app */
$app->bind(function (): \RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Source\SomeInterface {
return new SomeClass();
});

$app->bind(function (): \RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Source\SomeInterface {
return new SomeClass();
});

$app->singleton(function (): \RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Source\SomeInterface {
return new SomeClass();
});

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

namespace RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Source;

class SomeClass implements SomeInterface {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace RectorLaravel\Tests\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector\Source;

interface SomeInterface {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Rector\MethodCall\ContainerBindConcreteWithClosureOnlyRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');

$rectorConfig->rule(ContainerBindConcreteWithClosureOnlyRector::class);
};