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

declare(strict_types=1);

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

use PHPUnit\Framework\TestCase;

final class IncludeNoArgs extends TestCase
{
public function test()
{
$someMock = $this->getMockBuilder('AnyType')->getMock();

$someMock->expects($this->any())
->method('trans')
->with($this->callback(function (): bool {
$args= [1, 2, 3];
return count($args) === 5 && $args[0] === 'some_value';
}));
}
}

?>
-----
<?php

declare(strict_types=1);

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

use PHPUnit\Framework\TestCase;

final class IncludeNoArgs extends TestCase
{
public function test()
{
$someMock = $this->getMockBuilder('AnyType')->getMock();

$someMock->expects($this->any())
->method('trans')
->with($this->callback(function (): bool {
$args= [1, 2, 3];
$this->assertCount(5, $args);
$this->assertSame('some_value', $args[0]);
return true;
}));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

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

use PHPUnit\Framework\TestCase;

final class MultiCallbacks extends TestCase
{
public function test()
{
$someMock = $this->getMockBuilder('AnyType')->getMock();

$someMock->expects($this->any())
->method('trans')
->with(
$this->callback(fn (array $args) => array_key_exists(5, $args)),
$this->callback(fn (array $args) => array_key_exists(50, $args)),
);
}
}

?>
-----
<?php

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

use PHPUnit\Framework\TestCase;

final class MultiCallbacks extends TestCase
{
public function test()
{
$someMock = $this->getMockBuilder('AnyType')->getMock();

$someMock->expects($this->any())
->method('trans')
->with(
$this->callback(function (array $args): bool {
$this->assertArrayHasKey(5, $args);
return true;
}),
$this->callback(function (array $args): bool {
$this->assertArrayHasKey(50, $args);
return true;
}),
);
}
}

?>

This file was deleted.

74 changes: 74 additions & 0 deletions rules/CodeQuality/NodeAnalyser/ClosureUsesResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;

use PhpParser\Node\ClosureUse;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Variable;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpParser\Node\BetterNodeFinder;

final readonly class ClosureUsesResolver
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeNameResolver $nodeNameResolver
) {

}

/**
* @return ClosureUse[]
*/
public function resolveFromArrowFunction(ArrowFunction $arrowFunction): array
{
// fill needed uses from arrow function to closure
$arrowFunctionVariables = $this->betterNodeFinder->findInstancesOfScoped(
$arrowFunction->getStmts(),
Variable::class
);

$paramNames = $this->resolveParamNames($arrowFunction);

$externalVariableNames = [];

foreach ($arrowFunctionVariables as $arrowFunctionVariable) {
// skip those defined in params
if ($this->nodeNameResolver->isNames($arrowFunctionVariable, $paramNames)) {
continue;
}

$variableName = $this->nodeNameResolver->getName($arrowFunctionVariable);
if (! is_string($variableName)) {
continue;
}

$externalVariableNames[] = $variableName;
}

$externalVariableNames = array_unique($externalVariableNames);
$externalVariableNames = array_diff($externalVariableNames, ['this']);

$closureUses = [];
foreach ($externalVariableNames as $externalVariableName) {
$closureUses[] = new ClosureUse(new Variable($externalVariableName));
}

return $closureUses;
}

/**
* @return string[]
*/
private function resolveParamNames(ArrowFunction $arrowFunction): array
{
$paramNames = [];
foreach ($arrowFunction->getParams() as $param) {
$paramNames[] = $this->nodeNameResolver->getName($param);
}

return $paramNames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function __construct(

/**
* @param Expr[] $exprs
* @return Stmt[]|null
* @return Stmt[]
*/
public function create(array $exprs): ?array
public function create(array $exprs): array
{
$assertMethodCalls = [];

Expand Down Expand Up @@ -62,7 +62,7 @@ public function create(array $exprs): ?array
);
} else {
// not supported yet
return null;
return [];
}
}

Expand Down Expand Up @@ -102,7 +102,7 @@ public function create(array $exprs): ?array
}

// unclear, fallback to no change
return null;
return [];
}

// create assertSame()
Expand All @@ -113,12 +113,12 @@ public function create(array $exprs): ?array
);
} else {
// not supported expr
return null;
return [];
}
}

if ($assertMethodCalls === []) {
return null;
return [];
}

// to keep order from binary
Expand Down
Loading