mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-20 23:41:57 +02:00
reorder methods by use
This commit is contained in:
parent
c2d1a98f95
commit
dd0acef8f2
13
ecs.yml
13
ecs.yml
@ -49,11 +49,6 @@ services:
|
||||
- 'NodeVisitorAbstract'
|
||||
|
||||
parameters:
|
||||
exclude_checkers:
|
||||
# run manually from time to time, not to bother user with it
|
||||
- 'Symplify\CodingStandard\Fixer\Order\PropertyOrderByComplexityFixer'
|
||||
- 'Symplify\CodingStandard\Fixer\Order\PrivateMethodOrderByUseFixer'
|
||||
|
||||
exclude_files:
|
||||
- '*tests/*Source/*.php'
|
||||
# tests files
|
||||
@ -62,9 +57,17 @@ parameters:
|
||||
- '*packages/ContributorTools/templates/*'
|
||||
|
||||
skip:
|
||||
# run manually from time to time, not to bother user with it
|
||||
# Symplify\CodingStandard\Fixer\Order\PropertyOrderByComplexityFixer: ~
|
||||
# Symplify\CodingStandard\Fixer\Order\PrivateMethodOrderByUseFixer: ~
|
||||
|
||||
PhpCsFixer\Fixer\Phpdoc\PhpdocTypesFixer:
|
||||
- 'packages/Php/src/Rector/Double/RealToFloatTypeCastRector.php'
|
||||
|
||||
Symplify\CodingStandard\Sniffs\CleanCode\ForbiddenReferenceSniff:
|
||||
# 3rd party contract
|
||||
- 'src/PhpParser/Printer/BetterStandardPrinter.php'
|
||||
|
||||
Symplify\CodingStandard\Fixer\Php\ClassStringToClassConstantFixer:
|
||||
# classes might not exist
|
||||
- 'bin/bootstrap.php'
|
||||
|
@ -173,6 +173,20 @@ CODE_SAMPLE
|
||||
return ! $ifCondition instanceof Identical && ! $ifCondition instanceof Equal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Node[]|null
|
||||
*/
|
||||
private function matchNodes(BinaryOp $ifCondition, Expr $foreachValueNode): ?array
|
||||
{
|
||||
return $this->binaryOpMaintainer->matchFirstAndSecondConditionNode(
|
||||
$ifCondition,
|
||||
Variable::class,
|
||||
function (Node $node, Node $otherNode) use ($foreachValueNode) {
|
||||
return $this->areNodesEqual($otherNode, $foreachValueNode);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private function isIfBodyABoolReturnNode(If_ $firstNodeInsideForeach): bool
|
||||
{
|
||||
$ifStatment = $firstNodeInsideForeach->stmts[0];
|
||||
@ -223,18 +237,4 @@ CODE_SAMPLE
|
||||
|
||||
$newNode->setAttribute('comments', [new Comment($commentContent)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Node[]|null
|
||||
*/
|
||||
private function matchNodes(BinaryOp $ifCondition, Expr $foreachValueNode): ?array
|
||||
{
|
||||
return $this->binaryOpMaintainer->matchFirstAndSecondConditionNode(
|
||||
$ifCondition,
|
||||
Variable::class,
|
||||
function (Node $node, Node $otherNode) use ($foreachValueNode) {
|
||||
return $this->areNodesEqual($otherNode, $foreachValueNode);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -16,9 +16,9 @@ use Rector\RectorDefinition\RectorDefinition;
|
||||
final class ConsecutiveNullCompareReturnsToNullCoalesceQueueRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var IfMaintainer
|
||||
* @var Node[]
|
||||
*/
|
||||
private $ifMaintainer;
|
||||
private $nodesToRemove = [];
|
||||
|
||||
/**
|
||||
* @var Expr[]
|
||||
@ -26,9 +26,9 @@ final class ConsecutiveNullCompareReturnsToNullCoalesceQueueRector extends Abstr
|
||||
private $coalescingNodes = [];
|
||||
|
||||
/**
|
||||
* @var Node[]
|
||||
* @var IfMaintainer
|
||||
*/
|
||||
private $nodesToRemove = [];
|
||||
private $ifMaintainer;
|
||||
|
||||
public function __construct(IfMaintainer $ifMaintainer)
|
||||
{
|
||||
@ -115,6 +115,12 @@ CODE_SAMPLE
|
||||
return $this->createReturnCoalesceNode($this->coalescingNodes);
|
||||
}
|
||||
|
||||
private function reset(): void
|
||||
{
|
||||
$this->coalescingNodes = [];
|
||||
$this->nodesToRemove = [];
|
||||
}
|
||||
|
||||
private function isReturnNull(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Return_) {
|
||||
@ -141,10 +147,4 @@ CODE_SAMPLE
|
||||
|
||||
return new Return_($coalesceNode);
|
||||
}
|
||||
|
||||
private function reset(): void
|
||||
{
|
||||
$this->coalescingNodes = [];
|
||||
$this->nodesToRemove = [];
|
||||
}
|
||||
}
|
||||
|
@ -196,6 +196,11 @@ abstract class AbstractTypeInfo
|
||||
return array_values($types);
|
||||
}
|
||||
|
||||
private function hasRemovedTypes(): bool
|
||||
{
|
||||
return count($this->removedTypes) > 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $types
|
||||
*/
|
||||
@ -280,9 +285,4 @@ abstract class AbstractTypeInfo
|
||||
|
||||
return $types === $arraySubtypeGroup;
|
||||
}
|
||||
|
||||
private function hasRemovedTypes(): bool
|
||||
{
|
||||
return count($this->removedTypes) > 1;
|
||||
}
|
||||
}
|
||||
|
@ -155,6 +155,37 @@ CODE_SAMPLE
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add typehint to all children
|
||||
*/
|
||||
private function populateChildren(Node $node, int $position, ParamTypeInfo $paramTypeInfo): void
|
||||
{
|
||||
if (! $node instanceof ClassMethod) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var string $methodName */
|
||||
$methodName = $this->getName($node);
|
||||
|
||||
/** @var string $className */
|
||||
$className = $node->getAttribute(Attribute::CLASS_NAME);
|
||||
|
||||
$childrenClassLikes = $this->classLikeNodeCollector->findClassesAndInterfacesByType($className);
|
||||
|
||||
// update their methods as well
|
||||
foreach ($childrenClassLikes as $childClassLike) {
|
||||
if ($childClassLike instanceof Class_) {
|
||||
$usedTraits = $this->classLikeNodeCollector->findUsedTraitsInClass($childClassLike);
|
||||
|
||||
foreach ($usedTraits as $trait) {
|
||||
$this->addParamTypeToMethod($trait, $methodName, $position, $node, $paramTypeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
$this->addParamTypeToMethod($childClassLike, $methodName, $position, $node, $paramTypeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
private function addParamTypeToMethod(
|
||||
ClassLike $classLikeNode,
|
||||
string $methodName,
|
||||
@ -190,35 +221,4 @@ CODE_SAMPLE
|
||||
|
||||
$this->notifyNodeChangeFileInfo($paramNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add typehint to all children
|
||||
*/
|
||||
private function populateChildren(Node $node, int $position, ParamTypeInfo $paramTypeInfo): void
|
||||
{
|
||||
if (! $node instanceof ClassMethod) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var string $methodName */
|
||||
$methodName = $this->getName($node);
|
||||
|
||||
/** @var string $className */
|
||||
$className = $node->getAttribute(Attribute::CLASS_NAME);
|
||||
|
||||
$childrenClassLikes = $this->classLikeNodeCollector->findClassesAndInterfacesByType($className);
|
||||
|
||||
// update their methods as well
|
||||
foreach ($childrenClassLikes as $childClassLike) {
|
||||
if ($childClassLike instanceof Class_) {
|
||||
$usedTraits = $this->classLikeNodeCollector->findUsedTraitsInClass($childClassLike);
|
||||
|
||||
foreach ($usedTraits as $trait) {
|
||||
$this->addParamTypeToMethod($trait, $methodName, $position, $node, $paramTypeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
$this->addParamTypeToMethod($childClassLike, $methodName, $position, $node, $paramTypeInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -140,6 +140,38 @@ CODE_SAMPLE
|
||||
return $docReturnTypeInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add typehint to all children
|
||||
*/
|
||||
private function populateChildren(Node $node, ReturnTypeInfo $returnTypeInfo): void
|
||||
{
|
||||
if (! $node instanceof ClassMethod) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var string $methodName */
|
||||
$methodName = $this->getName($node);
|
||||
|
||||
/** @var string $className */
|
||||
$className = $node->getAttribute(Attribute::CLASS_NAME);
|
||||
|
||||
$childrenClassLikes = $this->classLikeNodeCollector->findClassesAndInterfacesByType($className);
|
||||
|
||||
// update their methods as well
|
||||
foreach ($childrenClassLikes as $childClassLike) {
|
||||
if (! $childClassLike instanceof Class_) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$usedTraits = $this->classLikeNodeCollector->findUsedTraitsInClass($childClassLike);
|
||||
foreach ($usedTraits as $trait) {
|
||||
$this->addReturnTypeToMethod($trait, $methodName, $node, $returnTypeInfo);
|
||||
}
|
||||
|
||||
$this->addReturnTypeToMethod($childClassLike, $methodName, $node, $returnTypeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
private function addReturnTypeToMethod(
|
||||
ClassLike $classLikeNode,
|
||||
string $methodName,
|
||||
@ -173,36 +205,4 @@ CODE_SAMPLE
|
||||
|
||||
$this->notifyNodeChangeFileInfo($classMethod);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add typehint to all children
|
||||
*/
|
||||
private function populateChildren(Node $node, ReturnTypeInfo $returnTypeInfo): void
|
||||
{
|
||||
if (! $node instanceof ClassMethod) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var string $methodName */
|
||||
$methodName = $this->getName($node);
|
||||
|
||||
/** @var string $className */
|
||||
$className = $node->getAttribute(Attribute::CLASS_NAME);
|
||||
|
||||
$childrenClassLikes = $this->classLikeNodeCollector->findClassesAndInterfacesByType($className);
|
||||
|
||||
// update their methods as well
|
||||
foreach ($childrenClassLikes as $childClassLike) {
|
||||
if (! $childClassLike instanceof Class_) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$usedTraits = $this->classLikeNodeCollector->findUsedTraitsInClass($childClassLike);
|
||||
foreach ($usedTraits as $trait) {
|
||||
$this->addReturnTypeToMethod($trait, $methodName, $node, $returnTypeInfo);
|
||||
}
|
||||
|
||||
$this->addReturnTypeToMethod($childClassLike, $methodName, $node, $returnTypeInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -155,23 +155,6 @@ CODE_SAMPLE
|
||||
return new Return_($spaceshipNode);
|
||||
}
|
||||
|
||||
private function areVariablesEqual(BinaryOp $node, ?Expr $firstValue, ?Expr $secondValue): bool
|
||||
{
|
||||
if ($firstValue === null || $secondValue === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->areNodesEqual($node->left, $firstValue) && $this->areNodesEqual($node->right, $secondValue)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->areNodesEqual($node->right, $firstValue) && $this->areNodesEqual($node->left, $secondValue)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function reset(): void
|
||||
{
|
||||
$this->onEqual = null;
|
||||
@ -204,4 +187,21 @@ CODE_SAMPLE
|
||||
$this->onSmaller = $this->getValue($ternaryNode->else);
|
||||
}
|
||||
}
|
||||
|
||||
private function areVariablesEqual(BinaryOp $node, ?Expr $firstValue, ?Expr $secondValue): bool
|
||||
{
|
||||
if ($firstValue === null || $secondValue === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->areNodesEqual($node->left, $firstValue) && $this->areNodesEqual($node->right, $secondValue)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->areNodesEqual($node->right, $firstValue) && $this->areNodesEqual($node->left, $secondValue)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -99,6 +99,24 @@ CODE_SAMPLE
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $preference
|
||||
*/
|
||||
private function ensurePreferceIsValid($preference): void
|
||||
{
|
||||
$allowedPreferences = [self::PREFER_THIS, self::PREFER_SELF];
|
||||
if (in_array($preference, $allowedPreferences, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new InvalidRectorConfigurationException(sprintf(
|
||||
'Preference configuration "%s" for "%s" is not valid. Use one of "%s"',
|
||||
$preference,
|
||||
self::class,
|
||||
implode('", "', $allowedPreferences)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MethodCall|StaticCall $node
|
||||
*/
|
||||
@ -130,22 +148,4 @@ CODE_SAMPLE
|
||||
|
||||
return new MethodCall(new Variable('this'), $node->name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $preference
|
||||
*/
|
||||
private function ensurePreferceIsValid($preference): void
|
||||
{
|
||||
$allowedPreferences = [self::PREFER_THIS, self::PREFER_SELF];
|
||||
if (in_array($preference, $allowedPreferences, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
throw new InvalidRectorConfigurationException(sprintf(
|
||||
'Preference configuration "%s" for "%s" is not valid. Use one of "%s"',
|
||||
$preference,
|
||||
self::class,
|
||||
implode('", "', $allowedPreferences)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -145,6 +145,22 @@ CODE_SAMPLE
|
||||
$this->processPreviousAssign($node, $firstArgument);
|
||||
}
|
||||
|
||||
private function processPreviousAssign(Node $node, Node $firstArgument): void
|
||||
{
|
||||
/** @var Assign|null $createdNode */
|
||||
$createdNode = $this->findPreviousNodeAssign($node, $firstArgument);
|
||||
|
||||
if ($createdNode instanceof Assign && $createdNode->expr instanceof FuncCall && $this->isName(
|
||||
$createdNode->expr,
|
||||
'sprintf'
|
||||
)) {
|
||||
$arrayNode = $this->nodeTransformer->transformSprintfToArray($createdNode->expr);
|
||||
if ($arrayNode) {
|
||||
$createdNode->expr = $arrayNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function findPreviousNodeAssign(Node $node, Node $firstArgument): ?Assign
|
||||
{
|
||||
return $this->betterNodeFinder->findFirstPrevious($node, function (Node $checkedNode) use ($firstArgument) {
|
||||
@ -161,20 +177,4 @@ CODE_SAMPLE
|
||||
return $checkedNode;
|
||||
});
|
||||
}
|
||||
|
||||
private function processPreviousAssign(Node $node, Node $firstArgument): void
|
||||
{
|
||||
/** @var Assign|null $createdNode */
|
||||
$createdNode = $this->findPreviousNodeAssign($node, $firstArgument);
|
||||
|
||||
if ($createdNode instanceof Assign && $createdNode->expr instanceof FuncCall && $this->isName(
|
||||
$createdNode->expr,
|
||||
'sprintf'
|
||||
)) {
|
||||
$arrayNode = $this->nodeTransformer->transformSprintfToArray($createdNode->expr);
|
||||
if ($arrayNode) {
|
||||
$createdNode->expr = $arrayNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -109,11 +109,6 @@ final class CallMaintainer
|
||||
});
|
||||
}
|
||||
|
||||
private function resolveMotherType(Node $callNode): string
|
||||
{
|
||||
return $callNode instanceof FuncCall ? Function_::class : ClassMethod::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StaticCall|FuncCall|MethodCall $callNode
|
||||
*/
|
||||
@ -154,4 +149,9 @@ final class CallMaintainer
|
||||
|
||||
return (bool) Strings::match($printedFunction, '#\b(' . implode('|', self::VARIADIC_FUNCTION_NAMES) . ')\b#');
|
||||
}
|
||||
|
||||
private function resolveMotherType(Node $callNode): string
|
||||
{
|
||||
return $callNode instanceof FuncCall ? Function_::class : ClassMethod::class;
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ use PhpParser\Node\Scalar\EncapsedStringPart;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Expression;
|
||||
use PhpParser\Node\Stmt\Namespace_;
|
||||
use PhpParser\PrettyPrinter\Standard;
|
||||
use Rector\NodeTypeResolver\Node\Attribute;
|
||||
use function Safe\sprintf;
|
||||
@ -72,6 +71,27 @@ final class BetterStandardPrinter extends Standard
|
||||
return $this->print($firstNode) === $this->print($secondNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $nodes
|
||||
* @param mixed[] $origNodes
|
||||
* @param int|null $fixup
|
||||
* @param string|null $insertStr
|
||||
*/
|
||||
public function pArray(
|
||||
array $nodes,
|
||||
array $origNodes,
|
||||
int &$pos,
|
||||
int $indentAdjustment,
|
||||
string $subNodeName,
|
||||
$fixup,
|
||||
$insertStr
|
||||
): ?string {
|
||||
// reindex positions for printer
|
||||
$nodes = array_values($nodes);
|
||||
|
||||
return parent::pArray($nodes, $origNodes, $pos, $indentAdjustment, $subNodeName, $fixup, $insertStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not preslash all slashes (parent behavior), but only those:
|
||||
*
|
||||
@ -194,27 +214,6 @@ final class BetterStandardPrinter extends Standard
|
||||
. ($node->stmts !== null ? $this->nl . '{' . $this->pStmts($node->stmts) . $this->nl . '}' : ';');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $nodes
|
||||
* @param mixed[] $origNodes
|
||||
* @param int|null $fixup
|
||||
* @param string|null $insertStr
|
||||
*/
|
||||
public function pArray(
|
||||
array $nodes,
|
||||
array $origNodes,
|
||||
int &$pos,
|
||||
int $indentAdjustment,
|
||||
string $subNodeName,
|
||||
$fixup,
|
||||
$insertStr
|
||||
): ?string {
|
||||
// reindex positions for printer
|
||||
$nodes = array_values($nodes);
|
||||
|
||||
return parent::pArray($nodes, $origNodes, $pos, $indentAdjustment, $subNodeName, $fixup, $insertStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows PHP 7.3 trailing comma in multiline args
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user