mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-21 18:42:24 +00:00
trait refactoring (#4718)
Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
parent
5c66890e46
commit
dade40765a
|
@ -53,7 +53,15 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
if ($this->isNodeUsedIn($node->keyVar, $node->stmts)) {
|
||||
$keyVar = $node->keyVar;
|
||||
|
||||
$isNodeUsed = (bool) $this->betterNodeFinder->findFirst($node->stmts, function (Node $node) use (
|
||||
$keyVar
|
||||
): bool {
|
||||
return $this->areNodesEqual($node, $keyVar);
|
||||
});
|
||||
|
||||
if ($isNodeUsed) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\ClassConst;
|
|||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\Core\Exception\InvalidNodeTypeException;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
|
||||
final class VisibilityManipulator
|
||||
{
|
||||
|
@ -33,6 +34,11 @@ final class VisibilityManipulator
|
|||
*/
|
||||
private const ABSTRACT = 'abstract';
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private const ALLOWED_VISIBILITIES = ['public', 'protected', 'private', 'static'];
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
|
@ -110,6 +116,51 @@ final class VisibilityManipulator
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 {
|
||||
throw new ShouldNotHappenException(sprintf(
|
||||
'Visibility "%s" is not valid. Use one of: ',
|
||||
implode('", "', self::ALLOWED_VISIBILITIES)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
public function makePublic(Node $node): void
|
||||
{
|
||||
$this->replaceVisibilityFlag($node, 'public');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
public function makeProtected(Node $node): void
|
||||
{
|
||||
$this->replaceVisibilityFlag($node, 'protected');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
public function makePrivate(Node $node): void
|
||||
{
|
||||
$this->replaceVisibilityFlag($node, 'private');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_|ClassMethod|Property|ClassConst $node
|
||||
*/
|
||||
|
|
|
@ -78,22 +78,4 @@ trait BetterStandardPrinterTrait
|
|||
{
|
||||
return $this->betterStandardPrinter->areNodesEqual($firstNode, $secondNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node[] $availableNodes
|
||||
*/
|
||||
protected function isNodeEqual(Node $singleNode, array $availableNodes): bool
|
||||
{
|
||||
return $this->betterStandardPrinter->isNodeEqual($singleNode, $availableNodes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Node|Node[] $nodes
|
||||
*/
|
||||
protected function isNodeUsedIn(Node $seekedNode, $nodes): bool
|
||||
{
|
||||
return (bool) $this->betterNodeFinder->findFirst($nodes, function (Node $node) use ($seekedNode): bool {
|
||||
return $this->areNodesEqual($node, $seekedNode);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ use PhpParser\Node\Stmt\Class_;
|
|||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\PhpParser\Node\Manipulator\VisibilityManipulator;
|
||||
|
||||
/**
|
||||
|
@ -36,22 +35,7 @@ trait VisibilityTrait
|
|||
*/
|
||||
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)
|
||||
));
|
||||
}
|
||||
$this->visibilityManipulator->changeNodeVisibility($node, $visibility);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -75,7 +59,7 @@ trait VisibilityTrait
|
|||
*/
|
||||
public function makePublic(Node $node): void
|
||||
{
|
||||
$this->visibilityManipulator->replaceVisibilityFlag($node, 'public');
|
||||
$this->visibilityManipulator->makePublic($node);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -83,7 +67,7 @@ trait VisibilityTrait
|
|||
*/
|
||||
public function makeProtected(Node $node): void
|
||||
{
|
||||
$this->visibilityManipulator->replaceVisibilityFlag($node, 'protected');
|
||||
$this->visibilityManipulator->makeProtected($node);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -91,7 +75,7 @@ trait VisibilityTrait
|
|||
*/
|
||||
public function makePrivate(Node $node): void
|
||||
{
|
||||
$this->visibilityManipulator->replaceVisibilityFlag($node, 'private');
|
||||
$this->visibilityManipulator->makePrivate($node);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue
Block a user