Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ForbiddenStaticClassConstFetchRule #131

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading