rector/rules/Strict/Rector/ClassMethod/AddConstructorParentCallRector.php
Tomas Votruba 26bbf728d6 Updated Rector to commit d8da061308902cf4ef7a30963d5c9f32b6b9ec02
d8da061308 Make SimplifyIfNullableReturnRector + ChangeNestedIfsToEarlyReturnRector work without next/prev (#3794)
2023-05-11 11:52:57 +00:00

101 lines
3.3 KiB
PHP

<?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\AbstractScopeAwareRector;
use Rector\Core\ValueObject\MethodName;
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 AbstractScopeAwareRector
{
/**
* @readonly
* @var \Rector\Core\NodeManipulator\Dependency\DependencyClassMethodDecorator
*/
private $dependencyClassMethodDecorator;
public function __construct(DependencyClassMethodDecorator $dependencyClassMethodDecorator)
{
$this->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 refactorWithScope(Node $node, Scope $scope) : ?Node
{
if (!$this->isName($node, MethodName::CONSTRUCT)) {
return null;
}
$classLike = $this->betterNodeFinder->findParentType($node, ClassLike::class);
if (!$classLike instanceof Class_) {
return null;
}
if ($this->hasParentCallOfMethod($node)) {
return null;
}
$this->dependencyClassMethodDecorator->decorateConstructorWithParentDependencies($classLike, $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);
});
}
}