diff --git a/src/RuleErrors/DisallowedConstantRuleErrors.php b/src/RuleErrors/DisallowedConstantRuleErrors.php index 3d62669..0c67e03 100644 --- a/src/RuleErrors/DisallowedConstantRuleErrors.php +++ b/src/RuleErrors/DisallowedConstantRuleErrors.php @@ -39,7 +39,7 @@ public function get(string $constant, Scope $scope, ?string $displayName, array $errorBuilder = RuleErrorBuilder::message(sprintf( 'Using %s%s is forbidden, %s', $disallowedConstant->getConstant(), - $displayName && $displayName !== $disallowedConstant->getConstant() ? ' (as ' . $displayName . ')' : '', + $displayName && ltrim($displayName, '\\') !== $disallowedConstant->getConstant() ? ' (as ' . $displayName . ')' : '', $disallowedConstant->getMessage() )); if ($disallowedConstant->getErrorIdentifier()) { diff --git a/src/Usages/ClassConstantUsages.php b/src/Usages/ClassConstantUsages.php index 812e449..e06e213 100644 --- a/src/Usages/ClassConstantUsages.php +++ b/src/Usages/ClassConstantUsages.php @@ -84,7 +84,8 @@ public function processNode(Node $node, Scope $scope): array throw new ShouldNotHappenException(sprintf('$node->name should be %s but is %s', Identifier::class, get_class($node->name))); } $constant = (string)$node->name; - $usedOnType = $this->typeResolver->getType($node->class, $scope); + $type = $this->typeResolver->getType($node->class, $scope); + $usedOnType = $type->getObjectTypeOrClassStringObjectType(); if (strtolower($constant) === 'class') { return []; @@ -99,16 +100,16 @@ function (ConstantStringType $constantString): string { $usedOnType->getConstantStrings() ); } else { - if ($usedOnType->hasConstant($constant)->no()) { + if ($usedOnType->hasConstant($constant)->yes()) { + $classNames = [$usedOnType->getConstant($constant)->getDeclaringClass()->getDisplayName()]; + } else { return [ RuleErrorBuilder::message(sprintf( 'Cannot access constant %s on %s', $constant, - $usedOnType->describe(VerbosityLevel::getRecommendedLevelByType($usedOnType)) + $type->describe(VerbosityLevel::getRecommendedLevelByType($type)) ))->build(), ]; - } else { - $classNames = [$usedOnType->getConstant($constant)->getDeclaringClass()->getDisplayName()]; } } return $this->disallowedConstantRuleErrors->get($this->getFullyQualified($classNames, $constant), $scope, $displayName, $this->disallowedConstants); diff --git a/tests/Usages/ClassConstantInvalidUsagesTest.php b/tests/Usages/ClassConstantInvalidUsagesTest.php index 67f9c42..a6879af 100644 --- a/tests/Usages/ClassConstantInvalidUsagesTest.php +++ b/tests/Usages/ClassConstantInvalidUsagesTest.php @@ -50,8 +50,8 @@ public function testRule(): void 14, ], [ - 'Cannot access constant UTC on class-string', - 18, + 'Cannot access constant FTC on class-string', + 24, ], ]); } diff --git a/tests/src/invalid/constantUsages.php b/tests/src/invalid/constantUsages.php index b73b615..0205db9 100644 --- a/tests/src/invalid/constantUsages.php +++ b/tests/src/invalid/constantUsages.php @@ -13,6 +13,12 @@ $monster = DateTime::class; $monster::COOKIE; +// a valid type, for a change /** @var class-string $tz */ $tz = DateTimeZone::class; $tz::UTC; + +// this constant doesn't exist +/** @var class-string $tz */ +$tz = DateTimeZone::class; +$tz::FTC;