Updated Rector to commit e38d2d90e75643e483b9c79b7967f221b1e8158f

e38d2d90e7 [PhpParser] Use general FunctionLike instanceof check for inner scope check on BetterNodeFinder (#6334)
This commit is contained in:
Tomas Votruba 2024-09-29 13:02:19 +00:00
parent a05b07c1af
commit a454368807
6 changed files with 170 additions and 6 deletions

View File

@ -1766,6 +1766,7 @@ return array(
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\AssertSameTrueFalseToAssertTrueFalseRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertSameTrueFalseToAssertTrueFalseRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\AssertTrueFalseToSpecificMethodRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\NarrowIdenticalWithConsecutiveRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\NarrowSingleWillReturnCallbackRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\RemoveExpectAnyFromMockRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/RemoveExpectAnyFromMockRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\SingleWithConsecutiveToWithRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\UseSpecificWillMethodRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/UseSpecificWillMethodRector.php',

View File

@ -1985,6 +1985,7 @@ class ComposerStaticInit37dd0ee81214e645ea9e766f4c5554db
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\AssertSameTrueFalseToAssertTrueFalseRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertSameTrueFalseToAssertTrueFalseRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\AssertTrueFalseToSpecificMethodRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\NarrowIdenticalWithConsecutiveRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/NarrowIdenticalWithConsecutiveRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\NarrowSingleWillReturnCallbackRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/NarrowSingleWillReturnCallbackRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\RemoveExpectAnyFromMockRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/RemoveExpectAnyFromMockRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\SingleWithConsecutiveToWithRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/SingleWithConsecutiveToWithRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\UseSpecificWillMethodRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/UseSpecificWillMethodRector.php',

View File

@ -1811,12 +1811,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/rectorphp\/rector-phpunit.git",
"reference": "daac9f0ddcc35c748e2bdc7cc989d1261b797844"
"reference": "a8243e5bdce41e3c3efd3914825674e5c1cca82f"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/daac9f0ddcc35c748e2bdc7cc989d1261b797844",
"reference": "daac9f0ddcc35c748e2bdc7cc989d1261b797844",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/a8243e5bdce41e3c3efd3914825674e5c1cca82f",
"reference": "a8243e5bdce41e3c3efd3914825674e5c1cca82f",
"shasum": ""
},
"require": {
@ -1840,7 +1840,7 @@
"tomasvotruba\/class-leak": "^0.2",
"tracy\/tracy": "^2.10"
},
"time": "2024-09-29T09:12:24+00:00",
"time": "2024-09-29T12:55:34+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 e75008c'), '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 d9cef57'), '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 daac9f0'), '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 14fcc87'));
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 e75008c'), '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 d9cef57'), '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 a8243e5'), '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 14fcc87'));
private function __construct()
{
}

View File

@ -0,0 +1,162 @@
<?php
declare (strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Expression;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector\NarrowSingleWillReturnCallbackRectorTest
*/
final class NarrowSingleWillReturnCallbackRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer
*/
private $testsNodeAnalyzer;
public function __construct(TestsNodeAnalyzer $testsNodeAnalyzer)
{
$this->testsNodeAnalyzer = $testsNodeAnalyzer;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Narrow single-value match willReturnCallback() to with() and willReturn() call', [new CodeSample(<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
$matcher = $this->exactly(1);
$this->personServiceMock->expects($matcher)
->willReturnCallback(function (...$parameters) use ($matcher) {
match ($matcher->getInvocationCount()) {
1 => $this->assertSame([1], $parameters),
};
});
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
public function run()
{
$matcher = $this->exactly(1);
$this->personServiceMock->expects($matcher)
->with([1], $parameters);
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<MethodCall>>
*/
public function getNodeTypes() : array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node) : ?\PhpParser\Node\Expr\MethodCall
{
if (!$this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
if (!$this->isName($node->name, 'willReturnCallback')) {
return null;
}
if ($node->isFirstClassCallable()) {
return null;
}
if (\count($node->getArgs()) !== 1) {
return null;
}
$firstArg = $node->getArgs()[0];
$match = $this->matchClosureSingleStmtMatch($firstArg->value);
if (!$match instanceof Match_) {
return null;
}
$matchArmBody = $this->matchSingleMatchArmBodyWithConditionOne($match);
if (!$matchArmBody instanceof MethodCall) {
return null;
}
// we look for $this->assertSame(...)
if (!$this->isLocalMethodCall($matchArmBody, 'assertSame')) {
return null;
}
$expectedArg = $matchArmBody->getArgs()[0];
$node->name = new Identifier('with');
$node->args = [new Arg($expectedArg->value)];
return $node;
}
private function matchClosureSingleStmtMatch(Expr $expr) : ?\PhpParser\Node\Expr\Match_
{
if (!$expr instanceof Closure) {
return null;
}
// handle easy path of single stmt first
if (\count($expr->stmts) !== 1) {
return null;
}
$onlyStmts = $expr->stmts[0];
if (!$onlyStmts instanceof Expression) {
return null;
}
if (!$onlyStmts->expr instanceof Match_) {
return null;
}
return $onlyStmts->expr;
}
private function isLocalMethodCall(Expr $expr, string $methodName) : bool
{
if (!$expr instanceof MethodCall) {
return \false;
}
if (!$this->isName($expr->var, 'this')) {
return \false;
}
return $this->isName($expr->name, $methodName);
}
private function matchSingleMatchArmBodyWithConditionOne(Match_ $match) : ?Expr
{
// has more options
if (\count($match->arms) !== 1) {
return null;
}
$onlyArm = $match->arms[0];
if ($onlyArm->conds === null || \count($onlyArm->conds) !== 1) {
return null;
}
$onlyArmCond = $onlyArm->conds[0];
if (!$this->isNumberOne($onlyArmCond)) {
return null;
}
return $onlyArm->body;
}
private function isNumberOne(Expr $expr) : bool
{
if (!$expr instanceof LNumber) {
return \false;
}
return $expr->value === 1;
}
}