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,47 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;

use PHPUnit\Framework\TestCase;

final class OnStaticClosure extends TestCase
{
public function test()
{
$this->createMock('SomeClass')
->expects($this->once())
->method('someMethod')
->with($this->callback(static function (array $args): bool {
return count($args) === 2 && $args[0] === 'correct';
}));
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WithCallbackIdenticalToStandaloneAssertsRector\Fixture;

use PHPUnit\Framework\TestCase;

final class OnStaticClosure extends TestCase
{
public function test()
{
$this->createMock('SomeClass')
->expects($this->once())
->method('someMethod')
->with($this->callback(static function (array $args): bool {
self::assertCount(2, $args);
self::assertSame('correct', $args[0]);
return true;
}));
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\Int_;
Expand All @@ -33,14 +34,14 @@ public function __construct(
* @param Expr[] $exprs
* @return Stmt[]
*/
public function create(array $exprs): array
public function create(array $exprs, bool $isStaticClosure = false): array
{
$assertMethodCalls = [];

foreach ($exprs as $expr) {
// implicit bool compare
if ($expr instanceof MethodCall) {
$assertMethodCalls[] = $this->nodeFactory->createMethodCall('this', 'assertTrue', [$expr]);
$assertMethodCalls[] = $this->createAssertMethodCall($isStaticClosure, 'assertTrue', [$expr]);

continue;
}
Expand All @@ -51,8 +52,8 @@ public function create(array $exprs): array
$dimExpr = $expr->getArgs()[0]
->value;

$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
$assertMethodCalls[] = $this->createAssertMethodCall(
$isStaticClosure,
'assertArrayHasKey',
[$dimExpr, $variableExpr]
);
Expand All @@ -62,9 +63,9 @@ public function create(array $exprs): array

if ($expr instanceof Isset_) {
foreach ($expr->vars as $issetVariable) {
if ($issetVariable instanceof ArrayDimFetch) {
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
if ($issetVariable instanceof ArrayDimFetch && $issetVariable->dim instanceof Expr) {
$assertMethodCalls[] = $this->createAssertMethodCall(
$isStaticClosure,
'assertArrayHasKey',
[$issetVariable->dim, $issetVariable->var]
);
Expand All @@ -86,8 +87,8 @@ public function create(array $exprs): array
$classNameExpr = $expr->class;
}

$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
$assertMethodCalls[] = $this->createAssertMethodCall(
$isStaticClosure,
'assertInstanceOf',
[$classNameExpr, $expr->expr]
);
Expand All @@ -102,8 +103,8 @@ public function create(array $exprs): array
->value;

// create assertCount()
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
$assertMethodCalls[] = $this->createAssertMethodCall(
$isStaticClosure,
'assertCount',
[$expr->right, $countedExpr]
);
Expand All @@ -116,8 +117,8 @@ public function create(array $exprs): array
}

// create assertSame()
$assertMethodCalls[] = $this->nodeFactory->createMethodCall(
'this',
$assertMethodCalls[] = $this->createAssertMethodCall(
$isStaticClosure,
$expr instanceof Identical ? 'assertSame' : 'assertEquals',
[$expr->right, $expr->left]
);
Expand All @@ -141,4 +142,16 @@ public function create(array $exprs): array

return $stmts;
}

/**
* @param Expr[] $args
*/
private function createAssertMethodCall(bool $isStaticClosure, string $method, array $args): MethodCall|StaticCall
{
if ($isStaticClosure) {
return new StaticCall(new Name('self'), $method, $this->nodeFactory->createArgs($args));
}

return $this->nodeFactory->createMethodCall('this', $method, $args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function refactor(Node $node): ?Node

$hasAnnotation = false;
foreach(NewLineSplitter::split($docComment->getText()) as $row) {
if (in_array(trim($row), ['*@test', '* @test'])) {
if (in_array(trim($row), ['*@test', '* @test'], true)) {
$hasAnnotation = true;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ public function refactor(Node $node): MethodCall|null
continue;
}

$assertExprStmts = $this->fromBinaryAndAssertExpressionsFactory->create($joinedExprs);
$innerFunctionLike = $argAndFunctionLike->getFunctionLike();
$isStaticClosure = $innerFunctionLike instanceof Closure && $innerFunctionLike->static;

$assertExprStmts = $this->fromBinaryAndAssertExpressionsFactory->create($joinedExprs, $isStaticClosure);
if ($assertExprStmts === []) {
continue;
}
Expand All @@ -134,7 +137,6 @@ public function refactor(Node $node): MethodCall|null

// last si return true;
$assertExprStmts[] = new Return_($this->nodeFactory->createTrue());
$innerFunctionLike = $argAndFunctionLike->getFunctionLike();

if ($innerFunctionLike instanceof Closure) {
$innerFunctionLike->stmts = array_merge($nonReturnCallbackStmts, $assertExprStmts);
Expand Down
Loading