Chris@0: getNode(); Chris@0: } elseif ($node instanceof Node) { Chris@0: return $node; Chris@0: } Chris@0: Chris@0: throw new \LogicException('Expected node or builder object'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Normalizes a name: Converts plain string names to PhpParser\Node\Name. Chris@0: * Chris@0: * @param Name|string $name The name to normalize Chris@0: * Chris@0: * @return Name The normalized name Chris@0: */ Chris@0: protected function normalizeName($name) { Chris@0: if ($name instanceof Name) { Chris@0: return $name; Chris@0: } elseif (is_string($name)) { Chris@0: if (!$name) { Chris@0: throw new \LogicException('Name cannot be empty'); Chris@0: } Chris@0: Chris@0: if ($name[0] == '\\') { Chris@0: return new Name\FullyQualified(substr($name, 1)); Chris@0: } elseif (0 === strpos($name, 'namespace\\')) { Chris@0: return new Name\Relative(substr($name, strlen('namespace\\'))); Chris@0: } else { Chris@0: return new Name($name); Chris@0: } Chris@0: } Chris@0: Chris@0: throw new \LogicException('Name must be a string or an instance of PhpParser\Node\Name'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Normalizes a type: Converts plain-text type names into proper AST representation. Chris@0: * Chris@0: * In particular, builtin types are left as strings, custom types become Names and nullables Chris@0: * are wrapped in NullableType nodes. Chris@0: * Chris@0: * @param Name|string|NullableType $type The type to normalize Chris@0: * Chris@0: * @return Name|string|NullableType The normalized type Chris@0: */ Chris@0: protected function normalizeType($type) { Chris@0: if (!is_string($type)) { Chris@0: if (!$type instanceof Name && !$type instanceof NullableType) { Chris@0: throw new \LogicException( Chris@0: 'Type must be a string, or an instance of Name or NullableType'); Chris@0: } Chris@0: return $type; Chris@0: } Chris@0: Chris@0: $nullable = false; Chris@0: if (strlen($type) > 0 && $type[0] === '?') { Chris@0: $nullable = true; Chris@0: $type = substr($type, 1); Chris@0: } Chris@0: Chris@0: $builtinTypes = array( Chris@0: 'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object' Chris@0: ); Chris@0: Chris@0: $lowerType = strtolower($type); Chris@0: if (in_array($lowerType, $builtinTypes)) { Chris@0: $type = $lowerType; Chris@0: } else { Chris@0: $type = $this->normalizeName($type); Chris@0: } Chris@0: Chris@0: if ($nullable && $type === 'void') { Chris@0: throw new \LogicException('void type cannot be nullable'); Chris@0: } Chris@0: Chris@0: return $nullable ? new Node\NullableType($type) : $type; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Normalizes a value: Converts nulls, booleans, integers, Chris@0: * floats, strings and arrays into their respective nodes Chris@0: * Chris@0: * @param mixed $value The value to normalize Chris@0: * Chris@0: * @return Expr The normalized value Chris@0: */ Chris@0: protected function normalizeValue($value) { Chris@0: if ($value instanceof Node) { Chris@0: return $value; Chris@0: } elseif (is_null($value)) { Chris@0: return new Expr\ConstFetch( Chris@0: new Name('null') Chris@0: ); Chris@0: } elseif (is_bool($value)) { Chris@0: return new Expr\ConstFetch( Chris@0: new Name($value ? 'true' : 'false') Chris@0: ); Chris@0: } elseif (is_int($value)) { Chris@0: return new Scalar\LNumber($value); Chris@0: } elseif (is_float($value)) { Chris@0: return new Scalar\DNumber($value); Chris@0: } elseif (is_string($value)) { Chris@0: return new Scalar\String_($value); Chris@0: } elseif (is_array($value)) { Chris@0: $items = array(); Chris@0: $lastKey = -1; Chris@0: foreach ($value as $itemKey => $itemValue) { Chris@0: // for consecutive, numeric keys don't generate keys Chris@0: if (null !== $lastKey && ++$lastKey === $itemKey) { Chris@0: $items[] = new Expr\ArrayItem( Chris@0: $this->normalizeValue($itemValue) Chris@0: ); Chris@0: } else { Chris@0: $lastKey = null; Chris@0: $items[] = new Expr\ArrayItem( Chris@0: $this->normalizeValue($itemValue), Chris@0: $this->normalizeValue($itemKey) Chris@0: ); Chris@0: } Chris@0: } Chris@0: Chris@0: return new Expr\Array_($items); Chris@0: } else { Chris@0: throw new \LogicException('Invalid value'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Normalizes a doc comment: Converts plain strings to PhpParser\Comment\Doc. Chris@0: * Chris@0: * @param Comment\Doc|string $docComment The doc comment to normalize Chris@0: * Chris@0: * @return Comment\Doc The normalized doc comment Chris@0: */ Chris@0: protected function normalizeDocComment($docComment) { Chris@0: if ($docComment instanceof Comment\Doc) { Chris@0: return $docComment; Chris@0: } else if (is_string($docComment)) { Chris@0: return new Comment\Doc($docComment); Chris@0: } else { Chris@0: throw new \LogicException('Doc comment must be a string or an instance of PhpParser\Comment\Doc'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a modifier in the $this->type property. Chris@0: * Chris@0: * @param int $modifier Modifier to set Chris@0: */ Chris@0: protected function setModifier($modifier) { Chris@0: Stmt\Class_::verifyModifier($this->flags, $modifier); Chris@0: $this->flags |= $modifier; Chris@0: } Chris@0: }