Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AssertInsideAnonymousClass extends TestCase
{
public function some($response)
{
return new class($response) {
public function __construct($response)
{
assert((bool) $response);
}
};
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;

use PHPUnit\Framework\TestCase;

final class AssertInsideAnonymousClass extends TestCase
{
public function some($response)
{
return new class($response) {
public function __construct($response)
{
\PHPUnit\Framework\Assert::assertTrue((bool) $response);
}
};
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public function isEntityClass(string $className): bool
return false;
}

return array_any(self::ENTITY_DOCBLOCK_MARKERS, fn(string $entityDocBlockMarkers): bool => str_contains($resolvedPhpDocBlock->getPhpDocString(), $entityDocBlockMarkers));
return array_any(
self::ENTITY_DOCBLOCK_MARKERS,
fn (string $entityDocBlockMarkers): bool => str_contains(
$resolvedPhpDocBlock->getPhpDocString(),
$entityDocBlockMarkers
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod;

use function in_array;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
Expand All @@ -16,6 +15,7 @@
use Rector\Util\NewLineSplitter;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use function in_array;

/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector\ReplaceTestAnnotationWithPrefixedFunctionRectorTest
Expand Down Expand Up @@ -86,7 +86,10 @@ public function refactor(Node $node): ?Node
return null;
}

$hasAnnotation = array_any(NewLineSplitter::split($docComment->getText()), fn(string $row): bool => in_array(trim($row), ['*@test', '* @test'], true));
$hasAnnotation = array_any(
NewLineSplitter::split($docComment->getText()),
fn (string $row): bool => in_array(trim($row), ['*@test', '* @test'], true)
);

if (! $hasAnnotation) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Rector\PHPUnit\CodeQuality\Rector\Class_;

use PhpParser\Node\Identifier;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Rector\PHPUnit\CodeQuality\Rector\Class_;

use PhpParser\Node\Identifier;
use PhpParser\Node;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,6 @@ public function refactor(Node $node): ?array
*/
private function hasAllKeysString(array $assertedArrayValues): bool
{
return array_all(array_keys($assertedArrayValues), fn(int|string $key): bool => is_string($key));
return array_all(array_keys($assertedArrayValues), fn (int|string $key): bool => is_string($key));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public function refactor(Node $node): ?Class_
return null;
}

// assert() inside an anonymous class has no $this of the test case
if ($node instanceof Class_ && $node->isAnonymous()) {
$useStaticAssert = true;
return null;
}

if (! $node instanceof FuncCall) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ private function hasRelateParentClass(Class_ $class): bool
return false;
}

return array_any(self::PARENT_CLASSES, fn(string $parentClass): bool => $classReflection->is($parentClass));
return array_any(self::PARENT_CLASSES, fn (string $parentClass): bool => $classReflection->is($parentClass));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ private function shouldAddAttribute(array $missedTestMethodsByMockPropertyName):
*/
private function isAtLeastOneMockPropertyMockedOnce(array $usingTestMethodsByMockPropertyName): bool
{
return array_any($usingTestMethodsByMockPropertyName, fn(array $usingTestMethods): bool => $usingTestMethods !== []);
return array_any(
$usingTestMethodsByMockPropertyName,
fn (array $usingTestMethods): bool => $usingTestMethods !== []
);
}

private function isMissingExpectsOnMockObjectMethodCallInSetUp(Class_ $class): bool
Expand Down
5 changes: 4 additions & 1 deletion src/NodeAnalyzer/AssertCallAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ public function isAssertMethodCall(MethodCall|StaticCall $call): bool
return false;
}

return array_any(self::ASSERT_METHOD_NAME_PREFIXES, fn(string $assertMethodNamePrefix): bool => str_starts_with($callName, $assertMethodNamePrefix));
return array_any(
self::ASSERT_METHOD_NAME_PREFIXES,
fn (string $assertMethodNamePrefix): bool => str_starts_with($callName, $assertMethodNamePrefix)
);
}

private function hasDirectAssertOrMockCall(ClassMethod $classMethod): bool
Expand Down
5 changes: 4 additions & 1 deletion src/NodeAnalyzer/TestsNodeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public function isInTestClass(Node $node): bool
return false;
}

return array_any(PHPUnitClassName::TEST_CLASSES, fn(string $testCaseObjectClass): bool => $classReflection->is($testCaseObjectClass));
return array_any(
PHPUnitClassName::TEST_CLASSES,
fn (string $testCaseObjectClass): bool => $classReflection->is($testCaseObjectClass)
);
}

public function isTestClassMethod(ClassMethod $classMethod): bool
Expand Down
Loading