Skip to content

Commit

Permalink
Add ForbiddenStaticClassConstFetchRule (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored May 31, 2024
1 parent 3d6f3d8 commit 019cc50
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/static-rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rules:
# explicit naming
- Symplify\PHPStanRules\Rules\ForbiddenMultipleClassLikeInOneFileRule


- Symplify\PHPStanRules\Rules\ForbiddenStaticClassConstFetchRule
- Symplify\PHPStanRules\Rules\Complexity\ForbiddenArrayMethodCallRule
- Symplify\PHPStanRules\Rules\CheckRequiredInterfaceInContractNamespaceRule

Expand Down
78 changes: 78 additions & 0 deletions src/Rules/ForbiddenStaticClassConstFetchRule.php
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
),
]);
}
}
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;
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
includes:
- ../../../config/included_services.neon

rules:
- Symplify\PHPStanRules\Rules\ForbiddenStaticClassConstFetchRule

0 comments on commit 019cc50

Please sign in to comment.