Skip to content

Commit

Permalink
sort private methods by call order
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 22, 2021
1 parent ac94f1d commit 87d71a9
Show file tree
Hide file tree
Showing 27 changed files with 455 additions and 409 deletions.
73 changes: 53 additions & 20 deletions config/set/php80.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);

use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
use Rector\Generic\NodeAnalyzer\ArgumentAddingScope;
use Rector\Generic\Rector\ClassMethod\ArgumentAdderRector;
Expand All @@ -23,27 +24,59 @@
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
return static function (\Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator $containerConfigurator) : void {

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(\Rector\Php80\Rector\FunctionLike\UnionTypesRector::class);
$services->set(\Rector\Php80\Rector\NotIdentical\StrContainsRector::class);
$services->set(\Rector\Php80\Rector\Identical\StrStartsWithRector::class);
$services->set(\Rector\Php80\Rector\Identical\StrEndsWithRector::class);
$services->set(\Rector\Php80\Rector\Class_\StringableForToStringRector::class);
$services->set(\Rector\Php80\Rector\Class_\AnnotationToAttributeRector::class);
$services->set(\Rector\Php80\Rector\FuncCall\ClassOnObjectRector::class);
$services->set(\Rector\Php80\Rector\Ternary\GetDebugTypeRector::class);
$services->set(\Rector\Php80\Rector\FuncCall\TokenGetAllToObjectRector::class);
$services->set(\Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector::class);
$services->set(\Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector::class);
$services->set(\Rector\Php80\Rector\Switch_\ChangeSwitchToMatchRector::class);
$services->set(UnionTypesRector::class);
$services->set(StrContainsRector::class);
$services->set(StrStartsWithRector::class);
$services->set(StrEndsWithRector::class);
$services->set(StringableForToStringRector::class);
$services->set(AnnotationToAttributeRector::class);
$services->set(ClassOnObjectRector::class);
$services->set(GetDebugTypeRector::class);
$services->set(TokenGetAllToObjectRector::class);
$services->set(RemoveUnusedVariableInCatchRector::class);
$services->set(ClassPropertyAssignToConstructorPromotionRector::class);
$services->set(ChangeSwitchToMatchRector::class);

// nette\utils and Strings::replace()
$services->set(\Rector\Generic\Rector\ClassMethod\ArgumentAdderRector::class)->call('configure', [[\Rector\Generic\NodeAnalyzer\ArgumentAddingScope::ADDED_ARGUMENTS => \Symplify\SymfonyPhpConfig\ValueObjectInliner::inline([new \Rector\Generic\ValueObject\ArgumentAdder('Nette\Utils\Strings', 'replace', 2, 'replacement', '')])]]);
$services->set(\Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector::class);
$services->set(\Rector\Php80\Rector\ClassMethod\SetStateToStaticRector::class);
$services->set(\Rector\Php80\Rector\ClassMethod\FinalPrivateToPrivateVisibilityRector::class);
$services->set(ArgumentAdderRector::class)->call('configure', [[
ArgumentAddingScope::ADDED_ARGUMENTS => ValueObjectInliner::inline([
new ArgumentAdder('Nette\Utils\Strings', 'replace', 2, 'replacement', ''),
]),
]]);
$services->set(RemoveParentCallWithoutParentRector::class);
$services->set(SetStateToStaticRector::class);
$services->set(FinalPrivateToPrivateVisibilityRector::class);
// @see https://php.watch/versions/8.0/pgsql-aliases-deprecated
$services->set(\Rector\Renaming\Rector\FuncCall\RenameFunctionRector::class)->call('configure', [[\Rector\Renaming\Rector\FuncCall\RenameFunctionRector::OLD_FUNCTION_TO_NEW_FUNCTION => ['pg_clientencoding' => 'pg_client_encoding', 'pg_cmdtuples' => 'pg_affected_rows', 'pg_errormessage' => 'pg_last_error', 'pg_fieldisnull' => 'pg_field_is_null', 'pg_fieldname' => 'pg_field_name', 'pg_fieldnum' => 'pg_field_num', 'pg_fieldprtlen' => 'pg_field_prtlen', 'pg_fieldsize' => 'pg_field_size', 'pg_fieldtype' => 'pg_field_type', 'pg_freeresult' => 'pg_free_result', 'pg_getlastoid' => 'pg_last_oid', 'pg_loclose' => 'pg_lo_close', 'pg_locreate' => 'pg_lo_create', 'pg_loexport' => 'pg_lo_export', 'pg_loimport' => 'pg_lo_import', 'pg_loopen' => 'pg_lo_open', 'pg_loread' => 'pg_lo_read', 'pg_loreadall' => 'pg_lo_read_all', 'pg_lounlink' => 'pg_lo_unlink', 'pg_lowrite' => 'pg_lo_write', 'pg_numfields' => 'pg_num_fields', 'pg_numrows' => 'pg_num_rows', 'pg_result' => 'pg_fetch_result', 'pg_setclientencoding' => 'pg_set_client_encoding']]]);
$services->set(\Rector\Php80\Rector\ClassMethod\OptionalParametersAfterRequiredRector::class);
$services->set(\Rector\Php80\Rector\Class_\TypedPropertyFromStrictConstructorRector::class);
$services->set(RenameFunctionRector::class)->call('configure', [[
RenameFunctionRector::OLD_FUNCTION_TO_NEW_FUNCTION => [
'pg_clientencoding' => 'pg_client_encoding',
'pg_cmdtuples' => 'pg_affected_rows',
'pg_errormessage' => 'pg_last_error',
'pg_fieldisnull' => 'pg_field_is_null',
'pg_fieldname' => 'pg_field_name',
'pg_fieldnum' => 'pg_field_num',
'pg_fieldprtlen' => 'pg_field_prtlen',
'pg_fieldsize' => 'pg_field_size',
'pg_fieldtype' => 'pg_field_type',
'pg_freeresult' => 'pg_free_result',
'pg_getlastoid' => 'pg_last_oid',
'pg_loclose' => 'pg_lo_close',
'pg_locreate' => 'pg_lo_create',
'pg_loexport' => 'pg_lo_export',
'pg_loimport' => 'pg_lo_import',
'pg_loopen' => 'pg_lo_open',
'pg_loread' => 'pg_lo_read',
'pg_loreadall' => 'pg_lo_read_all',
'pg_lounlink' => 'pg_lo_unlink',
'pg_lowrite' => 'pg_lo_write',
'pg_numfields' => 'pg_num_fields',
'pg_numrows' => 'pg_num_rows',
'pg_result' => 'pg_fetch_result',
'pg_setclientencoding' => 'pg_set_client_encoding',
],
]]);
$services->set(OptionalParametersAfterRequiredRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,6 @@ public function isRead(Node $node): bool
return ! $this->assignManipulator->isLeftPartOfAssign($node);
}

private function isNotInsideIssetUnset(ArrayDimFetch $arrayDimFetch): bool
{
return ! (bool) $this->betterNodeFinder->findParentTypes($arrayDimFetch, [Isset_::class, Unset_::class]);
}

private function unwrapPostPreIncDec(Node $node): Node
{
if ($node instanceof PreInc || $node instanceof PreDec || $node instanceof PostInc || $node instanceof PostDec) {
Expand All @@ -101,6 +96,11 @@ private function unwrapPostPreIncDec(Node $node): Node
return $node;
}

private function isNotInsideIssetUnset(ArrayDimFetch $arrayDimFetch): bool
{
return ! (bool) $this->betterNodeFinder->findParentTypes($arrayDimFetch, [Isset_::class, Unset_::class]);
}

private function isArrayDimFetchRead(ArrayDimFetch $arrayDimFetch): bool
{
$parentParent = $arrayDimFetch->getAttribute(AttributeKey::PARENT_NODE);
Expand Down
34 changes: 17 additions & 17 deletions packages/rector-generator/src/Command/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return ShellCode::SUCCESS;
}

private function getRectorRecipe(InputInterface $input): RectorRecipe
{
$isInteractive = $input->getOption(self::INTERACTIVE_MODE_NAME);
if (! $isInteractive) {
return $this->rectorRecipeProvider->provide();
}

return $this->rectorRecipeInteractiveFactory->create();
}

/**
* @param string[] $generatedFilePaths
*/
Expand All @@ -172,17 +182,6 @@ private function resolveTestCaseDirectoryPath(array $generatedFilePaths): string
throw new ShouldNotHappenException();
}

private function isGeneratedFilePathTestCase(string $generatedFilePath): bool
{
if (Strings::endsWith($generatedFilePath, 'Test.php')) {
return true;
}
if (! Strings::endsWith($generatedFilePath, 'Test.php.inc')) {
return false;
}
return StaticPHPUnitEnvironment::isPHPUnitRun();
}

/**
* @param string[] $generatedFilePaths
*/
Expand All @@ -204,13 +203,14 @@ private function printSuccess(string $name, array $generatedFilePaths, string $t
$this->symfonyStyle->success($message);
}

private function getRectorRecipe(InputInterface $input): RectorRecipe
private function isGeneratedFilePathTestCase(string $generatedFilePath): bool
{
$isInteractive = $input->getOption(self::INTERACTIVE_MODE_NAME);
if (! $isInteractive) {
return $this->rectorRecipeProvider->provide();
if (Strings::endsWith($generatedFilePath, 'Test.php')) {
return true;
}

return $this->rectorRecipeInteractiveFactory->create();
if (! Strings::endsWith($generatedFilePath, 'Test.php.inc')) {
return false;
}
return StaticPHPUnitEnvironment::isPHPUnitRun();
}
}
2 changes: 0 additions & 2 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
use Rector\CodingStyle\Rector\MethodCall\PreferThisOrSelfMethodCallRector;
use Rector\CodingStyle\Rector\String_\SplitStringClassConstantToClassConstFetchRector;
use Rector\Core\Configuration\Option;
use Rector\Core\Contract\Rector\PhpRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedClassConstantRector;
use Rector\Order\Rector\Class_\OrderPublicInterfaceMethodRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Restoration\Rector\ClassMethod\InferParamFromClassMethodReturnRector;
use Rector\Restoration\ValueObject\InferParamFromClassMethodReturn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,6 @@ public function refactor(Node $node): ?Node
return $node;
}

private function shouldSkipReAssign(Expression $expression, Assign $assign): bool
{
if ($this->hasReAssign($expression, $assign->var)) {
return true;
}

return $this->hasReAssign($expression, $assign->expr);
}

private function isUsedAsArraykeyOrInsideIfCondition(Expression $expression, Variable $variable): bool
{
$parentExpression = $expression->getAttribute(AttributeKey::PARENT_NODE);
Expand All @@ -149,39 +140,13 @@ private function hasPropertyInExpr(Expression $expression, Expr $expr): bool
});
}

private function hasReAssign(Expression $expression, Expr $expr): bool
private function shouldSkipReAssign(Expression $expression, Assign $assign): bool
{
$next = $expression->getAttribute(AttributeKey::NEXT_NODE);
$exprValues = $this->betterNodeFinder->find($expr, function (Node $node): bool {
return $node instanceof Variable;
});

if ($exprValues === []) {
return false;
}

while ($next) {
foreach ($exprValues as $value) {
$isReAssign = (bool) $this->betterNodeFinder->findFirst($next, function (Node $node): bool {
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
$node = $this->mayBeArrayDimFetch($node);
if (! $parent instanceof Assign) {
return false;
}
return (string) $this->getName($node) === (string) $this->getName($parent->var);
});

if (! $isReAssign) {
continue;
}

return true;
}

$next = $next->getAttribute(AttributeKey::NEXT_NODE);
if ($this->hasReAssign($expression, $assign->var)) {
return true;
}

return false;
return $this->hasReAssign($expression, $assign->expr);
}

private function getUsageInNextStmts(Expression $expression, Variable $variable): ?Variable
Expand Down Expand Up @@ -258,14 +223,39 @@ private function isInsideCondition(Expression $expression): bool
);
}

private function mayBeArrayDimFetch(Node $node): Node
private function hasReAssign(Expression $expression, Expr $expr): bool
{
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parent instanceof ArrayDimFetch) {
$node = $parent->var;
$next = $expression->getAttribute(AttributeKey::NEXT_NODE);
$exprValues = $this->betterNodeFinder->find($expr, function (Node $node): bool {
return $node instanceof Variable;
});

if ($exprValues === []) {
return false;
}

return $node;
while ($next) {
foreach ($exprValues as $value) {
$isReAssign = (bool) $this->betterNodeFinder->findFirst($next, function (Node $node): bool {
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
$node = $this->mayBeArrayDimFetch($node);
if (! $parent instanceof Assign) {
return false;
}
return (string) $this->getName($node) === (string) $this->getName($parent->var);
});

if (! $isReAssign) {
continue;
}

return true;
}

$next = $next->getAttribute(AttributeKey::NEXT_NODE);
}

return false;
}

private function hasStaticCall(Node $node): bool
Expand Down Expand Up @@ -339,6 +329,16 @@ private function getSameVarNameInNexts(Node $node, Variable $variable): ?Variabl
return null;
}

private function mayBeArrayDimFetch(Node $node): Node
{
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parent instanceof ArrayDimFetch) {
$node = $parent->var;
}

return $node;
}

private function countWithElseIf(Node $node, Variable $variable, int $countFound): int
{
if (! $node instanceof If_) {
Expand Down
26 changes: 13 additions & 13 deletions rules/code-quality/src/NodeFactory/AnonymousFunctionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,6 @@ public function create(ClassMethod $classMethod, Node $node): Closure
return $anonymousFunction;
}

/**
* @param Return_[] $nodes
*/
private function hasClassMethodReturn(array $nodes): bool
{
foreach ($nodes as $node) {
if ($node->expr !== null) {
return true;
}
}
return false;
}

/**
* @param Param[] $params
* @return Param[]
Expand All @@ -110,4 +97,17 @@ private function copyParams(array $params): array

return $newParams;
}

/**
* @param Return_[] $nodes
*/
private function hasClassMethodReturn(array $nodes): bool
{
foreach ($nodes as $node) {
if ($node->expr !== null) {
return true;
}
}
return false;
}
}
24 changes: 12 additions & 12 deletions rules/code-quality/src/Rector/For_/ForToForeachRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,31 +401,31 @@ private function isSmallerOrGreater(array $condExprs, string $keyValueName, stri
return false;
}

private function isArgParentCount(Node $node): bool
private function shouldSkipNode(ArrayDimFetch $arrayDimFetch): bool
{
if (! $node instanceof Arg) {
$parentNode = $arrayDimFetch->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Node) {
return false;
}

$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Node) {
return false;
if ($this->assignManipulator->isNodePartOfAssign($parentNode)) {
return true;
}

return $this->isFuncCallName($parent, self::COUNT);
return $this->isArgParentCount($parentNode);
}

private function shouldSkipNode(ArrayDimFetch $arrayDimFetch): bool
private function isArgParentCount(Node $node): bool
{
$parentNode = $arrayDimFetch->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Node) {
if (! $node instanceof Arg) {
return false;
}

if ($this->assignManipulator->isNodePartOfAssign($parentNode)) {
return true;
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Node) {
return false;
}

return $this->isArgParentCount($parentNode);
return $this->isFuncCallName($parent, self::COUNT);
}
}
Loading

0 comments on commit 87d71a9

Please sign in to comment.