[Laravel 5.7] Add parent boot rule (#4580)

* move file to templates, to remove confusion in the root

* use stable symplify

* [Laravel] Add AddParentBootToModelClassMethodRector

* [ci-review] Generate Rector & Nodes documentation

* [ci-review] propagate monorepo dependencies

* [ci-review] Rector Rectify

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Tomas Votruba 2020-11-11 17:09:57 +00:00 committed by GitHub
parent 75714bb3d6
commit 4c6da2c4bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 316 additions and 67 deletions

View File

@ -47,13 +47,13 @@
"symfony/finder": "^4.4.8|^5.1",
"symfony/http-kernel": "^4.4.8|^5.1",
"symfony/process": "^4.4.8|^5.1",
"symplify/autowire-array-parameter": "^8.3.48",
"symplify/composer-json-manipulator": "^8.3.48",
"symplify/console-color-diff": "^8.3.48",
"symplify/easy-testing": "^8.3.48",
"symplify/package-builder": "^8.3.48",
"symplify/set-config-resolver": "^8.3.48",
"symplify/smart-file-system": "^8.3.48",
"symplify/autowire-array-parameter": "8.3.48",
"symplify/composer-json-manipulator": "8.3.48",
"symplify/console-color-diff": "8.3.48",
"symplify/easy-testing": "8.3.48",
"symplify/package-builder": "8.3.48",
"symplify/set-config-resolver": "8.3.48",
"symplify/smart-file-system": "8.3.48",
"webmozart/assert": "^1.9"
},
"require-dev": {
@ -67,11 +67,11 @@
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpunit/phpunit": "^8.5|^9.2",
"psr/event-dispatcher": "^1.0",
"symplify/changelog-linker": "^8.3.48",
"symplify/easy-coding-standard": "^8.3.48",
"symplify/easy-testing": "^8.3.48",
"symplify/monorepo-builder": "^8.3.48",
"symplify/phpstan-extensions": "^8.3.48",
"symplify/changelog-linker": "8.3.48",
"symplify/easy-coding-standard": "8.3.48",
"symplify/easy-testing": "8.3.48",
"symplify/monorepo-builder": "8.3.48",
"symplify/phpstan-extensions": "8.3.48",
"tracy/tracy": "^2.7"
},
"replace": {
@ -345,7 +345,6 @@
"sort-packages": true,
"process-timeout": 0
},
"prefer-stable": true,
"extra": {
"patches": {
"nette/application": [

View File

@ -8,54 +8,48 @@ use Rector\Generic\Rector\ClassMethod\ChangeMethodVisibilityRector;
use Rector\Generic\ValueObject\ArgumentAdder;
use Rector\Generic\ValueObject\ArgumentRemover;
use Rector\Generic\ValueObject\ChangeMethodVisibility;
use Rector\Laravel\Rector\ClassMethod\AddParentBootToModelClassMethodRector;
use Rector\Laravel\Rector\StaticCall\Redirect301ToPermanentRedirectRector;
use function Rector\SymfonyPhpConfig\inline_value_objects;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
# see: https://laravel.com/docs/5.7/upgrade
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ChangeMethodVisibilityRector::class)
->call('configure', [[
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => inline_value_objects([
new ChangeMethodVisibility('Illuminate\Routing\Router', 'addRoute', 'public'),
new ChangeMethodVisibility('Illuminate\Contracts\Auth\Access\Gate', 'raw', 'public'),
]),
]]);
$services->set(ArgumentAdderRector::class)
->call('configure', [[
ArgumentAdderRector::ADDED_ARGUMENTS => inline_value_objects([
new ArgumentAdder('Illuminate\Auth\Middleware\Authenticate', 'authenticate', 0, 'request'),
new ArgumentAdder(
'Illuminate\Foundation\Auth\ResetsPasswords',
'sendResetResponse',
0,
'request',
null,
'Illuminate\Http\Illuminate\Http'
),
new ArgumentAdder(
'Illuminate\Foundation\Auth\SendsPasswordResetEmails',
'sendResetLinkResponse',
0,
'request',
null,
'Illuminate\Http\Illuminate\Http'
),
]),
]]);
$services->set(ChangeMethodVisibilityRector::class)->call('configure', [[
ChangeMethodVisibilityRector::METHOD_VISIBILITIES => inline_value_objects([
new ChangeMethodVisibility('Illuminate\Routing\Router', 'addRoute', 'public'),
new ChangeMethodVisibility('Illuminate\Contracts\Auth\Access\Gate', 'raw', 'public'),
]),
]]);
$services->set(ArgumentAdderRector::class)->call('configure', [[
ArgumentAdderRector::ADDED_ARGUMENTS => inline_value_objects([
new ArgumentAdder('Illuminate\Auth\Middleware\Authenticate', 'authenticate', 0, 'request'),
new ArgumentAdder(
'Illuminate\Foundation\Auth\ResetsPasswords',
'sendResetResponse',
0,
'request',
null,
'Illuminate\Http\Illuminate\Http'
),
new ArgumentAdder(
'Illuminate\Foundation\Auth\SendsPasswordResetEmails',
'sendResetLinkResponse',
0,
'request',
null,
'Illuminate\Http\Illuminate\Http'
),
]),
]]);
$services->set(Redirect301ToPermanentRedirectRector::class);
$services->set(ArgumentRemoverRector::class)
->call('configure', [[
ArgumentRemoverRector::REMOVED_ARGUMENTS => inline_value_objects([
new ArgumentRemover('Illuminate\Foundation\Application', 'register', 1, [
'name' => 'options',
]),
$services->set(ArgumentRemoverRector::class)->call('configure', [[
ArgumentRemoverRector::REMOVED_ARGUMENTS => inline_value_objects([
new ArgumentRemover('Illuminate\Foundation\Application', 'register', 1, [
'name' => 'options',
]),
]]);
]),
]]);
$services->set(AddParentBootToModelClassMethodRector::class);
};

View File

@ -1,4 +1,4 @@
# All 604 Rectors Overview
# All 605 Rectors Overview
- [Projects](#projects)
---
@ -23,7 +23,7 @@
- [FileSystemRector](#filesystemrector) (1)
- [Generic](#generic) (34)
- [JMS](#jms) (2)
- [Laravel](#laravel) (3)
- [Laravel](#laravel) (4)
- [Legacy](#legacy) (4)
- [MagicDisclosure](#magicdisclosure) (3)
- [MockeryToProphecy](#mockerytoprophecy) (2)
@ -7061,6 +7061,27 @@ Removes JMS\DiExtraBundle\Annotation\Services annotation
## Laravel
### `AddParentBootToModelClassMethodRector`
- class: [`Rector\Laravel\Rector\ClassMethod\AddParentBootToModelClassMethodRector`](/rules/laravel/src/Rector/ClassMethod/AddParentBootToModelClassMethodRector.php)
- [test fixtures](/rules/laravel/tests/Rector/ClassMethod/AddParentBootToModelClassMethodRector/Fixture)
Add parent::boot(); call to `boot()` class method in child of `Illuminate\Database\Eloquent\Model`
```diff
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function boot()
{
+ parent::boot();
}
}
```
<br><br>
### `MinutesToSecondsInCacheRector`
- class: [`Rector\Laravel\Rector\StaticCall\MinutesToSecondsInCacheRector`](/rules/laravel/src/Rector/StaticCall/MinutesToSecondsInCacheRector.php)

View File

@ -7,11 +7,11 @@
"symfony/dependency-injection": "^4.4.8|^5.1",
"symfony/config": "^4.4.8|^5.1",
"symfony/http-kernel": "^4.4.8|^5.1",
"symplify/package-builder": "^8.3.48"
"symplify/package-builder": "8.3.48"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.2",
"symplify/easy-testing": "^8.3.48"
"symplify/easy-testing": "8.3.48"
},
"autoload": {
"psr-4": {

View File

@ -6,7 +6,7 @@
"php": "^7.2.4|^8.0",
"symfony/dependency-injection": "^4.4.8|^5.1",
"symfony/http-kernel": "^4.4.8|^5.1",
"symplify/package-builder": "^8.3.48"
"symplify/package-builder": "8.3.48"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.2"

View File

@ -0,0 +1,120 @@
<?php
declare(strict_types=1);
namespace Rector\Laravel\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\Nette\NodeAnalyzer\StaticCallAnalyzer;
/**
* @see https://laracasts.com/discuss/channels/laravel/laravel-57-upgrade-observer-problem
*
* @see \Rector\Laravel\Tests\Rector\ClassMethod\AddParentBootToModelClassMethodRector\AddParentBootToModelClassMethodRectorTest
*/
final class AddParentBootToModelClassMethodRector extends AbstractRector
{
/**
* @var string
*/
private const BOOT = 'boot';
/**
* @var StaticCallAnalyzer
*/
private $staticCallAnalyzer;
public function __construct(StaticCallAnalyzer $staticCallAnalyzer)
{
$this->staticCallAnalyzer = $staticCallAnalyzer;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition(
'Add parent::boot(); call to boot() class method in child of Illuminate\Database\Eloquent\Model',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function boot()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function boot()
{
parent::boot();
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isInObjectType($node, 'Illuminate\Database\Eloquent\Model')) {
return null;
}
if (! $this->isName($node->name, self::BOOT)) {
return null;
}
foreach ((array) $node->stmts as $key => $classMethodStmt) {
if ($classMethodStmt instanceof Expression) {
$classMethodStmt = $classMethodStmt->expr;
}
// is in the 1st position? → only correct place
// @see https://laracasts.com/discuss/channels/laravel/laravel-57-upgrade-observer-problem?page=0#reply=454409
if (! $this->staticCallAnalyzer->isParentCallNamed($classMethodStmt, self::BOOT)) {
continue;
}
if ($key === 0) {
return null;
}
// wrong location → remove it
unset($node->stmts[$key]);
}
// missing, we need to add one
$staticCall = $this->nodeFactory->createStaticCall('parent', self::BOOT);
$parentStaticCallExpression = new Expression($staticCall);
$node->stmts = array_merge([$parentStaticCallExpression], (array) $node->stmts);
return $node;
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Rector\Laravel\Tests\Rector\ClassMethod\AddParentBootToModelClassMethodRector;
use Iterator;
use Rector\Laravel\Rector\ClassMethod\AddParentBootToModelClassMethodRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class AddParentBootToModelClassMethodRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return AddParentBootToModelClassMethodRector::class;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Rector\Laravel\Tests\Rector\ClassMethod\AddParentBootToModelClassMethodRector\Fixture;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function boot()
{
}
}
?>
-----
<?php
namespace Rector\Laravel\Tests\Rector\ClassMethod\AddParentBootToModelClassMethodRector\Fixture;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function boot()
{
parent::boot();
}
}
?>

View File

@ -0,0 +1,33 @@
<?php
namespace Rector\Laravel\Tests\Rector\ClassMethod\AddParentBootToModelClassMethodRector\Fixture;
use Illuminate\Database\Eloquent\Model;
class OnTheLastLine extends Model
{
public function boot()
{
$value = 1000;
parent::boot();
}
}
?>
-----
<?php
namespace Rector\Laravel\Tests\Rector\ClassMethod\AddParentBootToModelClassMethodRector\Fixture;
use Illuminate\Database\Eloquent\Model;
class OnTheLastLine extends Model
{
public function boot()
{
parent::boot();
$value = 1000;
}
}
?>

View File

@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Rector\Nette\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\StaticCall;
use Rector\NodeNameResolver\NodeNameResolver;
@ -19,20 +20,24 @@ final class StaticCallAnalyzer
$this->nodeNameResolver = $nodeNameResolver;
}
public function isParentCallNamed(StaticCall $staticCall, string $desiredMethodName): bool
public function isParentCallNamed(Node $node, string $desiredMethodName): bool
{
if ($staticCall->class instanceof Expr) {
if (! $node instanceof StaticCall) {
return false;
}
if (! $this->nodeNameResolver->isName($staticCall->class, 'parent')) {
if ($node->class instanceof Expr) {
return false;
}
if ($staticCall->name instanceof Expr) {
if (! $this->nodeNameResolver->isName($node->class, 'parent')) {
return false;
}
return $this->nodeNameResolver->isName($staticCall->name, $desiredMethodName);
if ($node->name instanceof Expr) {
return false;
}
return $this->nodeNameResolver->isName($node->name, $desiredMethodName);
}
}

View File

@ -45,10 +45,10 @@ final class InitCommand extends AbstractCommand
$rectorConfigFiles = $this->smartFileSystem->exists(getcwd() . '/rector.php');
if (! $rectorConfigFiles) {
$this->smartFileSystem->copy(__DIR__ . '/../../../rector.php.dist', getcwd() . '/rector.php');
$this->symfonyStyle->success('rector.php config file has been generated successfully');
$this->smartFileSystem->copy(__DIR__ . '/../../../templates/rector.php.dist', getcwd() . '/rector.php');
$this->symfonyStyle->success('"rector.php" config file has been generated successfully!');
} else {
$this->symfonyStyle->error('Config file not generated. A rector.php configuration file already exists');
$this->symfonyStyle->error('Config file not generated. A "rector.php" configuration file already exists');
}
return ShellCode::SUCCESS;

View File

@ -425,6 +425,11 @@ final class NodeFactory
return $uses;
}
public function createStaticCall(string $class, string $method): StaticCall
{
return new StaticCall(new Name($class), $method);
}
/**
* @param mixed $item
* @param string|int|null $key

View File

@ -0,0 +1,11 @@
<?php
namespace Illuminate\Database\Eloquent;
if (class_exists('Illuminate\Database\Eloquent\Model')) {
return;
}
abstract class Model
{
}