From 508064743828f21ffbd485b2018385fcd88b1f34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Wed, 26 Apr 2023 03:46:34 +0200 Subject: [PATCH] Don't throw "Cannot access constant FOO on mixed" 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 --- src/Usages/ClassConstantUsages.php | 4 +++- tests/src/invalid/constantUsages.php | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Usages/ClassConstantUsages.php b/src/Usages/ClassConstantUsages.php index 9979394..efd45fe 100644 --- a/src/Usages/ClassConstantUsages.php +++ b/src/Usages/ClassConstantUsages.php @@ -102,7 +102,7 @@ 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', @@ -110,6 +110,8 @@ function (ConstantStringType $constantString): string { $type->describe(VerbosityLevel::getRecommendedLevelByType($type)) ))->build(), ]; + } else { + return []; } } return $this->disallowedConstantRuleErrors->get($this->getFullyQualified($classNames, $constant), $scope, $displayName, $this->disallowedConstants); diff --git a/tests/src/invalid/constantUsages.php b/tests/src/invalid/constantUsages.php index 0205db9..34cb24f 100644 --- a/tests/src/invalid/constantUsages.php +++ b/tests/src/invalid/constantUsages.php @@ -22,3 +22,6 @@ /** @var class-string $tz */ $tz = DateTimeZone::class; $tz::FTC; + +/** @var mixed $tz */ +$tz::ALL;