Skip to content

Commit

Permalink
Updated Rector to commit d5d3bcd946dc0565312a46e5941c033615373ce0
Browse files Browse the repository at this point in the history
rectorphp/rector-src@d5d3bcd [NodeTraverser] Clean up call ->addVisitor() after NodeTraverser creation (#6690)
  • Loading branch information
TomasVotruba committed Jan 22, 2025
1 parent 3f34d02 commit cc97278
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 6 deletions.
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,7 @@
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\TestWithToDataProviderRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\YieldDataProviderRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\Foreach_\\SimplifyForeachInstanceOfRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/Foreach_/SimplifyForeachInstanceOfRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\FuncCall\\AssertFuncCallToPHPUnitAssertRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\AssertCompareOnCountableWithMethodToAssertCountRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertCompareOnCountableWithMethodToAssertCountRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\AssertCompareToSpecificMethodRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertCompareToSpecificMethodRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\AssertComparisonToSpecificMethodRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,7 @@ class ComposerStaticInit3327108c61eea19d09115fa0e353e8ec
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\TestWithToDataProviderRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\YieldDataProviderRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\Foreach_\\SimplifyForeachInstanceOfRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/Foreach_/SimplifyForeachInstanceOfRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\FuncCall\\AssertFuncCallToPHPUnitAssertRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\AssertCompareOnCountableWithMethodToAssertCountRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertCompareOnCountableWithMethodToAssertCountRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\AssertCompareToSpecificMethodRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertCompareToSpecificMethodRector.php',
'Rector\\PHPUnit\\CodeQuality\\Rector\\MethodCall\\AssertComparisonToSpecificMethodRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertComparisonToSpecificMethodRector.php',
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -1804,12 +1804,12 @@
"source": {
"type": "git",
"url": "https:\/\/github.com\/rectorphp\/rector-phpunit.git",
"reference": "289978fdd47b511c2d97d8abf1ae1990200304c1"
"reference": "57a5555c9fbe8740e1a6d8def5e10fd32e252dd9"
},
"dist": {
"type": "zip",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/289978fdd47b511c2d97d8abf1ae1990200304c1",
"reference": "289978fdd47b511c2d97d8abf1ae1990200304c1",
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/57a5555c9fbe8740e1a6d8def5e10fd32e252dd9",
"reference": "57a5555c9fbe8740e1a6d8def5e10fd32e252dd9",
"shasum": ""
},
"require": {
Expand All @@ -1830,7 +1830,7 @@
"tomasvotruba\/class-leak": "^1.2",
"tracy\/tracy": "^2.10"
},
"time": "2025-01-21T19:46:08+00:00",
"time": "2025-01-22T12:35:28+00:00",
"default-branch": true,
"type": "rector-extension",
"extra": {
Expand Down
2 changes: 1 addition & 1 deletion vendor/composer/installed.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion vendor/rector/extension-installer/src/GeneratedConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
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 ceda7a4'), '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 2e0cde6'), '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 289978f'), '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 4661c01'));
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 ceda7a4'), '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 2e0cde6'), '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 57a5555'), '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 4661c01'));
private function __construct()
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

declare (strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\FuncCall;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Reflection\ClassReflection;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPUnit\Enum\AssertMethod;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\AssertFuncCallToPHPUnitAssertRectorTest
*/
final class AssertFuncCallToPHPUnitAssertRector extends AbstractRector
{
/**
* @readonly
*/
private ValueResolver $valueResolver;
public function __construct(ValueResolver $valueResolver)
{
$this->valueResolver = $valueResolver;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Turns assert() calls to their explicit PHPUnit assert alternative', [new CodeSample('assert($value === 100, "message");', '$this->assertSame(100, $value, "message");')]);
}
/**
* @return class-string[]
*/
public function getNodeTypes() : array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
* @return \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|null
*/
public function refactor(Node $node)
{
if ($node->isFirstClassCallable()) {
return null;
}
if (!$this->isName($node, 'assert')) {
return null;
}
if (!$this->isTestFilePath($node) && !$this->isBehatContext($node)) {
return null;
}
$comparedExpr = $node->getArgs()[0]->value;
if ($comparedExpr instanceof Equal) {
$methodName = AssertMethod::ASSERT_EQUALS;
$exprs = [$comparedExpr->right, $comparedExpr->left];
} elseif ($comparedExpr instanceof Identical) {
$methodName = AssertMethod::ASSERT_SAME;
$exprs = [$comparedExpr->right, $comparedExpr->left];
} elseif ($comparedExpr instanceof NotIdentical) {
if ($this->valueResolver->isNull($comparedExpr->right)) {
$methodName = 'assertNotNull';
$exprs = [$comparedExpr->left];
} else {
return null;
}
} elseif ($comparedExpr instanceof Bool_) {
$methodName = 'assertTrue';
$exprs = [$comparedExpr];
} elseif ($comparedExpr instanceof FuncCall) {
if ($this->isName($comparedExpr, 'method_exists')) {
$methodName = 'assertTrue';
$exprs = [$comparedExpr];
} else {
return null;
}
} elseif ($comparedExpr instanceof Instanceof_) {
// outside TestCase
$methodName = 'assertInstanceOf';
$exprs = [];
if ($comparedExpr->class instanceof FullyQualified) {
$classConstFetch = new ClassConstFetch($comparedExpr->class, 'class');
$exprs[] = $classConstFetch;
} else {
return null;
}
$exprs[] = $comparedExpr->expr;
} else {
return null;
}
// is there a comment message
if (isset($node->getArgs()[1])) {
$exprs[] = $node->getArgs()[1]->value;
}
return $this->createCall($node, $methodName, $exprs);
}
private function isBehatContext(FuncCall $funcCall) : bool
{
$scope = ScopeFetcher::fetch($funcCall);
if (!$scope->getClassReflection() instanceof ClassReflection) {
return \false;
}
$className = $scope->getClassReflection()->getName();
// special case with static call
return \substr_compare($className, 'Context', -\strlen('Context')) === 0;
}
private function isTestFilePath(FuncCall $funcCall) : bool
{
$scope = ScopeFetcher::fetch($funcCall);
if (!$scope->getClassReflection() instanceof ClassReflection) {
return \false;
}
$className = $scope->getClassReflection()->getName();
if (\substr_compare($className, 'Test', -\strlen('Test')) === 0) {
return \true;
}
return \substr_compare($className, 'TestCase', -\strlen('TestCase')) === 0;
}
/**
* @param Expr[] $exprs
* @return \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall
*/
private function createCall(FuncCall $funcCall, string $methodName, array $exprs)
{
$args = [];
foreach ($exprs as $expr) {
$args[] = new Arg($expr);
}
if ($this->isBehatContext($funcCall)) {
$assertFullyQualified = new FullyQualified(PHPUnitClassName::ASSERT);
return new StaticCall($assertFullyQualified, $methodName, $args);
}
return new MethodCall(new Variable('this'), $methodName, $args);
}
}
8 changes: 8 additions & 0 deletions vendor/rector/rector-phpunit/src/Enum/AssertMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ final class AssertMethod
* @var string
*/
public const ASSERT_TRUE = 'assertTrue';
/**
* @var string
*/
public const ASSERT_EQUALS = 'assertEquals';
/**
* @var string
*/
public const ASSERT_SAME = 'assertSame';
}

0 comments on commit cc97278

Please sign in to comment.