2019-10-13 07:59:52 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2021-02-09 18:25:16 +01:00
|
|
|
namespace Rector\Privatization\NodeManipulator;
|
2018-07-31 10:54:00 +02:00
|
|
|
|
2021-07-22 23:37:17 +00:00
|
|
|
use PhpParser\Node\Param;
|
2018-07-31 10:54:00 +02:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2018-07-31 11:15:27 +02:00
|
|
|
use PhpParser\Node\Stmt\ClassConst;
|
2018-07-31 10:54:00 +02:00
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
2020-12-24 23:01:30 +01:00
|
|
|
use Rector\Core\ValueObject\Visibility;
|
2021-10-01 11:43:12 +00:00
|
|
|
use RectorPrefix20211001\Webmozart\Assert\Assert;
|
2019-02-27 22:54:39 +01:00
|
|
|
final class VisibilityManipulator
|
2018-07-31 10:54:00 +02:00
|
|
|
{
|
2018-07-31 11:15:27 +02:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Param $node
|
2018-07-31 11:15:27 +02:00
|
|
|
*/
|
2021-07-22 23:37:17 +00:00
|
|
|
public function hasVisibility($node, int $visibility) : bool
|
|
|
|
{
|
|
|
|
return (bool) ($node->flags & $visibility);
|
|
|
|
}
|
2019-02-09 14:22:20 +01:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
|
2019-02-09 14:22:20 +01:00
|
|
|
*/
|
2021-06-29 13:37:16 +00:00
|
|
|
public function makeStatic($node) : void
|
2019-02-09 14:22:20 +01:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->addVisibilityFlag($node, \Rector\Core\ValueObject\Visibility::STATIC);
|
2019-02-09 14:22:20 +01:00
|
|
|
}
|
2019-05-26 13:47:23 +02:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Class_ $node
|
2019-05-26 13:47:23 +02:00
|
|
|
*/
|
2021-06-29 13:37:16 +00:00
|
|
|
public function makeAbstract($node) : void
|
2019-05-26 13:47:23 +02:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->addVisibilityFlag($node, \Rector\Core\ValueObject\Visibility::ABSTRACT);
|
2019-05-26 13:47:23 +02:00
|
|
|
}
|
2020-09-10 09:27:03 +02:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property $node
|
2020-09-10 09:27:03 +02:00
|
|
|
*/
|
2021-06-29 13:37:16 +00:00
|
|
|
public function makeNonStatic($node) : void
|
2020-09-10 09:27:03 +02:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$node->isStatic()) {
|
2020-09-17 09:34:27 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
$node->flags -= \PhpParser\Node\Stmt\Class_::MODIFIER_STATIC;
|
2020-09-10 09:27:03 +02:00
|
|
|
}
|
2019-08-25 12:29:15 +02:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\ClassConst $node
|
2019-08-25 12:29:15 +02:00
|
|
|
*/
|
2021-06-29 14:24:45 +00:00
|
|
|
public function makeFinal($node) : void
|
2019-08-25 12:29:15 +02:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->addVisibilityFlag($node, \Rector\Core\ValueObject\Visibility::FINAL);
|
2019-08-25 12:29:15 +02:00
|
|
|
}
|
2020-12-05 22:33:30 +01:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\ClassMethod $node
|
2020-12-05 22:33:30 +01:00
|
|
|
*/
|
2021-06-29 14:24:45 +00:00
|
|
|
public function makeNonFinal($node) : void
|
2020-12-05 22:33:30 +01:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$node->isFinal()) {
|
2020-12-05 22:33:30 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
$node->flags -= \PhpParser\Node\Stmt\Class_::MODIFIER_FINAL;
|
2020-12-05 22:33:30 +01:00
|
|
|
}
|
2020-11-17 11:45:31 +01:00
|
|
|
/**
|
|
|
|
* This way "abstract", "static", "final" are kept
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
|
2020-11-17 11:45:31 +01:00
|
|
|
*/
|
2021-06-29 14:24:45 +00:00
|
|
|
public function removeVisibility($node) : void
|
2020-11-17 11:45:31 +01:00
|
|
|
{
|
|
|
|
// no modifier
|
|
|
|
if ($node->flags === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($node->isPublic()) {
|
2021-05-10 22:23:08 +00:00
|
|
|
$node->flags -= \PhpParser\Node\Stmt\Class_::MODIFIER_PUBLIC;
|
2020-11-17 11:45:31 +01:00
|
|
|
}
|
|
|
|
if ($node->isProtected()) {
|
2021-05-10 22:23:08 +00:00
|
|
|
$node->flags -= \PhpParser\Node\Stmt\Class_::MODIFIER_PROTECTED;
|
2020-11-17 11:45:31 +01:00
|
|
|
}
|
|
|
|
if ($node->isPrivate()) {
|
2021-05-10 22:23:08 +00:00
|
|
|
$node->flags -= \PhpParser\Node\Stmt\Class_::MODIFIER_PRIVATE;
|
2020-11-17 11:45:31 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-27 22:29:40 +01:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
|
2020-11-27 22:29:40 +01:00
|
|
|
*/
|
2021-06-29 14:24:45 +00:00
|
|
|
public function changeNodeVisibility($node, int $visibility) : void
|
2020-11-27 22:29:40 +01:00
|
|
|
{
|
2021-10-01 11:43:12 +00:00
|
|
|
\RectorPrefix20211001\Webmozart\Assert\Assert::oneOf($visibility, [\Rector\Core\ValueObject\Visibility::PUBLIC, \Rector\Core\ValueObject\Visibility::PROTECTED, \Rector\Core\ValueObject\Visibility::PRIVATE, \Rector\Core\ValueObject\Visibility::STATIC, \Rector\Core\ValueObject\Visibility::ABSTRACT, \Rector\Core\ValueObject\Visibility::FINAL]);
|
2020-12-25 00:18:02 +01:00
|
|
|
$this->replaceVisibilityFlag($node, $visibility);
|
2020-11-27 22:29:40 +01:00
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
|
2020-11-27 22:29:40 +01:00
|
|
|
*/
|
2021-06-29 14:24:45 +00:00
|
|
|
public function makePublic($node) : void
|
2020-11-27 22:29:40 +01:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->replaceVisibilityFlag($node, \Rector\Core\ValueObject\Visibility::PUBLIC);
|
2020-11-27 22:29:40 +01:00
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
|
2020-11-27 22:29:40 +01:00
|
|
|
*/
|
2021-06-29 14:24:45 +00:00
|
|
|
public function makeProtected($node) : void
|
2020-11-27 22:29:40 +01:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->replaceVisibilityFlag($node, \Rector\Core\ValueObject\Visibility::PROTECTED);
|
2020-11-27 22:29:40 +01:00
|
|
|
}
|
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
|
2020-11-27 22:29:40 +01:00
|
|
|
*/
|
2021-06-29 14:24:45 +00:00
|
|
|
public function makePrivate($node) : void
|
2020-11-27 22:29:40 +01:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$this->replaceVisibilityFlag($node, \Rector\Core\ValueObject\Visibility::PRIVATE);
|
2020-11-27 22:29:40 +01:00
|
|
|
}
|
2021-07-22 23:37:17 +00:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\ClassConst $node
|
2021-07-22 23:37:17 +00:00
|
|
|
*/
|
|
|
|
public function removeFinal($node) : void
|
2021-02-08 22:00:45 +01:00
|
|
|
{
|
2021-07-22 23:37:17 +00:00
|
|
|
$node->flags -= \PhpParser\Node\Stmt\Class_::MODIFIER_FINAL;
|
2021-02-08 22:00:45 +01:00
|
|
|
}
|
2021-08-08 19:52:23 +00:00
|
|
|
public function removeAbstract(\PhpParser\Node\Stmt\ClassMethod $classMethod) : void
|
2021-08-08 16:58:46 +00:00
|
|
|
{
|
2021-08-08 19:52:23 +00:00
|
|
|
$classMethod->flags -= \PhpParser\Node\Stmt\Class_::MODIFIER_ABSTRACT;
|
2021-08-08 16:58:46 +00:00
|
|
|
}
|
2018-07-31 11:15:27 +02:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\Property|\PhpParser\Node\Param $node
|
2018-07-31 11:15:27 +02:00
|
|
|
*/
|
2021-07-22 23:37:17 +00:00
|
|
|
public function makeReadonly($node) : void
|
2018-07-31 11:15:27 +02:00
|
|
|
{
|
2021-07-22 23:37:17 +00:00
|
|
|
$this->addVisibilityFlag($node, \PhpParser\Node\Stmt\Class_::MODIFIER_READONLY);
|
2018-07-31 11:15:27 +02:00
|
|
|
}
|
2021-07-22 23:37:17 +00:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst|\PhpParser\Node\Param $node
|
2021-07-22 23:37:17 +00:00
|
|
|
*/
|
|
|
|
private function addVisibilityFlag($node, int $visibility) : void
|
2018-07-31 10:54:00 +02:00
|
|
|
{
|
2021-07-22 23:37:17 +00:00
|
|
|
$node->flags |= $visibility;
|
2018-07-31 10:54:00 +02:00
|
|
|
}
|
2020-11-30 16:51:11 +07:00
|
|
|
/**
|
2021-08-23 00:20:32 +00:00
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Property|\PhpParser\Node\Stmt\ClassConst $node
|
2020-11-30 16:51:11 +07:00
|
|
|
*/
|
2021-06-29 14:24:45 +00:00
|
|
|
private function replaceVisibilityFlag($node, int $visibility) : void
|
2020-11-30 16:51:11 +07:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
$isStatic = $node instanceof \PhpParser\Node\Stmt\ClassMethod && $node->isStatic();
|
2020-12-23 21:28:30 +07:00
|
|
|
if ($isStatic) {
|
2021-01-30 22:41:25 +01:00
|
|
|
$this->removeVisibility($node);
|
2020-12-23 21:28:30 +07:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($visibility !== \Rector\Core\ValueObject\Visibility::STATIC && $visibility !== \Rector\Core\ValueObject\Visibility::ABSTRACT && $visibility !== \Rector\Core\ValueObject\Visibility::FINAL) {
|
2021-01-30 22:41:25 +01:00
|
|
|
$this->removeVisibility($node);
|
2020-11-30 16:51:11 +07:00
|
|
|
}
|
|
|
|
$this->addVisibilityFlag($node, $visibility);
|
2020-12-23 21:28:30 +07:00
|
|
|
if ($isStatic) {
|
|
|
|
$this->makeStatic($node);
|
|
|
|
}
|
2020-11-30 16:51:11 +07:00
|
|
|
}
|
2018-07-31 10:54:00 +02:00
|
|
|
}
|