-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
AddConstructorParentCallRector
(#3430)
* Implement AddConstructorParentCallRector * Move to Strict namespace * Fix bug when params have same name but different type * Fix tests
- Loading branch information
1 parent
9d33ac9
commit f87827c
Showing
10 changed files
with
357 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
.../Rector/ClassMethod/AddConstructorParentCallRector/AddConstructorParentCallRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class AddConstructorParentCallRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
rules-tests/Strict/Rector/ClassMethod/AddConstructorParentCallRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Fixture; | ||
|
||
use Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Source\ParentClassWithConstructor; | ||
|
||
class SunshineCommand extends ParentClassWithConstructor | ||
{ | ||
public function __construct() | ||
{ | ||
$value = 5; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Fixture; | ||
|
||
use Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Source\ParentClassWithConstructor; | ||
|
||
class SunshineCommand extends ParentClassWithConstructor | ||
{ | ||
public function __construct(\stdClass $stdClass) | ||
{ | ||
$value = 5; | ||
parent::__construct($stdClass); | ||
} | ||
} | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...s-tests/Strict/Rector/ClassMethod/AddConstructorParentCallRector/Fixture/fixture2.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Fixture; | ||
|
||
use Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Source\ParentClassWithConstructor; | ||
|
||
class SunshineCommand extends ParentClassWithConstructor | ||
{ | ||
public function __construct(private \stdClass $stdClass) | ||
{ | ||
$value = 5; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Fixture; | ||
|
||
use Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Source\ParentClassWithConstructor; | ||
|
||
class SunshineCommand extends ParentClassWithConstructor | ||
{ | ||
public function __construct(private \stdClass $stdClass) | ||
{ | ||
$value = 5; | ||
parent::__construct($stdClass); | ||
} | ||
} | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...s-tests/Strict/Rector/ClassMethod/AddConstructorParentCallRector/Fixture/fixture3.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Fixture; | ||
|
||
use Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Source\ParentClassWithConstructor; | ||
|
||
class SunshineCommand extends ParentClassWithConstructor | ||
{ | ||
public function __construct(private string $stdClass) | ||
{ | ||
$value = 5; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Fixture; | ||
|
||
use Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Source\ParentClassWithConstructor; | ||
|
||
class SunshineCommand extends ParentClassWithConstructor | ||
{ | ||
public function __construct(\stdClass $stdClass1, private string $stdClass) | ||
{ | ||
$value = 5; | ||
parent::__construct($stdClass1); | ||
} | ||
} | ||
|
||
?> |
15 changes: 15 additions & 0 deletions
15
...Strict/Rector/ClassMethod/AddConstructorParentCallRector/Fixture/skip_already_has.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DependencyInjection\Rector\ClassMethod\AddMethodParentCallRector\Fixture; | ||
|
||
use Rector\Tests\DependencyInjection\Rector\ClassMethod\AddMethodParentCallRector\Source\ParentClassWithNewConstructor; | ||
|
||
class SkipAlreadyHas extends ParentClassWithNewConstructor | ||
{ | ||
public function __construct(\stdClass $stdClass) | ||
{ | ||
$value = 5; | ||
|
||
parent::__construct($stdClass); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...t/Rector/ClassMethod/AddConstructorParentCallRector/Source/ParentClassWithConstructor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\Source; | ||
|
||
class ParentClassWithConstructor | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $defaultValue; | ||
|
||
public function __construct(private \stdClass $stdClass) | ||
{ | ||
$this->defaultValue = 5; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...tests/Strict/Rector/ClassMethod/AddConstructorParentCallRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\Strict\Rector\ClassMethod\AddConstructorParentCallRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(AddConstructorParentCallRector::class); | ||
}; |
123 changes: 123 additions & 0 deletions
123
rules/Strict/Rector/ClassMethod/AddConstructorParentCallRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Strict\Rector\ClassMethod; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassLike; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PHPStan\Analyser\Scope; | ||
use Rector\Core\Enum\ObjectReference; | ||
use Rector\Core\NodeManipulator\Dependency\DependencyClassMethodDecorator; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\ValueObject\MethodName; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* Fixer Rector for PHPStan rule: | ||
* https://github.com/phpstan/phpstan-strict-rules/blob/b7dd96a5503919a43b3cd06a2dced9d4252492f2/src/Rules/Classes/RequireParentConstructCallRule.php | ||
* | ||
* @see \Rector\Tests\Strict\Rector\ClassMethod\AddConstructorParentCallRector\AddConstructorParentCallRectorTest | ||
*/ | ||
final class AddConstructorParentCallRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly DependencyClassMethodDecorator $dependencyClassMethodDecorator, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
$errorMessage = sprintf( | ||
'Fixer for PHPStan reports by strict type rule - "%s"', | ||
'PHPStan\Rules\Classes\RequireParentConstructCallRule' | ||
); | ||
return new RuleDefinition( | ||
$errorMessage, | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SunshineCommand extends ParentClassWithConstructor | ||
{ | ||
public function __construct() | ||
{ | ||
$value = 5; | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SunshineCommand extends ParentClassWithConstructor | ||
{ | ||
public function __construct(ParentDependency $parentDependency) | ||
{ | ||
$value = 5; | ||
parent::__construct($parentDependency); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$class = $this->betterNodeFinder->findParentType($node, ClassLike::class); | ||
if (! $class instanceof Class_) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node, MethodName::CONSTRUCT)) { | ||
return null; | ||
} | ||
|
||
$scope = $node->getAttribute(AttributeKey::SCOPE); | ||
if (! $scope instanceof Scope) { | ||
return null; | ||
} | ||
|
||
if ($this->hasParentCallOfMethod($node)) { | ||
return null; | ||
} | ||
|
||
$this->dependencyClassMethodDecorator->decorateConstructorWithParentDependencies($class, $node, $scope); | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* Looks for "parent::__construct" | ||
*/ | ||
private function hasParentCallOfMethod(ClassMethod $classMethod): bool | ||
{ | ||
return (bool) $this->betterNodeFinder->findFirst((array) $classMethod->stmts, function (Node $node): bool { | ||
if (! $node instanceof StaticCall) { | ||
return false; | ||
} | ||
|
||
if (! $this->isName($node->class, ObjectReference::PARENT)) { | ||
return false; | ||
} | ||
|
||
return $this->isName($node->name, MethodName::CONSTRUCT); | ||
}); | ||
} | ||
} |
Oops, something went wrong.