Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use Rector\Exception\Rector\InvalidRectorConfigurationException;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\ConfiguredCodeSample;
Expand Down Expand Up @@ -146,6 +147,10 @@ private function processToThis(Node $node): ?MethodCall
return null;
}

if (! $node->class instanceof Name) {
return null;
}

if (! $this->isName($node->class, 'self')) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Class_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Manipulator\ClassMethodManipulator;
Expand Down Expand Up @@ -72,6 +73,10 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $node->class instanceof Name) {
return null;
}

if (! $this->isName($node->class, 'parent')) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
Expand Down Expand Up @@ -85,6 +86,10 @@ private function shouldSkip(Node $node): bool
}

if ($node instanceof StaticCall) {
if (! $node->class instanceof Name) {
return true;
}

if ($this->isName($node->class, 'parent')) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -50,6 +51,10 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $node->class instanceof Name) {
return null;
}

if (! $this->isName($node->class, 'ReflectionFunction')) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -43,6 +44,10 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $node->class instanceof Name) {
return null;
}

if (! $this->isName($node->class, ProcessBuilder::class)) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private function addFilterWithExcludedPaths(Finder $finder): void
$finder->filter(function (SplFileInfo $splFileInfo): bool {
/** @var string|false $realPath */
$realPath = $splFileInfo->getRealPath();
if (!$realPath) {
if (! $realPath) {
//dead symlink
return false;
}
Expand Down
6 changes: 4 additions & 2 deletions src/PhpParser/Node/Resolver/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ public function isName(Node $node, string $name): bool
if ($node instanceof MethodCall) {
$debugBacktrace = debug_backtrace();

$previousCaller = $debugBacktrace[0];
$previousCaller = $debugBacktrace[1];
$fileInfo = new SmartFileInfo($previousCaller['file']);
$location = $fileInfo->getRelativeFilePathFromDirectory(getcwd()) . ':' . $previousCaller['line'];

throw new ShouldNotHappenException(sprintf(
'Cannot get name on "%s" node. Use $node->name instead. Called in: %s',
'Cannot get name on "%s" node. Use "$node->name" or check if "$node->class" is of "%s" type.%sCalled in: %s',
MethodCall::class,
Name::class,
PHP_EOL,
$location
));
}
Expand Down
39 changes: 29 additions & 10 deletions src/Rector/Argument/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -159,19 +160,14 @@ private function processPositionWithDefaultValues(Node $node, array $positionWit
$defaultValue = $parameterConfiguration['default_value'] ?? null;
$type = $parameterConfiguration['type'] ?? null;

if ($this->shouldSkipParameter($node, $position, $name)) {
continue;
}

// is correct scope?
if (! $this->isInCorrectScope($node, $parameterConfiguration)) {
if ($this->shouldSkipParameter($node, $position, $name, $parameterConfiguration)) {
continue;
}

if ($node instanceof ClassMethod) {
$this->addClassMethodParam($node, $name, $defaultValue, $type, $position);
} elseif ($node instanceof StaticCall && $this->isName($node->class, 'parent')) {
$node->args[$position] = new Arg(new Variable($name));
} elseif ($node instanceof StaticCall) {
$this->processStaticCall($node, $position, $name);
} else {
$arg = new Arg(BuilderHelpers::normalizeValue($defaultValue));
$node->args[$position] = $arg;
Expand All @@ -181,16 +177,22 @@ private function processPositionWithDefaultValues(Node $node, array $positionWit

/**
* @param ClassMethod|MethodCall|StaticCall $node
* @param mixed[] $parameterConfiguration
*/
private function shouldSkipParameter(Node $node, int $position, string $name): bool
private function shouldSkipParameter(Node $node, int $position, string $name, array $parameterConfiguration): bool
{
if ($node instanceof ClassMethod) {
// already added?
return isset($node->params[$position]) && $this->isName($node->params[$position], $name);
}

// already added?
return isset($node->args[$position]) && $this->isName($node->args[$position], $name);
if (isset($node->args[$position]) && $this->isName($node->args[$position], $name)) {
return true;
}

// is correct scope?
return ! $this->isInCorrectScope($node, $parameterConfiguration);
}

/**
Expand All @@ -211,6 +213,10 @@ private function isInCorrectScope(Node $node, array $parameterConfiguration): bo
}

if ($node instanceof StaticCall) {
if (! $node->class instanceof Name) {
return false;
}

if ($this->isName($node->class, 'parent')) {
return in_array('parent_call', $scope, true);
}
Expand All @@ -236,4 +242,17 @@ private function addClassMethodParam(

$classMethod->params[$position] = $param;
}

private function processStaticCall(StaticCall $staticCall, int $position, $name): void
{
if (! $staticCall->class instanceof Name) {
return;
}

if (! $this->isName($staticCall->class, 'parent')) {
return;
}

$staticCall->args[$position] = new Arg(new Variable($name));
}
}
4 changes: 4 additions & 0 deletions src/Rector/ClassMethod/AddMethodParentCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ private function hasParentCallOfMethod(ClassMethod $classMethod, string $method)
return false;
}

if (! $node->class instanceof Name) {
return false;
}

if (! $this->isName($node->class, 'parent')) {
return false;
}
Expand Down