mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-26 20:53:31 +00:00
improve PHPStan rules for Rector
This commit is contained in:
parent
34de1afc01
commit
61aeeaeebd
|
@ -13,6 +13,7 @@
|
|||
<directory>rules/*/tests</directory>
|
||||
<directory>packages/*/tests</directory>
|
||||
<directory>tests</directory>
|
||||
<directory>utils/*/tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
|
|
|
@ -9,11 +9,17 @@ use PhpParser\Node;
|
|||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Rules\Rule;
|
||||
use PHPStan\Rules\RuleError;
|
||||
use PHPStan\Rules\RuleErrorBuilder;
|
||||
|
||||
/**
|
||||
* @see \Rector\PHPStanExtensions\Tests\Rule\ClassLike\KeepRectorNamespaceForRectorRuleTest
|
||||
*/
|
||||
final class KeepRectorNamespaceForRectorRule implements Rule
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const ERROR_MESSAGE = 'Change namespace for "%s". It cannot be in "Rector" namespace, unless Rector rule.';
|
||||
|
||||
public function getNodeType(): string
|
||||
{
|
||||
return ClassLike::class;
|
||||
|
@ -21,7 +27,7 @@ final class KeepRectorNamespaceForRectorRule implements Rule
|
|||
|
||||
/**
|
||||
* @param ClassLike $node
|
||||
* @return RuleError[]
|
||||
* @return string[]
|
||||
*/
|
||||
public function processNode(Node $node, Scope $scope): array
|
||||
{
|
||||
|
@ -32,20 +38,23 @@ final class KeepRectorNamespaceForRectorRule implements Rule
|
|||
/** @var string $classLikeName */
|
||||
$classLikeName = $node->name->toString();
|
||||
|
||||
$ruleError = $this->createRuleError($node, $scope, $classLikeName);
|
||||
$errorMessage = sprintf(self::ERROR_MESSAGE, $classLikeName);
|
||||
|
||||
return [$ruleError];
|
||||
return [$errorMessage];
|
||||
}
|
||||
|
||||
private function shouldSkip(Node $node, Scope $scope): bool
|
||||
private function shouldSkip(ClassLike $classLike, Scope $scope): bool
|
||||
{
|
||||
$namespace = $scope->getNamespace();
|
||||
if ($namespace === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// skip interface and tests
|
||||
if (Strings::match($namespace, '#\\\\(Contract|Exception|Tests)\\\\#')) {
|
||||
// skip interface and tests, except tests here
|
||||
if (Strings::match($namespace, '#\\\\(Contract|Exception|Tests)\\\\#') && ! Strings::contains(
|
||||
$namespace,
|
||||
'PHPStanExtensions'
|
||||
)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -53,7 +62,7 @@ final class KeepRectorNamespaceForRectorRule implements Rule
|
|||
return true;
|
||||
}
|
||||
|
||||
$name = $node->name;
|
||||
$name = $classLike->name;
|
||||
if ($name === null) {
|
||||
return true;
|
||||
}
|
||||
|
@ -63,18 +72,4 @@ final class KeepRectorNamespaceForRectorRule implements Rule
|
|||
|
||||
return (bool) Strings::match($classLikeName, '#(Rector|Test|Trait)$#');
|
||||
}
|
||||
|
||||
private function createRuleError(Node $node, Scope $scope, string $classLikeName): RuleError
|
||||
{
|
||||
$message = sprintf(
|
||||
'Change namespace for "%s". It cannot be in "Rector" namespace, unless Rector rule.',
|
||||
$classLikeName
|
||||
);
|
||||
|
||||
$ruleErrorBuilder = RuleErrorBuilder::message($message);
|
||||
$ruleErrorBuilder->line($node->getLine());
|
||||
$ruleErrorBuilder->file($scope->getFile());
|
||||
|
||||
return $ruleErrorBuilder->build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,13 +8,19 @@ use PhpParser\Node;
|
|||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PHPStan\Analyser\Scope;
|
||||
use PHPStan\Rules\Rule;
|
||||
use PHPStan\Rules\RuleError;
|
||||
use PHPStan\Rules\RuleErrorBuilder;
|
||||
use Rector\Core\Exception\NotImplementedException;
|
||||
use ReflectionMethod;
|
||||
|
||||
/**
|
||||
* @see \Rector\PHPStanExtensions\Tests\Rule\ClassMethod\PreventParentMethodVisibilityOverrideRuleTest
|
||||
*/
|
||||
final class PreventParentMethodVisibilityOverrideRule implements Rule
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public const ERROR_MESSAGE = 'Change "%s()" method visibility to "%s" to respect parent method visibility.';
|
||||
|
||||
public function getNodeType(): string
|
||||
{
|
||||
return ClassMethod::class;
|
||||
|
@ -22,7 +28,7 @@ final class PreventParentMethodVisibilityOverrideRule implements Rule
|
|||
|
||||
/**
|
||||
* @param ClassMethod $node
|
||||
* @return RuleError[]
|
||||
* @return string[]
|
||||
*/
|
||||
public function processNode(Node $node, Scope $scope): array
|
||||
{
|
||||
|
@ -49,8 +55,8 @@ final class PreventParentMethodVisibilityOverrideRule implements Rule
|
|||
|
||||
$methodVisibility = $this->resolveReflectionMethodVisibilityAsStrings($parentReflectionMethod);
|
||||
|
||||
$ruleError = $this->createRuleError($node, $scope, $methodName, $methodVisibility);
|
||||
return [$ruleError];
|
||||
$errorMessage = sprintf(self::ERROR_MESSAGE, $methodName, $methodVisibility);
|
||||
return [$errorMessage];
|
||||
}
|
||||
|
||||
return [];
|
||||
|
@ -87,19 +93,4 @@ final class PreventParentMethodVisibilityOverrideRule implements Rule
|
|||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private function createRuleError(Node $node, Scope $scope, string $methodName, string $methodVisibility): RuleError
|
||||
{
|
||||
$message = sprintf(
|
||||
'Change "%s()" method visibility to "%s" to respect parent method visibility.',
|
||||
$methodName,
|
||||
$methodVisibility
|
||||
);
|
||||
|
||||
$ruleErrorBuilder = RuleErrorBuilder::message($message);
|
||||
$ruleErrorBuilder->line($node->getLine());
|
||||
$ruleErrorBuilder->file($scope->getFile());
|
||||
|
||||
return $ruleErrorBuilder->build();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,21 +12,19 @@ use Rector\PHPStanExtensions\Rule\ClassLike\KeepRectorNamespaceForRectorRule;
|
|||
final class KeepRectorNamespaceForRectorRuleTest extends RuleTestCase
|
||||
{
|
||||
/**
|
||||
* @param array<string|int> $expectedErrorsWithLines
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function testRule(string $filePath, array $expectedErrorsWithLines): void
|
||||
{
|
||||
$this->analyse([$filePath], [$expectedErrorsWithLines]);
|
||||
$this->analyse([$filePath], $expectedErrorsWithLines);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
yield [__DIR__ . '/Source/Rector/ClassInCorrectNamespaceRector.php', []];
|
||||
yield [
|
||||
__DIR__ . '/Source/Rector/WrongClass.php',
|
||||
['Change namespace for "WrongClass". It cannot be in Rector namespace, unless Rector rule.', 8],
|
||||
];
|
||||
|
||||
$errorMessage = sprintf(KeepRectorNamespaceForRectorRule::ERROR_MESSAGE, 'WrongClass', 'Rector');
|
||||
yield [__DIR__ . '/Source/Rector/WrongClass.php', [[$errorMessage, 7]]];
|
||||
}
|
||||
|
||||
protected function getRule(): Rule
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace Rector\PHPStanExtensions\Tests\Rule\ClassLike\Source\Rector;
|
||||
|
||||
final class WrongClass
|
||||
|
|
|
@ -4,18 +4,25 @@ declare(strict_types=1);
|
|||
|
||||
namespace Rector\PHPStanExtensions\Tests\Rule\ClassMethod;
|
||||
|
||||
use Iterator;
|
||||
use PHPStan\Rules\Rule;
|
||||
use PHPStan\Testing\RuleTestCase;
|
||||
use Rector\PHPStanExtensions\Rule\ClassMethod\PreventParentMethodVisibilityOverrideRule;
|
||||
|
||||
final class PreventParentMethodVisibilityOverrideRuleTest extends RuleTestCase
|
||||
{
|
||||
public function testRule(): void
|
||||
/**
|
||||
* @dataProvider provideData()
|
||||
*/
|
||||
public function testRule(string $filePath, array $expectedErrorsWithLines): void
|
||||
{
|
||||
$this->analyse(
|
||||
[__DIR__ . '/Source/ClassWithOverridingVisibility.php'],
|
||||
[['Change "run()" method visibility to "protected" to respect parent method visibility.', 10]]
|
||||
);
|
||||
$this->analyse([$filePath], $expectedErrorsWithLines);
|
||||
}
|
||||
|
||||
public function provideData(): Iterator
|
||||
{
|
||||
$errorMessage = sprintf(PreventParentMethodVisibilityOverrideRule::ERROR_MESSAGE, 'run', 'protected');
|
||||
yield [__DIR__ . '/Source/ClassWithOverridingVisibility.php', [[$errorMessage, 9]]];
|
||||
}
|
||||
|
||||
protected function getRule(): Rule
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace Rector\PHPStanExtensions\Tests\Rule\ClassMethod\Source;
|
||||
|
||||
final class ClassWithOverridingVisibility extends GoodVisibility
|
||||
|
@ -11,11 +10,3 @@ final class ClassWithOverridingVisibility extends GoodVisibility
|
|||
{
|
||||
}
|
||||
}
|
||||
|
||||
abstract class GoodVisibility
|
||||
{
|
||||
protected function run()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\PHPStanExtensions\Tests\Rule\ClassMethod\Source;
|
||||
|
||||
abstract class GoodVisibility
|
||||
{
|
||||
protected function run()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user