Skip to content

Commit e3f770f

Browse files
authored
Support for rector 0.15 (#91)
* remove deprecated ClassMethodManipulator@addMethodParameterIfMissing call * support latest rector 0.15 releases & upgrade to phpunit 10
1 parent 1c33058 commit e3f770f

File tree

34 files changed

+158
-170
lines changed

34 files changed

+158
-170
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ composer.lock
55
.idea/
66

77
.phpunit.result.cache
8+
.phpunit.cache
89

910
# often customized locally - example on Github is just fine
1011
rector-recipe.php

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
"license": "MIT",
55
"description": "Rector upgrades rules for Laravel Framework",
66
"require": {
7-
"php": ">=8.1"
7+
"php": ">=8.1",
8+
"rector/rector": "^0.15.12"
89
},
910
"require-dev": {
10-
"rector/rector": "^0.14.7",
11-
"phpunit/phpunit": "^9.5",
11+
"phpunit/phpunit": "^10.0",
1212
"phpstan/phpstan": "^1.8.2",
1313
"symplify/phpstan-rules": "^11.0",
1414
"symplify/phpstan-extensions": "^11.0",

config/sets/laravel60.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Rector\Renaming\Rector\StaticCall\RenameStaticMethodRector;
1313
use Rector\Renaming\ValueObject\MethodCallRename;
1414
use Rector\Renaming\ValueObject\RenameStaticMethod;
15-
use Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector;
1615
use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector;
1716
use Rector\Visibility\ValueObject\ChangeMethodVisibility;
1817

@@ -22,16 +21,14 @@
2221
return static function (RectorConfig $rectorConfig): void {
2322
$rectorConfig->import(__DIR__ . '/../config.php');
2423

25-
# https://github.com/laravel/framework/commit/67a38ba0fa2acfbd1f4af4bf7d462bb4419cc091
26-
$rectorConfig->rule(ParamTypeDeclarationRector::class);
27-
2824
$rectorConfig
29-
->ruleWithConfiguration(RenameMethodRector::class, [new MethodCallRename(
30-
'Illuminate\Auth\Access\Gate',
31-
# https://github.com/laravel/framework/commit/69de466ddc25966a0f6551f48acab1afa7bb9424
32-
'access',
33-
'inspect'
34-
),
25+
->ruleWithConfiguration(RenameMethodRector::class, [
26+
new MethodCallRename(
27+
'Illuminate\Auth\Access\Gate',
28+
# https://github.com/laravel/framework/commit/69de466ddc25966a0f6551f48acab1afa7bb9424
29+
'access',
30+
'inspect'
31+
),
3532
new MethodCallRename(
3633
'Illuminate\Support\Facades\Lang',
3734
# https://github.com/laravel/framework/commit/efbe23c4116f86846ad6edc0d95cd56f4175a446
@@ -64,11 +61,12 @@
6461
]);
6562

6663
$rectorConfig
67-
->ruleWithConfiguration(ChangeMethodVisibilityRector::class, [new ChangeMethodVisibility(
68-
'Illuminate\Foundation\Http\FormRequest',
69-
'validationData',
70-
Visibility::PUBLIC
71-
),
64+
->ruleWithConfiguration(ChangeMethodVisibilityRector::class, [
65+
new ChangeMethodVisibility(
66+
'Illuminate\Foundation\Http\FormRequest',
67+
'validationData',
68+
Visibility::PUBLIC
69+
),
7270
]);
7371

7472
$rectorConfig

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?xml version="1.0"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
45
bootstrap="vendor/autoload.php"
56
colors="true"
7+
executionOrder="defects"
8+
cacheDirectory=".phpunit.cache"
69
>
710
<testsuites>
811
<testsuite name="main">

src/Rector/ClassMethod/MigrateToSimplifiedAttributeRector.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@ public function refactor(Node $node): ?Node
5959
}
6060

6161
/** @var ClassLike $parentClass */
62-
$parentClass = $this->betterNodeFinder->findParentType(
63-
$node,
64-
ClassLike::class
65-
);
62+
$parentClass = $this->betterNodeFinder->findParentType($node, ClassLike::class);
6663

6764
// Skip if the new attribute name is already used
6865
foreach ($parentClass->getMethods() as $classMethod) {
@@ -83,9 +80,7 @@ public function refactor(Node $node): ?Node
8380
// So we generate the new method where the accessor
8481
// is placed on the model and remove the mutator,
8582
// so we don't run the refactoring twice
86-
if ($accessor instanceof ClassMethod && $mutator instanceof ClassMethod && $this->isMutator(
87-
$nodeName
88-
)) {
83+
if ($accessor instanceof ClassMethod && $mutator instanceof ClassMethod && $this->isMutator($nodeName)) {
8984
$this->removeNode($mutator);
9085
return null;
9186
}
@@ -327,9 +322,7 @@ function (Stmt $stmt) {
327322
);
328323

329324
// Append the updated attributes assignment statements
330-
$statements[] = new Return_(new Array_(
331-
$attributesAssignmentStatements
332-
));
325+
$statements[] = new Return_(new Array_($attributesAssignmentStatements));
333326

334327
return $statements;
335328
}

src/Rector/StaticCall/RequestStaticValidateToInjectRector.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use PhpParser\Node\Stmt\Class_;
1414
use PhpParser\Node\Stmt\ClassMethod;
1515
use PHPStan\Type\ObjectType;
16-
use Rector\Core\NodeManipulator\ClassMethodManipulator;
1716
use Rector\Core\Rector\AbstractRector;
1817
use Rector\Core\Reflection\ReflectionResolver;
1918
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -31,7 +30,6 @@ final class RequestStaticValidateToInjectRector extends AbstractRector
3130
private array $requestObjectTypes = [];
3231

3332
public function __construct(
34-
private readonly ClassMethodManipulator $classMethodManipulator,
3533
private readonly ReflectionResolver $reflectionResolver
3634
) {
3735
$this->requestObjectTypes = [new ObjectType('Illuminate\Http\Request'), new ObjectType('Request')];
@@ -88,28 +86,27 @@ public function refactor(Node $node): ?Node
8886
return null;
8987
}
9088

91-
$requestName = $this->classMethodManipulator->addMethodParameterIfMissing(
92-
$node,
93-
new ObjectType('Illuminate\Http\Request'),
94-
['request', 'httpRequest']
95-
);
89+
$requestParam = $this->addRequestParameterIfMissing($node, new ObjectType('Illuminate\Http\Request'));
9690

97-
$variable = new Variable($requestName);
91+
if ($requestParam === null) {
92+
return null;
93+
}
9894

9995
$methodName = $this->getName($node->name);
96+
10097
if ($methodName === null) {
10198
return null;
10299
}
103100

104101
if ($node instanceof FuncCall) {
105102
if ($node->args === []) {
106-
return $variable;
103+
return $requestParam->var;
107104
}
108105

109106
$methodName = 'input';
110107
}
111108

112-
return new MethodCall($variable, new Identifier($methodName), $node->args);
109+
return new MethodCall($requestParam->var, new Identifier($methodName), $node->args);
113110
}
114111

115112
private function shouldSkip(StaticCall|FuncCall $node): bool
@@ -134,4 +131,27 @@ private function shouldSkip(StaticCall|FuncCall $node): bool
134131

135132
return ! $this->isName($node, 'request');
136133
}
134+
135+
private function addRequestParameterIfMissing(Node $node, ObjectType $objectType): ?Node\Param
136+
{
137+
$classMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);
138+
139+
if (! $classMethod instanceof ClassMethod) {
140+
return null;
141+
}
142+
143+
foreach ($classMethod->params as $paramNode) {
144+
if (! $this->nodeTypeResolver->isObjectType($paramNode, $objectType)) {
145+
continue;
146+
}
147+
148+
return $paramNode;
149+
}
150+
151+
$classMethod->params[] = $paramNode = new Node\Param(new Variable(
152+
'request'
153+
), null, new Node\Name\FullyQualified($objectType->getClassName()));
154+
155+
return $paramNode;
156+
}
137157
}

tests/Rector/Assign/CallOnAppArrayAccessToStandaloneAssignRector/CallOnAppArrayAccessToStandaloneAssignRectorTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
namespace RectorLaravel\Tests\Rector\Assign\CallOnAppArrayAccessToStandaloneAssignRector;
66

77
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
910

1011
final class CallOnAppArrayAccessToStandaloneAssignRectorTest extends AbstractRectorTestCase
1112
{
12-
/**
13-
* @dataProvider provideData()
14-
*/
13+
#[DataProvider('provideData')]
1514
public function test(string $filePath): void
1615
{
1716
$this->doTestFile($filePath);
1817
}
1918

20-
public function provideData(): Iterator
19+
public static function provideData(): Iterator
2120
{
22-
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
2322
}
2423

2524
public function provideConfigFilePath(): string

tests/Rector/ClassMethod/AddArgumentDefaultValueRector/AddArgumentDefaultValueRectorTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
namespace RectorLaravel\Tests\Rector\ClassMethod\AddArgumentDefaultValueRector;
66

77
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
910

1011
final class AddArgumentDefaultValueRectorTest extends AbstractRectorTestCase
1112
{
12-
/**
13-
* @dataProvider provideData()
14-
*/
13+
#[DataProvider('provideData')]
1514
public function test(string $filePath): void
1615
{
1716
$this->doTestFile($filePath);
1817
}
1918

20-
public function provideData(): Iterator
19+
public static function provideData(): Iterator
2120
{
22-
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
2322
}
2423

2524
public function provideConfigFilePath(): string

tests/Rector/ClassMethod/AddArgumentDefaultValueRector/config/configured_rule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
76
use RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector;
87
use RectorLaravel\ValueObject\AddArgumentDefaultValue;
98

tests/Rector/ClassMethod/AddGenericReturnTypeToRelationsRector/AddGenericReturnTypeToRelationsRectorTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
namespace RectorLaravel\Tests\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector;
66

77
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
910

1011
final class AddGenericReturnTypeToRelationsRectorTest extends AbstractRectorTestCase
1112
{
12-
/**
13-
* @dataProvider provideData()
14-
*/
13+
#[DataProvider('provideData')]
1514
public function test(string $filePath): void
1615
{
1716
$this->doTestFile($filePath);
1817
}
1918

20-
public function provideData(): Iterator
19+
public static function provideData(): Iterator
2120
{
22-
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
2322
}
2423

2524
public function provideConfigFilePath(): string

tests/Rector/ClassMethod/AddParentBootToModelClassMethodRector/AddParentBootToModelClassMethodRectorTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
namespace RectorLaravel\Tests\Rector\ClassMethod\AddParentBootToModelClassMethodRector;
66

77
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
910

1011
final class AddParentBootToModelClassMethodRectorTest extends AbstractRectorTestCase
1112
{
12-
/**
13-
* @dataProvider provideData()
14-
*/
13+
#[DataProvider('provideData')]
1514
public function test(string $filePath): void
1615
{
1716
$this->doTestFile($filePath);
1817
}
1918

20-
public function provideData(): Iterator
19+
public static function provideData(): Iterator
2120
{
22-
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
2322
}
2423

2524
public function provideConfigFilePath(): string

tests/Rector/ClassMethod/AddParentRegisterToEventServiceProviderRector/AddParentRegisterToEventServiceProviderRectorTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
namespace RectorLaravel\Tests\Rector\ClassMethod\AddParentRegisterToEventServiceProviderRector;
66

77
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
910

1011
final class AddParentRegisterToEventServiceProviderRectorTest extends AbstractRectorTestCase
1112
{
12-
/**
13-
* @dataProvider provideData()
14-
*/
13+
#[DataProvider('provideData')]
1514
public function test(string $filePath): void
1615
{
1716
$this->doTestFile($filePath);
1817
}
1918

20-
public function provideData(): Iterator
19+
public static function provideData(): Iterator
2120
{
22-
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
2322
}
2423

2524
public function provideConfigFilePath(): string

tests/Rector/ClassMethod/MigrateToSimplifiedAttributeRector/MigrateToSimplifiedAttributeRectorTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
namespace RectorLaravel\Tests\Rector\ClassMethod\MigrateToSimplifiedAttributeRector;
66

77
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
910

1011
class MigrateToSimplifiedAttributeRectorTest extends AbstractRectorTestCase
1112
{
12-
/**
13-
* @dataProvider provideData()
14-
*/
13+
#[DataProvider('provideData')]
1514
public function test(string $filePath): void
1615
{
1716
$this->doTestFile($filePath);
@@ -20,9 +19,9 @@ public function test(string $filePath): void
2019
/**
2120
* @return Iterator<string>
2221
*/
23-
public function provideData(): Iterator
22+
public static function provideData(): Iterator
2423
{
25-
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
24+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
2625
}
2726

2827
public function provideConfigFilePath(): string

tests/Rector/Class_/AddMockConsoleOutputFalseToConsoleTestsRector/AddMockConsoleOutputFalseToConsoleTestsRectorTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
namespace RectorLaravel\Tests\Rector\Class_\AddMockConsoleOutputFalseToConsoleTestsRector;
66

77
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
89
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
910

1011
final class AddMockConsoleOutputFalseToConsoleTestsRectorTest extends AbstractRectorTestCase
1112
{
12-
/**
13-
* @dataProvider provideData()
14-
*/
13+
#[DataProvider('provideData')]
1514
public function test(string $filePath): void
1615
{
1716
$this->doTestFile($filePath);
1817
}
1918

20-
public function provideData(): Iterator
19+
public static function provideData(): Iterator
2120
{
22-
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
2322
}
2423

2524
public function provideConfigFilePath(): string

0 commit comments

Comments
 (0)