Skip to content

Commit

Permalink
[CodeQuality] Skip multi params on method on on setValue($a, $b, $c .…
Browse files Browse the repository at this point in the history
…..) on ExplicitMethodCallOverMagicGetSetRector on assign (#1763)
  • Loading branch information
samsonasik committed Feb 3, 2022
1 parent 335206f commit 1090aa6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector\Fixture;

use Rector\Tests\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector\Source\ObjectWithMagicCallsMultiParam;

final class SkipNoMethodOnMultiParamOnAssign
{
public function run(ObjectWithMagicCallsMultiParam $obj)
{
$obj->name = 20;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector\Fixture;

use Rector\Tests\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector\Source\ObjectWithMagicCallsMultiParam;

final class SkipNoMethodOnMultiParamOnAssign2
{
public function run()
{
$obj = new ObjectWithMagicCallsMultiParam();
$obj->name = 20;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\PropertyFetch\ExplicitMethodCallOverMagicGetSetRector\Source;

use Nette\SmartObject;

final class ObjectWithMagicCallsMultiParam
{
// adds magic __get() and __set() methods
use SmartObject;

private $name;

public function getName()
{
return $this->name;
}

public function setName(string $name, string $value)
{
$this->name = $name . $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ResolvedMethodReflection;
use PHPStan\Type\ObjectType;
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;

Expand Down Expand Up @@ -178,6 +182,18 @@ private function refactorMagicSet(Expr $expr, PropertyFetch $propertyFetch): Met
return null;
}

$scope = $propertyFetch->getAttribute(AttributeKey::SCOPE);
if ($scope instanceof Scope) {
$methodReflection = $propertyCallerType->getMethod($setterMethodName, $scope);

if ($methodReflection instanceof ResolvedMethodReflection) {
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
if (count($parametersAcceptor->getParameters()) > 1) {
return null;
}
}
}

return $this->nodeFactory->createMethodCall($propertyFetch->var, $setterMethodName, [$expr]);
}
}

0 comments on commit 1090aa6

Please sign in to comment.