-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
270326a
commit 6be077e
Showing
5 changed files
with
117 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Constants; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassConst; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** @implements Rule<ClassConst> */ | ||
class FinalConstantRule implements Rule | ||
{ | ||
|
||
private PhpVersion $phpVersion; | ||
|
||
public function __construct(PhpVersion $phpVersion) | ||
{ | ||
$this->phpVersion = $phpVersion; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return ClassConst::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->isFinal()) { | ||
return []; | ||
} | ||
|
||
if ($this->phpVersion->supportsFinalConstants()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message('Final class constants are supported only on PHP 8.1 and later.')->nonIgnorable()->build(), | ||
]; | ||
} | ||
|
||
} |
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,57 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Constants; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<FinalConstantRule> | ||
*/ | ||
class FinalConstantRuleTest extends RuleTestCase | ||
{ | ||
|
||
/** @var int */ | ||
private $phpVersionId; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new FinalConstantRule(new PhpVersion($this->phpVersionId)); | ||
} | ||
|
||
public function dataRule(): array | ||
{ | ||
return [ | ||
[ | ||
80000, | ||
[ | ||
[ | ||
'Final class constants are supported only on PHP 8.1 and later.', | ||
9, | ||
], | ||
], | ||
], | ||
[ | ||
80100, | ||
[], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataRule | ||
* @param int $phpVersionId | ||
* @param mixed[] $errors | ||
*/ | ||
public function testRule(int $phpVersionId, array $errors): void | ||
{ | ||
if (!self::$useStaticReflectionProvider) { | ||
$this->markTestSkipped('Test requires static reflection.'); | ||
} | ||
|
||
$this->phpVersionId = $phpVersionId; | ||
$this->analyse([__DIR__ . '/data/final-constant.php'], $errors); | ||
} | ||
|
||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace FinalConstant; | ||
|
||
class Foo | ||
{ | ||
|
||
const TEST = 1; | ||
final const BAR = 2; | ||
|
||
} |