Skip to content

Commit

Permalink
Rector 2.0 (#277)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
4 people authored Dec 12, 2024
1 parent f8d624d commit 44674a8
Show file tree
Hide file tree
Showing 153 changed files with 418 additions and 509 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/downgraded_release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ jobs:

steps:
-
uses: "actions/checkout@v3"
uses: "actions/checkout@v4"

-
uses: "shivammathur/setup-php@v2"
with:
php-version: 8.2
php-version: 8.3
coverage: none

- uses: "ramsey/composer-install@v2"
Expand Down
2 changes: 1 addition & 1 deletion build/composer-php-72.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Rector upgrades rules for Laravel Framework",
"require": {
"php": "^7.2 || ^8.0",
"rector/rector": "^1.0"
"rector/rector": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
5 changes: 1 addition & 4 deletions build/rector-downgrade-php-72.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\DowngradeLevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([DowngradeLevelSetList::DOWN_TO_PHP_72]);
};
return RectorConfig::configure()->withDowngradeSets(php72: true);
23 changes: 11 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
"description": "Rector upgrades rules for Laravel Framework",
"require": {
"php": ">=8.2",
"rector/rector": "^1.2.5"
"rector/rector": "^2.0.0",
"webmozart/assert": "^1.11",
"symplify/rule-doc-generator-contracts": "^11.2"
},
"require-dev": {
"nikic/php-parser": "^4.18",
"nikic/php-parser": "^5.3",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-strict-rules": "^1.5",
"phpstan/phpstan-webmozart-assert": "^1.2",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpstan/phpstan-webmozart-assert": "^2.0",
"phpunit/phpunit": "^10.5",
"symplify/phpstan-extensions": "^11.4",
"symplify/phpstan-rules": "^12.4",
"symplify/rule-doc-generator": "^12.1",
"tightenco/duster": "^2.7"
"symplify/rule-doc-generator": "^12.2",
"tightenco/duster": "^3.1"
},
"autoload": {
"psr-4": {
Expand All @@ -31,9 +32,7 @@
"classmap": ["stubs"]
},
"scripts": {
"phpstan": "vendor/bin/phpstan analyse --ansi --error-format symplify",
"check-cs": "vendor/bin/duster lint",
"fix-cs": "vendor/bin/duster fix",
"phpstan": "vendor/bin/phpstan analyse --ansi",
"lint": "vendor/bin/duster lint",
"fix": "vendor/bin/duster fix",
"rector-dry-run": "vendor/bin/rector process --dry-run --ansi",
Expand Down
3 changes: 1 addition & 2 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
};
return static function (RectorConfig $rectorConfig): void {};
4 changes: 4 additions & 0 deletions config/sets/laravel90.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Rector\Visibility\ValueObject\ChangeMethodVisibility;
use RectorLaravel\Rector\Class_\AddExtendsAnnotationToModelFactoriesRector;
use RectorLaravel\Rector\ClassMethod\MigrateToSimplifiedAttributeRector;
use RectorLaravel\Rector\PropertyFetch\ReplaceFakerInstanceWithHelperRector;

// see https://laravel.com/docs/9.x/upgrade
Expand Down Expand Up @@ -106,4 +107,7 @@
// https://github.com/laravel/framework/commit/9894c2c64dc70f7dfda2ac46dfdaa8769ce4596a
new MethodCallRename('Illuminate\Testing\TestResponse', 'assertDeleted', 'assertModelMissing'),
]);

// https://github.com/laravel/framework/commit/e0c2620b57be6416820ea7ca8e46fd2f71d2fe35
$rectorConfig->rule(MigrateToSimplifiedAttributeRector::class);
};
8 changes: 5 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
includes:
- vendor/symplify/phpstan-rules/config/rector-rules.neon
parameters:
level: max

Expand All @@ -15,7 +13,7 @@ parameters:
- */Source/*
- *Source/*

# reportUnmatchedIgnoredErrors: false
treatPhpDocTypesAsCertain: false

ignoreErrors:
# false positive
Expand All @@ -32,3 +30,7 @@ parameters:

# Laravel Container not being recognized properly in some of the tests
- '#Call to method needs\(\) on an unknown class Illuminate\\Contracts\\Container\\ContextualBindingBuilder#'
- '#Cannot call method give\(\) on mixed.#'

# No easy replacement for this check, it's also ignored in core Rector
- '#Doing instanceof PHPStan\\Type\\Generic\\GenericObjectType is error\-prone and deprecated#'
6 changes: 0 additions & 6 deletions pint.json

This file was deleted.

8 changes: 8 additions & 0 deletions src/AbstractRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace RectorLaravel;

use Rector\Rector\AbstractRector as BaseAbstractRector;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;

abstract class AbstractRector extends BaseAbstractRector implements DocumentedRuleInterface {}
3 changes: 1 addition & 2 deletions src/NodeAnalyzer/LumenRouteRegisteringMethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
public function __construct(
private NodeTypeResolver $nodeTypeResolver,
private NodeNameResolver $nodeNameResolver
) {
}
) {}

public function isLumenRoutingClass(MethodCall $methodCall): bool
{
Expand Down
3 changes: 1 addition & 2 deletions src/NodeAnalyzer/StaticCallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
{
public function __construct(
private NodeNameResolver $nodeNameResolver
) {
}
) {}

public function isParentCallNamed(Node $node, string $desiredMethodName): bool
{
Expand Down
3 changes: 1 addition & 2 deletions src/NodeFactory/AppAssignFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
{
public function __construct(
private PhpDocInfoFactory $phpDocInfoFactory
) {
}
) {}

public function createAssignExpression(
ServiceNameTypeAndVariableName $serviceNameTypeAndVariableName,
Expand Down
3 changes: 1 addition & 2 deletions src/NodeFactory/ModelFactoryNodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public function __construct(
private NodeFactory $nodeFactory,
private ValueResolver $valueResolver,
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser
) {
}
) {}

public function createEmptyFactory(string $name, Expr $expr): Class_
{
Expand Down
3 changes: 1 addition & 2 deletions src/NodeFactory/RouterRegisterNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
public function __construct(
private NodeNameResolver $nodeNameResolver,
private NodeTypeResolver $nodeTypeResolver
) {
}
) {}

public function isRegisterMethodStaticCall(MethodCall|StaticCall $node): bool
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rector/ArrayDimFetch/EnvVariableToEnvHelperRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\StaticCall;
use Rector\Rector\AbstractRector;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use PHPStan\Type\ObjectType;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use RectorLaravel\AbstractRector;
use RectorLaravel\NodeFactory\AppAssignFactory;
use RectorLaravel\ValueObject\ServiceNameTypeAndVariableName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -53,8 +53,9 @@ public function getNodeTypes(): array

/**
* @param Expression $node
* @return array<int, Node>|null
*/
public function refactor(Node $node): Node|array|int|null
public function refactor(Node $node): ?array
{
if (! $node->expr instanceof Assign) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\StaticCall;
use Rector\Rector\AbstractRector;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down
2 changes: 1 addition & 1 deletion src/Rector/ClassMethod/AddArgumentDefaultValueRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassLike;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use RectorLaravel\AbstractRector;
use RectorLaravel\ValueObject\AddArgumentDefaultValue;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down
42 changes: 13 additions & 29 deletions src/Rector/ClassMethod/AddGenericReturnTypeToRelationsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
Expand All @@ -24,8 +22,9 @@
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractScopeAwareRector;
use Rector\PHPStan\ScopeFetcher;
use Rector\StaticTypeMapper\StaticTypeMapper;
use RectorLaravel\AbstractRector;
use ReflectionClassConstant;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -34,7 +33,7 @@
* @see \RectorLaravel\Tests\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector\AddGenericReturnTypeToRelationsRectorNewGenericsTest
* @see \RectorLaravel\Tests\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector\AddGenericReturnTypeToRelationsRectorOldGenericsTest
*/
class AddGenericReturnTypeToRelationsRector extends AbstractScopeAwareRector
class AddGenericReturnTypeToRelationsRector extends AbstractRector
{
// Relation methods which are supported by this Rector.
private const RELATION_METHODS = [
Expand All @@ -59,8 +58,7 @@ public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly string $applicationClass = 'Illuminate\Foundation\Application',
) {
}
) {}

public function getRuleDefinition(): RuleDefinition
{
Expand Down Expand Up @@ -141,12 +139,14 @@ public function getNodeTypes(): array
return [ClassMethod::class];
}

public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
if (! $node instanceof ClassMethod) {
return null;
}

$scope = ScopeFetcher::fetch($node);

if ($this->shouldSkipNode($node, $scope)) {
return null;
}
Expand Down Expand Up @@ -239,21 +239,13 @@ private function getRelatedModelClassFromMethodCall(MethodCall $methodCall): ?st
{
$argType = $this->getType($methodCall->getArgs()[0]->value);

if ($argType instanceof ConstantStringType && $argType->isClassStringType()->yes()) {
return $argType->getValue();
}

if (! $argType instanceof GenericClassStringType) {
return null;
}

$modelType = $argType->getGenericType();
$objectClassNames = $argType->getClassStringObjectType()->getObjectClassNames();

if (! $modelType instanceof ObjectType) {
if ($objectClassNames === []) {
return null;
}

return $modelType->getClassName();
return $objectClassNames[0];
}

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

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

if ($argType instanceof ConstantStringType && $argType->isClassStringType()->yes()) {
return $argType->getValue();
}

if (! $argType instanceof GenericClassStringType) {
return null;
}

$modelType = $argType->getGenericType();
$objectClassNames = $argType->getClassStringObjectType()->getObjectClassNames();

if (! $modelType instanceof ObjectType) {
if ($objectClassNames === []) {
return null;
}

return $modelType->getClassName();
return $objectClassNames[0];
}

private function areNativeTypeAndPhpDocReturnTypeEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Reflection\ClassReflection;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use RectorLaravel\AbstractRector;
use RectorLaravel\NodeAnalyzer\StaticCallAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -29,8 +29,7 @@ final class AddParentBootToModelClassMethodRector extends AbstractRector
public function __construct(
private readonly StaticCallAnalyzer $staticCallAnalyzer,
private readonly ReflectionResolver $reflectionResolver,
) {
}
) {}

public function getRuleDefinition(): RuleDefinition
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Reflection\ClassReflection;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use RectorLaravel\AbstractRector;
use RectorLaravel\NodeAnalyzer\StaticCallAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -29,8 +29,7 @@ final class AddParentRegisterToEventServiceProviderRector extends AbstractRector
public function __construct(
private readonly StaticCallAnalyzer $staticCallAnalyzer,
private readonly ReflectionResolver $reflectionResolver,
) {
}
) {}

public function getRuleDefinition(): RuleDefinition
{
Expand Down
Loading

0 comments on commit 44674a8

Please sign in to comment.