Skip to content

Commit

Permalink
Update ClassReflection::getConstant() return type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 22, 2024
1 parent b60d12c commit 8d4ea60
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ClassReflection
/** @var PropertyReflection[] */
private array $properties = [];

/** @var ConstantReflection[] */
/** @var ClassConstantReflection[] */
private array $constants = [];

/** @var int[]|null */
Expand Down Expand Up @@ -1005,7 +1005,7 @@ public function hasConstant(string $name): bool
return $this->reflectionProvider->hasClass($reflectionConstant->getDeclaringClass()->getName());
}

public function getConstant(string $name): ConstantReflection
public function getConstant(string $name): ClassConstantReflection
{
if (!isset($this->constants[$name])) {
$reflectionConstant = $this->getNativeReflection()->getReflectionConstant($name);
Expand Down
13 changes: 3 additions & 10 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1906,28 +1906,21 @@ function (Type $type, callable $traverse): Type {

$constantReflection = $constantClassReflection->getConstant($constantName);
if (
$constantReflection instanceof ClassConstantReflection
&& !$constantClassReflection->isFinal()
!$constantClassReflection->isFinal()
&& !$constantReflection->hasPhpDocType()
&& !$constantReflection->hasNativeType()
) {
unset($this->currentlyResolvingClassConstant[$resolvingName]);
return new MixedType();
}

if (
!$constantReflection instanceof ClassConstantReflection
|| !$constantClassReflection->isFinal()
) {
if (!$constantClassReflection->isFinal()) {
$constantType = $constantReflection->getValueType();
} else {
$constantType = $this->getType($constantReflection->getValueExpr(), InitializerExprContext::fromClassReflection($constantReflection->getDeclaringClass()));
}

$nativeType = null;
if ($constantReflection instanceof ClassConstantReflection) {
$nativeType = $constantReflection->getNativeType();
}
$nativeType = $constantReflection->getNativeType();
$constantType = $this->constantResolver->resolveClassConstantType(
$constantClassReflection->getName(),
$constantName,
Expand Down
4 changes: 0 additions & 4 deletions src/Rules/Constants/OverridingConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ private function processSingleConstant(ClassReflection $classReflection, string
}

$constantReflection = $classReflection->getConstant($constantName);
if (!$constantReflection instanceof ClassConstantReflection) {
return [];
}

$errors = [];
if ($prototype->isFinal()) {
$errors[] = RuleErrorBuilder::message(sprintf(
Expand Down
5 changes: 0 additions & 5 deletions src/Rules/Constants/ValueAssignedToClassConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
Expand Down Expand Up @@ -58,10 +57,6 @@ public function processNode(Node $node, Scope $scope): array
private function processSingleConstant(ClassReflection $classReflection, string $constantName, Type $valueExprType, ?Type $nativeType): array
{
$constantReflection = $classReflection->getConstant($constantName);
if (!$constantReflection instanceof ClassConstantReflection) {
return [];
}

$phpDocType = $constantReflection->getPhpDocType();
if ($phpDocType === null) {
if ($nativeType === null) {
Expand Down
5 changes: 0 additions & 5 deletions src/Rules/PhpDoc/IncompatibleClassConstantPhpDocTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Internal\SprintfHelper;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Generics\GenericObjectTypeCheck;
use PHPStan\Rules\Rule;
Expand Down Expand Up @@ -62,10 +61,6 @@ public function processNode(Node $node, Scope $scope): array
private function processSingleConstant(ClassReflection $classReflection, ?Type $nativeType, string $constantName): array
{
$constantReflection = $classReflection->getConstant($constantName);
if (!$constantReflection instanceof ClassConstantReflection) {
return [];
}

$phpDocType = $constantReflection->getPhpDocType();
if ($phpDocType === null) {
return [];
Expand Down

0 comments on commit 8d4ea60

Please sign in to comment.