-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ForbiddenStaticClassConstFetchRule (#131)
- Loading branch information
1 parent
3d6f3d8
commit 019cc50
Showing
5 changed files
with
144 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symplify\PHPStanRules\Rules; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use Symplify\RuleDocGenerator\Contract\ConfigurableRuleInterface; | ||
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Symplify\PHPStanRules\Tests\Rules\ForbiddenStaticClassConstFetchRule\ForbiddenStaticClassConstFetchRuleTest | ||
*/ | ||
final class ForbiddenStaticClassConstFetchRule implements Rule, DocumentedRuleInterface, ConfigurableRuleInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const ERROR_MESSAGE = 'Avoid static access of constants, as they can change value. Use interface and contract method instead'; | ||
|
||
/** | ||
* @return class-string<Node> | ||
*/ | ||
public function getNodeType(): string | ||
{ | ||
return ClassConstFetch::class; | ||
} | ||
|
||
/** | ||
* @param ClassConstFetch $node | ||
* @return string[] | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (! $node->class instanceof Name) { | ||
return []; | ||
} | ||
|
||
if ($node->class->toString() !== 'static') { | ||
return []; | ||
} | ||
|
||
return [self::ERROR_MESSAGE]; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition(self::ERROR_MESSAGE, [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
return static::SOME_CONST; | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run() | ||
{ | ||
return self::SOME_CONST; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/Rules/ForbiddenStaticClassConstFetchRule/Fixture/SomeClassWithStaticConstFetch.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,13 @@ | ||
<?php | ||
|
||
namespace Symplify\PHPStanRules\Tests\Rules\ForbiddenStaticClassConstFetchRule\Fixture; | ||
|
||
class SomeClassWithStaticConstFetch | ||
{ | ||
protected const SOME_CONST = 'some_const'; | ||
|
||
public function run() | ||
{ | ||
return static::SOME_CONST; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
tests/Rules/ForbiddenStaticClassConstFetchRule/ForbiddenStaticClassConstFetchRuleTest.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 Symplify\PHPStanRules\Tests\Rules\ForbiddenStaticClassConstFetchRule; | ||
|
||
use Iterator; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Symplify\PHPStanRules\Rules\ForbiddenStaticClassConstFetchRule; | ||
|
||
final class ForbiddenStaticClassConstFetchRuleTest extends RuleTestCase | ||
{ | ||
/** | ||
* @param mixed[] $expectedErrorMessagesWithLines | ||
*/ | ||
#[DataProvider('provideData')] | ||
public function testRule(string $filePath, array $expectedErrorMessagesWithLines): void | ||
{ | ||
$this->analyse([$filePath], $expectedErrorMessagesWithLines); | ||
} | ||
|
||
/** | ||
* @return \Iterator<array<int, array<int[]|string[]>>|string[]> | ||
*/ | ||
public static function provideData(): Iterator | ||
{ | ||
yield [ | ||
__DIR__ . '/Fixture/SomeClassWithStaticConstFetch.php', | ||
[[ForbiddenStaticClassConstFetchRule::ERROR_MESSAGE, 11]], | ||
]; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [__DIR__ . '/config/configured_rule.neon']; | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return self::getContainer()->getByType(ForbiddenStaticClassConstFetchRule::class); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
tests/Rules/ForbiddenStaticClassConstFetchRule/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 @@ | ||
includes: | ||
- ../../../config/included_services.neon | ||
|
||
rules: | ||
- Symplify\PHPStanRules\Rules\ForbiddenStaticClassConstFetchRule |