[Order] Cleanup set from rules that are nicely handled by php-cs-fixer (#245)

This commit is contained in:
Tomas Votruba 2021-06-18 20:01:14 +02:00 committed by GitHub
parent dce60231f7
commit c251806319
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 0 additions and 1263 deletions

View File

@ -2,18 +2,10 @@
declare(strict_types=1);
use Rector\Order\Rector\Class_\OrderClassConstantsByIntegerValueRector;
use Rector\Order\Rector\Class_\OrderConstantsByVisibilityRector;
use Rector\Order\Rector\Class_\OrderMethodsByVisibilityRector;
use Rector\Order\Rector\Class_\OrderPrivateMethodsByUseRector;
use Rector\Order\Rector\Class_\OrderPropertiesByVisibilityRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(OrderPrivateMethodsByUseRector::class);
$services->set(OrderClassConstantsByIntegerValueRector::class);
$services->set(OrderMethodsByVisibilityRector::class);
$services->set(OrderPropertiesByVisibilityRector::class);
$services->set(OrderConstantsByVisibilityRector::class);
};

View File

@ -1,28 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderClassConstantsByIntegerValueRector\Fixture;
class Fixture
{
const MODE_ON = 100;
const MODE_OFF = 250;
const MODE_MAYBE = 125;
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderClassConstantsByIntegerValueRector\Fixture;
class Fixture
{
const MODE_ON = 100;
const MODE_MAYBE = 125;
const MODE_OFF = 250;
}
?>

View File

@ -1,28 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderClassConstantsByIntegerValueRector\Fixture;
class FromZero
{
const MODE_ON = 100;
const MODE_OFF = 0;
const MODE_MAYBE = 125;
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderClassConstantsByIntegerValueRector\Fixture;
class FromZero
{
const MODE_OFF = 0;
const MODE_ON = 100;
const MODE_MAYBE = 125;
}
?>

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Order\Rector\Class_\OrderClassConstantsByIntegerValueRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class OrderClassConstantsByIntegerValueRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
use Rector\Order\Rector\Class_\OrderClassConstantsByIntegerValueRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(OrderClassConstantsByIntegerValueRector::class);
};

View File

@ -1,25 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderConstantsByVisibilityRector\Fixture;
abstract class AbstractClass
{
private const PRIVATE_CONST = 'private';
protected const PROTECTED_CONST = 'protected';
public const PUBLIC_CONST = 'public';
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderConstantsByVisibilityRector\Fixture;
abstract class AbstractClass
{
public const PUBLIC_CONST = 'public';
protected const PROTECTED_CONST = 'protected';
private const PRIVATE_CONST = 'private';
}
?>

View File

@ -1,25 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderConstantsByVisibilityRector\Fixture;
final class Fixture
{
private const PRIVATE_CONST = 'private';
protected const PROTECTED_CONST = 'protected';
public const PUBLIC_CONST = 'public';
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderConstantsByVisibilityRector\Fixture;
final class Fixture
{
public const PUBLIC_CONST = 'public';
protected const PROTECTED_CONST = 'protected';
private const PRIVATE_CONST = 'private';
}
?>

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Order\Rector\Class_\OrderConstantsByVisibilityRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class OrderConstantsByVisibilityRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
use Rector\Order\Rector\Class_\OrderConstantsByVisibilityRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(OrderConstantsByVisibilityRector::class);
};

View File

@ -1,39 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\Fixture;
use Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\Source\TraitName;
use Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\Source\TraitName2;
final class SomeClass
{
public function functionName()
{
}
protected $propertyName;
use TraitName;
private const CONST_NAME = 'constant_value';
use TraitName2;
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\Fixture;
use Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\Source\TraitName;
use Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\Source\TraitName2;
final class SomeClass
{
use TraitName;
use TraitName2;
private const CONST_NAME = 'constant_value';
protected $propertyName;
public function functionName()
{
}
}
?>

View File

@ -1,29 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\Fixture;
final class SameNames
{
public function name()
{
}
protected $name;
protected $anotherName;
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\Fixture;
final class SameNames
{
protected $name;
protected $anotherName;
public function name()
{
}
}
?>

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class OrderFirstLevelClassStatementsRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -1,10 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\Source;
trait TraitName
{
}

View File

@ -1,10 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\Source;
trait TraitName2
{
}

View File

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
use Rector\Order\Rector\Class_\OrderFirstLevelClassStatementsRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(OrderFirstLevelClassStatementsRector::class);
};

View File

@ -1,58 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderMethodsByVisibilityRector\Fixture;
abstract class AbstractClass
{
abstract protected function abstractProtectedFunctionName();
public function publicFunctionName()
{
}
abstract public function abstractPublicFunctionName();
abstract static public function abstractStaticPublicFunctionName();
private function privateFunctionName()
{
}
abstract static protected function abstractStaticProtectedFunctionName();
protected function protectedFunctionName()
{
}
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderMethodsByVisibilityRector\Fixture;
abstract class AbstractClass
{
abstract public function abstractPublicFunctionName();
public function publicFunctionName()
{
}
abstract static public function abstractStaticPublicFunctionName();
abstract protected function abstractProtectedFunctionName();
protected function protectedFunctionName()
{
}
abstract static protected function abstractStaticProtectedFunctionName();
private function privateFunctionName()
{
}
}
?>

View File

@ -1,91 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderMethodsByVisibilityRector\Fixture;
class Fixture
{
protected function protectedFunctionName()
{
}
private function privateFunctionName()
{
}
final protected function finalProtectedFunctionName()
{
}
static final protected function staticFinalProtectedFunctionName()
{
}
public function publicFunctionName()
{
}
final private function finalPrivateFunctionName()
{
}
static final public function staticFinalPublicFunctionName()
{
}
static protected function staticProtectedFunctionName()
{
}
static private function staticPrivateFunctionName()
{
}
static public function staticPublicFunctionName()
{
}
final public function finalPublicFunctionName()
{
}
static final private function staticFinalPrivateFunctionName()
{
}
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderMethodsByVisibilityRector\Fixture;
class Fixture
{
public function publicFunctionName()
{
}
final public function finalPublicFunctionName()
{
}
static public function staticPublicFunctionName()
{
}
static final public function staticFinalPublicFunctionName()
{
}
protected function protectedFunctionName()
{
}
final protected function finalProtectedFunctionName()
{
}
static protected function staticProtectedFunctionName()
{
}
static final protected function staticFinalProtectedFunctionName()
{
}
private function privateFunctionName()
{
}
final private function finalPrivateFunctionName()
{
}
static private function staticPrivateFunctionName()
{
}
static final private function staticFinalPrivateFunctionName()
{
}
}
?>

View File

@ -1,58 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderMethodsByVisibilityRector\Fixture;
class MagicPhpunitPreferredOrder
{
protected function protectedFunctionName()
{
}
public function tearDown()
{
}
public function publicFunctionName()
{
}
public function __construct()
{
}
static private function staticPrivateFunctionName()
{
}
public function __get($argument)
{
}
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderMethodsByVisibilityRector\Fixture;
class MagicPhpunitPreferredOrder
{
public function __construct()
{
}
public function __get($argument)
{
}
public function tearDown()
{
}
public function publicFunctionName()
{
}
protected function protectedFunctionName()
{
}
static private function staticPrivateFunctionName()
{
}
}
?>

View File

@ -1,35 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Order\Rector\Class_\OrderMethodsByVisibilityRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class OrderMethodsByVisibilityRectorTest extends AbstractRectorTestCase
{
/**
* Final + private method breaks :)
* @requires PHP < 8.0
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
use Rector\Order\Rector\Class_\OrderMethodsByVisibilityRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(OrderMethodsByVisibilityRector::class);
};

View File

@ -1,31 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderPropertiesByVisibilityRector\Fixture;
abstract class AbstractClass
{
protected $protectedProperty;
public $publicProperty;
protected static $protectedStaticProperty;
private $privateProperty;
public static $publicStaticProperty;
private static $privateStaticProperty;
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderPropertiesByVisibilityRector\Fixture;
abstract class AbstractClass
{
public $publicProperty;
public static $publicStaticProperty;
protected $protectedProperty;
protected static $protectedStaticProperty;
private $privateProperty;
private static $privateStaticProperty;
}
?>

View File

@ -1,31 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderPropertiesByVisibilityRector\Fixture;
final class Fixture
{
protected $protectedProperty;
public $publicProperty;
protected static $protectedStaticProperty;
private $privateProperty;
public static $publicStaticProperty;
private static $privateStaticProperty;
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderPropertiesByVisibilityRector\Fixture;
final class Fixture
{
public $publicProperty;
public static $publicStaticProperty;
protected $protectedProperty;
protected static $protectedStaticProperty;
private $privateProperty;
private static $privateStaticProperty;
}
?>

View File

@ -1,31 +0,0 @@
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderPropertiesByVisibilityRector\Fixture;
trait TraitClass
{
protected $protectedProperty;
public $publicProperty;
protected static $protectedStaticProperty;
private $privateProperty;
public static $publicStaticProperty;
private static $privateStaticProperty;
}
?>
-----
<?php
namespace Rector\Tests\Order\Rector\Class_\OrderPropertiesByVisibilityRector\Fixture;
trait TraitClass
{
public $publicProperty;
public static $publicStaticProperty;
protected $protectedProperty;
protected static $protectedStaticProperty;
private $privateProperty;
private static $privateStaticProperty;
}
?>

View File

@ -1,33 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\Order\Rector\Class_\OrderPropertiesByVisibilityRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class OrderPropertiesByVisibilityRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -1,11 +0,0 @@
<?php
declare(strict_types=1);
use Rector\Order\Rector\Class_\OrderPropertiesByVisibilityRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(OrderPropertiesByVisibilityRector::class);
};

View File

@ -1,141 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Order\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use Rector\Core\Rector\AbstractRector;
use Rector\Order\Order\OrderChangeAnalyzer;
use Rector\Order\StmtOrder;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Order\Rector\Class_\OrderClassConstantsByIntegerValueRector\OrderClassConstantsByIntegerValueRectorTest
*/
final class OrderClassConstantsByIntegerValueRector extends AbstractRector
{
public function __construct(
private OrderChangeAnalyzer $orderChangeAnalyzer,
private StmtOrder $stmtOrder
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Order class constant order by their integer value',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
const MODE_ON = 0;
const MODE_OFF = 2;
const MODE_MAYBE = 1;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
const MODE_ON = 0;
const MODE_MAYBE = 1;
const MODE_OFF = 2;
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$numericClassConstsByKey = $this->resolveClassConstByPosition($node);
if ($numericClassConstsByKey === []) {
return null;
}
$classConstConstsByValue = $this->resolveClassConstConstByUniqueValue($numericClassConstsByKey);
$sortedClassConstConstsByValue = $classConstConstsByValue;
asort($sortedClassConstConstsByValue);
$oldToNewKeys = $this->stmtOrder->createOldToNewKeys($sortedClassConstConstsByValue, $classConstConstsByValue);
if (! $this->orderChangeAnalyzer->hasOrderChanged($oldToNewKeys)) {
return null;
}
$this->stmtOrder->reorderClassStmtsByOldToNewKeys($node, $oldToNewKeys);
return $node;
}
/**
* @return ClassConst[]
*/
private function resolveClassConstByPosition(Class_ $class): array
{
$classConstConstsByValue = [];
foreach ($class->stmts as $key => $classStmt) {
if (! $classStmt instanceof ClassConst) {
continue;
}
if (count($classStmt->consts) !== 1) {
continue;
}
$classConstConst = $classStmt->consts[0];
if (! $classConstConst->value instanceof LNumber) {
continue;
}
$classConstConstsByValue[$key] = $classStmt;
}
return $classConstConstsByValue;
}
/**
* @param array<int, ClassConst> $numericClassConstsByKey
* @return array<int, string>
*/
private function resolveClassConstConstByUniqueValue(array $numericClassConstsByKey): array
{
$classConstConstsByValue = [];
foreach ($numericClassConstsByKey as $position => $numericClassConst) {
$constantValue = $this->valueResolver->getValue($numericClassConst->consts[0]->value);
$classConstConstsByValue[$position] = $constantValue;
}
$arrayCountValue = array_count_values($classConstConstsByValue);
// work only with unique constants
foreach ($classConstConstsByValue as $position => $constantValue) {
if ($arrayCountValue[$constantValue] > 1) {
unset($classConstConstsByValue[$position]);
}
}
return $classConstConstsByValue;
}
}

View File

@ -1,80 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Order\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use Rector\Core\Rector\AbstractRector;
use Rector\Order\Order\OrderChangeAnalyzer;
use Rector\Order\StmtOrder;
use Rector\Order\StmtVisibilitySorter;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Order\Rector\Class_\OrderConstantsByVisibilityRector\OrderConstantsByVisibilityRectorTest
*/
final class OrderConstantsByVisibilityRector extends AbstractRector
{
public function __construct(
private OrderChangeAnalyzer $orderChangeAnalyzer,
private StmtOrder $stmtOrder,
private StmtVisibilitySorter $stmtVisibilitySorter
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Orders constants by visibility', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
private const PRIVATE_CONST = 'private';
protected const PROTECTED_CONST = 'protected';
public const PUBLIC_CONST = 'public';
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public const PUBLIC_CONST = 'public';
protected const PROTECTED_CONST = 'protected';
private const PRIVATE_CONST = 'private';
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$currentClassConstsOrder = $this->stmtOrder->getStmtsOfTypeOrder($node, ClassConst::class);
$classConstsInDesiredOrder = $this->stmtVisibilitySorter->sortConstants($node);
$oldToNewKeys = $this->stmtOrder->createOldToNewKeys($classConstsInDesiredOrder, $currentClassConstsOrder);
if (! $this->orderChangeAnalyzer->hasOrderChanged($oldToNewKeys)) {
return null;
}
$this->stmtOrder->reorderClassStmtsByOldToNewKeys($node, $oldToNewKeys);
return $node;
}
}

View File

@ -1,107 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Order\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Order\Rector\Class_\OrderFirstLevelClassStatementsRector\OrderFirstLevelClassStatementsRectorTest
*/
final class OrderFirstLevelClassStatementsRector extends AbstractRector
{
/**
* @var array<string, int>
*/
private const TYPE_TO_RANK = [
ClassMethod::class => 3,
Property::class => 2,
ClassConst::class => 1,
];
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Orders first level Class statements', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function functionName();
protected $propertyName;
private const CONST_NAME = 'constant_value';
use TraitName;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
use TraitName;
private const CONST_NAME = 'constant_value';
protected $propertyName;
public function functionName();
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class, Trait_::class];
}
/**
* @param Class_|Trait_ $node
*/
public function refactor(Node $node): ?Node
{
$node->stmts = $this->getStmtsInDesiredPosition($node->stmts);
return $node;
}
/**
* @param Stmt[] $stmts
* @return Stmt[]
*/
private function getStmtsInDesiredPosition(array $stmts): array
{
uasort(
$stmts,
fn (Stmt $firstStmt, Stmt $secondStmt): int => [
$this->resolveClassElementRank($firstStmt),
$firstStmt->getLine(),
]
<=> [$this->resolveClassElementRank($secondStmt), $secondStmt->getLine()]
);
return $stmts;
}
private function resolveClassElementRank(Stmt $stmt): int
{
foreach (self::TYPE_TO_RANK as $type => $rank) {
if (is_a($stmt, $type, true)) {
return $rank;
}
}
// TraitUse
return 0;
}
}

View File

@ -1,129 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Order\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Trait_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Order\Order\OrderChangeAnalyzer;
use Rector\Order\StmtOrder;
use Rector\Order\StmtVisibilitySorter;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Order\Rector\Class_\OrderMethodsByVisibilityRector\OrderMethodsByVisibilityRectorTest
*/
final class OrderMethodsByVisibilityRector extends AbstractRector
{
/**
* @var string[]
*/
private const PREFERRED_ORDER = [
MethodName::CONSTRUCT,
MethodName::DESCTRUCT,
'__call',
'__callStatic',
'__get',
'__set',
'__isset',
'__unset',
'__sleep',
'__wakeup',
'__serialize',
'__unserialize',
'__toString',
'__invoke',
MethodName::SET_STATE,
MethodName::CLONE,
'setUpBeforeClass',
'tearDownAfterClass',
MethodName::SET_UP,
MethodName::TEAR_DOWN,
];
public function __construct(
private OrderChangeAnalyzer $orderChangeAnalyzer,
private StmtOrder $stmtOrder,
private StmtVisibilitySorter $stmtVisibilitySorter
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Orders method by visibility', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
protected function protectedFunctionName();
private function privateFunctionName();
public function publicFunctionName();
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function publicFunctionName();
protected function protectedFunctionName();
private function privateFunctionName();
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class, Trait_::class];
}
/**
* @param Class_|Trait_ $node
*/
public function refactor(Node $node): ?Node
{
$currentMethodsOrder = $this->stmtOrder->getStmtsOfTypeOrder($node, ClassMethod::class);
$methodsInDesiredOrder = $this->getMethodsInDesiredOrder($node);
$oldToNewKeys = $this->stmtOrder->createOldToNewKeys($methodsInDesiredOrder, $currentMethodsOrder);
// nothing to re-order
if (! $this->orderChangeAnalyzer->hasOrderChanged($oldToNewKeys)) {
return null;
}
$this->stmtOrder->reorderClassStmtsByOldToNewKeys($node, $oldToNewKeys);
return $node;
}
/**
* @return string[]
*/
private function getMethodsInDesiredOrder(ClassLike $classLike): array
{
$classMethodNames = $this->stmtVisibilitySorter->sortMethods($classLike);
return $this->applyPreferredPosition($classMethodNames);
}
/**
* @param string[] $classMethods
* @return string[]
*/
private function applyPreferredPosition(array $classMethods): array
{
$mergedMethods = array_merge(self::PREFERRED_ORDER, $classMethods);
return array_unique($mergedMethods);
}
}

View File

@ -1,82 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Order\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use Rector\Core\Rector\AbstractRector;
use Rector\Order\Order\OrderChangeAnalyzer;
use Rector\Order\StmtOrder;
use Rector\Order\StmtVisibilitySorter;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Order\Rector\Class_\OrderPropertiesByVisibilityRector\OrderPropertiesByVisibilityRectorTest
*/
final class OrderPropertiesByVisibilityRector extends AbstractRector
{
public function __construct(
private OrderChangeAnalyzer $orderChangeAnalyzer,
private StmtOrder $stmtOrder,
private StmtVisibilitySorter $stmtVisibilitySorter
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Orders properties by visibility', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
protected $protectedProperty;
private $privateProperty;
public $publicProperty;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public $publicProperty;
protected $protectedProperty;
private $privateProperty;
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class, Trait_::class];
}
/**
* @param Class_|Trait_ $node
*/
public function refactor(Node $node): ?Node
{
$currentPropertiesOrder = $this->stmtOrder->getStmtsOfTypeOrder($node, Property::class);
$propertiesInDesiredOrder = $this->stmtVisibilitySorter->sortProperties($node);
$oldToNewKeys = $this->stmtOrder->createOldToNewKeys($propertiesInDesiredOrder, $currentPropertiesOrder);
// nothing to re-order
if (! $this->orderChangeAnalyzer->hasOrderChanged($oldToNewKeys)) {
return null;
}
$this->stmtOrder->reorderClassStmtsByOldToNewKeys($node, $oldToNewKeys);
return $node;
}
}