|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Parser Reflection API |
| 4 | + * |
| 5 | + * @copyright Copyright 2019, Lisachenko Alexander <lisachenko.it@gmail.com> |
| 6 | + * |
| 7 | + * This source file is subject to the license that is bundled |
| 8 | + * with this source code in the file LICENSE. |
| 9 | + */ |
| 10 | +declare(strict_types=1); |
| 11 | + |
| 12 | +namespace Go\ParserReflection; |
| 13 | + |
| 14 | +use Go\ParserReflection\ValueResolver\NodeExpressionResolver; |
| 15 | +use PhpParser\Node\Const_; |
| 16 | +use PhpParser\Node\Stmt\ClassConst; |
| 17 | +use PhpParser\Node\Stmt\ClassLike; |
| 18 | +use \ReflectionClassConstant as BaseReflectionClassConstant; |
| 19 | + |
| 20 | +class ReflectionClassConstant extends BaseReflectionClassConstant |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Concrete class constant node |
| 24 | + * |
| 25 | + * @var ClassConst |
| 26 | + */ |
| 27 | + private $classConstantNode; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Const_ |
| 31 | + */ |
| 32 | + private $constNode; |
| 33 | + |
| 34 | + /** |
| 35 | + * Name of the class |
| 36 | + * |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + private $className; |
| 40 | + |
| 41 | + /** |
| 42 | + * Parses class constants from the concrete class node |
| 43 | + * |
| 44 | + * @param ClassLike $classLikeNode Class-like node |
| 45 | + * @param string $reflectionClassName FQN of the class |
| 46 | + * |
| 47 | + * @return array|ReflectionClassConstant[] |
| 48 | + */ |
| 49 | + public static function collectFromClassNode(ClassLike $classLikeNode, string $reflectionClassName): array |
| 50 | + { |
| 51 | + $classConstants = []; |
| 52 | + |
| 53 | + foreach ($classLikeNode->stmts as $classLevelNode) { |
| 54 | + if ($classLevelNode instanceof ClassConst) { |
| 55 | + foreach ($classLevelNode->consts as $const) { |
| 56 | + $classConstName = $const->name->toString(); |
| 57 | + $classConstants[$classConstName] = new ReflectionClassConstant( |
| 58 | + $reflectionClassName, |
| 59 | + $classConstName, |
| 60 | + $classLevelNode, |
| 61 | + $const |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return $classConstants; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Initializes a reflection for the class constant |
| 72 | + * |
| 73 | + * @param string $className Name of the class |
| 74 | + * @param string $classConstantName Name of the class constant to reflect |
| 75 | + * @param ClassConst $classConstNode ClassConstant definition node |
| 76 | + * @param Const_|null $constNode Concrete const definition node |
| 77 | + */ |
| 78 | + public function __construct( |
| 79 | + string $className, |
| 80 | + string $classConstantName, |
| 81 | + ClassConst $classConstNode = null, |
| 82 | + Const_ $constNode = null |
| 83 | + ) { |
| 84 | + $this->className = ltrim($className, '\\'); |
| 85 | + |
| 86 | + if (!$classConstNode || !$constNode) { |
| 87 | + list($classConstNode, $constNode) = ReflectionEngine::parseClassConstant($className, $classConstantName); |
| 88 | + } |
| 89 | + |
| 90 | + $this->classConstantNode = $classConstNode; |
| 91 | + $this->constNode = $constNode; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @inheritDoc |
| 96 | + */ |
| 97 | + public function getDeclaringClass() |
| 98 | + { |
| 99 | + return new ReflectionClass($this->className); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @inheritDoc |
| 104 | + */ |
| 105 | + public function getDocComment() |
| 106 | + { |
| 107 | + $docBlock = $this->classConstantNode->getDocComment(); |
| 108 | + |
| 109 | + return $docBlock ? $docBlock->getText() : false; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @inheritDoc |
| 114 | + */ |
| 115 | + public function getModifiers() |
| 116 | + { |
| 117 | + $modifiers = 0; |
| 118 | + if ($this->isPublic()) { |
| 119 | + $modifiers += ReflectionMethod::IS_PUBLIC; |
| 120 | + } |
| 121 | + if ($this->isProtected()) { |
| 122 | + $modifiers += ReflectionMethod::IS_PROTECTED; |
| 123 | + } |
| 124 | + if ($this->isPrivate()) { |
| 125 | + $modifiers += ReflectionMethod::IS_PRIVATE; |
| 126 | + } |
| 127 | + |
| 128 | + return $modifiers; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @inheritDoc |
| 133 | + */ |
| 134 | + public function getName() |
| 135 | + { |
| 136 | + return $this->constNode->name->toString(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @inheritDoc |
| 141 | + */ |
| 142 | + public function getValue() |
| 143 | + { |
| 144 | + $solver = new NodeExpressionResolver($this->getDeclaringClass()); |
| 145 | + $solver->process($this->constNode->value); |
| 146 | + return $solver->getValue(); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @inheritDoc |
| 151 | + */ |
| 152 | + public function isPrivate() |
| 153 | + { |
| 154 | + return $this->classConstantNode->isPrivate(); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @inheritDoc |
| 159 | + */ |
| 160 | + public function isProtected() |
| 161 | + { |
| 162 | + return $this->classConstantNode->isProtected(); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @inheritDoc |
| 167 | + */ |
| 168 | + public function isPublic() |
| 169 | + { |
| 170 | + return $this->classConstantNode->isPublic(); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @inheritDoc |
| 175 | + */ |
| 176 | + public function __toString() |
| 177 | + { |
| 178 | + $value = $this->getValue(); |
| 179 | + $valueType = new ReflectionType(gettype($value), null, true); |
| 180 | + |
| 181 | + return sprintf( |
| 182 | + "Constant [ %s %s %s ] { %s }\n", |
| 183 | + implode(' ', \Reflection::getModifierNames($this->getModifiers())), |
| 184 | + strtolower((string) ReflectionType::convertToDisplayType($valueType)), |
| 185 | + $this->getName(), |
| 186 | + (string) $value |
| 187 | + ); |
| 188 | + } |
| 189 | +} |
0 commit comments