You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
9.5 KiB

3 years ago
  1. <?php declare(strict_types=1);
  2. namespace PhpParser;
  3. use PhpParser\Node\Expr;
  4. use PhpParser\Node\Identifier;
  5. use PhpParser\Node\Name;
  6. use PhpParser\Node\NullableType;
  7. use PhpParser\Node\Scalar;
  8. use PhpParser\Node\Stmt;
  9. use PhpParser\Node\UnionType;
  10. /**
  11. * This class defines helpers used in the implementation of builders. Don't use it directly.
  12. *
  13. * @internal
  14. */
  15. final class BuilderHelpers
  16. {
  17. /**
  18. * Normalizes a node: Converts builder objects to nodes.
  19. *
  20. * @param Node|Builder $node The node to normalize
  21. *
  22. * @return Node The normalized node
  23. */
  24. public static function normalizeNode($node) : Node {
  25. if ($node instanceof Builder) {
  26. return $node->getNode();
  27. }
  28. if ($node instanceof Node) {
  29. return $node;
  30. }
  31. throw new \LogicException('Expected node or builder object');
  32. }
  33. /**
  34. * Normalizes a node to a statement.
  35. *
  36. * Expressions are wrapped in a Stmt\Expression node.
  37. *
  38. * @param Node|Builder $node The node to normalize
  39. *
  40. * @return Stmt The normalized statement node
  41. */
  42. public static function normalizeStmt($node) : Stmt {
  43. $node = self::normalizeNode($node);
  44. if ($node instanceof Stmt) {
  45. return $node;
  46. }
  47. if ($node instanceof Expr) {
  48. return new Stmt\Expression($node);
  49. }
  50. throw new \LogicException('Expected statement or expression node');
  51. }
  52. /**
  53. * Normalizes strings to Identifier.
  54. *
  55. * @param string|Identifier $name The identifier to normalize
  56. *
  57. * @return Identifier The normalized identifier
  58. */
  59. public static function normalizeIdentifier($name) : Identifier {
  60. if ($name instanceof Identifier) {
  61. return $name;
  62. }
  63. if (\is_string($name)) {
  64. return new Identifier($name);
  65. }
  66. throw new \LogicException('Expected string or instance of Node\Identifier');
  67. }
  68. /**
  69. * Normalizes strings to Identifier, also allowing expressions.
  70. *
  71. * @param string|Identifier|Expr $name The identifier to normalize
  72. *
  73. * @return Identifier|Expr The normalized identifier or expression
  74. */
  75. public static function normalizeIdentifierOrExpr($name) {
  76. if ($name instanceof Identifier || $name instanceof Expr) {
  77. return $name;
  78. }
  79. if (\is_string($name)) {
  80. return new Identifier($name);
  81. }
  82. throw new \LogicException('Expected string or instance of Node\Identifier or Node\Expr');
  83. }
  84. /**
  85. * Normalizes a name: Converts string names to Name nodes.
  86. *
  87. * @param Name|string $name The name to normalize
  88. *
  89. * @return Name The normalized name
  90. */
  91. public static function normalizeName($name) : Name {
  92. return self::normalizeNameCommon($name, false);
  93. }
  94. /**
  95. * Normalizes a name: Converts string names to Name nodes, while also allowing expressions.
  96. *
  97. * @param Expr|Name|string $name The name to normalize
  98. *
  99. * @return Name|Expr The normalized name or expression
  100. */
  101. public static function normalizeNameOrExpr($name) {
  102. return self::normalizeNameCommon($name, true);
  103. }
  104. /**
  105. * Normalizes a name: Converts string names to Name nodes, optionally allowing expressions.
  106. *
  107. * @param Expr|Name|string $name The name to normalize
  108. * @param bool $allowExpr Whether to also allow expressions
  109. *
  110. * @return Name|Expr The normalized name, or expression (if allowed)
  111. */
  112. private static function normalizeNameCommon($name, bool $allowExpr) {
  113. if ($name instanceof Name) {
  114. return $name;
  115. }
  116. if (is_string($name)) {
  117. if (!$name) {
  118. throw new \LogicException('Name cannot be empty');
  119. }
  120. if ($name[0] === '\\') {
  121. return new Name\FullyQualified(substr($name, 1));
  122. }
  123. if (0 === strpos($name, 'namespace\\')) {
  124. return new Name\Relative(substr($name, strlen('namespace\\')));
  125. }
  126. return new Name($name);
  127. }
  128. if ($allowExpr) {
  129. if ($name instanceof Expr) {
  130. return $name;
  131. }
  132. throw new \LogicException(
  133. 'Name must be a string or an instance of Node\Name or Node\Expr'
  134. );
  135. }
  136. throw new \LogicException('Name must be a string or an instance of Node\Name');
  137. }
  138. /**
  139. * Normalizes a type: Converts plain-text type names into proper AST representation.
  140. *
  141. * In particular, builtin types become Identifiers, custom types become Names and nullables
  142. * are wrapped in NullableType nodes.
  143. *
  144. * @param string|Name|Identifier|NullableType|UnionType $type The type to normalize
  145. *
  146. * @return Name|Identifier|NullableType|UnionType The normalized type
  147. */
  148. public static function normalizeType($type) {
  149. if (!is_string($type)) {
  150. if (
  151. !$type instanceof Name && !$type instanceof Identifier &&
  152. !$type instanceof NullableType && !$type instanceof UnionType
  153. ) {
  154. throw new \LogicException(
  155. 'Type must be a string, or an instance of Name, Identifier, NullableType or UnionType'
  156. );
  157. }
  158. return $type;
  159. }
  160. $nullable = false;
  161. if (strlen($type) > 0 && $type[0] === '?') {
  162. $nullable = true;
  163. $type = substr($type, 1);
  164. }
  165. $builtinTypes = [
  166. 'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object', 'mixed', 'never',
  167. ];
  168. $lowerType = strtolower($type);
  169. if (in_array($lowerType, $builtinTypes)) {
  170. $type = new Identifier($lowerType);
  171. } else {
  172. $type = self::normalizeName($type);
  173. }
  174. $notNullableTypes = [
  175. 'void', 'mixed', 'never',
  176. ];
  177. if ($nullable && in_array((string) $type, $notNullableTypes)) {
  178. throw new \LogicException(sprintf('%s type cannot be nullable', $type));
  179. }
  180. return $nullable ? new NullableType($type) : $type;
  181. }
  182. /**
  183. * Normalizes a value: Converts nulls, booleans, integers,
  184. * floats, strings and arrays into their respective nodes
  185. *
  186. * @param Node\Expr|bool|null|int|float|string|array $value The value to normalize
  187. *
  188. * @return Expr The normalized value
  189. */
  190. public static function normalizeValue($value) : Expr {
  191. if ($value instanceof Node\Expr) {
  192. return $value;
  193. }
  194. if (is_null($value)) {
  195. return new Expr\ConstFetch(
  196. new Name('null')
  197. );
  198. }
  199. if (is_bool($value)) {
  200. return new Expr\ConstFetch(
  201. new Name($value ? 'true' : 'false')
  202. );
  203. }
  204. if (is_int($value)) {
  205. return new Scalar\LNumber($value);
  206. }
  207. if (is_float($value)) {
  208. return new Scalar\DNumber($value);
  209. }
  210. if (is_string($value)) {
  211. return new Scalar\String_($value);
  212. }
  213. if (is_array($value)) {
  214. $items = [];
  215. $lastKey = -1;
  216. foreach ($value as $itemKey => $itemValue) {
  217. // for consecutive, numeric keys don't generate keys
  218. if (null !== $lastKey && ++$lastKey === $itemKey) {
  219. $items[] = new Expr\ArrayItem(
  220. self::normalizeValue($itemValue)
  221. );
  222. } else {
  223. $lastKey = null;
  224. $items[] = new Expr\ArrayItem(
  225. self::normalizeValue($itemValue),
  226. self::normalizeValue($itemKey)
  227. );
  228. }
  229. }
  230. return new Expr\Array_($items);
  231. }
  232. throw new \LogicException('Invalid value');
  233. }
  234. /**
  235. * Normalizes a doc comment: Converts plain strings to PhpParser\Comment\Doc.
  236. *
  237. * @param Comment\Doc|string $docComment The doc comment to normalize
  238. *
  239. * @return Comment\Doc The normalized doc comment
  240. */
  241. public static function normalizeDocComment($docComment) : Comment\Doc {
  242. if ($docComment instanceof Comment\Doc) {
  243. return $docComment;
  244. }
  245. if (is_string($docComment)) {
  246. return new Comment\Doc($docComment);
  247. }
  248. throw new \LogicException('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
  249. }
  250. /**
  251. * Normalizes a attribute: Converts attribute to the Attribute Group if needed.
  252. *
  253. * @param Node\Attribute|Node\AttributeGroup $attribute
  254. *
  255. * @return Node\AttributeGroup The Attribute Group
  256. */
  257. public static function normalizeAttribute($attribute) : Node\AttributeGroup
  258. {
  259. if ($attribute instanceof Node\AttributeGroup) {
  260. return $attribute;
  261. }
  262. if (!($attribute instanceof Node\Attribute)) {
  263. throw new \LogicException('Attribute must be an instance of PhpParser\Node\Attribute or PhpParser\Node\AttributeGroup');
  264. }
  265. return new Node\AttributeGroup([$attribute]);
  266. }
  267. /**
  268. * Adds a modifier and returns new modifier bitmask.
  269. *
  270. * @param int $modifiers Existing modifiers
  271. * @param int $modifier Modifier to set
  272. *
  273. * @return int New modifiers
  274. */
  275. public static function addModifier(int $modifiers, int $modifier) : int {
  276. Stmt\Class_::verifyModifier($modifiers, $modifier);
  277. return $modifiers | $modifier;
  278. }
  279. }