Skip to content

Commit

Permalink
Don't throw "Cannot access constant FOO on mixed"
Browse files Browse the repository at this point in the history
Mixed type object may have the constant but we're not sure, only throw the error when we're damn sure. This regression has been introduced in #186
  • Loading branch information
spaze committed Apr 26, 2023
1 parent 084104a commit 5080647
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Usages/ClassConstantUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ function (ConstantStringType $constantString): string {
} else {
if ($usedOnType->hasConstant($constant)->yes()) {
$classNames = [$usedOnType->getConstant($constant)->getDeclaringClass()->getDisplayName()];
} else {
} elseif ($type->hasConstant($constant)->no()) {
return [
RuleErrorBuilder::message(sprintf(
'Cannot access constant %s on %s',
$constant,
$type->describe(VerbosityLevel::getRecommendedLevelByType($type))
))->build(),
];
} else {
return [];
}
}
return $this->disallowedConstantRuleErrors->get($this->getFullyQualified($classNames, $constant), $scope, $displayName, $this->disallowedConstants);
Expand Down
3 changes: 3 additions & 0 deletions tests/src/invalid/constantUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
/** @var class-string<DateTimeZone> $tz */
$tz = DateTimeZone::class;
$tz::FTC;

/** @var mixed $tz */
$tz::ALL;

0 comments on commit 5080647

Please sign in to comment.