| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace PHPStan\BetterReflection\Reflection; |
| 6: | |
| 7: | use PhpParser\Node; |
| 8: | use PhpParser\Node\Stmt\ClassConst; |
| 9: | use ReflectionClassConstant as CoreReflectionClassConstant; |
| 10: | use PHPStan\BetterReflection\BetterReflection; |
| 11: | use PHPStan\BetterReflection\NodeCompiler\CompiledValue; |
| 12: | use PHPStan\BetterReflection\NodeCompiler\CompileNodeToValue; |
| 13: | use PHPStan\BetterReflection\NodeCompiler\CompilerContext; |
| 14: | use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClassConstant as ReflectionClassConstantAdapter; |
| 15: | use PHPStan\BetterReflection\Reflection\Annotation\AnnotationHelper; |
| 16: | use PHPStan\BetterReflection\Reflection\Attribute\ReflectionAttributeHelper; |
| 17: | use PHPStan\BetterReflection\Reflection\StringCast\ReflectionClassConstantStringCast; |
| 18: | use PHPStan\BetterReflection\Reflector\Reflector; |
| 19: | use PHPStan\BetterReflection\Util\CalculateReflectionColumn; |
| 20: | use PHPStan\BetterReflection\Util\GetLastDocComment; |
| 21: | |
| 22: | use function array_map; |
| 23: | use function assert; |
| 24: | |
| 25: | |
| 26: | class ReflectionClassConstant |
| 27: | { |
| 28: | |
| 29: | private $name; |
| 30: | |
| 31: | |
| 32: | private $modifiers; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | private $type; |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | private $value; |
| 43: | |
| 44: | |
| 45: | private $docComment; |
| 46: | |
| 47: | |
| 48: | private $attributes; |
| 49: | |
| 50: | |
| 51: | private $startLine; |
| 52: | |
| 53: | |
| 54: | private $endLine; |
| 55: | |
| 56: | |
| 57: | private $startColumn; |
| 58: | |
| 59: | |
| 60: | private $endColumn; |
| 61: | |
| 62: | |
| 63: | |
| 64: | private $compiledValue = null; |
| 65: | |
| 66: | |
| 67: | |
| 68: | private $reflector; |
| 69: | |
| 70: | |
| 71: | |
| 72: | private $declaringClass; |
| 73: | |
| 74: | |
| 75: | |
| 76: | private $implementingClass; |
| 77: | private function __construct(Reflector $reflector, ClassConst $node, int $positionInNode, ReflectionClass $declaringClass, ReflectionClass $implementingClass) |
| 78: | { |
| 79: | $this->reflector = $reflector; |
| 80: | $this->declaringClass = $declaringClass; |
| 81: | $this->implementingClass = $implementingClass; |
| 82: | $name = $node->consts[$positionInNode]->name->name; |
| 83: | assert($name !== ''); |
| 84: | $this->name = $name; |
| 85: | $this->modifiers = $this->computeModifiers($node); |
| 86: | $this->type = $this->createType($node); |
| 87: | $this->value = $node->consts[$positionInNode]->value; |
| 88: | $this->docComment = GetLastDocComment::forNode($node); |
| 89: | $this->attributes = ReflectionAttributeHelper::createAttributes($reflector, $this, $node->attrGroups); |
| 90: | $startLine = $node->getStartLine(); |
| 91: | assert($startLine > 0); |
| 92: | $endLine = $node->getEndLine(); |
| 93: | assert($endLine > 0); |
| 94: | $this->startLine = $startLine; |
| 95: | $this->endLine = $endLine; |
| 96: | $this->startColumn = CalculateReflectionColumn::getStartColumn($declaringClass->getLocatedSource()->getSource(), $node); |
| 97: | $this->endColumn = CalculateReflectionColumn::getEndColumn($declaringClass->getLocatedSource()->getSource(), $node); |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | public static function createFromNode(Reflector $reflector, ClassConst $node, int $positionInNode, ReflectionClass $declaringClass, ReflectionClass $implementingClass): self |
| 105: | { |
| 106: | return new self($reflector, $node, $positionInNode, $declaringClass, $implementingClass); |
| 107: | } |
| 108: | |
| 109: | |
| 110: | public function withImplementingClass(ReflectionClass $implementingClass): self |
| 111: | { |
| 112: | $clone = clone $this; |
| 113: | $clone->implementingClass = $implementingClass; |
| 114: | |
| 115: | $clone->attributes = array_map(static function (ReflectionAttribute $attribute) use ($clone) : ReflectionAttribute { |
| 116: | return $attribute->withOwner($clone); |
| 117: | }, $this->attributes); |
| 118: | |
| 119: | $this->compiledValue = null; |
| 120: | |
| 121: | return $clone; |
| 122: | } |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | public function getName(): string |
| 131: | { |
| 132: | return $this->name; |
| 133: | } |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | private function createType(ClassConst $node) |
| 139: | { |
| 140: | $type = $node->type; |
| 141: | |
| 142: | if ($type === null) { |
| 143: | return null; |
| 144: | } |
| 145: | |
| 146: | assert($type instanceof Node\Identifier || $type instanceof Node\Name || $type instanceof Node\NullableType || $type instanceof Node\UnionType || $type instanceof Node\IntersectionType); |
| 147: | |
| 148: | return ReflectionType::createFromNode($this->reflector, $this, $type); |
| 149: | } |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | public function getType() |
| 155: | { |
| 156: | return $this->type; |
| 157: | } |
| 158: | |
| 159: | public function hasType(): bool |
| 160: | { |
| 161: | return $this->type !== null; |
| 162: | } |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | public function getValueExpr(): Node\Expr |
| 168: | { |
| 169: | return $this->getValueExpression(); |
| 170: | } |
| 171: | |
| 172: | public function getValueExpression(): Node\Expr |
| 173: | { |
| 174: | return $this->value; |
| 175: | } |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | public function getValue() |
| 184: | { |
| 185: | if ($this->compiledValue === null) { |
| 186: | $this->compiledValue = (new CompileNodeToValue())->__invoke($this->value, new CompilerContext($this->reflector, $this)); |
| 187: | } |
| 188: | |
| 189: | return $this->compiledValue->value; |
| 190: | } |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | public function isPublic(): bool |
| 196: | { |
| 197: | return ($this->modifiers & ReflectionClassConstantAdapter::IS_PUBLIC_COMPATIBILITY) === ReflectionClassConstantAdapter::IS_PUBLIC_COMPATIBILITY; |
| 198: | } |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | public function isPrivate(): bool |
| 204: | { |
| 205: | |
| 206: | return $this->modifiers === ReflectionClassConstantAdapter::IS_PRIVATE_COMPATIBILITY; |
| 207: | } |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | public function isProtected(): bool |
| 213: | { |
| 214: | return ($this->modifiers & ReflectionClassConstantAdapter::IS_PROTECTED_COMPATIBILITY) === ReflectionClassConstantAdapter::IS_PROTECTED_COMPATIBILITY; |
| 215: | } |
| 216: | |
| 217: | public function isFinal(): bool |
| 218: | { |
| 219: | $final = ($this->modifiers & ReflectionClassConstantAdapter::IS_FINAL_COMPATIBILITY) === ReflectionClassConstantAdapter::IS_FINAL_COMPATIBILITY; |
| 220: | if ($final) { |
| 221: | return true; |
| 222: | } |
| 223: | |
| 224: | if (BetterReflection::$phpVersion >= 80100) { |
| 225: | return false; |
| 226: | } |
| 227: | |
| 228: | return $this->getDeclaringClass()->isInterface(); |
| 229: | } |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | |
| 235: | |
| 236: | public function getModifiers(): int |
| 237: | { |
| 238: | return $this->modifiers; |
| 239: | } |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | public function getStartLine(): int |
| 247: | { |
| 248: | return $this->startLine; |
| 249: | } |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | |
| 255: | |
| 256: | public function getEndLine(): int |
| 257: | { |
| 258: | return $this->endLine; |
| 259: | } |
| 260: | |
| 261: | |
| 262: | public function getStartColumn(): int |
| 263: | { |
| 264: | return $this->startColumn; |
| 265: | } |
| 266: | |
| 267: | |
| 268: | public function getEndColumn(): int |
| 269: | { |
| 270: | return $this->endColumn; |
| 271: | } |
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: | public function getDeclaringClass(): ReflectionClass |
| 277: | { |
| 278: | return $this->declaringClass; |
| 279: | } |
| 280: | |
| 281: | |
| 282: | |
| 283: | |
| 284: | public function getImplementingClass(): ReflectionClass |
| 285: | { |
| 286: | return $this->implementingClass; |
| 287: | } |
| 288: | |
| 289: | |
| 290: | public function getDocComment(): ?string |
| 291: | { |
| 292: | return $this->docComment; |
| 293: | } |
| 294: | |
| 295: | public function isDeprecated(): bool |
| 296: | { |
| 297: | return AnnotationHelper::isDeprecated($this->getDocComment()); |
| 298: | } |
| 299: | |
| 300: | |
| 301: | public function __toString(): string |
| 302: | { |
| 303: | return ReflectionClassConstantStringCast::toString($this); |
| 304: | } |
| 305: | |
| 306: | |
| 307: | public function getAttributes(): array |
| 308: | { |
| 309: | return $this->attributes; |
| 310: | } |
| 311: | |
| 312: | |
| 313: | public function getAttributesByName(string $name): array |
| 314: | { |
| 315: | return ReflectionAttributeHelper::filterAttributesByName($this->getAttributes(), $name); |
| 316: | } |
| 317: | |
| 318: | |
| 319: | |
| 320: | |
| 321: | |
| 322: | |
| 323: | public function getAttributesByInstance(string $className): array |
| 324: | { |
| 325: | return ReflectionAttributeHelper::filterAttributesByInstance($this->getAttributes(), $className); |
| 326: | } |
| 327: | |
| 328: | |
| 329: | private function computeModifiers(ClassConst $node): int |
| 330: | { |
| 331: | $modifiers = $node->isFinal() ? ReflectionClassConstantAdapter::IS_FINAL_COMPATIBILITY : 0; |
| 332: | $modifiers += $node->isPrivate() ? ReflectionClassConstantAdapter::IS_PRIVATE_COMPATIBILITY : 0; |
| 333: | $modifiers += $node->isProtected() ? ReflectionClassConstantAdapter::IS_PROTECTED_COMPATIBILITY : 0; |
| 334: | $modifiers += $node->isPublic() ? ReflectionClassConstantAdapter::IS_PUBLIC_COMPATIBILITY : 0; |
| 335: | |
| 336: | return $modifiers; |
| 337: | } |
| 338: | } |
| 339: | |