Skip to content

Commit

Permalink
Ensure isFirstClassCallable() before getArgs() on other rules (#182)
Browse files Browse the repository at this point in the history
* Ensure isFirstClassCallable() before getArgs()

* fix

* fix phpstan
  • Loading branch information
samsonasik committed Jun 2, 2023
1 parent b980109 commit 71fabfb
Show file tree
Hide file tree
Showing 34 changed files with 138 additions and 1 deletion.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ parameters:
# rector co-variant
- '#Parameter \#1 \$node (.*?) of method Rector\\(.*?)\(\) should be contravariant with parameter \$node \(PhpParser\\Node\) of method Rector\\Core\\Contract\\Rector\\PhpRectorInterface\:\:refactor\(\)#'

- '#Cognitive complexity for "Rector\\PHPUnit\\Rector\\ClassMethod\\CreateMockToAnonymousClassRector\:\:refactor\(\)" is 23, keep it under 9#'
- '#Cognitive complexity for "Rector\\PHPUnit\\Rector\\ClassMethod\\CreateMockToAnonymousClassRector\:\:refactor\(\)" is 25, keep it under 9#'

- '#Cognitive complexity for "Rector\\PHPUnit\\Rector\\MethodCall\\DelegateExceptionArgumentsRector\:\:refactor\(\)" is 12, keep it under 9#'

Expand Down
4 changes: 4 additions & 0 deletions src/NodeFactory/ArgumentShiftingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ final class ArgumentShiftingFactory
{
public function removeAllButFirstArgMethodCall(MethodCall $methodCall, string $methodName): void
{
if ($methodCall->isFirstClassCallable()) {
return;
}

$methodCall->name = new Identifier($methodName);
foreach (array_keys($methodCall->getArgs()) as $i) {
// keep first arg
Expand Down
4 changes: 4 additions & 0 deletions src/NodeFactory/ExpectExceptionCodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function create(MethodCall $methodCall, Variable $exceptionVariable): ?Me
return null;
}

if ($methodCall->isFirstClassCallable()) {
return null;
}

$secondArgument = $methodCall->getArgs()[1]
->value;
if (! $secondArgument instanceof MethodCall) {
Expand Down
4 changes: 4 additions & 0 deletions src/NodeFactory/ExpectExceptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function create(MethodCall $methodCall, Variable $variable): ?MethodCall
return null;
}

if ($methodCall->isFirstClassCallable()) {
return null;
}

$argumentVariableName = $this->nodeNameResolver->getName($methodCall->getArgs()[1]->value);
if ($argumentVariableName === null) {
return null;
Expand Down
4 changes: 4 additions & 0 deletions src/NodeFactory/ExpectExceptionMessageFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function create(MethodCall $methodCall, Variable $exceptionVariable): ?Me
return null;
}

if ($methodCall->isFirstClassCallable()) {
return null;
}

$secondArgument = $methodCall->getArgs()[1]
->value;
if (! $secondArgument instanceof MethodCall) {
Expand Down
4 changes: 4 additions & 0 deletions src/NodeFactory/ExpectExceptionMessageRegExpFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function create(MethodCall $methodCall, Variable $exceptionVariable): ?Me
return null;
}

if ($methodCall->isFirstClassCallable()) {
return null;
}

$secondArgument = $methodCall->getArgs()[1]
->value;
if (! $secondArgument instanceof MethodCall) {
Expand Down
4 changes: 4 additions & 0 deletions src/NodeManipulator/ArgumentMover.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ final class ArgumentMover
{
public function removeFirst(MethodCall|StaticCall $node): void
{
if ($node->isFirstClassCallable()) {
return;
}

$methodArguments = $node->getArgs();
array_shift($methodArguments);

Expand Down
9 changes: 9 additions & 0 deletions src/Rector/ClassMethod/CreateMockToAnonymousClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ public function refactor(Node $node): ?Node
continue;
}

if ($methodCall->isFirstClassCallable()) {
continue;
}

// has dynamic return?
if ($hasDynamicReturnExprs === false) {
$returnedExpr = $methodCall->getArgs()[0]
Expand All @@ -146,6 +150,11 @@ public function refactor(Node $node): ?Node
// change to anonymous class
/** @var MethodCall $methodCall */
$methodCall = $createMockMethodCallAssign->expr;

if ($methodCall->isFirstClassCallable()) {
continue;
}

$firstArg = $methodCall->getArgs()[0];

$mockExpr = $createMockMethodCallAssign->var;
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/Class_/ArrayArgumentToDataProviderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ private function refactorMethodCallWithConfiguration(
return;
}

if ($methodCall->isFirstClassCallable()) {
return;
}

if (count($methodCall->getArgs()) !== 1) {
throw new ShouldNotHappenException();
}
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/Class_/ProphecyPHPDocRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ private function matchThisProphesizeMethodCallFirstArg(Expr $expr): ?Arg
return null;
}

if ($expr->isFirstClassCallable()) {
return null;
}

return $expr->getArgs()[0];
}

Expand Down
4 changes: 4 additions & 0 deletions src/Rector/Foreach_/SimplifyForeachInstanceOfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($expr->isFirstClassCallable()) {
return null;
}

if (! $this->nodeComparator->areNodesEqual($node->valueVar, $expr->getArgs()[1]->value)) {
return null;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/AssertCompareToSpecificMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

// we need 2 args
if (! isset($node->args[1])) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$firstArgumentValue = $node->getArgs()[0]
->value;
if (! $firstArgumentValue instanceof BinaryOp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

// 1. refactor to "assertEqualsIgnoringCase()"
$newMethodCall = $this->processAssertEqualsIgnoringCase($node);
if ($newMethodCall !== null) {
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/AssertEqualsToSameRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$args = $node->getArgs();
if (! isset($args[0])) {
return null;
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/AssertFalseStrposToContainsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$firstArgumentValue = $node->getArgs()[0]
->value;
if ($firstArgumentValue instanceof StaticCall) {
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/AssertInstanceOfComparisonRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$firstArgumentValue = $node->getArgs()[0]
->value;
if (! $firstArgumentValue instanceof Instanceof_) {
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/AssertIssetToSpecificMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$firstArgumentValue = $node->getArgs()[0]
->value;
// is property access
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/AssertNotOperatorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$firstArgumentValue = $node->getArgs()[0]
->value;
if (! $firstArgumentValue instanceof BooleanNot) {
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/AssertPropertyExistsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$firstArgumentValue = $node->getArgs()[0]
->value;
if (! $firstArgumentValue instanceof FuncCall) {
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/AssertRegExpRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

/** @var FuncCall|Node $secondArgumentValue */
$secondArgumentValue = $node->getArgs()[1]
->value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

if (! isset($node->getArgs()[0])) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$firstArgumentValue = $node->getArgs()[0]
->value;
if (! $firstArgumentValue instanceof ConstFetch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$firstArg = $node->getArgs()[0];

if ($this->valueResolver->isTrue($firstArg->value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$firstArgumentValue = $node->getArgs()[0]
->value;
if (! $firstArgumentValue instanceof FuncCall) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

if (! isset($node->args[0])) {
return null;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/ExplicitPhpErrorApiRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ private function replaceExceptionWith(
string $exceptionClass,
string $explicitMethod
): ?Node {
if ($node->isFirstClassCallable()) {
return null;
}

if (! isset($node->getArgs()[0])) {
return null;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/RemoveExpectAnyFromMockRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

if (count($node->args) !== 1) {
return null;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/SpecificAssertContainsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

if (! $this->isPossiblyStringType($node->getArgs()[1]->value)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

// when second argument is string: do nothing
$secondArgType = $this->getType($node->getArgs()[1]->value);
if ($secondArgType instanceof StringType) {
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/SpecificAssertInternalTypeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$typeNode = $node->getArgs()[0]
->value;
if (! $typeNode instanceof String_) {
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/UseSpecificWillMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

$callArgs = $node->getArgs();
if (! $callArgs[0]->value instanceof MethodCall) {
return null;
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/MethodCall/UseSpecificWithMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

foreach ($node->getArgs() as $i => $argNode) {
if (! $argNode->value instanceof MethodCall) {
continue;
Expand Down
4 changes: 4 additions & 0 deletions src/Rector/StaticCall/GetMockRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

// narrow args to one
if (count($node->args) > 1) {
$node->args = [$node->getArgs()[0]];
Expand Down

0 comments on commit 71fabfb

Please sign in to comment.