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

Support $foo::BAR where $foo is declared as class-string<Foo> #186

Merged
merged 1 commit into from
Apr 25, 2023
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 src/RuleErrors/DisallowedConstantRuleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . ')' : '',
spaze marked this conversation as resolved.
Show resolved Hide resolved
$disallowedConstant->getMessage()
));
if ($disallowedConstant->getErrorIdentifier()) {
Expand Down
11 changes: 6 additions & 5 deletions src/Usages/ClassConstantUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
spaze marked this conversation as resolved.
Show resolved Hide resolved
$usedOnType = $type->getObjectTypeOrClassStringObjectType();

if (strtolower($constant) === 'class') {
return [];
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/Usages/ClassConstantInvalidUsagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function testRule(): void
14,
],
[
'Cannot access constant UTC on class-string<DateTimeZone>',
18,
'Cannot access constant FTC on class-string<DateTimeZone>',
24,
],
]);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/src/invalid/constantUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
$monster = DateTime::class;
$monster::COOKIE;

// a valid type, for a change
/** @var class-string<DateTimeZone> $tz */
$tz = DateTimeZone::class;
$tz::UTC;

// this constant doesn't exist
/** @var class-string<DateTimeZone> $tz */
$tz = DateTimeZone::class;
$tz::FTC;
spaze marked this conversation as resolved.
Show resolved Hide resolved