BuilderAbstract.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace PhpParser;
  3. use PhpParser\Node\Name;
  4. use PhpParser\Node\Expr;
  5. use PhpParser\Node\Stmt;
  6. use PhpParser\Node\Scalar;
  7. use PhpParser\Comment;
  8. abstract class BuilderAbstract implements Builder {
  9. /**
  10. * Normalizes a node: Converts builder objects to nodes.
  11. *
  12. * @param Node|Builder $node The node to normalize
  13. *
  14. * @return Node The normalized node
  15. */
  16. protected function normalizeNode($node) {
  17. if ($node instanceof Builder) {
  18. return $node->getNode();
  19. } elseif ($node instanceof Node) {
  20. return $node;
  21. }
  22. throw new \LogicException('Expected node or builder object');
  23. }
  24. /**
  25. * Normalizes a name: Converts plain string names to PhpParser\Node\Name.
  26. *
  27. * @param Name|string $name The name to normalize
  28. *
  29. * @return Name The normalized name
  30. */
  31. protected function normalizeName($name) {
  32. if ($name instanceof Name) {
  33. return $name;
  34. } elseif (is_string($name)) {
  35. if (!$name) {
  36. throw new \LogicException('Name cannot be empty');
  37. }
  38. if ($name[0] == '\\') {
  39. return new Name\FullyQualified(substr($name, 1));
  40. } elseif (0 === strpos($name, 'namespace\\')) {
  41. return new Name\Relative(substr($name, strlen('namespace\\')));
  42. } else {
  43. return new Name($name);
  44. }
  45. }
  46. throw new \LogicException('Name must be a string or an instance of PhpParser\Node\Name');
  47. }
  48. /**
  49. * Normalizes a value: Converts nulls, booleans, integers,
  50. * floats, strings and arrays into their respective nodes
  51. *
  52. * @param mixed $value The value to normalize
  53. *
  54. * @return Expr The normalized value
  55. */
  56. protected function normalizeValue($value) {
  57. if ($value instanceof Node) {
  58. return $value;
  59. } elseif (is_null($value)) {
  60. return new Expr\ConstFetch(
  61. new Name('null')
  62. );
  63. } elseif (is_bool($value)) {
  64. return new Expr\ConstFetch(
  65. new Name($value ? 'true' : 'false')
  66. );
  67. } elseif (is_int($value)) {
  68. return new Scalar\LNumber($value);
  69. } elseif (is_float($value)) {
  70. return new Scalar\DNumber($value);
  71. } elseif (is_string($value)) {
  72. return new Scalar\String_($value);
  73. } elseif (is_array($value)) {
  74. $items = array();
  75. $lastKey = -1;
  76. foreach ($value as $itemKey => $itemValue) {
  77. // for consecutive, numeric keys don't generate keys
  78. if (null !== $lastKey && ++$lastKey === $itemKey) {
  79. $items[] = new Expr\ArrayItem(
  80. $this->normalizeValue($itemValue)
  81. );
  82. } else {
  83. $lastKey = null;
  84. $items[] = new Expr\ArrayItem(
  85. $this->normalizeValue($itemValue),
  86. $this->normalizeValue($itemKey)
  87. );
  88. }
  89. }
  90. return new Expr\Array_($items);
  91. } else {
  92. throw new \LogicException('Invalid value');
  93. }
  94. }
  95. /**
  96. * Normalizes a doc comment: Converts plain strings to PhpParser\Comment\Doc.
  97. *
  98. * @param Comment\Doc|string $docComment The doc comment to normalize
  99. *
  100. * @return Comment\Doc The normalized doc comment
  101. */
  102. protected function normalizeDocComment($docComment) {
  103. if ($docComment instanceof Comment\Doc) {
  104. return $docComment;
  105. } else if (is_string($docComment)) {
  106. return new Comment\Doc($docComment);
  107. } else {
  108. throw new \LogicException('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
  109. }
  110. }
  111. /**
  112. * Sets a modifier in the $this->type property.
  113. *
  114. * @param int $modifier Modifier to set
  115. */
  116. protected function setModifier($modifier) {
  117. Stmt\Class_::verifyModifier($this->type, $modifier);
  118. $this->type |= $modifier;
  119. }
  120. }