Updated Rector to commit 6eac87db32eff9308a037fb32ce973ccf905fc92

6eac87db32 Ignore phpdoc type in RecastingRemovalRector (#5841)
This commit is contained in:
Tomas Votruba 2024-04-22 20:26:54 +00:00
parent fdfd07f062
commit 09acbdda13
5 changed files with 180 additions and 46 deletions

View File

@ -1802,12 +1802,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/rectorphp\/rector-phpunit.git",
"reference": "e8af39b93f68f4a7a81c20866600175ae239eb02"
"reference": "b9cff121a6821e6dedfbbfc59f4989c48505438b"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/e8af39b93f68f4a7a81c20866600175ae239eb02",
"reference": "e8af39b93f68f4a7a81c20866600175ae239eb02",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/b9cff121a6821e6dedfbbfc59f4989c48505438b",
"reference": "b9cff121a6821e6dedfbbfc59f4989c48505438b",
"shasum": ""
},
"require": {
@ -1830,7 +1830,7 @@
"tomasvotruba\/class-leak": "^0.2",
"tracy\/tracy": "^2.10"
},
"time": "2024-04-17T07:15:20+00:00",
"time": "2024-04-22T20:22:53+00:00",
"default-branch": true,
"type": "rector-extension",
"extra": {

File diff suppressed because one or more lines are too long

View File

@ -9,7 +9,7 @@ namespace Rector\RectorInstaller;
*/
final class GeneratedConfig
{
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main ea7e844'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main 05e44cf'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main e8af39b'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main b8126e8'));
public const EXTENSIONS = array('rector/rector-doctrine' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-doctrine', 'relative_install_path' => '../../rector-doctrine', 'extra' => NULL, 'version' => 'dev-main ea7e844'), 'rector/rector-downgrade-php' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-downgrade-php', 'relative_install_path' => '../../rector-downgrade-php', 'extra' => NULL, 'version' => 'dev-main 05e44cf'), 'rector/rector-phpunit' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-phpunit', 'relative_install_path' => '../../rector-phpunit', 'extra' => NULL, 'version' => 'dev-main b9cff12'), 'rector/rector-symfony' => array('install_path' => '/home/runner/work/rector-src/rector-src/rector-build/vendor/rector/rector-symfony', 'relative_install_path' => '../../rector-symfony', 'extra' => NULL, 'version' => 'dev-main b8126e8'));
private function __construct()
{
}

View File

@ -1198,12 +1198,12 @@ Refactor deprecated `withConsecutive()` to `willReturnCallback()` structure
- [1, 2],
- [3, 4],
- );
+ ->willReturnCallback(function () use ($matcher) {
+ return match ($matcher->numberOfInvocations()) {
+ 1 => [1, 2],
+ 2 => [3, 4]
+ ->willReturnCallback(function ($parameters) use ($matcher) {
+ match ($matcher->numberOfInvocations()) {
+ 1 => self::assertEquals([1, 2], $parameters),
+ 2 => self::assertEquals([3, 4], $parameters),
+ };
+ });
+ });
- $this->userServiceMock->expects(self::exactly(2))
+ $matcher = self::exactly(2);
@ -1214,12 +1214,12 @@ Refactor deprecated `withConsecutive()` to `willReturnCallback()` structure
- [1, 2],
- [3, 4],
- );
+ ->willReturnCallback(function () use ($matcher) {
+ return match ($matcher->numberOfInvocations()) {
+ 1 => [1, 2],
+ 2 => [3, 4]
+ };
+ });
+ ->willReturnCallback(function ($parameters) use ($matcher) {
+ match ($matcher->numberOfInvocations()) {
+ 1 => self::assertEquals([1, 2], $parameters),
+ 2 => self::assertEquals([3, 4], $parameters),
+ }
+ });
}
}
```

View File

@ -3,8 +3,10 @@
declare (strict_types=1);
namespace Rector\PHPUnit\Rector\StmtsAwareInterface;
use PhpParser\BuilderFactory;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
@ -14,11 +16,13 @@ use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\NodeTraverser;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\Node\BetterNodeFinder;
@ -48,11 +52,17 @@ final class WithConsecutiveRector extends AbstractRector implements MinPhpVersio
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;
public function __construct(TestsNodeAnalyzer $testsNodeAnalyzer, BetterNodeFinder $betterNodeFinder, SimpleCallableNodeTraverser $simpleCallableNodeTraverser)
/**
* @readonly
* @var \PhpParser\BuilderFactory
*/
private $builderFactory;
public function __construct(TestsNodeAnalyzer $testsNodeAnalyzer, BetterNodeFinder $betterNodeFinder, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, BuilderFactory $builderFactory)
{
$this->testsNodeAnalyzer = $testsNodeAnalyzer;
$this->betterNodeFinder = $betterNodeFinder;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
$this->builderFactory = $builderFactory;
}
public function getRuleDefinition() : RuleDefinition
{
@ -90,23 +100,23 @@ final class SomeTest extends TestCase
$this->personServiceMock->expects($matcher)
->method('prepare')
->willReturnCallback(function () use ($matcher) {
return match ($matcher->numberOfInvocations()) {
1 => [1, 2],
2 => [3, 4]
->willReturnCallback(function ($parameters) use ($matcher) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, 2], $parameters),
2 => self::assertEquals([3, 4], $parameters),
};
});
});
$matcher = self::exactly(2);
$this->userServiceMock->expects($matcher)
->method('prepare')
->willReturnCallback(function () use ($matcher) {
return match ($matcher->numberOfInvocations()) {
1 => [1, 2],
2 => [3, 4]
};
});
->willReturnCallback(function ($parameters) use ($matcher) {
match ($matcher->numberOfInvocations()) {
1 => self::assertEquals([1, 2], $parameters),
2 => self::assertEquals([3, 4], $parameters),
}
});
}
}
CODE_SAMPLE
@ -127,17 +137,100 @@ CODE_SAMPLE
if (!$this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
$withConsecutiveMethodCall = $this->findWithConsecutiveMethodCall($node);
$withConsecutiveMethodCall = $this->findMethodCall($node, 'withConsecutive');
if (!$withConsecutiveMethodCall instanceof MethodCall) {
return null;
}
if ($this->hasWillReturnMapOrWill($node)) {
return null;
}
$returnStmts = [];
$willReturn = $this->findMethodCall($node, 'willReturn');
if ($willReturn instanceof MethodCall) {
$args = $willReturn->getArgs();
if (\count($args) !== 1 || !$args[0] instanceof Arg) {
return null;
}
$returnStmts = [new Return_($args[0]->value)];
}
$willReturnSelf = $this->findMethodCall($node, 'willReturnSelf');
if ($willReturnSelf instanceof MethodCall) {
if ($returnStmts !== []) {
return null;
}
$selfVariable = $willReturnSelf;
while (\true) {
if (!$selfVariable instanceof MethodCall) {
break;
}
$selfVariable = $selfVariable->var;
}
$returnStmts = [new Return_($selfVariable)];
}
$willReturnArgument = $this->findMethodCall($node, 'willReturnArgument');
if ($willReturnArgument instanceof MethodCall) {
if ($returnStmts !== []) {
return null;
}
$parametersVariable = new Variable('parameters');
$args = $willReturnArgument->getArgs();
if (\count($args) !== 1 || !$args[0] instanceof Arg) {
return null;
}
$returnStmts = [new Return_(new ArrayDimFetch($parametersVariable, $args[0]->value))];
}
$willReturnOnConsecutiveCallsArgument = $this->findMethodCall($node, 'willReturnOnConsecutiveCalls');
if ($willReturnOnConsecutiveCallsArgument instanceof MethodCall) {
if ($returnStmts !== []) {
return null;
}
$matcherVariable = new Variable('matcher');
$numberOfInvocationsMethodCall = new MethodCall($matcherVariable, new Identifier('numberOfInvocations'));
$matchArms = [];
foreach ($willReturnOnConsecutiveCallsArgument->getArgs() as $key => $arg) {
$matchArms[] = new MatchArm([new LNumber($key + 1)], $arg->value);
}
$returnStmts = [new Return_(new Match_($numberOfInvocationsMethodCall, $matchArms))];
}
$willReturnReferenceArgument = $this->findMethodCall($node, 'willReturnReference');
$referenceVariable = null;
if ($willReturnReferenceArgument instanceof MethodCall) {
if ($returnStmts !== []) {
return null;
}
$args = $willReturnReferenceArgument->args;
if (\count($args) !== 1 || !$args[0] instanceof Arg) {
return null;
}
$referenceVariable = $args[0]->value;
if (!$referenceVariable instanceof Variable) {
return null;
}
$returnStmts = [new Return_($referenceVariable)];
}
$willThrowException = $this->findMethodCall($node, 'willThrowException');
if ($willThrowException instanceof MethodCall) {
if ($returnStmts !== []) {
return null;
}
$args = $willThrowException->getArgs();
if (\count($args) !== 1 || !$args[0] instanceof Arg) {
return null;
}
$returnStmts = [new Throw_($args[0]->value)];
}
/**
* remove willReturn, willReturnArgument, willReturnOnConsecutiveCalls, willReturnReference
* willReturnSelf and willThrowException
*/
$this->removeWills($node);
$expectsCall = $this->matchAndRefactorExpectsMethodCall($node);
if (!$expectsCall instanceof MethodCall && !$expectsCall instanceof StaticCall) {
return null;
}
// 2. rename and replace withConsecutive()
$withConsecutiveMethodCall->name = new Identifier('willReturnCallback');
$withConsecutiveMethodCall->args = [new Arg($this->createClosure($withConsecutiveMethodCall))];
$withConsecutiveMethodCall->args = [new Arg($this->createClosure($withConsecutiveMethodCall, $returnStmts, $referenceVariable))];
$matcherAssign = new Assign(new Variable('matcher'), $expectsCall);
return [new Expression($matcherAssign), $node];
}
@ -173,17 +266,28 @@ CODE_SAMPLE
});
return $foundNodes;
}
private function createClosure(MethodCall $expectsMethodCall) : Closure
/**
* @param Node\Stmt[] $returnStmts
*/
private function createClosure(MethodCall $expectsMethodCall, array $returnStmts, ?Variable $referenceVariable) : Closure
{
$closure = new Closure();
$byRef = $referenceVariable instanceof Variable;
$closure->byRef = $byRef;
$matcherVariable = new Variable('matcher');
$closure->uses[] = new ClosureUse($matcherVariable);
$usedVariables = $this->resolveUniqueUsedVariables($expectsMethodCall);
$usedVariables = $this->resolveUniqueUsedVariables(\array_merge($expectsMethodCall->getArgs(), $this->resolveUniqueUsedVariables($returnStmts)));
foreach ($usedVariables as $usedVariable) {
$closure->uses[] = new ClosureUse($usedVariable);
$closureUse = new ClosureUse($usedVariable);
if ($byRef && $this->getName($usedVariable) === $this->getName($referenceVariable)) {
$closureUse->byRef = \true;
}
$closure->uses[] = $closureUse;
}
$match = $this->createMatch($matcherVariable, $expectsMethodCall);
$closure->stmts[] = new Return_($match);
$parametersVariable = new Variable('parameters');
$match = $this->createMatch($matcherVariable, $expectsMethodCall, $parametersVariable);
$closure->params[] = new Param($parametersVariable);
$closure->stmts = \array_merge([new Expression($match)], $returnStmts);
return $closure;
}
/**
@ -213,39 +317,41 @@ CODE_SAMPLE
});
return $exactlyCall;
}
private function findWithConsecutiveMethodCall(Expression $expression) : ?MethodCall
private function findMethodCall(Expression $expression, string $methodName) : ?MethodCall
{
if (!$expression->expr instanceof MethodCall) {
return null;
}
/** @var MethodCall|null $withConsecutiveMethodCall */
$withConsecutiveMethodCall = $this->betterNodeFinder->findFirst($expression->expr, function (Node $node) : bool {
/** @var MethodCall|null $methodCall */
$methodCall = $this->betterNodeFinder->findFirst($expression->expr, function (Node $node) use($methodName) : bool {
if (!$node instanceof MethodCall) {
return \false;
}
return $this->isName($node->name, 'withConsecutive');
return $this->isName($node->name, $methodName);
});
return $withConsecutiveMethodCall;
return $methodCall;
}
private function createMatch(Variable $matcherVariable, MethodCall $expectsMethodCall) : Match_
private function createMatch(Variable $matcherVariable, MethodCall $expectsMethodCall, Variable $parameters) : Match_
{
$numberOfInvocationsMethodCall = new MethodCall($matcherVariable, new Identifier('numberOfInvocations'));
$matchArms = [];
foreach ($expectsMethodCall->getArgs() as $key => $arg) {
$matchArms[] = new MatchArm([new LNumber($key + 1)], $arg->value);
$assertEquals = $this->builderFactory->staticCall('self', 'assertEquals', [$arg, $parameters]);
$matchArms[] = new MatchArm([new LNumber($key + 1)], $assertEquals);
}
return new Match_($numberOfInvocationsMethodCall, $matchArms);
}
/**
* @param Node[] $nodes
* @return Variable[]
*/
private function resolveUniqueUsedVariables(MethodCall $expectsMethodCall) : array
private function resolveUniqueUsedVariables(array $nodes) : array
{
/** @var Variable[] $usedVariables */
$usedVariables = $this->findInstancesOfScoped($expectsMethodCall->getArgs(), Variable::class);
$usedVariables = $this->findInstancesOfScoped($nodes, Variable::class);
$uniqueUsedVariables = [];
foreach ($usedVariables as $usedVariable) {
if ($this->isName($usedVariable, 'this')) {
if ($this->isNames($usedVariable, ['this', 'matcher', 'parameters'])) {
continue;
}
$usedVariableName = $this->getName($usedVariable);
@ -253,4 +359,32 @@ CODE_SAMPLE
}
return $uniqueUsedVariables;
}
/**
* @param \PhpParser\Node\Stmt\Expression|\PhpParser\Node $node
*/
private function hasWillReturnMapOrWill($node) : bool
{
$nodesWithWillReturnMap = $this->betterNodeFinder->find($node, function (Node $node) : bool {
if (!$node instanceof MethodCall) {
return \false;
}
return $this->isNames($node->name, ['willReturnMap', 'will']);
});
return $nodesWithWillReturnMap !== [];
}
/**
* @param \PhpParser\Node\Stmt\Expression|\PhpParser\Node $expression
*/
private function removeWills($expression) : void
{
$this->traverseNodesWithCallable($expression, function (Node $node) : ?Node {
if (!$node instanceof MethodCall) {
return null;
}
if (!$this->isNames($node->name, ['willReturn', 'willReturnArgument', 'willReturnSelf', 'willReturnOnConsecutiveCalls', 'willReturnReference', 'willThrowException'])) {
return null;
}
return $node->var;
});
}
}