Skip to content

Commit

Permalink
Bump to Rector 1.2.5 and fix compatible of isObjectType() usage to us…
Browse files Browse the repository at this point in the history
…e ClassLike instead of ClassMethod (#247)

* Bump to Rector 1.2.5 and fix compatible of isObjectType() usage on ClassLike

* cs fix
  • Loading branch information
samsonasik authored Sep 9, 2024
1 parent ca45e6d commit 3ec89de
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 48 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Rector upgrades rules for Laravel Framework",
"require": {
"php": ">=8.2",
"rector/rector": "^1.2"
"rector/rector": "^1.2.5"
},
"require-dev": {
"nikic/php-parser": "^4.18",
Expand Down
45 changes: 27 additions & 18 deletions src/Rector/ClassMethod/AddArgumentDefaultValueRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\ClassLike;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use RectorLaravel\ValueObject\AddArgumentDefaultValue;
Expand Down Expand Up @@ -70,40 +70,49 @@ public function someMethod($value = false)
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
return [ClassLike::class];
}

/**
* @param ClassMethod $node
* @param ClassLike $node
*/
public function refactor(Node $node): ClassMethod
public function refactor(Node $node): ?ClassLike
{
$hasChanged = false;
foreach ($this->addedArguments as $addedArgument) {
if (! $this->nodeTypeResolver->isObjectType($node, $addedArgument->getObjectType())) {
continue;
}

if (! $this->isName($node->name, $addedArgument->getMethod())) {
continue;
}
foreach ($node->getMethods() as $classMethod) {
if (! $this->isName($classMethod->name, $addedArgument->getMethod())) {
continue;
}

if (! isset($node->params[$addedArgument->getPosition()])) {
continue;
}
if (! isset($classMethod->params[$addedArgument->getPosition()])) {
continue;
}

$position = $addedArgument->getPosition();
$param = $node->params[$position];
$position = $addedArgument->getPosition();
$param = $classMethod->params[$position];

if ($param->default instanceof Expr) {
continue;
if ($param->default instanceof Expr) {
continue;
}

$classMethod->params[$position] = new Param($param->var, BuilderHelpers::normalizeValue(
$addedArgument->getDefaultValue()
));

$hasChanged = true;
}
}

$node->params[$position] = new Param($param->var, BuilderHelpers::normalizeValue(
$addedArgument->getDefaultValue()
));
if ($hasChanged) {
return $node;
}

return $node;
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeTraverser;
use PHPStan\Type\ObjectType;
Expand Down Expand Up @@ -48,15 +48,15 @@ public function getRuleDefinition(): RuleDefinition

public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class, ClassMethod::class];
return [MethodCall::class, StaticCall::class, ClassLike::class];
}

/**
* @param MethodCall|StaticCall|ClassMethod $node
* @param MethodCall|StaticCall|ClassLike $node
*/
public function refactor(Node $node): MethodCall|StaticCall|ClassMethod|null
public function refactor(Node $node): MethodCall|StaticCall|ClassLike|null
{
if ($node instanceof ClassMethod) {
if ($node instanceof ClassLike) {
return $this->refactorClassMethod($node);
}

Expand Down Expand Up @@ -122,42 +122,47 @@ private function refactorCall(StaticCall|MethodCall $node): StaticCall|MethodCal
return $this->processValidationRules($rulesArgument) ? $node : null;
}

private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
private function refactorClassMethod(ClassLike $classLike): ?ClassLike
{
if (! $this->isObjectType($classMethod, new ObjectType('Illuminate\Foundation\Http\FormRequest'))) {
if (! $this->isObjectType($classLike, new ObjectType('Illuminate\Foundation\Http\FormRequest'))) {
return null;
}

if (! $this->isName($classMethod, 'rules')) {
return null;
}
$hasChanged = false;
foreach ($classLike->getMethods() as $classMethod) {
if (! $this->isName($classMethod, 'rules')) {
continue;
}

$changed = false;
$changed = false;
$this->traverseNodesWithCallable($classMethod, function (Node $node) use (&$changed, &$hasChanged): Return_|int|null {
if ($changed) {
$hasChanged = true;

$this->traverseNodesWithCallable($classMethod, function (Node $node) use (&$changed): Return_|int|null {
if ($changed) {
return NodeTraverser::STOP_TRAVERSAL;
}
return NodeTraverser::STOP_TRAVERSAL;
}

if (! $node instanceof Return_) {
return null;
}
if (! $node instanceof Return_) {
return null;
}

if (! $node->expr instanceof Array_) {
return null;
}
if (! $node->expr instanceof Array_) {
return null;
}

if ($this->processValidationRules($node->expr)) {
$changed = true;
if ($this->processValidationRules($node->expr)) {
$hasChanged = true;
$changed = true;

return $node;
}
return $node;
}

return null;
});
return null;
});
}

if ($changed) {
return $classMethod;
if ($hasChanged) {
return $classLike;
}

return null;
Expand Down

0 comments on commit 3ec89de

Please sign in to comment.