mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 00:10:51 +00:00
[Order] Order properties by visibility (#3929)
This commit is contained in:
parent
29d44284cc
commit
05ece4e8d1
|
@ -7,6 +7,7 @@ use Rector\Order\Rector\Class_\OrderPrivateMethodsByUseRector;
|
|||
use Rector\Order\Rector\Class_\OrderPropertyByComplexityRector;
|
||||
use Rector\Order\Rector\Class_\OrderPublicInterfaceMethodRector;
|
||||
use Rector\Order\Rector\ClassLike\OrderMethodsByVisibilityRector;
|
||||
use Rector\Order\Rector\ClassLike\OrderPropertiesByVisibilityRector;
|
||||
use Rector\Order\Rector\ClassMethod\OrderConstructorDependenciesByTypeAlphabeticallyRector;
|
||||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||
|
||||
|
@ -24,4 +25,6 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
|||
$services->set(OrderConstructorDependenciesByTypeAlphabeticallyRector::class);
|
||||
|
||||
$services->set(OrderMethodsByVisibilityRector::class);
|
||||
|
||||
$services->set(OrderPropertiesByVisibilityRector::class);
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# All 550 Rectors Overview
|
||||
# All 552 Rectors Overview
|
||||
|
||||
- [Projects](#projects)
|
||||
---
|
||||
|
@ -36,7 +36,7 @@
|
|||
- [NetteTesterToPHPUnit](#nettetestertophpunit) (3)
|
||||
- [NetteToSymfony](#nettetosymfony) (9)
|
||||
- [NetteUtilsCodeQuality](#netteutilscodequality) (1)
|
||||
- [Order](#order) (5)
|
||||
- [Order](#order) (7)
|
||||
- [PHPOffice](#phpoffice) (14)
|
||||
- [PHPStan](#phpstan) (3)
|
||||
- [PHPUnit](#phpunit) (37)
|
||||
|
@ -7749,6 +7749,25 @@ Order __constructor dependencies by type A-Z
|
|||
|
||||
<br><br>
|
||||
|
||||
### `OrderMethodsByVisibilityRector`
|
||||
|
||||
- class: [`Rector\Order\Rector\ClassLike\OrderMethodsByVisibilityRector`](/../master/rules/order/src/Rector/ClassLike/OrderMethodsByVisibilityRector.php)
|
||||
- [test fixtures](/../master/rules/order/tests/Rector/ClassLike/OrderMethodsByVisibilityRector/Fixture)
|
||||
|
||||
Orders method by visibility
|
||||
|
||||
```diff
|
||||
class SomeClass
|
||||
{
|
||||
+ public function publicFunctionName();
|
||||
protected function protectedFunctionName();
|
||||
private function privateFunctionName();
|
||||
- public function publicFunctionName();
|
||||
}
|
||||
```
|
||||
|
||||
<br><br>
|
||||
|
||||
### `OrderPrivateMethodsByUseRector`
|
||||
|
||||
- class: [`Rector\Order\Rector\Class_\OrderPrivateMethodsByUseRector`](/../master/rules/order/src/Rector/Class_/OrderPrivateMethodsByUseRector.php)
|
||||
|
@ -7779,6 +7798,25 @@ Order private methods in order of their use
|
|||
|
||||
<br><br>
|
||||
|
||||
### `OrderPropertiesByVisibilityRector`
|
||||
|
||||
- class: [`Rector\Order\Rector\ClassLike\OrderPropertiesByVisibilityRector`](/../master/rules/order/src/Rector/ClassLike/OrderPropertiesByVisibilityRector.php)
|
||||
- [test fixtures](/../master/rules/order/tests/Rector/ClassLike/OrderPropertiesByVisibilityRector/Fixture)
|
||||
|
||||
Orders properties by visibility
|
||||
|
||||
```diff
|
||||
final class SomeClass
|
||||
{
|
||||
+ public $publicProperty;
|
||||
protected $protectedProperty;
|
||||
private $privateProperty;
|
||||
- public $publicProperty;
|
||||
}
|
||||
```
|
||||
|
||||
<br><br>
|
||||
|
||||
### `OrderPropertyByComplexityRector`
|
||||
|
||||
- class: [`Rector\Order\Rector\Class_\OrderPropertyByComplexityRector`](/../master/rules/order/src/Rector/Class_/OrderPropertyByComplexityRector.php)
|
||||
|
|
|
@ -116,7 +116,7 @@ PHP
|
|||
$classMethodsByName[$position] = $classMethodName;
|
||||
|
||||
$classMethods[$classMethodName]['name'] = $classMethodName;
|
||||
$classMethods[$classMethodName][self::VISIBILITY] = $this->getVisibilityOrder($classStmt);
|
||||
$classMethods[$classMethodName][self::VISIBILITY] = $this->stmtOrder->getOrderByVisibility($classStmt);
|
||||
$classMethods[$classMethodName]['abstract'] = $classStmt->isAbstract();
|
||||
$classMethods[$classMethodName]['final'] = $classStmt->isFinal();
|
||||
$classMethods[$classMethodName]['static'] = $classStmt->isStatic();
|
||||
|
@ -131,19 +131,6 @@ PHP
|
|||
return $this->stmtOrder->reorderClassStmtsByOldToNewKeys($node, $oldToNewKeys);
|
||||
}
|
||||
|
||||
private function getVisibilityOrder(ClassMethod $classMethod): int
|
||||
{
|
||||
if ($classMethod->isPrivate()) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
if ($classMethod->isProtected()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function getMethodsSortedByVisibility(array $classMethods): array
|
||||
{
|
||||
uasort(
|
||||
|
|
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Order\Rector\ClassLike;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\Order\StmtOrder;
|
||||
|
||||
/**
|
||||
* @see \Rector\Order\Tests\Rector\ClassLike\OrderPropertiesByVisibilityRector\OrderPropertiesByVisibilityRectorTest
|
||||
*/
|
||||
final class OrderPropertiesByVisibilityRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const VISIBILITY = 'visibility';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private const POSITION = 'position';
|
||||
|
||||
/**
|
||||
* @var StmtOrder
|
||||
*/
|
||||
private $stmtOrder;
|
||||
|
||||
public function __construct(StmtOrder $stmtOrder)
|
||||
{
|
||||
$this->stmtOrder = $stmtOrder;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Orders properties by visibility', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
final class SomeClass
|
||||
{
|
||||
protected $protectedProperty;
|
||||
private $privateProperty;
|
||||
public $publicProperty;
|
||||
}
|
||||
PHP
|
||||
|
||||
,
|
||||
<<<'PHP'
|
||||
final class SomeClass
|
||||
{
|
||||
public $publicProperty;
|
||||
protected $protectedProperty;
|
||||
private $privateProperty;
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [ClassLike::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassLike $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($node instanceof Node\Stmt\Interface_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$properties = [];
|
||||
$propertiesByName = [];
|
||||
foreach ($node->stmts as $position => $propertyStmt) {
|
||||
if (! $propertyStmt instanceof Node\Stmt\Property) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var string $propertyName */
|
||||
$propertyName = $this->getName($propertyStmt);
|
||||
$propertiesByName[$position] = $propertyName;
|
||||
|
||||
$properties[$propertyName]['name'] = $propertyName;
|
||||
$properties[$propertyName][self::VISIBILITY] = $this->stmtOrder->getOrderByVisibility($propertyStmt);
|
||||
$properties[$propertyName]['static'] = $propertyStmt->isStatic();
|
||||
$properties[$propertyName][self::POSITION] = $position;
|
||||
}
|
||||
|
||||
$sortedProperties = $this->getPropertiesSortedByVisibility($properties);
|
||||
$oldToNewKeys = $this->stmtOrder->createOldToNewKeys($sortedProperties, $propertiesByName);
|
||||
|
||||
return $this->stmtOrder->reorderClassStmtsByOldToNewKeys($node, $oldToNewKeys);
|
||||
}
|
||||
|
||||
private function getPropertiesSortedByVisibility(array $properties): array
|
||||
{
|
||||
uasort(
|
||||
$properties,
|
||||
function (array $firstArray, array $secondArray): int {
|
||||
return [
|
||||
$firstArray[self::VISIBILITY],
|
||||
$firstArray['static'],
|
||||
$firstArray[self::POSITION],
|
||||
] <=> [$secondArray[self::VISIBILITY], $secondArray['static'], $secondArray[self::POSITION]];
|
||||
}
|
||||
);
|
||||
|
||||
return array_keys($properties);
|
||||
}
|
||||
}
|
|
@ -4,7 +4,10 @@ declare(strict_types=1);
|
|||
|
||||
namespace Rector\Order;
|
||||
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
|
||||
final class StmtOrder
|
||||
{
|
||||
|
@ -57,4 +60,20 @@ final class StmtOrder
|
|||
|
||||
return $classLike;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClassMethod|Property $stmt
|
||||
*/
|
||||
public function getOrderByVisibility(Stmt $stmt): int
|
||||
{
|
||||
if ($stmt->isPrivate()) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
if ($stmt->isProtected()) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Order\Tests\Rector\ClassLike\OrderPropertiesByVisibilityRector\Fixture;
|
||||
|
||||
abstract class AbstractClass
|
||||
{
|
||||
protected $protectedProperty;
|
||||
public $publicProperty;
|
||||
protected static $protectedStaticProperty;
|
||||
private $privateProperty;
|
||||
public static $publicStaticProperty;
|
||||
private static $privateStaticProperty;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Order\Tests\Rector\ClassLike\OrderPropertiesByVisibilityRector\Fixture;
|
||||
|
||||
abstract class AbstractClass
|
||||
{
|
||||
public $publicProperty;
|
||||
public static $publicStaticProperty;
|
||||
protected $protectedProperty;
|
||||
protected static $protectedStaticProperty;
|
||||
private $privateProperty;
|
||||
private static $privateStaticProperty;
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Order\Tests\Rector\ClassLike\OrderPropertiesByVisibilityRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
protected $protectedProperty;
|
||||
public $publicProperty;
|
||||
protected static $protectedStaticProperty;
|
||||
private $privateProperty;
|
||||
public static $publicStaticProperty;
|
||||
private static $privateStaticProperty;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Order\Tests\Rector\ClassLike\OrderPropertiesByVisibilityRector\Fixture;
|
||||
|
||||
final class SomeClass
|
||||
{
|
||||
public $publicProperty;
|
||||
public static $publicStaticProperty;
|
||||
protected $protectedProperty;
|
||||
protected static $protectedStaticProperty;
|
||||
private $privateProperty;
|
||||
private static $privateStaticProperty;
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Order\Tests\Rector\ClassLike\OrderPropertiesByVisibilityRector\Fixture;
|
||||
|
||||
trait TraitClass
|
||||
{
|
||||
protected $protectedProperty;
|
||||
public $publicProperty;
|
||||
protected static $protectedStaticProperty;
|
||||
private $privateProperty;
|
||||
public static $publicStaticProperty;
|
||||
private static $privateStaticProperty;
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Order\Tests\Rector\ClassLike\OrderPropertiesByVisibilityRector\Fixture;
|
||||
|
||||
trait TraitClass
|
||||
{
|
||||
public $publicProperty;
|
||||
public static $publicStaticProperty;
|
||||
protected $protectedProperty;
|
||||
protected static $protectedStaticProperty;
|
||||
private $privateProperty;
|
||||
private static $privateStaticProperty;
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Order\Tests\Rector\ClassLike\OrderPropertiesByVisibilityRector;
|
||||
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
use Rector\Order\Rector\ClassLike\OrderPropertiesByVisibilityRector;
|
||||
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||
|
||||
final class OrderPropertiesByVisibilityRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function test(SmartFileInfo $fileInfo): void
|
||||
{
|
||||
$this->doTestFileInfo($fileInfo);
|
||||
}
|
||||
|
||||
public function provideData(): \Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return OrderPropertiesByVisibilityRector::class;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user