Skip to content

Commit

Permalink
[Php80] Add AddParamBasedOnParentClassMethodRector (#1431)
Browse files Browse the repository at this point in the history
* [Php80] Add AddParamBasedOnParentClassMethodRector

* use ParentClassMethodTypeOverrideGuard to get parent MethodReflection

* add test

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* register to php80 config set

* phpstan

* try compare

* skip __construct, it is allowed to have different parameter

* [ci-review] Rector Rectify

* Fixed

* move parent to Source dir

* skip construct test

* skip no parent method test

* skip parent with private method tset

* skip same params

* skip child more params

* eol

* eol

* do not change same param different name

* eol

* add failing fixture for skip duplicated previous param

* skip duplicated next param next param name

* clean up

* clean up

* final touch: skip used in stmts, eg, in coalesce

* add findFirstInFunctionLikeScoped in BetterNodeFinder to look node in scoped

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

* final touch: check nearest FunctionLike for ClassMethod, Function_ or Closure for parent to check scope

* [ci-review] Rector Rectify

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Dec 9, 2021
1 parent c26d6cc commit c445b5a
Show file tree
Hide file tree
Showing 21 changed files with 482 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/set/php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Php80\Rector\Class_\StringableForToStringRector;
use Rector\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector;
use Rector\Php80\Rector\ClassMethod\FinalPrivateToPrivateVisibilityRector;
use Rector\Php80\Rector\ClassMethod\OptionalParametersAfterRequiredRector;
use Rector\Php80\Rector\ClassMethod\SetStateToStaticRector;
Expand Down Expand Up @@ -100,4 +101,5 @@
]);

$services->set(Php8ResourceReturnToObjectRector::class);
$services->set(AddParamBasedOnParentClassMethodRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function hasParentClassMethodDifferentType(ClassMethod $classMethod, int
return $inferedType::class !== $currentType::class;
}

private function getParentClassMethod(ClassMethod $classMethod): ?MethodReflection
public function getParentClassMethod(ClassMethod $classMethod): ?MethodReflection
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class AddParamBasedOnParentClassMethodRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\MultiParams;

class DoNotChangeSameParamDifferentName extends MultiParams{
public function execute($x)
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\MultiParams;

class DoNotChangeSameParamDifferentName extends MultiParams{
public function execute($x, $bar)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParam;

class B extends ParentWithParam{
public function execute()
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParam;

class B extends ParentWithParam{
public function execute($foo)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParam;

// ref https://3v4l.org/op5lE
class SkipChildMoreParams extends ParentWithParam{
public function execute($foo, $bar)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithConstruct;

class SkipConstruct extends ParentWithConstruct
{
public function __construct()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\MultiParams;

/**
* @bar, $bar will cause error Fatal error: Redefinition of parameter $bar
*
* @see https://3v4l.org/JaX5m
*/
class SkipDuplicatedPreviousParam extends MultiParams{
public function execute($bar)
{
echo $bar;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

class SkipNoParentMethod
{
public function execute()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithPrivateMethod;

class SkipParentWithPrivateMethod extends ParentWithPrivateMethod
{
public function execute()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParam;

class SkipSameParam extends ParentWithParam{
public function execute($foo)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParam;

class SkipUsedInCoalesce extends ParentWithParam{
public function execute()
{
echo $foo ?? 'test';
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParam;

class UsedButInClosure extends ParentWithParam{
public function execute()
{
$x = function () {
echo $foo ?? 'test';
};
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParam;

class UsedButInClosure extends ParentWithParam{
public function execute($foo)
{
$x = function () {
echo $foo ?? 'test';
};
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source;

class MultiParams
{
public function execute($foo, $bar)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source;

class ParentWithConstruct
{
public function __construct($foo)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source;

class ParentWithParam
{
public function execute($foo)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source;

class ParentWithPrivateMethod
{
private function execute($foo)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(AddParamBasedOnParentClassMethodRector::class);
};
Loading

0 comments on commit c445b5a

Please sign in to comment.