BuilderHelpers.php 9.5 KB

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