mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-16 22:04:54 +01:00
[StaticRemoval] Various fixes (#4245)
This commit is contained in:
parent
9431c8107b
commit
2630e1b900
rules
magic-disclosure/src/Rector/MethodCall
removing-static
src/Rector/Class_
tests/Rector/Class_/SingleStaticServiceToDynamicRector/Fixture
src
@ -134,6 +134,7 @@ abstract class AbstractFluentChainMethodCallRector extends AbstractRector
|
||||
$classOfClassMethod[] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return count(array_unique($classOfClassMethod)) <= 1;
|
||||
}
|
||||
|
||||
|
@ -195,13 +195,18 @@ CODE_SAMPLE
|
||||
|
||||
// is the same class or external call?
|
||||
$className = $this->getName($staticCall->class);
|
||||
|
||||
if ($className === 'self') {
|
||||
return new MethodCall(new Variable(self::THIS), $staticCall->name, $staticCall->args);
|
||||
}
|
||||
|
||||
$propertyName = $this->propertyNaming->fqnToVariableName($classType);
|
||||
$propertyFetch = new PropertyFetch(new Variable(self::THIS), $propertyName);
|
||||
|
||||
$currentMethodName = $staticCall->getAttribute(AttributeKey::METHOD_NAME);
|
||||
if ($currentMethodName === MethodName::CONSTRUCT) {
|
||||
$propertyFetch = new Variable($propertyName);
|
||||
} else {
|
||||
$propertyFetch = new PropertyFetch(new Variable(self::THIS), $propertyName);
|
||||
}
|
||||
return new MethodCall($propertyFetch, $staticCall->name, $staticCall->args);
|
||||
}
|
||||
|
||||
@ -296,10 +301,30 @@ CODE_SAMPLE
|
||||
}
|
||||
|
||||
$propertyExpectedName = $this->propertyNaming->fqnToVariableName(new ObjectType($classType));
|
||||
|
||||
if ($this->isTypeAlreadyInParamMethod($constructClassMethod, $classType)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$constructClassMethod->params[] = new Param(
|
||||
new Variable($propertyExpectedName),
|
||||
null,
|
||||
new FullyQualified($this->classTypes)
|
||||
new FullyQualified($classType)
|
||||
);
|
||||
}
|
||||
|
||||
private function isTypeAlreadyInParamMethod(ClassMethod $classMethod, string $classType): bool
|
||||
{
|
||||
foreach ($classMethod->getParams() as $param) {
|
||||
if ($param->type === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($this->isName($param->type, $classType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
31
rules/removing-static/tests/Rector/Class_/SingleStaticServiceToDynamicRector/Fixture/construct_already_there_dependency.php.inc
Normal file
31
rules/removing-static/tests/Rector/Class_/SingleStaticServiceToDynamicRector/Fixture/construct_already_there_dependency.php.inc
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\RemovingStatic\Tests\Rector\Class_\SingleStaticServiceToDynamicRector\Fixture;
|
||||
|
||||
use Rector\RemovingStatic\Tests\Rector\Class_\SingleStaticServiceToDynamicRector\Source\FirstStaticClass;
|
||||
|
||||
class ConstructAlreadyThereDependency
|
||||
{
|
||||
public function __construct(FirstStaticClass $firstStaticClass)
|
||||
{
|
||||
FirstStaticClass::someStaticFunction();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\RemovingStatic\Tests\Rector\Class_\SingleStaticServiceToDynamicRector\Fixture;
|
||||
|
||||
use Rector\RemovingStatic\Tests\Rector\Class_\SingleStaticServiceToDynamicRector\Source\FirstStaticClass;
|
||||
|
||||
class ConstructAlreadyThereDependency
|
||||
{
|
||||
public function __construct(FirstStaticClass $firstStaticClass)
|
||||
{
|
||||
$firstStaticClass->someStaticFunction();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -50,10 +50,14 @@ final class VisibilityManipulator
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
* @param ClassMethod|Property $node
|
||||
*/
|
||||
public function makeNonStatic(Node $node): void
|
||||
{
|
||||
if (! $node->isStatic()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$node->flags -= Class_::MODIFIER_STATIC;
|
||||
}
|
||||
|
||||
|
@ -95,7 +95,7 @@ trait VisibilityTrait
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
* @param ClassMethod|Property $node
|
||||
*/
|
||||
public function makeNonStatic(Node $node): void
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user