Skip to content

Commit

Permalink
Align rector classes with rest of cakephp formatting standards (#297)
Browse files Browse the repository at this point in the history
All mechanical changes from the modification in `phpcs.xml` and running
`composer cs-fix`.

* Fix typehint
* Fix remaining phpcs errors and ignore doc block sniffs
* Fix more phpcs errors in 5.x
  • Loading branch information
markstory authored Sep 11, 2024
1 parent 4ad8ea0 commit 1db9833
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 116 deletions.
17 changes: 16 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@

<rule ref="CakePHP"/>

<exclude-pattern>src/Rector</exclude-pattern>
<exclude-pattern>tests/test_apps</exclude-pattern>
<exclude-pattern>tests/TestCase/Rector</exclude-pattern>

<rule ref="CakePHP.Commenting.FunctionComment.Missing">
<severity>0</severity>
</rule>
<rule ref="CakePHP.Commenting.FunctionComment.MissingParamTag">
<severity>0</severity>
</rule>
<rule ref="CakePHP.Commenting.FunctionComment.MissingReturn">
<severity>0</severity>
</rule>
<rule ref="CakePHP.Commenting.FunctionComment.MissingParamComment">
<severity>0</severity>
</rule>
<rule ref="CakePHP.Commenting.FunctionComment.MissingParamComment">
<severity>0</severity>
</rule>
</ruleset>
9 changes: 6 additions & 3 deletions src/Rector/NodeAnalyzer/FluentChainMethodCallNodeAnalyzer.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare (strict_types=1);
declare(strict_types=1);

namespace Cake\Upgrade\Rector\NodeAnalyzer;

Expand All @@ -20,7 +20,7 @@ final class FluentChainMethodCallNodeAnalyzer
/**
* @api doctrine
*/
public function resolveRootMethodCall(MethodCall $methodCall) : ?MethodCall
public function resolveRootMethodCall(MethodCall $methodCall): ?MethodCall
{
$callerNode = $methodCall->var;
while ($callerNode instanceof MethodCall && $callerNode->var instanceof MethodCall) {
Expand All @@ -29,17 +29,20 @@ public function resolveRootMethodCall(MethodCall $methodCall) : ?MethodCall
if ($callerNode instanceof MethodCall) {
return $callerNode;
}

return null;
}

/**
* @return \PhpParser\Node\Expr|\PhpParser\Node\Name
*/
public function resolveRootExpr(MethodCall $methodCall)
public function resolveRootExpr(MethodCall $methodCall): Expr|Name
{
$callerNode = $methodCall->var;
while ($callerNode instanceof MethodCall || $callerNode instanceof StaticCall) {
$callerNode = $callerNode instanceof StaticCall ? $callerNode->class : $callerNode->var;
}

return $callerNode;
}
}
4 changes: 2 additions & 2 deletions src/Rector/Rector/MethodCall/AddMethodCallArgsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class AddMethodCallArgsRector extends AbstractRector implements Configurab
public const ADD_METHOD_CALL_ARGS = 'add_method_call_args';

/**
* @var \Cake\Upgrade\Rector\ValueObject\AddMethodCallArgs[]
* @var array<\Cake\Upgrade\Rector\ValueObject\AddMethodCallArgs>
*/
private array $callsWithAddMethodCallArgs = [];

Expand Down Expand Up @@ -97,7 +97,7 @@ public function refactor(Node $node): ?Node
}

/**
* @param mixed[] $configuration
* @param array<mixed> $configuration
*/
public function configure(array $configuration): void
{
Expand Down
12 changes: 6 additions & 6 deletions src/Rector/Rector/MethodCall/ArrayToFluentCallRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace Cake\Upgrade\Rector\Rector\MethodCall;

use Cake\Upgrade\Rector\ValueObject\ArrayItemsAndFluentClass;
use Cake\Upgrade\Rector\ValueObject\ArrayToFluentCall;
use Cake\Upgrade\Rector\ValueObject\FactoryMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use Cake\Upgrade\Rector\ValueObject\ArrayItemsAndFluentClass;
use Cake\Upgrade\Rector\ValueObject\ArrayToFluentCall;
use Cake\Upgrade\Rector\ValueObject\FactoryMethod;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand All @@ -31,12 +31,12 @@ final class ArrayToFluentCallRector extends AbstractRector implements Configurab
public const FACTORY_METHODS = 'factory_methods';

/**
* @var \Cake\Upgrade\Rector\ValueObject\ArrayToFluentCall[]
* @var array<\Cake\Upgrade\Rector\ValueObject\ArrayToFluentCall>
*/
private array $arraysToFluentCalls = [];

/**
* @var \Cake\Upgrade\Rector\ValueObject\FactoryMethod[]
* @var array<\Cake\Upgrade\Rector\ValueObject\FactoryMethod>
*/
private array $factoryMethods = [];

Expand Down Expand Up @@ -115,7 +115,7 @@ public function refactor(Node $node): ?Node
}

/**
* @param mixed[] $configuration
* @param array<mixed> $configuration
*/
public function configure(array $configuration): void
{
Expand Down
6 changes: 3 additions & 3 deletions src/Rector/Rector/MethodCall/ModalToGetSetRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace Cake\Upgrade\Rector\Rector\MethodCall;

use Cake\Upgrade\Rector\ValueObject\ModalToGetSet;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use Cake\Upgrade\Rector\ValueObject\ModalToGetSet;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand All @@ -27,7 +27,7 @@ final class ModalToGetSetRector extends AbstractRector implements ConfigurableRe
public const UNPREFIXED_METHODS_TO_GET_SET = 'unprefixed_methods_to_get_set';

/**
* @var \Cake\Upgrade\Rector\ValueObject\ModalToGetSet[]
* @var array<\Cake\Upgrade\Rector\ValueObject\ModalToGetSet>
*/
private array $unprefixedMethodsToGetSet = [];

Expand Down Expand Up @@ -92,7 +92,7 @@ public function refactor(Node $node): ?Node
}

/**
* @param mixed[] $configuration
* @param array<mixed> $configuration
*/
public function configure(array $configuration): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,58 @@

namespace Cake\Upgrade\Rector\Rector\MethodCall;

use Cake\Upgrade\Rector\ValueObject\OptionsArrayToNamedParameters;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use Cake\Upgrade\Rector\ValueObject\OptionsArrayToNamedParameters;
use PhpParser\Node\Arg;
use PhpParser\Node\Identifier;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

use function RectorPrefix202304\dump_node;

final class OptionsArrayToNamedParametersRector extends AbstractRector implements ConfigurableRectorInterface
{
public const OPTIONS_TO_NAMED_PARAMETERS = 'options_to_named_parameters';

/**
* @var \Cake\Upgrade\Rector\ValueObject\OptionsArrayToNamedParameters
* @var array<\Cake\Upgrade\Rector\ValueObject\OptionsArrayToNamedParameters>
*/
private $optionsToNamed = [];
private array $optionsToNamed = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Converts trailing options arrays into named parameters. Will preserve all other arguments.', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
use Cake\ORM\TableRegistry;
$articles = TableRegistry::get('Articles');
$query = $articles->find('list', ['field' => ['title']]);
$query = $articles->find('all', ['conditions' => ['Articles.title' => $title]]);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Cake\ORM\TableRegistry;
$articles = TableRegistry::get('Articles');
$query = $articles->find('list', field: ['title']]);
$query = $articles->find('all', conditions: ['Articles.title' => $title]);
CODE_SAMPLE
,
[
return new RuleDefinition(
'Converts trailing options arrays into named parameters. Will preserve all other arguments.',
[
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
use Cake\ORM\TableRegistry;
$articles = TableRegistry::get('Articles');
$query = $articles->find('list', ['field' => ['title']]);
$query = $articles->find('all', ['conditions' => ['Articles.title' => $title]]);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Cake\ORM\TableRegistry;
$articles = TableRegistry::get('Articles');
$query = $articles->find('list', field: ['title']]);
$query = $articles->find('all', conditions: ['Articles.title' => $title]);
CODE_SAMPLE
,
[
new OptionsArrayToNamedParameters('Table', ['find']),
],
]
),
]);
[
new OptionsArrayToNamedParameters('Table', ['find']),
],
]
),
]
);
}

/**
Expand Down Expand Up @@ -82,23 +82,28 @@ public function refactor(Node $node): ?Node
if (!$this->matchTypeAndMethodName($optionsToNamed, $node)) {
continue;
}
return $this->replaceMethodCall($optionsToNamed, $node);

return $this->replaceMethodCall($optionsToNamed, $node);
}

return null;
}

private function matchtypeAndMethodName(OptionsArrayToNamedParameters $optionsToNamed, MethodCall $methodCall): bool
{
private function matchtypeAndMethodName(
OptionsArrayToNamedParameters $optionsToNamed,
MethodCall $methodCall
): bool {
if (!$this->isObjectType($methodCall->var, $optionsToNamed->getObjectType())) {
return false;
}

return $methodCall->name == $optionsToNamed->getMethod();
}

private function replaceMethodCall(OptionsArrayToNamedParameters $optionsToNamed, MethodCall $methodCall): ?MethodCall
{
private function replaceMethodCall(
OptionsArrayToNamedParameters $optionsToNamed,
MethodCall $methodCall
): ?MethodCall {
$argCount = count($methodCall->args);
// Only modify method calls that have exactly two arguments.
// This is important for idempotency.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace Cake\Upgrade\Rector\Rector\MethodCall;

use Cake\Upgrade\Rector\NodeAnalyzer\FluentChainMethodCallNodeAnalyzer;
use Cake\Upgrade\Rector\ValueObject\RemoveIntermediaryMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use Cake\Upgrade\Rector\NodeAnalyzer\FluentChainMethodCallNodeAnalyzer;
use Cake\Upgrade\Rector\ValueObject\RemoveIntermediaryMethod;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand All @@ -28,7 +28,7 @@ final class RemoveIntermediaryMethodRector extends AbstractRector implements Con
public const REMOVE_INTERMEDIARY_METHOD = 'remove_intermediary_method';

/**
* @var \Cake\Upgrade\Rector\ValueObject\RemoveIntermediaryMethod[]
* @var array<\Cake\Upgrade\Rector\ValueObject\RemoveIntermediaryMethod>
*/
private array $removeIntermediaryMethod = [];

Expand Down Expand Up @@ -86,7 +86,7 @@ public function refactor(Node $node): ?Node
}

/**
* @param mixed[] $configuration
* @param array<mixed> $configuration
*/
public function configure(array $configuration): void
{
Expand Down
21 changes: 11 additions & 10 deletions src/Rector/Rector/MethodCall/RemoveMethodCallRector.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php

declare (strict_types = 1);
declare(strict_types=1);

namespace Cake\Upgrade\Rector\Rector\MethodCall;

Expand All @@ -24,9 +23,9 @@ final class RemoveMethodCallRector extends AbstractRector implements Configurabl
public const REMOVE_METHOD_CALL_ARGS = 'remove_method_call_args';

/**
* @var \Cake\Upgrade\Rector\ValueObject\RemoveMethodCall[]
* @var array<\Cake\Upgrade\Rector\ValueObject\RemoveMethodCall>
*/
private $callsWithRemoveMethodCallArgs = [];
private array $callsWithRemoveMethodCallArgs = [];

public function getRuleDefinition(): RuleDefinition
{
Expand All @@ -36,24 +35,26 @@ public function getRuleDefinition(): RuleDefinition
$obj = new SomeClass();
$obj->methodCall1();
$obj->methodCall2();
CODE_SAMPLE, <<<'CODE_SAMPLE'
CODE_SAMPLE,
<<<'CODE_SAMPLE'
$obj = new SomeClass();
$obj->methodCall2();
CODE_SAMPLE, ['SomeClass', 'methodCall1']
)
CODE_SAMPLE,
['SomeClass', 'methodCall1']
),
]);
}

/**
* @return array<class-string<Node>>
* @return array<class-string<\PhpParser\Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class];
}

/**
* @param Expression $node
* @param \PhpParser\Node\Stmt\Expression $node
*/
public function refactor(Node $node): ?int
{
Expand All @@ -77,7 +78,7 @@ public function refactor(Node $node): ?int
}

/**
* @param mixed[] $configuration
* @param array<mixed> $configuration
*/
public function configure(array $configuration): void
{
Expand Down
Loading

0 comments on commit 1db9833

Please sign in to comment.