-
Notifications
You must be signed in to change notification settings - Fork 574
Validate define() and const values against explicit types in dynamicConstantNames
#5648
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
base: 2.1.x
Are you sure you want to change the base?
Changes from all commits
d3725fa
e17cbb1
ef0d121
1d465d8
10de40c
c3524c1
5b39181
23610b1
822b755
6cb0618
b6ad165
12b7d5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Constants; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\ConstantResolver; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Type\VerbosityLevel; | ||
| use function count; | ||
| use function sprintf; | ||
| use function strtolower; | ||
|
|
||
| /** | ||
| * @implements Rule<FuncCall> | ||
| */ | ||
| final class ValueAssignedToDefineRule implements Rule | ||
| { | ||
|
|
||
| public function __construct(private ConstantResolver $constantResolver) | ||
| { | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return FuncCall::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (!($node->name instanceof Node\Name)) { | ||
| return []; | ||
| } | ||
|
|
||
| if (strtolower((string) $node->name) !== 'define') { | ||
| return []; | ||
| } | ||
|
|
||
| $args = $node->getArgs(); | ||
| if (count($args) < 2) { | ||
| return []; | ||
| } | ||
|
|
||
| $constantNameStrings = $scope->getType($args[0]->value)->getConstantStrings(); | ||
| if (count($constantNameStrings) === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| $valueType = $scope->getType($args[1]->value); | ||
| $errors = []; | ||
|
|
||
| foreach ($constantNameStrings as $constantNameString) { | ||
| $constantName = $constantNameString->getValue(); | ||
| if ($constantName === '') { | ||
| continue; | ||
| } | ||
|
|
||
| $configuredType = $this->constantResolver->getConfiguredGlobalConstantType($constantName); | ||
| if ($configuredType === null) { | ||
| continue; | ||
| } | ||
|
|
||
| $accepts = $configuredType->accepts($valueType, true); | ||
| if ($accepts->yes()) { | ||
| continue; | ||
| } | ||
|
|
||
| $verbosity = VerbosityLevel::getRecommendedLevelByType($configuredType, $valueType); | ||
|
|
||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Configuration defined type for constant %s (%s) does not accept value %s.', | ||
| $constantName, | ||
| $configuredType->describe(VerbosityLevel::typeOnly()), | ||
| $valueType->describe($verbosity), | ||
| ))->acceptsReasonsTip($accepts->reasons)->identifier('constant.defineValue')->build(); | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Constants; | ||
|
|
||
| use PhpParser\Node; | ||
| use PHPStan\Analyser\ConstantResolver; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Type\VerbosityLevel; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * @implements Rule<Node\Stmt\Const_> | ||
| */ | ||
| final class ValueAssignedToGlobalConstantRule implements Rule | ||
|
staabm marked this conversation as resolved.
|
||
| { | ||
|
|
||
| public function __construct(private ConstantResolver $constantResolver) | ||
| { | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return Node\Stmt\Const_::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| $errors = []; | ||
|
|
||
| foreach ($node->consts as $const) { | ||
| if ($const->namespacedName !== null) { | ||
| $constantName = $const->namespacedName->toString(); | ||
| } else { | ||
| $constantName = $const->name->toString(); | ||
| } | ||
|
|
||
| $configuredType = $this->constantResolver->getConfiguredGlobalConstantType($constantName); | ||
| if ($configuredType === null) { | ||
| continue; | ||
| } | ||
|
|
||
| $valueType = $scope->getType($const->value); | ||
| $accepts = $configuredType->accepts($valueType, true); | ||
| if ($accepts->yes()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a test to cover this path when $accepts is Maybe
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Added a test covering the |
||
| continue; | ||
| } | ||
|
|
||
| $verbosity = VerbosityLevel::getRecommendedLevelByType($configuredType, $valueType); | ||
|
|
||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Configuration defined type for constant %s (%s) does not accept value %s.', | ||
| $constantName, | ||
| $configuredType->describe(VerbosityLevel::typeOnly()), | ||
| $valueType->describe($verbosity), | ||
| ))->acceptsReasonsTip($accepts->reasons)->identifier('constant.value')->build(); | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Constants; | ||
|
|
||
| use PHPStan\Analyser\ConstantResolver; | ||
| use PHPStan\Rules\Rule as TRule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<ValueAssignedToClassConstantRule> | ||
| */ | ||
| class ValueAssignedToClassConstantWithDynamicNamesRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): TRule | ||
| { | ||
| return new ValueAssignedToClassConstantRule( | ||
| self::getContainer()->getByType(ConstantResolver::class), | ||
| true, | ||
| ); | ||
| } | ||
|
|
||
| public static function getAdditionalConfigFiles(): array | ||
| { | ||
| return [ | ||
| __DIR__ . '/value-assigned-dynamic-constant.neon', | ||
| ]; | ||
| } | ||
|
|
||
| public function testRule(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/value-assigned-to-class-constant-dynamic-names.php'], [ | ||
| [ | ||
| 'Configuration defined type for constant ValueAssignedToClassConstantDynamicNames\Foo::BAR (int|string|null) does not accept value false.', | ||
| 12, | ||
| ], | ||
| [ | ||
| 'Configuration defined type for constant ValueAssignedToClassConstantDynamicNames\Foo::MAYBE_BAR (int<1, max>) does not accept value int.', | ||
| 14, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cache resolved types locally, so we don't need to re-resolve them over and over again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Added a
configuredTypesCacheproperty toConstantResolverthat caches the resolvedTypeobjects fromgetConfiguredGlobalConstantType()andgetConfiguredClassConstantType(), so repeated calls with the same constant name return the cached result instead of re-resolving the type string throughTypeStringResolvereach time. All 12077 tests pass andmake phpstanreports no errors.