mirror of
https://github.com/rectorphp/rector.git
synced 2025-01-17 21:38:22 +01:00
add VisibilityTrait
This commit is contained in:
parent
7beababe25
commit
26dc927e31
@ -9,7 +9,6 @@ use PhpParser\Node\Expr\StaticCall;
|
||||
use Rector\NodeTypeResolver\Application\FunctionLikeNodeCollector;
|
||||
use Rector\NodeTypeResolver\Node\Attribute;
|
||||
use Rector\PhpParser\Node\Maintainer\ClassMethodMaintainer;
|
||||
use Rector\PhpParser\Node\Maintainer\VisibilityMaintainer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
@ -28,11 +27,6 @@ final class StaticCallOnNonStaticToInstanceCallRector extends AbstractRector
|
||||
*/
|
||||
private $functionLikeNodeCollector;
|
||||
|
||||
/**
|
||||
* @var VisibilityMaintainer
|
||||
*/
|
||||
private $visibilityMaintainer;
|
||||
|
||||
/**
|
||||
* @var ClassMethodMaintainer
|
||||
*/
|
||||
@ -40,11 +34,9 @@ final class StaticCallOnNonStaticToInstanceCallRector extends AbstractRector
|
||||
|
||||
public function __construct(
|
||||
FunctionLikeNodeCollector $functionLikeNodeCollector,
|
||||
VisibilityMaintainer $visibilityMaintainer,
|
||||
ClassMethodMaintainer $classMethodMaintainer
|
||||
) {
|
||||
$this->functionLikeNodeCollector = $functionLikeNodeCollector;
|
||||
$this->visibilityMaintainer = $visibilityMaintainer;
|
||||
$this->classMethodMaintainer = $classMethodMaintainer;
|
||||
}
|
||||
|
||||
@ -144,7 +136,7 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->visibilityMaintainer->makeStatic($classMethodNode);
|
||||
$this->makeStatic($classMethodNode);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
@ -120,3 +120,5 @@ parameters:
|
||||
# part of test
|
||||
- '#Class Manual\\Twig\\TwigFilter not found#'
|
||||
- '#Class Manual_Twig_Filter not found#'
|
||||
|
||||
- '#Parameter \#1 \$node of method Rector\\PhpParser\\Node\\Maintainer\\VisibilityMaintainer\:\:(.*?)\(\) expects PhpParser\\Node\\Stmt\\ClassConst\|PhpParser\\Node\\Stmt\\ClassMethod\|PhpParser\\Node\\Stmt\\Property, PhpParser\\Node given#'
|
||||
|
@ -24,6 +24,7 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
|
||||
use BetterStandardPrinterTrait;
|
||||
use NodeCommandersTrait;
|
||||
use NodeFactoryTrait;
|
||||
use VisibilityTrait;
|
||||
|
||||
/**
|
||||
* @var AppliedRectorCollector
|
||||
|
@ -4,7 +4,6 @@ namespace Rector\Rector\Visibility;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use Rector\PhpParser\Node\Maintainer\VisibilityMaintainer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\ConfiguredCodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
@ -16,18 +15,12 @@ final class ChangeConstantVisibilityRector extends AbstractRector
|
||||
*/
|
||||
private $constantToVisibilityByClass = [];
|
||||
|
||||
/**
|
||||
* @var VisibilityMaintainer
|
||||
*/
|
||||
private $VisibilityMaintainer;
|
||||
|
||||
/**
|
||||
* @param string[][] $constantToVisibilityByClass
|
||||
*/
|
||||
public function __construct(array $constantToVisibilityByClass, VisibilityMaintainer $VisibilityMaintainer)
|
||||
public function __construct(array $constantToVisibilityByClass)
|
||||
{
|
||||
$this->constantToVisibilityByClass = $constantToVisibilityByClass;
|
||||
$this->VisibilityMaintainer = $VisibilityMaintainer;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
@ -91,7 +84,7 @@ CODE_SAMPLE
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->VisibilityMaintainer->replaceVisibilityFlag($node, $visibility);
|
||||
$this->changeNodeVisibility($node, $visibility);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ namespace Rector\Rector\Visibility;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\NodeTypeResolver\Node\Attribute;
|
||||
use Rector\PhpParser\Node\Maintainer\VisibilityMaintainer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\ConfiguredCodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
@ -17,18 +16,12 @@ final class ChangeMethodVisibilityRector extends AbstractRector
|
||||
*/
|
||||
private $methodToVisibilityByClass = [];
|
||||
|
||||
/**
|
||||
* @var VisibilityMaintainer
|
||||
*/
|
||||
private $visibilityMaintainer;
|
||||
|
||||
/**
|
||||
* @param string[][] $methodToVisibilityByClass
|
||||
*/
|
||||
public function __construct(array $methodToVisibilityByClass, VisibilityMaintainer $visibilityMaintainer)
|
||||
public function __construct(array $methodToVisibilityByClass)
|
||||
{
|
||||
$this->methodToVisibilityByClass = $methodToVisibilityByClass;
|
||||
$this->visibilityMaintainer = $visibilityMaintainer;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
@ -108,16 +101,11 @@ CODE_SAMPLE
|
||||
return null;
|
||||
}
|
||||
|
||||
$newVisibility = $this->resolveNewVisibilityForNode($node, $methodName);
|
||||
$this->visibilityMaintainer->replaceVisibilityFlag($node, $newVisibility);
|
||||
$nodeParentClassName = $node->getAttribute(Attribute::PARENT_CLASS_NAME);
|
||||
$visibility = $this->methodToVisibilityByClass[$nodeParentClassName][$methodName];
|
||||
|
||||
$this->changeNodeVisibility($node, $visibility);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function resolveNewVisibilityForNode(ClassMethod $classMethodNode, string $methodName): string
|
||||
{
|
||||
$nodeParentClassName = $classMethodNode->getAttribute(Attribute::PARENT_CLASS_NAME);
|
||||
|
||||
return $this->methodToVisibilityByClass[$nodeParentClassName][$methodName];
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ namespace Rector\Rector\Visibility;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\NodeTypeResolver\Node\Attribute;
|
||||
use Rector\PhpParser\Node\Maintainer\VisibilityMaintainer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\ConfiguredCodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
@ -17,18 +16,12 @@ final class ChangePropertyVisibilityRector extends AbstractRector
|
||||
*/
|
||||
private $propertyToVisibilityByClass = [];
|
||||
|
||||
/**
|
||||
* @var VisibilityMaintainer
|
||||
*/
|
||||
private $visibilityMaintainer;
|
||||
|
||||
/**
|
||||
* @param string[][] $propertyToVisibilityByClass
|
||||
*/
|
||||
public function __construct(array $propertyToVisibilityByClass, VisibilityMaintainer $VisibilityMaintainer)
|
||||
public function __construct(array $propertyToVisibilityByClass)
|
||||
{
|
||||
$this->propertyToVisibilityByClass = $propertyToVisibilityByClass;
|
||||
$this->visibilityMaintainer = $VisibilityMaintainer;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
@ -97,7 +90,7 @@ CODE_SAMPLE
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->visibilityMaintainer->replaceVisibilityFlag($node, $visibility);
|
||||
$this->changeNodeVisibility($node, $visibility);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
85
src/Rector/VisibilityTrait.php
Normal file
85
src/Rector/VisibilityTrait.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector;
|
||||
|
||||
use PhpParser\Builder\Property;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Exception\ShouldNotHappenException;
|
||||
use Rector\PhpParser\Node\Maintainer\VisibilityMaintainer;
|
||||
|
||||
/**
|
||||
* This could be part of @see AbstractRector, but decopuling to trait
|
||||
* makes clear what code has 1 purpose.
|
||||
*/
|
||||
trait VisibilityTrait
|
||||
{
|
||||
/**
|
||||
* @var VisibilityMaintainer
|
||||
*/
|
||||
private $visibilityMaintainer;
|
||||
|
||||
/**
|
||||
* @required
|
||||
*/
|
||||
public function autowireVisbilityTrait(VisibilityMaintainer $visibilityMaintainer): void
|
||||
{
|
||||
$this->visibilityMaintainer = $visibilityMaintainer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
public function changeNodeVisibility(Node $node, string $visibility): void
|
||||
{
|
||||
if ($visibility === 'public') {
|
||||
$this->makePublic($node);
|
||||
} elseif ($visibility === 'protected') {
|
||||
$this->makeProtected($node);
|
||||
} elseif ($visibility === 'private') {
|
||||
$this->makePrivate($node);
|
||||
} elseif ($visibility === 'static') {
|
||||
$this->makeStatic($node);
|
||||
} else {
|
||||
$allowedVisibilities = ['public', 'protected', 'private', 'static'];
|
||||
|
||||
throw new ShouldNotHappenException(sprintf(
|
||||
'Visibility "%s" is not valid. Use one of: ',
|
||||
implode('", "', $allowedVisibilities)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
public function makePublic(Node $node): void
|
||||
{
|
||||
$this->visibilityMaintainer->replaceVisibilityFlag($node, 'public');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
public function makeProtected(Node $node): void
|
||||
{
|
||||
$this->visibilityMaintainer->replaceVisibilityFlag($node, 'protected');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
public function makePrivate(Node $node): void
|
||||
{
|
||||
$this->visibilityMaintainer->replaceVisibilityFlag($node, 'private');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
public function makeStatic(Node $node): void
|
||||
{
|
||||
$this->visibilityMaintainer->makeStatic($node);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user