mirror of https://github.com/rectorphp/rector.git
Updated Rector to commit 3df5a626e5ae8e13aaef8abfc41a136dbf1f367e
3df5a626e5
Clean up SafeLeftTypeBooleanAndOrAnalyzer (#5819)
This commit is contained in:
parent
3a03381bf8
commit
51fa5a731d
|
@ -1712,6 +1712,7 @@ return array(
|
|||
'Rector\\PHPUnit\\CodeQuality\\Rector\\ClassMethod\\DataProviderArrayItemsNewLinedRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php',
|
||||
'Rector\\PHPUnit\\CodeQuality\\Rector\\ClassMethod\\RemoveEmptyTestMethodRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/ClassMethod/RemoveEmptyTestMethodRector.php',
|
||||
'Rector\\PHPUnit\\CodeQuality\\Rector\\ClassMethod\\ReplaceTestAnnotationWithPrefixedFunctionRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php',
|
||||
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\AddCoversClassAttributeRector' => $vendorDir . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/AddCoversClassAttributeRector.php',
|
||||
'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',
|
||||
|
|
|
@ -1931,6 +1931,7 @@ class ComposerStaticInit005507a96a37a412a8adf9cfaae1e621
|
|||
'Rector\\PHPUnit\\CodeQuality\\Rector\\ClassMethod\\DataProviderArrayItemsNewLinedRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/ClassMethod/DataProviderArrayItemsNewLinedRector.php',
|
||||
'Rector\\PHPUnit\\CodeQuality\\Rector\\ClassMethod\\RemoveEmptyTestMethodRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/ClassMethod/RemoveEmptyTestMethodRector.php',
|
||||
'Rector\\PHPUnit\\CodeQuality\\Rector\\ClassMethod\\ReplaceTestAnnotationWithPrefixedFunctionRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/ClassMethod/ReplaceTestAnnotationWithPrefixedFunctionRector.php',
|
||||
'Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\AddCoversClassAttributeRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/AddCoversClassAttributeRector.php',
|
||||
'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',
|
||||
|
|
|
@ -1802,12 +1802,12 @@
|
|||
"source": {
|
||||
"type": "git",
|
||||
"url": "https:\/\/github.com\/rectorphp\/rector-phpunit.git",
|
||||
"reference": "a620d41d54f70f29086a545dd5ab94a51496b4e2"
|
||||
"reference": "71e9ad758448529f76e3358c91ba402cb9903cf5"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/a620d41d54f70f29086a545dd5ab94a51496b4e2",
|
||||
"reference": "a620d41d54f70f29086a545dd5ab94a51496b4e2",
|
||||
"url": "https:\/\/api.github.com\/repos\/rectorphp\/rector-phpunit\/zipball\/71e9ad758448529f76e3358c91ba402cb9903cf5",
|
||||
"reference": "71e9ad758448529f76e3358c91ba402cb9903cf5",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -1830,7 +1830,7 @@
|
|||
"tomasvotruba\/class-leak": "^0.2",
|
||||
"tracy\/tracy": "^2.10"
|
||||
},
|
||||
"time": "2024-04-09T11:54:26+00:00",
|
||||
"time": "2024-04-13T07:45:28+00:00",
|
||||
"default-branch": true,
|
||||
"type": "rector-extension",
|
||||
"extra": {
|
||||
|
|
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 b3da143'), '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 a620d41'), '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 607447e'));
|
||||
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 b3da143'), '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 71e9ad7'), '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 607447e'));
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@
|
|||
],
|
||||
"phpstan": "vendor\/bin\/phpstan analyse --ansi --error-format symplify",
|
||||
"check-cs": "vendor\/bin\/ecs check --ansi",
|
||||
"class-leak": "vendor\/bin\/class-leak check config src rules",
|
||||
"class-leak": "vendor\/bin\/class-leak check config src rules --skip-type=\"Rector\\PHPUnit\\CodeQuality\\Rector\\Class_\\AddCoversClassAttributeRector\"",
|
||||
"fix-cs": "vendor\/bin\/ecs check --fix --ansi",
|
||||
"rector": "vendor\/bin\/rector process --ansi",
|
||||
"docs": "vendor\/bin\/rule-doc-generator generate src rules --output-file docs\/rector_rules_overview.md --ansi"
|
||||
|
|
|
@ -1,4 +1,26 @@
|
|||
# 52 Rules Overview
|
||||
# 53 Rules Overview
|
||||
|
||||
## AddCoversClassAttributeRector
|
||||
|
||||
Adds `#[CoversClass(...)]` attribute to test files guessing source class name.
|
||||
|
||||
- class: [`Rector\PHPUnit\CodeQuality\Rector\Class_\AddCoversClassAttributeRector`](../rules/CodeQuality/Rector/Class_/AddCoversClassAttributeRector.php)
|
||||
|
||||
```diff
|
||||
class SomeService
|
||||
{
|
||||
}
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
+use PHPUnit\Framework\Attributes\CoversClass;
|
||||
|
||||
+#[CoversClass(SomeService::class)]
|
||||
class SomeServiceTest extends TestCase
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
## AddDoesNotPerformAssertionToNonAssertingTestRector
|
||||
|
||||
|
|
147
vendor/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/AddCoversClassAttributeRector.php
vendored
Normal file
147
vendor/rector/rector-phpunit/rules/CodeQuality/Rector/Class_/AddCoversClassAttributeRector.php
vendored
Normal file
|
@ -0,0 +1,147 @@
|
|||
<?php
|
||||
|
||||
declare (strict_types=1);
|
||||
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\AttributeGroup;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PHPStan\Reflection\ReflectionProvider;
|
||||
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
|
||||
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
|
||||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||||
use function array_filter;
|
||||
use function array_merge;
|
||||
use function count;
|
||||
use function explode;
|
||||
use function implode;
|
||||
use function in_array;
|
||||
use function preg_replace;
|
||||
use function strtolower;
|
||||
use function trim;
|
||||
final class AddCoversClassAttributeRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @readonly
|
||||
* @var \PHPStan\Reflection\ReflectionProvider
|
||||
*/
|
||||
private $reflectionProvider;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory
|
||||
*/
|
||||
private $phpAttributeGroupFactory;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer
|
||||
*/
|
||||
private $phpAttributeAnalyzer;
|
||||
/**
|
||||
* @readonly
|
||||
* @var \Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer
|
||||
*/
|
||||
private $testsNodeAnalyzer;
|
||||
public function __construct(ReflectionProvider $reflectionProvider, PhpAttributeGroupFactory $phpAttributeGroupFactory, PhpAttributeAnalyzer $phpAttributeAnalyzer, TestsNodeAnalyzer $testsNodeAnalyzer)
|
||||
{
|
||||
$this->reflectionProvider = $reflectionProvider;
|
||||
$this->phpAttributeGroupFactory = $phpAttributeGroupFactory;
|
||||
$this->phpAttributeAnalyzer = $phpAttributeAnalyzer;
|
||||
$this->testsNodeAnalyzer = $testsNodeAnalyzer;
|
||||
}
|
||||
public function getRuleDefinition() : RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Adds `#[CoversClass(...)]` attribute to test files guessing source class name.', [new CodeSample(<<<'CODE_SAMPLE'
|
||||
class SomeService
|
||||
{
|
||||
}
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class SomeServiceTest extends TestCase
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
, <<<'CODE_SAMPLE'
|
||||
class SomeService
|
||||
{
|
||||
}
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
|
||||
#[CoversClass(SomeService::class)]
|
||||
class SomeServiceTest extends TestCase
|
||||
{
|
||||
}
|
||||
CODE_SAMPLE
|
||||
)]);
|
||||
}
|
||||
/**
|
||||
* @return array<class-string<Node>>
|
||||
*/
|
||||
public function getNodeTypes() : array
|
||||
{
|
||||
return [Class_::class];
|
||||
}
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node) : ?Node
|
||||
{
|
||||
$className = $this->getName($node);
|
||||
if ($className === null) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->testsNodeAnalyzer->isInTestClass($node)) {
|
||||
return null;
|
||||
}
|
||||
if ($this->phpAttributeAnalyzer->hasPhpAttributes($node, ['PHPUnit\\Framework\\Attributes\\CoversNothing', 'PHPUnit\\Framework\\Attributes\\CoversClass', 'PHPUnit\\Framework\\Attributes\\CoversFunction'])) {
|
||||
return null;
|
||||
}
|
||||
$possibleTestClassNames = $this->resolveSourceClassNames($className);
|
||||
$matchingTestClassName = $this->matchExistingClassName($possibleTestClassNames);
|
||||
if ($matchingTestClassName === null) {
|
||||
return null;
|
||||
}
|
||||
$coversAttributeGroup = $this->createAttributeGroup('\\' . $matchingTestClassName);
|
||||
$node->attrGroups = array_merge($node->attrGroups, [$coversAttributeGroup]);
|
||||
return $node;
|
||||
}
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
private function resolveSourceClassNames(string $className) : array
|
||||
{
|
||||
$classNameParts = explode('\\', $className);
|
||||
$partCount = count($classNameParts);
|
||||
$classNameParts[$partCount - 1] = preg_replace('#Test$#', '', $classNameParts[$partCount - 1]);
|
||||
$possibleTestClassNames = [implode('\\', $classNameParts)];
|
||||
$partsWithoutTests = array_filter($classNameParts, static function (?string $part) : bool {
|
||||
return $part === null ? \false : !in_array(strtolower($part), ['test', 'tests'], \true);
|
||||
});
|
||||
$possibleTestClassNames[] = implode('\\', $partsWithoutTests);
|
||||
return $possibleTestClassNames;
|
||||
}
|
||||
/**
|
||||
* @param string[] $classNames
|
||||
*/
|
||||
private function matchExistingClassName(array $classNames) : ?string
|
||||
{
|
||||
foreach ($classNames as $className) {
|
||||
if (!$this->reflectionProvider->hasClass($className)) {
|
||||
continue;
|
||||
}
|
||||
return $className;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private function createAttributeGroup(string $annotationValue) : AttributeGroup
|
||||
{
|
||||
$attributeClass = 'PHPUnit\\Framework\\Attributes\\CoversClass';
|
||||
$attributeValue = trim($annotationValue) . '::class';
|
||||
return $this->phpAttributeGroupFactory->createFromClassWithItems($attributeClass, [$attributeValue]);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue