PhpParser.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\CodeParser;
  12. use Hyperf\CodeParser\Exception\InvalidArgumentException;
  13. use Hyperf\Collection\Arr;
  14. use PhpParser\Node;
  15. use PhpParser\Parser;
  16. use PhpParser\ParserFactory;
  17. use ReflectionClass;
  18. use ReflectionException;
  19. use ReflectionNamedType;
  20. use ReflectionParameter;
  21. use ReflectionType;
  22. use ReflectionUnionType;
  23. use function Hyperf\Support\value;
  24. class PhpParser
  25. {
  26. public const TYPES = [
  27. 'int',
  28. 'float',
  29. 'string',
  30. 'bool',
  31. 'array',
  32. 'object',
  33. 'resource',
  34. 'mixed',
  35. 'null',
  36. ];
  37. protected static ?PhpParser $instance = null;
  38. protected Parser $parser;
  39. public function __construct()
  40. {
  41. $parserFactory = new ParserFactory();
  42. $this->parser = $parserFactory->create(ParserFactory::ONLY_PHP7);
  43. }
  44. public static function getInstance(): PhpParser
  45. {
  46. if (static::$instance) {
  47. return static::$instance;
  48. }
  49. return static::$instance = new static();
  50. }
  51. /**
  52. * @return null|Node\Stmt[]
  53. */
  54. public function getNodesFromReflectionClass(ReflectionClass $reflectionClass): ?array
  55. {
  56. $code = file_get_contents($reflectionClass->getFileName());
  57. return $this->parser->parse($code);
  58. }
  59. public function getNodeFromReflectionType(ReflectionType $reflection): Node\ComplexType|Node\Identifier|Node\Name
  60. {
  61. if ($reflection instanceof ReflectionUnionType) {
  62. $unionType = [];
  63. foreach ($reflection->getTypes() as $objType) {
  64. $type = $objType->getName();
  65. if (! in_array($type, static::TYPES)) {
  66. $unionType[] = new Node\Name('\\' . $type);
  67. } else {
  68. $unionType[] = new Node\Identifier($type);
  69. }
  70. }
  71. return new Node\UnionType($unionType);
  72. }
  73. return $this->getTypeWithNullableOrNot($reflection);
  74. }
  75. public function getNodeFromReflectionParameter(ReflectionParameter $parameter): Node\Param
  76. {
  77. $result = new Node\Param(
  78. new Node\Expr\Variable($parameter->getName())
  79. );
  80. if ($parameter->isDefaultValueAvailable()) {
  81. $result->default = $this->getExprFromValue($parameter->getDefaultValue());
  82. }
  83. if ($parameter->hasType()) {
  84. $result->type = $this->getNodeFromReflectionType($parameter->getType());
  85. }
  86. if ($parameter->isPassedByReference()) {
  87. $result->byRef = true;
  88. }
  89. if ($parameter->isVariadic()) {
  90. $result->variadic = true;
  91. }
  92. return $result;
  93. }
  94. public function getExprFromValue($value): Node\Expr
  95. {
  96. return match (gettype($value)) {
  97. 'array' => value(function ($value) {
  98. $isList = ! Arr::isAssoc($value);
  99. $result = [];
  100. foreach ($value as $i => $item) {
  101. $key = null;
  102. if (! $isList) {
  103. $key = is_int($i) ? new Node\Scalar\LNumber($i) : new Node\Scalar\String_($i);
  104. }
  105. $result[] = new Node\Expr\ArrayItem($this->getExprFromValue($item), $key);
  106. }
  107. return new Node\Expr\Array_($result, [
  108. 'kind' => Node\Expr\Array_::KIND_SHORT,
  109. ]);
  110. }, $value),
  111. 'string' => new Node\Scalar\String_($value),
  112. 'integer' => new Node\Scalar\LNumber($value),
  113. 'double' => new Node\Scalar\DNumber($value),
  114. 'NULL' => new Node\Expr\ConstFetch(new Node\Name('null')),
  115. 'boolean' => new Node\Expr\ConstFetch(new Node\Name($value ? 'true' : 'false')),
  116. 'object' => $this->getExprFromObject($value),
  117. default => throw new InvalidArgumentException($value . ' is invalid'),
  118. };
  119. }
  120. /**
  121. * @return Node\Stmt\ClassMethod[]
  122. */
  123. public function getAllMethodsFromStmts(array $stmts): array
  124. {
  125. $methods = [];
  126. foreach ($stmts as $namespace) {
  127. if (! $namespace instanceof Node\Stmt\Namespace_) {
  128. continue;
  129. }
  130. foreach ($namespace->stmts as $class) {
  131. if (! $class instanceof Node\Stmt\Class_ && ! $class instanceof Node\Stmt\Interface_) {
  132. continue;
  133. }
  134. foreach ($class->getMethods() as $method) {
  135. $methods[] = $method;
  136. }
  137. }
  138. }
  139. return $methods;
  140. }
  141. private function getExprFromObject(object $value)
  142. {
  143. $ref = new ReflectionClass($value);
  144. if (method_exists($ref, 'isEnum') && $ref->isEnum()) {
  145. return new Node\Expr\ClassConstFetch(
  146. new Node\Name('\\' . $value::class),
  147. $value->name
  148. );
  149. }
  150. return new Node\Expr\New_(
  151. new Node\Name\FullyQualified($value::class)
  152. );
  153. }
  154. private function getTypeWithNullableOrNot(ReflectionType $reflection): Node\ComplexType|Node\Identifier|Node\Name
  155. {
  156. if (! $reflection instanceof ReflectionNamedType) {
  157. throw new ReflectionException('ReflectionType must be ReflectionNamedType.');
  158. }
  159. $name = $reflection->getName();
  160. if ($reflection->allowsNull() && $name !== 'mixed') {
  161. return new Node\NullableType($this->getTypeFromString($name));
  162. }
  163. if (! in_array($name, static::TYPES)) {
  164. return new Node\Name('\\' . $name);
  165. }
  166. return new Node\Identifier($name);
  167. }
  168. private function getTypeFromString(string $name)
  169. {
  170. if (! in_array($name, static::TYPES)) {
  171. return '\\' . $name;
  172. }
  173. return $name;
  174. }
  175. }