Skip to content

Commit

Permalink
[CodeQuality] Skip ExplicitMethodCallOverMagicGetSetRector on method …
Browse files Browse the repository at this point in the history
…no param + variadic (#1768)

* [CodeQuality] Skip ExplicitMethodCallOverMagicGetSetRector on method no param

* [CodeQuality] Skip ExplicitMethodCallOverMagicGetSetRector on method no param

* also check variadic

* phpstan
  • Loading branch information
samsonasik committed Feb 5, 2022
1 parent f374b9f commit fd7667b
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 10 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\ObjectWithMagicCallsNoParam;

final class SkipNoMethodNoParamOnAssign
{
public function run(ObjectWithMagicCallsNoParam $obj)
{
$obj->name = 20;
}
}
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\ObjectWithMagicCallsVariadicParam;

final class SkipNoMethodVariadicParamOnAssign
{
public function run(ObjectWithMagicCallsVariadicParam $obj)
{
$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 ObjectWithMagicCallsNoParam
{
// adds magic __get() and __set() methods
use SmartObject;

private $name;

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

public function setName()
{
$this->name = 'test';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

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

use Nette\SmartObject;

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

private $name;

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

public function setName(...$params)
{
if (isset($params[1])) {
$this->name = $params[0] . $params[1];
return;
}

$this->name = current($params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,35 @@ 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;
}
}
if ($this->hasNoParamOrVariadic($propertyCallerType, $propertyFetch, $setterMethodName)) {
return null;
}

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

private function hasNoParamOrVariadic(
ObjectType $objectType,
PropertyFetch $propertyFetch,
string $setterMethodName
): bool {
$scope = $propertyFetch->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}

$methodReflection = $objectType->getMethod($setterMethodName, $scope);

if (! $methodReflection instanceof ResolvedMethodReflection) {
return false;
}

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
$parameters = $parametersAcceptor->getParameters();
if (count($parameters) !== 1) {
return true;
}

return $parameters[0]->isVariadic();
}
}

0 comments on commit fd7667b

Please sign in to comment.