Skip to content

Commit

Permalink
[BCB] ConstantReflection
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 1, 2024
1 parent c067207 commit 41275dc
Show file tree
Hide file tree
Showing 36 changed files with 260 additions and 223 deletions.
5 changes: 5 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,8 @@ Instead of `PHPStanTestCase::createBroker()`, call `PHPStanTestCase::createRefle
* Remove `Type::acceptsWithReason()`, `Type:accepts()` return type changed from `TrinaryLogic` to [`AcceptsResult`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.AcceptsResult.html)
* Remove `CompoundType::isAcceptedWithReasonBy()`, `CompoundType::isAcceptedBy()` return type changed from `TrinaryLogic` to [`AcceptsResult`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.AcceptsResult.html)
* `RuleLevelHelper::accepts()` return type changed from `bool` to [`RuleLevelHelperAcceptsResult`](https://apiref.phpstan.org/2.0.x/PHPStan.Type.AcceptsResult.html)
* Changes around `ClassConstantReflection`
* Class `ClassConstantReflection` removed from BC promise, renamed to `RealClassConstantReflection`
* Interface `ConstantReflection` renamed to `ClassConstantReflection`
* Added more methods around PHPDoc types and native types to the (new) `ClassConstantReflection`
* Interface `GlobalConstantReflection` renamed to `ConstantReflection`
6 changes: 3 additions & 3 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
use PHPStan\Reflection\Callables\CallableParametersAcceptor;
use PHPStan\Reflection\Callables\SimpleImpurePoint;
use PHPStan\Reflection\Callables\SimpleThrowPoint;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ClassMemberReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\Dummy\DummyConstructorReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
Expand Down Expand Up @@ -5326,7 +5326,7 @@ public function canCallMethod(MethodReflection $methodReflection): bool
}

/** @api */
public function canAccessConstant(ConstantReflection $constantReflection): bool
public function canAccessConstant(ClassConstantReflection $constantReflection): bool
{
return $this->canAccessClassMember($constantReflection);
}
Expand Down Expand Up @@ -5690,7 +5690,7 @@ private function propertyFetchType(Type $fetchedOnType, string $propertyName, Ex
return $propertyReflection->getReadableType();
}

public function getConstantReflection(Type $typeWithConstant, string $constantName): ?ConstantReflection
public function getConstantReflection(Type $typeWithConstant, string $constantName): ?ClassConstantReflection
{
if ($typeWithConstant instanceof UnionType) {
$newTypes = [];
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/OutOfClassScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace PHPStan\Analyser;

use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\PropertyReflection;

Expand Down Expand Up @@ -36,7 +36,7 @@ public function canCallMethod(MethodReflection $methodReflection): bool
return $methodReflection->isPublic();
}

public function canAccessConstant(ConstantReflection $constantReflection): bool
public function canAccessConstant(ClassConstantReflection $constantReflection): bool
{
return $constantReflection->isPublic();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ClassMemberAccessAnswerer;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ExtendedPropertyReflection;
use PHPStan\Reflection\FunctionReflection;
Expand Down Expand Up @@ -73,7 +73,7 @@ public function getPropertyReflection(Type $typeWithProperty, string $propertyNa

public function getMethodReflection(Type $typeWithMethod, string $methodName): ?ExtendedMethodReflection;

public function getConstantReflection(Type $typeWithConstant, string $constantName): ?ConstantReflection;
public function getConstantReflection(Type $typeWithConstant, string $constantName): ?ClassConstantReflection;

public function getIterableKeyType(Type $iteratee): Type;

Expand Down
4 changes: 2 additions & 2 deletions src/PhpDoc/PhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace PHPStan\PhpDoc;

use PHPStan\PhpDoc\Tag\AssertTagParameter;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
Expand Down Expand Up @@ -334,7 +334,7 @@ private static function resolvePhpDocBlockFromClass(
): ?self
{
if ($classReflection->$hasMethodName($name)) {
/** @var PropertyReflection|MethodReflection|ConstantReflection $parentReflection */
/** @var PropertyReflection|MethodReflection|ClassConstantReflection $parentReflection */
$parentReflection = $classReflection->$getMethodName($name);
if ($parentReflection->isPrivate()) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions src/Reflection/BetterReflection/BetterReflectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
use PHPStan\Reflection\ClassNameHelper;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Constant\RuntimeConstantReflection;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\FunctionReflectionFactory;
use PHPStan\Reflection\GlobalConstantReflection;
use PHPStan\Reflection\InitializerExprContext;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\NamespaceAnswerer;
Expand Down Expand Up @@ -71,7 +71,7 @@ final class BetterReflectionProvider implements ReflectionProvider
/** @var ClassReflection[] */
private static array $anonymousClasses = [];

/** @var array<string, GlobalConstantReflection> */
/** @var array<string, ConstantReflection> */
private array $cachedConstants = [];

/**
Expand Down Expand Up @@ -389,7 +389,7 @@ public function hasConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAn
return $this->resolveConstantName($nameNode, $namespaceAnswerer) !== null;
}

public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAnswerer): GlobalConstantReflection
public function getConstant(Node\Name $nameNode, ?NamespaceAnswerer $namespaceAnswerer): ConstantReflection
{
$constantName = $this->resolveConstantName($nameNode, $namespaceAnswerer);
if ($constantName === null) {
Expand Down
135 changes: 8 additions & 127 deletions src/Reflection/ClassConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,141 +3,22 @@
namespace PHPStan\Reflection;

use PhpParser\Node\Expr;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClassConstant;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use PHPStan\Type\TypehintHelper;

/**
* @api
*/
final class ClassConstantReflection implements ConstantReflection
/** @api */
interface ClassConstantReflection extends ClassMemberReflection, ConstantReflection
{

private ?Type $valueType = null;
public function getValueExpr(): Expr;

public function __construct(
private InitializerExprTypeResolver $initializerExprTypeResolver,
private ClassReflection $declaringClass,
private ReflectionClassConstant $reflection,
private ?Type $nativeType,
private ?Type $phpDocType,
private ?string $deprecatedDescription,
private bool $isDeprecated,
private bool $isInternal,
)
{
}
public function isFinal(): bool;

public function getName(): string
{
return $this->reflection->getName();
}
public function hasPhpDocType(): bool;

public function getFileName(): ?string
{
return $this->declaringClass->getFileName();
}
public function getPhpDocType(): ?Type;

public function getValueExpr(): Expr
{
return $this->reflection->getValueExpression();
}
public function hasNativeType(): bool;

public function hasPhpDocType(): bool
{
return $this->phpDocType !== null;
}

public function getPhpDocType(): ?Type
{
return $this->phpDocType;
}

public function hasNativeType(): bool
{
return $this->nativeType !== null;
}

public function getNativeType(): ?Type
{
return $this->nativeType;
}

public function getValueType(): Type
{
if ($this->valueType === null) {
if ($this->phpDocType !== null) {
if ($this->nativeType !== null) {
return $this->valueType = TypehintHelper::decideType(
$this->nativeType,
$this->phpDocType,
);
}

return $this->phpDocType;
} elseif ($this->nativeType !== null) {
return $this->nativeType;
}

$this->valueType = $this->initializerExprTypeResolver->getType($this->getValueExpr(), InitializerExprContext::fromClassReflection($this->declaringClass));
}

return $this->valueType;
}

public function getDeclaringClass(): ClassReflection
{
return $this->declaringClass;
}

public function isStatic(): bool
{
return true;
}

public function isPrivate(): bool
{
return $this->reflection->isPrivate();
}

public function isPublic(): bool
{
return $this->reflection->isPublic();
}

public function isFinal(): bool
{
return $this->reflection->isFinal();
}

public function isDeprecated(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->isDeprecated);
}

public function getDeprecatedDescription(): ?string
{
if ($this->isDeprecated) {
return $this->deprecatedDescription;
}

return null;
}

public function isInternal(): TrinaryLogic
{
return TrinaryLogic::createFromBoolean($this->isInternal);
}

public function getDocComment(): ?string
{
$docComment = $this->reflection->getDocComment();
if ($docComment === false) {
return null;
}

return $docComment;
}
public function getNativeType(): ?Type;

}
2 changes: 1 addition & 1 deletion src/Reflection/ClassMemberAccessAnswerer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function canAccessProperty(PropertyReflection $propertyReflection): bool;

public function canCallMethod(MethodReflection $methodReflection): bool;

public function canAccessConstant(ConstantReflection $constantReflection): bool;
public function canAccessConstant(ClassConstantReflection $constantReflection): bool;

}
4 changes: 2 additions & 2 deletions src/Reflection/ClassReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ final class ClassReflection
/** @var ExtendedPropertyReflection[] */
private array $properties = [];

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

/** @var EnumCaseReflection[]|null */
Expand Down Expand Up @@ -1082,7 +1082,7 @@ public function getConstant(string $name): ClassConstantReflection
$nativeType = $this->signatureMapProvider->getClassConstantMetadata($declaringClass->getName(), $name)['nativeType'];
}

$this->constants[$name] = new ClassConstantReflection(
$this->constants[$name] = new RealClassClassConstantReflection(
$this->initializerExprTypeResolver,
$declaringClass,
$reflectionConstant,
Expand Down
4 changes: 2 additions & 2 deletions src/Reflection/Constant/RuntimeConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace PHPStan\Reflection\Constant;

use PHPStan\Reflection\GlobalConstantReflection;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;

final class RuntimeConstantReflection implements GlobalConstantReflection
final class RuntimeConstantReflection implements ConstantReflection
{

public function __construct(
Expand Down
17 changes: 14 additions & 3 deletions src/Reflection/ConstantReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@

namespace PHPStan\Reflection;

use PhpParser\Node\Expr;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;

/** @api */
interface ConstantReflection extends ClassMemberReflection, GlobalConstantReflection
interface ConstantReflection
{

public function getValueExpr(): Expr;
public function getName(): string;

public function getValueType(): Type;

public function isDeprecated(): TrinaryLogic;

public function getDeprecatedDescription(): ?string;

public function isInternal(): TrinaryLogic;

public function getFileName(): ?string;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use PhpParser\Node\Expr;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Reflection\ClassConstantReflection;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ConstantReflection;
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
use PHPStan\TrinaryLogic;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use stdClass;

final class DummyConstantReflection implements ConstantReflection
final class DummyClassConstantReflection implements ClassConstantReflection
{

public function __construct(private string $name)
Expand All @@ -26,6 +26,11 @@ public function getDeclaringClass(): ClassReflection
return $reflectionProvider->getClass(stdClass::class);
}

public function isFinal(): bool
{
return false;
}

public function getFileName(): ?string
{
return null;
Expand Down Expand Up @@ -81,4 +86,24 @@ public function getDocComment(): ?string
return null;
}

public function hasPhpDocType(): bool
{
return false;
}

public function getPhpDocType(): ?Type
{
return null;
}

public function hasNativeType(): bool
{
return false;
}

public function getNativeType(): ?Type
{
return null;
}

}
Loading

0 comments on commit 41275dc

Please sign in to comment.