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

[Laravel 5.7] Add parent boot rule #4580

Merged
merged 6 commits into from
Nov 11, 2020
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
25 changes: 12 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down Expand Up @@ -345,7 +345,6 @@
"sort-packages": true,
"process-timeout": 0
},
"prefer-stable": true,
"extra": {
"patches": {
"nette/application": [
Expand Down
76 changes: 35 additions & 41 deletions config/set/laravel57.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,48 @@
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);
};
25 changes: 23 additions & 2 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 604 Rectors Overview
# All 605 Rectors Overview

- [Projects](#projects)
---
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions packages/simple-php-doc-parser/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/symfony-php-config/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

?>
Loading