Skip to content

Fix test warnings #335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
colors="true"
executionOrder="defects"
cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerWarnings="true"
>
<testsuites>
<testsuite name="main">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public function refactor(Node $node): ?Node

$classMethods = $node->getMethods();

$hasChanged = false;

foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof ClassMethod) {
continue;
Expand All @@ -68,12 +70,17 @@ public function refactor(Node $node): ?Node

if ($newNode instanceof ClassMethod) {
$node->stmts[$key] = $newNode;
$hasChanged = true;
} elseif ($newNode === NodeVisitor::REMOVE_NODE) {
unset($node->stmts[$key]);
$hasChanged = true;
}
}

return $node;
return $hasChanged
? $node
: null;

}

public function getRuleDefinition(): RuleDefinition
Expand Down
22 changes: 15 additions & 7 deletions src/Rector/Class_/AddExtendsAnnotationToModelFactoriesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function refactor(Node $node): ?Node
return null;
}

$hasChanged = false;

foreach ($node->stmts as $stmt) {
if (! $stmt instanceof Property) {
continue;
Expand All @@ -92,32 +94,36 @@ public function refactor(Node $node): ?Node
continue;
}

$this->addExtendsPhpDocTag($node, $stmt);
$hasChanged = $this->addExtendsPhpDocTag($node, $stmt);

break;
}

if ($hasChanged) {
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

break;
return $node;
}

return $node;
return null;
}

public function addExtendsPhpDocTag(Node $node, Property $property): void
private function addExtendsPhpDocTag(Node $node, Property $property): bool
{
if ($property->props === []) {
return;
return false;
}

$modelName = $this->getModelName($property->props[0]->default);

if ($modelName === null) {
return;
return false;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

if ($phpDocInfo->hasByName(self::EXTENDS_TAG_NAME)) {
return;
return false;
}

$phpDocTagNode = new PhpDocTagNode(self::EXTENDS_TAG_NAME, new ExtendsTagValueNode(
Expand All @@ -129,6 +135,8 @@ public function addExtendsPhpDocTag(Node $node, Property $property): void
));

$phpDocInfo->addPhpDocTagNode($phpDocTagNode);

return true;
}

private function getModelName(?Expr $expr): ?string
Expand Down
9 changes: 8 additions & 1 deletion src/Rector/Class_/ReplaceExpectsMethodsInTestsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public function refactor(Node $node): ?Class_
return null;
}

$hasChanged = false;

foreach ($node->getMethods() as $classMethod) {
$result = $this->expectedClassMethodAnalyzer->findExpectedJobCallsWithClassMethod($classMethod);

Expand All @@ -96,6 +98,7 @@ public function refactor(Node $node): ?Class_

if ($result->isActionable()) {
$this->fixUpClassMethod($classMethod, $result, 'Illuminate\Support\Facades\Bus');
$hasChanged = true;
}

$result = $this->expectedClassMethodAnalyzer->findExpectedEventCallsWithClassMethod($classMethod);
Expand All @@ -106,10 +109,14 @@ public function refactor(Node $node): ?Class_

if ($result->isActionable()) {
$this->fixUpClassMethod($classMethod, $result, 'Illuminate\Support\Facades\Event');
$hasChanged = true;
}
}

return $node;
return $hasChanged
? $node
: null;

}

private function fixUpClassMethod(
Expand Down
12 changes: 7 additions & 5 deletions src/Rector/MethodCall/EloquentOrderByToLatestOrOldestRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ private function isOrderByMethodCall(MethodCall $methodCall): bool

private function isAllowedPattern(MethodCall $methodCall): bool
{
$columnArg = $methodCall->args[0] instanceof Arg ? $methodCall->args[0]->value : null;
$columnArg = $methodCall->args !== [] && $methodCall->args[0] instanceof Arg
? $methodCall->args[0]->value
: null;

// If no patterns are specified, consider all column names as matching
if ($this->allowedPatterns === []) {
Expand Down Expand Up @@ -142,19 +144,19 @@ private function isAllowedPattern(MethodCall $methodCall): bool
return false;
}

private function convertOrderByToLatest(MethodCall $methodCall): MethodCall
private function convertOrderByToLatest(MethodCall $methodCall): ?MethodCall
{
if (! isset($methodCall->args[0]) && ! $methodCall->args[0] instanceof VariadicPlaceholder) {
return $methodCall;
return null;
}

$columnVar = $methodCall->args[0] instanceof Arg ? $methodCall->args[0]->value : null;
if (! $columnVar instanceof Expr) {
return $methodCall;
return null;
}

if (isset($methodCall->args[1]) && (! $methodCall->args[1] instanceof Arg || ! $methodCall->args[1]->value instanceof String_)) {
return $methodCall;
return null;
}

if (isset($methodCall->args[1]) && $methodCall->args[1] instanceof Arg && $methodCall->args[1]->value instanceof String_) {
Expand Down
27 changes: 22 additions & 5 deletions src/Rector/MethodCall/UseComponentPropertyWithinCommandsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,36 @@ public function refactor(Node $node): ?Node
return null;
}

$hasChanged = false;

foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof ClassMethod) {
continue;
}

$node->stmts[$key] = $this->refactorClassMethod($stmt);
$changedClassMethod = $this->refactorClassMethod($stmt);

if ($changedClassMethod instanceof ClassMethod) {
$node->stmts[$key] = $changedClassMethod;

$hasChanged = true;
}
}

return $node;
return $hasChanged
? $node
: null;

}

private function refactorClassMethod(ClassMethod $classMethod): ClassMethod
private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
{
if ($classMethod->stmts === null) {
return $classMethod;
return null;
}

$hasChanged = false;

foreach ($classMethod->stmts as $stmt) {
if (! $stmt instanceof Expression) {
continue;
Expand Down Expand Up @@ -123,8 +136,12 @@ private function refactorClassMethod(ClassMethod $classMethod): ClassMethod

$stmt->expr->var =
new PropertyFetch(new Variable('this'), 'components');

$hasChanged = true;
}

return $classMethod;
return $hasChanged
? $classMethod
: null;
}
}