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

Support for rector 0.15 #91

Merged
merged 3 commits into from
Feb 25, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ composer.lock
.idea/

.phpunit.result.cache
.phpunit.cache

# often customized locally - example on Github is just fine
rector-recipe.php
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"license": "MIT",
"description": "Rector upgrades rules for Laravel Framework",
"require": {
"php": ">=8.1"
"php": ">=8.1",
"rector/rector": "^0.15.12"
},
"require-dev": {
"rector/rector": "^0.14.7",
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "^1.8.2",
"symplify/phpstan-rules": "^11.0",
"symplify/phpstan-extensions": "^11.0",
Expand Down
28 changes: 13 additions & 15 deletions config/sets/laravel60.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Rector\Renaming\Rector\StaticCall\RenameStaticMethodRector;
use Rector\Renaming\ValueObject\MethodCallRename;
use Rector\Renaming\ValueObject\RenameStaticMethod;
use Rector\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector;
use Rector\Visibility\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Rector\Visibility\ValueObject\ChangeMethodVisibility;

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

# https://github.com/laravel/framework/commit/67a38ba0fa2acfbd1f4af4bf7d462bb4419cc091
$rectorConfig->rule(ParamTypeDeclarationRector::class);

$rectorConfig
->ruleWithConfiguration(RenameMethodRector::class, [new MethodCallRename(
'Illuminate\Auth\Access\Gate',
# https://github.com/laravel/framework/commit/69de466ddc25966a0f6551f48acab1afa7bb9424
'access',
'inspect'
),
->ruleWithConfiguration(RenameMethodRector::class, [
new MethodCallRename(
'Illuminate\Auth\Access\Gate',
# https://github.com/laravel/framework/commit/69de466ddc25966a0f6551f48acab1afa7bb9424
'access',
'inspect'
),
new MethodCallRename(
'Illuminate\Support\Facades\Lang',
# https://github.com/laravel/framework/commit/efbe23c4116f86846ad6edc0d95cd56f4175a446
Expand Down Expand Up @@ -64,11 +61,12 @@
]);

$rectorConfig
->ruleWithConfiguration(ChangeMethodVisibilityRector::class, [new ChangeMethodVisibility(
'Illuminate\Foundation\Http\FormRequest',
'validationData',
Visibility::PUBLIC
),
->ruleWithConfiguration(ChangeMethodVisibilityRector::class, [
new ChangeMethodVisibility(
'Illuminate\Foundation\Http\FormRequest',
'validationData',
Visibility::PUBLIC
),
]);

$rectorConfig
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
executionOrder="defects"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="main">
Expand Down
13 changes: 3 additions & 10 deletions src/Rector/ClassMethod/MigrateToSimplifiedAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ public function refactor(Node $node): ?Node
}

/** @var ClassLike $parentClass */
$parentClass = $this->betterNodeFinder->findParentType(
$node,
ClassLike::class
);
$parentClass = $this->betterNodeFinder->findParentType($node, ClassLike::class);

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

// Append the updated attributes assignment statements
$statements[] = new Return_(new Array_(
$attributesAssignmentStatements
));
$statements[] = new Return_(new Array_($attributesAssignmentStatements));

return $statements;
}
Expand Down
40 changes: 30 additions & 10 deletions src/Rector/StaticCall/RequestStaticValidateToInjectRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\Core\NodeManipulator\ClassMethodManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -31,7 +30,6 @@ final class RequestStaticValidateToInjectRector extends AbstractRector
private array $requestObjectTypes = [];

public function __construct(
private readonly ClassMethodManipulator $classMethodManipulator,
private readonly ReflectionResolver $reflectionResolver
) {
$this->requestObjectTypes = [new ObjectType('Illuminate\Http\Request'), new ObjectType('Request')];
Expand Down Expand Up @@ -88,28 +86,27 @@ public function refactor(Node $node): ?Node
return null;
}

$requestName = $this->classMethodManipulator->addMethodParameterIfMissing(
$node,
new ObjectType('Illuminate\Http\Request'),
['request', 'httpRequest']
);
$requestParam = $this->addRequestParameterIfMissing($node, new ObjectType('Illuminate\Http\Request'));

$variable = new Variable($requestName);
if ($requestParam === null) {
return null;
}

$methodName = $this->getName($node->name);

if ($methodName === null) {
return null;
}

if ($node instanceof FuncCall) {
if ($node->args === []) {
return $variable;
return $requestParam->var;
}

$methodName = 'input';
}

return new MethodCall($variable, new Identifier($methodName), $node->args);
return new MethodCall($requestParam->var, new Identifier($methodName), $node->args);
}

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

return ! $this->isName($node, 'request');
}

private function addRequestParameterIfMissing(Node $node, ObjectType $objectType): ?Node\Param
{
$classMethod = $this->betterNodeFinder->findParentType($node, ClassMethod::class);

if (! $classMethod instanceof ClassMethod) {
return null;
}

foreach ($classMethod->params as $paramNode) {
if (! $this->nodeTypeResolver->isObjectType($paramNode, $objectType)) {
continue;
}

return $paramNode;
}

$classMethod->params[] = $paramNode = new Node\Param(new Variable(
'request'
), null, new Node\Name\FullyQualified($objectType->getClassName()));

return $paramNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
namespace RectorLaravel\Tests\Rector\Assign\CallOnAppArrayAccessToStandaloneAssignRector;

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

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

public function provideData(): Iterator
public static function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
namespace RectorLaravel\Tests\Rector\ClassMethod\AddArgumentDefaultValueRector;

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

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

public function provideData(): Iterator
public static function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use RectorLaravel\Rector\ClassMethod\AddArgumentDefaultValueRector;
use RectorLaravel\ValueObject\AddArgumentDefaultValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
namespace RectorLaravel\Tests\Rector\ClassMethod\AddGenericReturnTypeToRelationsRector;

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

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

public function provideData(): Iterator
public static function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
namespace RectorLaravel\Tests\Rector\ClassMethod\AddParentBootToModelClassMethodRector;

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

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

public function provideData(): Iterator
public static function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
namespace RectorLaravel\Tests\Rector\ClassMethod\AddParentRegisterToEventServiceProviderRector;

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

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

public function provideData(): Iterator
public static function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace RectorLaravel\Tests\Rector\ClassMethod\MigrateToSimplifiedAttributeRector;

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

class MigrateToSimplifiedAttributeRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
Expand All @@ -20,9 +19,9 @@ public function test(string $filePath): void
/**
* @return Iterator<string>
*/
public function provideData(): Iterator
public static function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@
namespace RectorLaravel\Tests\Rector\Class_\AddMockConsoleOutputFalseToConsoleTestsRector;

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

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

public function provideData(): Iterator
public static function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
Expand Down
Loading