Skip to content

Commit

Permalink
ClassConstantAttributesRule
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 18, 2020
1 parent 8ec0a96 commit 4a33890
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ rules:
- PHPStan\Rules\Arrays\OffsetAccessWithoutDimForReadingRule
- PHPStan\Rules\Cast\UnsetCastRule
- PHPStan\Rules\Classes\ClassAttributesRule
- PHPStan\Rules\Classes\ClassConstantAttributesRule
- PHPStan\Rules\Classes\ClassConstantRule
- PHPStan\Rules\Classes\DuplicateDeclarationRule
- PHPStan\Rules\Classes\ExistingClassesInClassImplementsRule
Expand Down
38 changes: 38 additions & 0 deletions src/Rules/Classes/ClassConstantAttributesRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Classes;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\Rule;

/**
* @implements Rule<Node\Stmt\ClassConst>
*/
class ClassConstantAttributesRule implements Rule
{

private AttributesCheck $attributesCheck;

public function __construct(AttributesCheck $attributesCheck)
{
$this->attributesCheck = $attributesCheck;
}

public function getNodeType(): string
{
return Node\Stmt\ClassConst::class;
}

public function processNode(Node $node, Scope $scope): array
{
return $this->attributesCheck->check(
$scope,
$node->attrGroups,
\Attribute::TARGET_CLASS_CONSTANT,
'class constant'
);
}

}
54 changes: 54 additions & 0 deletions tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Classes;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\AttributesCheck;
use PHPStan\Rules\ClassCaseSensitivityCheck;
use PHPStan\Rules\FunctionCallParametersCheck;
use PHPStan\Rules\NullsafeCheck;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<ClassConstantAttributesRule>
*/
class ClassConstantAttributesRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
$reflectionProvider = $this->createReflectionProvider();
return new ClassConstantAttributesRule(
new AttributesCheck(
$reflectionProvider,
new FunctionCallParametersCheck(
new RuleLevelHelper($reflectionProvider, true, false, true),
new NullsafeCheck(),
new PhpVersion(80000),
true,
true,
true,
true
),
new ClassCaseSensitivityCheck($reflectionProvider, false)
)
);
}

public function testRule(): void
{
if (!self::$useStaticReflectionProvider && PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->analyse([__DIR__ . '/data/class-constant-attributes.php'], [
[
'Attribute class ClassConstantAttributes\Foo does not have the class constant target.',
26,
],
]);
}

}
45 changes: 45 additions & 0 deletions tests/PHPStan/Rules/Classes/data/class-constant-attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace ClassConstantAttributes;

#[\Attribute(\Attribute::TARGET_CLASS)]
class Foo
{

}

#[\Attribute(\Attribute::TARGET_CLASS_CONSTANT)]
class Bar
{

}

#[\Attribute(\Attribute::TARGET_ALL)]
class Baz
{

}

class Lorem
{

#[Foo]
private const FOO = 1;

}

class Ipsum
{

#[Bar]
private const FOO = 1;

}

class Dolor
{

#[Baz]
private const FOO = 1;

}

0 comments on commit 4a33890

Please sign in to comment.