Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeQuality] Skip ExplicitMethodCallOverMagicGetSetRector on method no param + variadic #1768

Merged
merged 4 commits into from
Feb 5, 2022
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
@@ -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();
}
}