-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DX] Add PhpUpgradeDowngradeRegisteredInSetRule PHPStan rule (#905)
* [DX] Add PhpUpgradeDowngradeRegisteredInSetRule PHPStan rule * phpstan * register test * [ci-review] Rector Rectify * phpstan * phpstan fix * check prefix * Fix * fix * final touch: service config * skip implements ConfigurableRectorInterface * [ci-review] Rector Rectify * [ci-review] Rector Rectify * register services * final touch: example of non-configurable service * include configured rector Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
e78fadd
commit e09cf11
Showing
14 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
utils/phpstan-rules/src/Rules/PhpUpgradeDowngradeRegisteredInSetRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPStanRules\Rules; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PHPStan\Analyser\Scope; | ||
use Symplify\PHPStanRules\Rules\AbstractSymplifyRule; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Symplify\SmartFileSystem\SmartFileSystem; | ||
|
||
/** | ||
* @see \Rector\PHPStanRules\Tests\Rules\PhpUpgradeDowngradeRegisteredInSetRule\PhpUpgradeDowngradeRegisteredInSetRuleTest | ||
*/ | ||
final class PhpUpgradeDowngradeRegisteredInSetRule extends AbstractSymplifyRule | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const ERROR_MESSAGE = 'Register %s to %s config set'; | ||
|
||
/** | ||
* @var string | ||
* @see https://regex101.com/r/C3nz6e/1/ | ||
*/ | ||
private const PREFIX_REGEX = '#(Downgrade)?Php\d+#'; | ||
|
||
public function __construct( | ||
private SmartFileSystem $smartFileSystem | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
* @return string[] | ||
*/ | ||
public function process(Node $node, Scope $scope): array | ||
{ | ||
/** @var string $className */ | ||
$className = (string) $node->namespacedName; | ||
if (! str_ends_with($className, 'Rector')) { | ||
return []; | ||
} | ||
|
||
[, $prefix] = explode('\\', $className); | ||
if (! Strings::match($prefix, self::PREFIX_REGEX)) { | ||
return []; | ||
} | ||
|
||
$phpVersion = Strings::substring($prefix, -2); | ||
|
||
$configFile = str_starts_with($prefix, 'Downgrade') | ||
? 'downgrade-php' . $phpVersion | ||
: 'php' . $phpVersion; | ||
|
||
$configContent = $this->smartFileSystem->readFile( | ||
__DIR__ . '/../../../../config/set/' . $configFile . '.php' | ||
); | ||
|
||
$shortClassName = (string) $node->name; | ||
$toSearch = sprintf('$services->set(%s::class)', $shortClassName); | ||
|
||
if (! str_contains($configContent, $toSearch)) { | ||
return [sprintf(self::ERROR_MESSAGE, $className, $configFile)]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition(self::ERROR_MESSAGE, [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
// config/set/php74.php | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
// config/set/php74.php | ||
$services->set(RealToFloatTypeCastRector::class); | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...s/tests/Rules/PhpUpgradeDowngradeRegisteredInSetRule/Fixture/SkipSomePhpFeatureRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Arguments\Rector\ClassMethod; | ||
|
||
final class SkipSomePhpFeatureRector | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
...ules/tests/Rules/PhpUpgradeDowngradeRegisteredInSetRule/Fixture/SomePhpFeature2Rector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DowngradePhp80\Rector\Class_; | ||
|
||
final class SomePhpFeature2Rector | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
...rules/tests/Rules/PhpUpgradeDowngradeRegisteredInSetRule/Fixture/SomePhpFeatureRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php80\Rector\Class_; | ||
|
||
final class SomePhpFeatureRector | ||
{ | ||
} |
47 changes: 47 additions & 0 deletions
47
...les/PhpUpgradeDowngradeRegisteredInSetRule/PhpUpgradeDowngradeRegisteredInSetRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPStanRules\Tests\Rules\PhpUpgradeDowngradeRegisteredInSetRule; | ||
|
||
use Iterator; | ||
use PHPStan\Rules\Rule; | ||
use Rector\PHPStanRules\Rules\PhpUpgradeDowngradeRegisteredInSetRule; | ||
use Symplify\PHPStanExtensions\Testing\AbstractServiceAwareRuleTestCase; | ||
|
||
/** | ||
* @extends AbstractServiceAwareRuleTestCase<PhpUpgradeDowngradeRegisteredInSetRule> | ||
*/ | ||
final class PhpUpgradeDowngradeRegisteredInSetRuleTest extends AbstractServiceAwareRuleTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
* @param array<string|int> $expectedErrorMessagesWithLines | ||
*/ | ||
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void | ||
{ | ||
$this->analyse([$filePath], $expectedErrorMessagesWithLines); | ||
} | ||
|
||
/** | ||
* @return Iterator<string[]|array<int, mixed[]>> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
yield [__DIR__ . '/Fixture/SkipSomePhpFeatureRector.php', []]; | ||
yield [__DIR__ . '/Fixture/SomePhpFeatureRector.php', [ | ||
[sprintf(PhpUpgradeDowngradeRegisteredInSetRule::ERROR_MESSAGE, 'Rector\Php80\Rector\Class_\SomePhpFeatureRector', 'php80'), 7] | ||
]]; | ||
yield [__DIR__ . '/Fixture/SomePhpFeature2Rector.php', [ | ||
[sprintf(PhpUpgradeDowngradeRegisteredInSetRule::ERROR_MESSAGE, 'Rector\DowngradePhp80\Rector\Class_\SomePhpFeature2Rector', 'downgrade-php80'), 7] | ||
]]; | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return $this->getRuleFromConfig( | ||
PhpUpgradeDowngradeRegisteredInSetRule::class, | ||
__DIR__ . '/config/configured_rule.neon' | ||
); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...stan-rules/tests/Rules/PhpUpgradeDowngradeRegisteredInSetRule/config/configured_rule.neon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
- Symplify\SmartFileSystem\SmartFileSystem | ||
- | ||
class: Rector\PHPStanRules\Rules\PhpUpgradeDowngradeRegisteredInSetRule | ||
tags: [phpstan.rules.rule] |