Skip to content

Commit 44674a8

Browse files
GeniJahomwikberg-virtasamsonasikmaks-oleksyuk
authored
Rector 2.0 (#277)
* PHPStan & Rector 2.0 support (#271) * Update rector dependency to 2.0.0-rc1 (#273) * Update rector to 2.0.0.rc2 * Simplify composer scripts * Fix downgrade scripts * Add MigrateToSimplifiedAttributeRector to Laravel 9 set (#275) * Update Duster and use latest Pint configuration (#276) * Update Duster * Remove pint.json and run Duster * Refactor into our own AbstractRector that implements DocumentedRuleInterface * Update to Rector 2.0 --------- Co-authored-by: Michael Wikberg <[email protected]> Co-authored-by: Abdul Malik Ikhsan <[email protected]> Co-authored-by: M ɑ k s <[email protected]>
1 parent f8d624d commit 44674a8

File tree

153 files changed

+418
-509
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

153 files changed

+418
-509
lines changed

.github/workflows/downgraded_release.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ jobs:
1515

1616
steps:
1717
-
18-
uses: "actions/checkout@v3"
18+
uses: "actions/checkout@v4"
1919

2020
-
2121
uses: "shivammathur/setup-php@v2"
2222
with:
23-
php-version: 8.2
23+
php-version: 8.3
2424
coverage: none
2525

2626
- uses: "ramsey/composer-install@v2"

build/composer-php-72.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "Rector upgrades rules for Laravel Framework",
66
"require": {
77
"php": "^7.2 || ^8.0",
8-
"rector/rector": "^1.0"
8+
"rector/rector": "^2.0"
99
},
1010
"autoload": {
1111
"psr-4": {

build/rector-downgrade-php-72.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\Set\ValueObject\DowngradeLevelSetList;
76

8-
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_72]);
10-
};
7+
return RectorConfig::configure()->withDowngradeSets(php72: true);

composer.json

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@
55
"description": "Rector upgrades rules for Laravel Framework",
66
"require": {
77
"php": ">=8.2",
8-
"rector/rector": "^1.2.5"
8+
"rector/rector": "^2.0.0",
9+
"webmozart/assert": "^1.11",
10+
"symplify/rule-doc-generator-contracts": "^11.2"
911
},
1012
"require-dev": {
11-
"nikic/php-parser": "^4.18",
13+
"nikic/php-parser": "^5.3",
1214
"phpstan/extension-installer": "^1.3",
13-
"phpstan/phpstan": "^1.10",
14-
"phpstan/phpstan-strict-rules": "^1.5",
15-
"phpstan/phpstan-webmozart-assert": "^1.2",
15+
"phpstan/phpstan": "^2.0",
16+
"phpstan/phpstan-deprecation-rules": "^2.0",
17+
"phpstan/phpstan-strict-rules": "^2.0",
18+
"phpstan/phpstan-webmozart-assert": "^2.0",
1619
"phpunit/phpunit": "^10.5",
17-
"symplify/phpstan-extensions": "^11.4",
18-
"symplify/phpstan-rules": "^12.4",
19-
"symplify/rule-doc-generator": "^12.1",
20-
"tightenco/duster": "^2.7"
20+
"symplify/rule-doc-generator": "^12.2",
21+
"tightenco/duster": "^3.1"
2122
},
2223
"autoload": {
2324
"psr-4": {
@@ -31,9 +32,7 @@
3132
"classmap": ["stubs"]
3233
},
3334
"scripts": {
34-
"phpstan": "vendor/bin/phpstan analyse --ansi --error-format symplify",
35-
"check-cs": "vendor/bin/duster lint",
36-
"fix-cs": "vendor/bin/duster fix",
35+
"phpstan": "vendor/bin/phpstan analyse --ansi",
3736
"lint": "vendor/bin/duster lint",
3837
"fix": "vendor/bin/duster fix",
3938
"rector-dry-run": "vendor/bin/rector process --dry-run --ansi",

config/config.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44

55
use Rector\Config\RectorConfig;
66

7-
return static function (RectorConfig $rectorConfig): void {
8-
};
7+
return static function (RectorConfig $rectorConfig): void {};

config/sets/laravel90.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector;
1313
use Rector\Visibility\ValueObject\ChangeMethodVisibility;
1414
use RectorLaravel\Rector\Class_\AddExtendsAnnotationToModelFactoriesRector;
15+
use RectorLaravel\Rector\ClassMethod\MigrateToSimplifiedAttributeRector;
1516
use RectorLaravel\Rector\PropertyFetch\ReplaceFakerInstanceWithHelperRector;
1617

1718
// see https://laravel.com/docs/9.x/upgrade
@@ -106,4 +107,7 @@
106107
// https://github.com/laravel/framework/commit/9894c2c64dc70f7dfda2ac46dfdaa8769ce4596a
107108
new MethodCallRename('Illuminate\Testing\TestResponse', 'assertDeleted', 'assertModelMissing'),
108109
]);
110+
111+
// https://github.com/laravel/framework/commit/e0c2620b57be6416820ea7ca8e46fd2f71d2fe35
112+
$rectorConfig->rule(MigrateToSimplifiedAttributeRector::class);
109113
};

phpstan.neon

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
includes:
2-
- vendor/symplify/phpstan-rules/config/rector-rules.neon
31
parameters:
42
level: max
53

@@ -15,7 +13,7 @@ parameters:
1513
- */Source/*
1614
- *Source/*
1715

18-
# reportUnmatchedIgnoredErrors: false
16+
treatPhpDocTypesAsCertain: false
1917

2018
ignoreErrors:
2119
# false positive
@@ -32,3 +30,7 @@ parameters:
3230

3331
# Laravel Container not being recognized properly in some of the tests
3432
- '#Call to method needs\(\) on an unknown class Illuminate\\Contracts\\Container\\ContextualBindingBuilder#'
33+
- '#Cannot call method give\(\) on mixed.#'
34+
35+
# No easy replacement for this check, it's also ignored in core Rector
36+
- '#Doing instanceof PHPStan\\Type\\Generic\\GenericObjectType is error\-prone and deprecated#'

pint.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/AbstractRector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace RectorLaravel;
4+
5+
use Rector\Rector\AbstractRector as BaseAbstractRector;
6+
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
7+
8+
abstract class AbstractRector extends BaseAbstractRector implements DocumentedRuleInterface {}

src/NodeAnalyzer/LumenRouteRegisteringMethodAnalyzer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
public function __construct(
1717
private NodeTypeResolver $nodeTypeResolver,
1818
private NodeNameResolver $nodeNameResolver
19-
) {
20-
}
19+
) {}
2120

2221
public function isLumenRoutingClass(MethodCall $methodCall): bool
2322
{

src/NodeAnalyzer/StaticCallAnalyzer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
{
1414
public function __construct(
1515
private NodeNameResolver $nodeNameResolver
16-
) {
17-
}
16+
) {}
1817

1918
public function isParentCallNamed(Node $node, string $desiredMethodName): bool
2019
{

src/NodeFactory/AppAssignFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
{
1818
public function __construct(
1919
private PhpDocInfoFactory $phpDocInfoFactory
20-
) {
21-
}
20+
) {}
2221

2322
public function createAssignExpression(
2423
ServiceNameTypeAndVariableName $serviceNameTypeAndVariableName,

src/NodeFactory/ModelFactoryNodeFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ public function __construct(
3838
private NodeFactory $nodeFactory,
3939
private ValueResolver $valueResolver,
4040
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser
41-
) {
42-
}
41+
) {}
4342

4443
public function createEmptyFactory(string $name, Expr $expr): Class_
4544
{

src/NodeFactory/RouterRegisterNodeAnalyzer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
public function __construct(
1818
private NodeNameResolver $nodeNameResolver,
1919
private NodeTypeResolver $nodeTypeResolver
20-
) {
21-
}
20+
) {}
2221

2322
public function isRegisterMethodStaticCall(MethodCall|StaticCall $node): bool
2423
{

src/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PhpParser\Node\Arg;
77
use PhpParser\Node\Expr\ArrayDimFetch;
88
use PhpParser\Node\Expr\StaticCall;
9-
use Rector\Rector\AbstractRector;
9+
use RectorLaravel\AbstractRector;
1010
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1111
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1212

src/Rector/ArrayDimFetch/RequestVariablesToRequestFacadeRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use PhpParser\Node\Expr\StaticCall;
99
use PhpParser\Node\Scalar;
1010
use PhpParser\Node\Scalar\String_;
11-
use Rector\Rector\AbstractRector;
11+
use RectorLaravel\AbstractRector;
1212
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1313
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1414

src/Rector/Assign/CallOnAppArrayAccessToStandaloneAssignRector.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use PHPStan\Type\ObjectType;
1616
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1717
use Rector\PhpParser\Node\Value\ValueResolver;
18-
use Rector\Rector\AbstractRector;
18+
use RectorLaravel\AbstractRector;
1919
use RectorLaravel\NodeFactory\AppAssignFactory;
2020
use RectorLaravel\ValueObject\ServiceNameTypeAndVariableName;
2121
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -53,8 +53,9 @@ public function getNodeTypes(): array
5353

5454
/**
5555
* @param Expression $node
56+
* @return array<int, Node>|null
5657
*/
57-
public function refactor(Node $node): Node|array|int|null
58+
public function refactor(Node $node): ?array
5859
{
5960
if (! $node->expr instanceof Assign) {
6061
return null;

src/Rector/Cast/DatabaseExpressionCastsToMethodCallRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\Cast\String_;
99
use PhpParser\Node\Expr\StaticCall;
10-
use Rector\Rector\AbstractRector;
10+
use RectorLaravel\AbstractRector;
1111
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1212
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1313

src/Rector/ClassMethod/AddArgumentDefaultValueRector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PhpParser\Node\Param;
1111
use PhpParser\Node\Stmt\ClassLike;
1212
use Rector\Contract\Rector\ConfigurableRectorInterface;
13-
use Rector\Rector\AbstractRector;
13+
use RectorLaravel\AbstractRector;
1414
use RectorLaravel\ValueObject\AddArgumentDefaultValue;
1515
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
1616
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

src/Rector/ClassMethod/AddGenericReturnTypeToRelationsRector.php

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
1515
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
1616
use PHPStan\Reflection\ClassReflection;
17-
use PHPStan\Type\Constant\ConstantStringType;
18-
use PHPStan\Type\Generic\GenericClassStringType;
1917
use PHPStan\Type\Generic\GenericObjectType;
2018
use PHPStan\Type\ObjectType;
2119
use PHPStan\Type\ThisType;
@@ -24,8 +22,9 @@
2422
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
2523
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
2624
use Rector\PhpParser\Node\BetterNodeFinder;
27-
use Rector\Rector\AbstractScopeAwareRector;
25+
use Rector\PHPStan\ScopeFetcher;
2826
use Rector\StaticTypeMapper\StaticTypeMapper;
27+
use RectorLaravel\AbstractRector;
2928
use ReflectionClassConstant;
3029
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
3130
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -34,7 +33,7 @@
3433
* @see \RectorLaravel\Tests\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector\AddGenericReturnTypeToRelationsRectorNewGenericsTest
3534
* @see \RectorLaravel\Tests\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector\AddGenericReturnTypeToRelationsRectorOldGenericsTest
3635
*/
37-
class AddGenericReturnTypeToRelationsRector extends AbstractScopeAwareRector
36+
class AddGenericReturnTypeToRelationsRector extends AbstractRector
3837
{
3938
// Relation methods which are supported by this Rector.
4039
private const RELATION_METHODS = [
@@ -59,8 +58,7 @@ public function __construct(
5958
private readonly BetterNodeFinder $betterNodeFinder,
6059
private readonly StaticTypeMapper $staticTypeMapper,
6160
private readonly string $applicationClass = 'Illuminate\Foundation\Application',
62-
) {
63-
}
61+
) {}
6462

6563
public function getRuleDefinition(): RuleDefinition
6664
{
@@ -141,12 +139,14 @@ public function getNodeTypes(): array
141139
return [ClassMethod::class];
142140
}
143141

144-
public function refactorWithScope(Node $node, Scope $scope): ?Node
142+
public function refactor(Node $node): ?Node
145143
{
146144
if (! $node instanceof ClassMethod) {
147145
return null;
148146
}
149147

148+
$scope = ScopeFetcher::fetch($node);
149+
150150
if ($this->shouldSkipNode($node, $scope)) {
151151
return null;
152152
}
@@ -239,21 +239,13 @@ private function getRelatedModelClassFromMethodCall(MethodCall $methodCall): ?st
239239
{
240240
$argType = $this->getType($methodCall->getArgs()[0]->value);
241241

242-
if ($argType instanceof ConstantStringType && $argType->isClassStringType()->yes()) {
243-
return $argType->getValue();
244-
}
245-
246-
if (! $argType instanceof GenericClassStringType) {
247-
return null;
248-
}
249-
250-
$modelType = $argType->getGenericType();
242+
$objectClassNames = $argType->getClassStringObjectType()->getObjectClassNames();
251243

252-
if (! $modelType instanceof ObjectType) {
244+
if ($objectClassNames === []) {
253245
return null;
254246
}
255247

256-
return $modelType->getClassName();
248+
return $objectClassNames[0];
257249
}
258250

259251
private function getRelationMethodCall(ClassMethod $classMethod): ?MethodCall
@@ -326,21 +318,13 @@ private function getClassForIntermediateGeneric(MethodCall $methodCall): ?string
326318

327319
$argType = $this->getType($args[1]->value);
328320

329-
if ($argType instanceof ConstantStringType && $argType->isClassStringType()->yes()) {
330-
return $argType->getValue();
331-
}
332-
333-
if (! $argType instanceof GenericClassStringType) {
334-
return null;
335-
}
336-
337-
$modelType = $argType->getGenericType();
321+
$objectClassNames = $argType->getClassStringObjectType()->getObjectClassNames();
338322

339-
if (! $modelType instanceof ObjectType) {
323+
if ($objectClassNames === []) {
340324
return null;
341325
}
342326

343-
return $modelType->getClassName();
327+
return $objectClassNames[0];
344328
}
345329

346330
private function areNativeTypeAndPhpDocReturnTypeEqual(

src/Rector/ClassMethod/AddParentBootToModelClassMethodRector.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use PhpParser\Node\Stmt\ClassMethod;
99
use PhpParser\Node\Stmt\Expression;
1010
use PHPStan\Reflection\ClassReflection;
11-
use Rector\Rector\AbstractRector;
1211
use Rector\Reflection\ReflectionResolver;
12+
use RectorLaravel\AbstractRector;
1313
use RectorLaravel\NodeAnalyzer\StaticCallAnalyzer;
1414
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1515
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -29,8 +29,7 @@ final class AddParentBootToModelClassMethodRector extends AbstractRector
2929
public function __construct(
3030
private readonly StaticCallAnalyzer $staticCallAnalyzer,
3131
private readonly ReflectionResolver $reflectionResolver,
32-
) {
33-
}
32+
) {}
3433

3534
public function getRuleDefinition(): RuleDefinition
3635
{

src/Rector/ClassMethod/AddParentRegisterToEventServiceProviderRector.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use PhpParser\Node\Stmt\ClassMethod;
99
use PhpParser\Node\Stmt\Expression;
1010
use PHPStan\Reflection\ClassReflection;
11-
use Rector\Rector\AbstractRector;
1211
use Rector\Reflection\ReflectionResolver;
12+
use RectorLaravel\AbstractRector;
1313
use RectorLaravel\NodeAnalyzer\StaticCallAnalyzer;
1414
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1515
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -29,8 +29,7 @@ final class AddParentRegisterToEventServiceProviderRector extends AbstractRector
2929
public function __construct(
3030
private readonly StaticCallAnalyzer $staticCallAnalyzer,
3131
private readonly ReflectionResolver $reflectionResolver,
32-
) {
33-
}
32+
) {}
3433

3534
public function getRuleDefinition(): RuleDefinition
3635
{

0 commit comments

Comments
 (0)