mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 17:00:51 +00:00
Updated Rector to commit 6d8074534f3b568a0cecd373e584ea6c8d06580b
6d8074534f
Rectify (#5226)
This commit is contained in:
parent
4fde7813cb
commit
1a3632d316
1
vendor/composer/autoload_classmap.php
vendored
1
vendor/composer/autoload_classmap.php
vendored
|
@ -1688,6 +1688,7 @@ return array(
|
|||
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\AddSeeTestAnnotationRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/AddSeeTestAnnotationRector.php',
|
||||
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\ConstructClassMethodToSetUpTestCaseRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php',
|
||||
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\PreferPHPUnitThisCallRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/PreferPHPUnitThisCallRector.php',
|
||||
'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\\MethodCall\\AssertCompareToSpecificMethodRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertCompareToSpecificMethodRector.php',
|
||||
|
|
1
vendor/composer/autoload_static.php
vendored
1
vendor/composer/autoload_static.php
vendored
|
@ -1906,6 +1906,7 @@ class ComposerStaticInit18ad0e678efbbb500e116f7c54cccdd4
|
|||
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\AddSeeTestAnnotationRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/AddSeeTestAnnotationRector.php',
|
||||
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\ConstructClassMethodToSetUpTestCaseRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/ConstructClassMethodToSetUpTestCaseRector.php',
|
||||
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\PreferPHPUnitThisCallRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/PreferPHPUnitThisCallRector.php',
|
||||
'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\\MethodCall\\AssertCompareToSpecificMethodRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/MethodCall/AssertCompareToSpecificMethodRector.php',
|
||||
|
|
8
vendor/composer/installed.json
vendored
8
vendor/composer/installed.json
vendored
|
@ -1808,12 +1808,12 @@
|
|||
"source": {
|
||||
"type": "git",
|
||||
"url": "https:\/\/github.com\/rectorphp\/rector-phpunit.git",
|
||||
"reference": "e13d9807afb8b271d2e0f246714711fa696b3439"
|
||||
"reference": "7c9ae3437ecd631ca6f86fed45cc3ad561cc3c40"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/e13d9807afb8b271d2e0f246714711fa696b3439",
|
||||
"reference": "e13d9807afb8b271d2e0f246714711fa696b3439",
|
||||
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/7c9ae3437ecd631ca6f86fed45cc3ad561cc3c40",
|
||||
"reference": "7c9ae3437ecd631ca6f86fed45cc3ad561cc3c40",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -1843,7 +1843,7 @@
|
|||
"tomasvotruba\/unused-public": "^0.3",
|
||||
"tracy\/tracy": "^2.10"
|
||||
},
|
||||
"time": "2023-11-06T10:01:29+00:00",
|
||||
"time": "2023-11-06T10:11:07+00:00",
|
||||
"default-branch": true,
|
||||
"type": "rector-extension",
|
||||
"extra": {
|
||||
|
|
2
vendor/composer/installed.php
vendored
2
vendor/composer/installed.php
vendored
File diff suppressed because one or more lines are too long
|
@ -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 9de7d58'), '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 4056166'), '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 e13d980'), '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 38014d4'));
|
||||
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 9de7d58'), '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 4056166'), '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 7c9ae34'), '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 38014d4'));
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ use Rector\Config\RectorConfig;
|
|||
use Rector\PHPUnit\CodeQuality\Rector\Class_\AddSeeTestAnnotationRector;
|
||||
use Rector\PHPUnit\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector;
|
||||
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
|
||||
use Rector\PHPUnit\CodeQuality\Rector\Class_\TestWithToDataProviderRector;
|
||||
use Rector\PHPUnit\CodeQuality\Rector\Class_\YieldDataProviderRector;
|
||||
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\RemoveEmptyTestMethodRector;
|
||||
use Rector\PHPUnit\CodeQuality\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector;
|
||||
|
@ -39,6 +40,7 @@ return static function (RectorConfig $rectorConfig) : void {
|
|||
YieldDataProviderRector::class,
|
||||
RemoveEmptyTestMethodRector::class,
|
||||
ReplaceTestAnnotationWithPrefixedFunctionRector::class,
|
||||
TestWithToDataProviderRector::class,
|
||||
AssertEqualsOrAssertSameFloatParameterToSpecificMethodsTypeRector::class,
|
||||
// specific asserts
|
||||
AssertCompareToSpecificMethodRector::class,
|
||||
|
|
199
vendor/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php
vendored
Normal file
199
vendor/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/TestWithToDataProviderRector.php
vendored
Normal file
|
@ -0,0 +1,199 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
|
||||
|
||||
use RectorPrefix202311\Nette\Utils\Json;
|
||||
use RectorPrefix202311\Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Array_;
|
||||
use PhpParser\Node\Expr\ArrayItem;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Return_;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
|
||||
use Rector\Core\NodeManipulator\ClassInsertManipulator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
/**
|
||||
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\TestWithToDataProviderRector\TestWithToDataProviderRectorTest
|
||||
*/
|
||||
final class TestWithToDataProviderRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer
|
||||
*/
|
||||
private $testsNodeAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
||||
*/
|
||||
private $phpDocInfoFactory;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Comments\NodeDocBlock\DocBlockUpdater
|
||||
*/
|
||||
private $docBlockUpdater;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Core\NodeManipulator\ClassInsertManipulator
|
||||
*/
|
||||
private $classInsertManipulator;
|
||||
public function __construct(TestsNodeAnalyzer $testsNodeAnalyzer, PhpDocInfoFactory $phpDocInfoFactory, PhpDocTagRemover $phpDocTagRemover, DocBlockUpdater $docBlockUpdater, ClassInsertManipulator $classInsertManipulator)
|
||||
{
|
||||
$this->testsNodeAnalyzer = $testsNodeAnalyzer;
|
||||
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
$this->docBlockUpdater = $docBlockUpdater;
|
||||
$this->classInsertManipulator = $classInsertManipulator;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Replace testWith annotation to data provider.', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
/**
|
||||
* @testWith [0, 0, 0]
|
||||
* @testWith [0, 1, 1]
|
||||
* @testWith [1, 0, 1]
|
||||
* @testWith [1, 1, 3]
|
||||
*/
|
||||
public function testSum(int $a, int $b, int $expected)
|
||||
{
|
||||
$this->assertSame($expected, $a + $b);
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
public function dataProviderSum()
|
||||
{
|
||||
return [
|
||||
[0, 0, 0],
|
||||
[0, 1, 1],
|
||||
[1, 0, 1],
|
||||
[1, 1, 3]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataProviderSum
|
||||
*/
|
||||
public function test(int $a, int $b, int $expected)
|
||||
{
|
||||
$this->assertSame($expected, $a + $b);
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Class_::class];
|
||||
}
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
if (!$this->testsNodeAnalyzer->isInTestClass($node)) {
|
||||
return null;
|
||||
}
|
||||
foreach ($node->stmts as $classMethod) {
|
||||
if (!$classMethod instanceof ClassMethod) {
|
||||
continue;
|
||||
}
|
||||
$this->refactorClassMethod($node, $classMethod);
|
||||
}
|
||||
return $node;
|
||||
}
|
||||
private function refactorClassMethod(Class_ $class, ClassMethod $classMethod) : void
|
||||
{
|
||||
$arrayItems = [];
|
||||
$hasChanged = \false;
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod);
|
||||
if (!$phpDocInfo instanceof PhpDocInfo) {
|
||||
return;
|
||||
}
|
||||
$testWithPhpDocTagNodes = \array_merge($phpDocInfo->getTagsByName('testWith'), $phpDocInfo->getTagsByName('testwith'));
|
||||
if ($testWithPhpDocTagNodes === []) {
|
||||
return;
|
||||
}
|
||||
foreach ($testWithPhpDocTagNodes as $testWithPhpDocTagNode) {
|
||||
if (!$testWithPhpDocTagNode->value instanceof GenericTagValueNode) {
|
||||
continue;
|
||||
}
|
||||
[$values] = $this->extractTestWithData($testWithPhpDocTagNode->value);
|
||||
$arrayItems[] = new ArrayItem($this->createArrayItem($values));
|
||||
//cleanup
|
||||
$hasChanged = $this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $testWithPhpDocTagNode);
|
||||
}
|
||||
if (!$hasChanged) {
|
||||
return;
|
||||
}
|
||||
$dataProviderName = $this->generateDataProviderName($classMethod);
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
|
||||
$phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@dataProvider', new GenericTagValueNode($dataProviderName)));
|
||||
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
|
||||
$providerMethod = new ClassMethod($dataProviderName);
|
||||
$providerMethod->flags = Class_::MODIFIER_PUBLIC;
|
||||
$providerMethod->stmts[] = new Return_(new Array_($arrayItems));
|
||||
$this->classInsertManipulator->addAsFirstMethod($class, $providerMethod);
|
||||
}
|
||||
/**
|
||||
* @return array<array<int, mixed>>
|
||||
*/
|
||||
private function extractTestWithData(GenericTagValueNode $genericTagValueNode) : array
|
||||
{
|
||||
$testWithItems = \explode("\n", \trim($genericTagValueNode->value));
|
||||
$jsonArray = [];
|
||||
foreach ($testWithItems as $testWithItem) {
|
||||
$jsonArray[] = Json::decode(\trim($testWithItem), Json::FORCE_ARRAY);
|
||||
}
|
||||
return $jsonArray;
|
||||
}
|
||||
/**
|
||||
* @param array<int|string, mixed> $data
|
||||
*/
|
||||
private function createArrayItem(array $data) : Array_
|
||||
{
|
||||
$values = [];
|
||||
foreach ($data as $index => $item) {
|
||||
$key = null;
|
||||
if (\is_string($index)) {
|
||||
$key = new String_($index);
|
||||
}
|
||||
$values[] = new ArrayItem($this->parseArrayItemValue($item), $key);
|
||||
}
|
||||
return new Array_($values);
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
private function parseArrayItemValue($value) : Expr
|
||||
{
|
||||
if (\is_array($value)) {
|
||||
return $this->createArrayItem($value);
|
||||
}
|
||||
$name = new Name(Json::encode($value));
|
||||
return new ConstFetch($name);
|
||||
}
|
||||
private function generateDataProviderName(ClassMethod $classMethod) : string
|
||||
{
|
||||
$methodName = Strings::replace($classMethod->name->name, '/^test/', '');
|
||||
$upperCaseFirstLatter = \ucfirst($methodName);
|
||||
return \sprintf('%s%s', 'dataProvider', $upperCaseFirstLatter);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user