2012-03-10 23:25:26 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Comment;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Expr;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Node\Name;
|
2016-09-16 13:52:47 +02:00
|
|
|
use PhpParser\Node\NullableType;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Scalar;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Node\Stmt;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
|
|
|
abstract class BuilderAbstract implements Builder {
|
2012-03-10 23:25:26 +01:00
|
|
|
/**
|
|
|
|
* Normalizes a node: Converts builder objects to nodes.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Node|Builder $node The node to normalize
|
2012-03-10 23:25:26 +01:00
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Node The normalized node
|
2012-03-10 23:25:26 +01:00
|
|
|
*/
|
|
|
|
protected function normalizeNode($node) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($node instanceof Builder) {
|
2012-03-10 23:25:26 +01:00
|
|
|
return $node->getNode();
|
2014-02-06 14:44:16 +01:00
|
|
|
} elseif ($node instanceof Node) {
|
2012-03-10 23:25:26 +01:00
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new \LogicException('Expected node or builder object');
|
2012-03-10 23:25:26 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 00:06:02 +01:00
|
|
|
/**
|
2014-02-12 17:47:34 +01:00
|
|
|
* Normalizes a name: Converts plain string names to PhpParser\Node\Name.
|
2012-03-11 00:06:02 +01:00
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Name|string $name The name to normalize
|
2012-03-11 00:06:02 +01:00
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Name The normalized name
|
2012-03-11 00:06:02 +01:00
|
|
|
*/
|
|
|
|
protected function normalizeName($name) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($name instanceof Name) {
|
2012-03-11 00:06:02 +01:00
|
|
|
return $name;
|
2014-12-19 18:48:21 +01:00
|
|
|
} elseif (is_string($name)) {
|
|
|
|
if (!$name) {
|
|
|
|
throw new \LogicException('Name cannot be empty');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($name[0] == '\\') {
|
|
|
|
return new Name\FullyQualified(substr($name, 1));
|
|
|
|
} elseif (0 === strpos($name, 'namespace\\')) {
|
|
|
|
return new Name\Relative(substr($name, strlen('namespace\\')));
|
|
|
|
} else {
|
|
|
|
return new Name($name);
|
|
|
|
}
|
2012-03-11 00:06:02 +01:00
|
|
|
}
|
2014-12-19 18:50:16 +01:00
|
|
|
|
|
|
|
throw new \LogicException('Name must be a string or an instance of PhpParser\Node\Name');
|
2012-03-11 00:06:02 +01:00
|
|
|
}
|
|
|
|
|
2016-09-16 13:52:47 +02:00
|
|
|
/**
|
|
|
|
* Normalizes a type: Converts plain-text type names into proper AST representation.
|
|
|
|
*
|
|
|
|
* In particular, builtin types are left as strings, custom types become Names and nullables
|
|
|
|
* are wrapped in NullableType nodes.
|
|
|
|
*
|
2016-12-05 13:30:29 +01:00
|
|
|
* @param Name|string|NullableType $type The type to normalize
|
2016-09-16 13:52:47 +02:00
|
|
|
*
|
2016-12-05 13:30:29 +01:00
|
|
|
* @return Name|string|NullableType The normalized type
|
2016-09-16 13:52:47 +02:00
|
|
|
*/
|
|
|
|
protected function normalizeType($type) {
|
|
|
|
if (!is_string($type)) {
|
|
|
|
if (!$type instanceof Name && !$type instanceof NullableType) {
|
|
|
|
throw new \LogicException(
|
|
|
|
'Type must be a string, or an instance of Name or NullableType');
|
|
|
|
}
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
|
|
|
|
$nullable = false;
|
|
|
|
if (strlen($type) > 0 && $type[0] === '?') {
|
|
|
|
$nullable = true;
|
|
|
|
$type = substr($type, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$builtinTypes = array(
|
|
|
|
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void'
|
|
|
|
);
|
|
|
|
|
|
|
|
$lowerType = strtolower($type);
|
|
|
|
if (in_array($lowerType, $builtinTypes)) {
|
|
|
|
$type = $lowerType;
|
|
|
|
} else {
|
|
|
|
$type = $this->normalizeName($type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($nullable && $type === 'void') {
|
|
|
|
throw new \LogicException('void type cannot be nullable');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $nullable ? new Node\NullableType($type) : $type;
|
|
|
|
}
|
|
|
|
|
2012-03-11 00:06:02 +01:00
|
|
|
/**
|
|
|
|
* Normalizes a value: Converts nulls, booleans, integers,
|
2012-11-06 18:28:15 +01:00
|
|
|
* floats, strings and arrays into their respective nodes
|
2012-03-11 00:06:02 +01:00
|
|
|
*
|
|
|
|
* @param mixed $value The value to normalize
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Expr The normalized value
|
2012-03-11 00:06:02 +01:00
|
|
|
*/
|
|
|
|
protected function normalizeValue($value) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($value instanceof Node) {
|
2012-03-11 00:06:02 +01:00
|
|
|
return $value;
|
|
|
|
} elseif (is_null($value)) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Expr\ConstFetch(
|
|
|
|
new Name('null')
|
2012-03-11 00:06:02 +01:00
|
|
|
);
|
|
|
|
} elseif (is_bool($value)) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Expr\ConstFetch(
|
|
|
|
new Name($value ? 'true' : 'false')
|
2012-03-11 00:06:02 +01:00
|
|
|
);
|
|
|
|
} elseif (is_int($value)) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Scalar\LNumber($value);
|
2012-03-11 00:06:02 +01:00
|
|
|
} elseif (is_float($value)) {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Scalar\DNumber($value);
|
2012-03-11 00:06:02 +01:00
|
|
|
} elseif (is_string($value)) {
|
2015-03-20 21:47:20 +01:00
|
|
|
return new Scalar\String_($value);
|
2012-03-11 08:53:04 +01:00
|
|
|
} elseif (is_array($value)) {
|
|
|
|
$items = array();
|
|
|
|
$lastKey = -1;
|
|
|
|
foreach ($value as $itemKey => $itemValue) {
|
|
|
|
// for consecutive, numeric keys don't generate keys
|
|
|
|
if (null !== $lastKey && ++$lastKey === $itemKey) {
|
2014-02-06 14:44:16 +01:00
|
|
|
$items[] = new Expr\ArrayItem(
|
2012-03-11 08:53:04 +01:00
|
|
|
$this->normalizeValue($itemValue)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$lastKey = null;
|
2014-02-06 14:44:16 +01:00
|
|
|
$items[] = new Expr\ArrayItem(
|
2012-03-11 08:53:04 +01:00
|
|
|
$this->normalizeValue($itemValue),
|
|
|
|
$this->normalizeValue($itemKey)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Expr\Array_($items);
|
2012-03-11 00:06:02 +01:00
|
|
|
} else {
|
2014-02-06 14:44:16 +01:00
|
|
|
throw new \LogicException('Invalid value');
|
2012-03-11 00:06:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-13 13:44:40 +01:00
|
|
|
/**
|
|
|
|
* Normalizes a doc comment: Converts plain strings to PhpParser\Comment\Doc.
|
|
|
|
*
|
|
|
|
* @param Comment\Doc|string $docComment The doc comment to normalize
|
|
|
|
*
|
|
|
|
* @return Comment\Doc The normalized doc comment
|
|
|
|
*/
|
|
|
|
protected function normalizeDocComment($docComment) {
|
|
|
|
if ($docComment instanceof Comment\Doc) {
|
|
|
|
return $docComment;
|
|
|
|
} else if (is_string($docComment)) {
|
|
|
|
return new Comment\Doc($docComment);
|
|
|
|
} else {
|
|
|
|
throw new \LogicException('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-10 23:25:26 +01:00
|
|
|
/**
|
|
|
|
* Sets a modifier in the $this->type property.
|
|
|
|
*
|
|
|
|
* @param int $modifier Modifier to set
|
|
|
|
*/
|
|
|
|
protected function setModifier($modifier) {
|
2016-07-25 13:33:19 +02:00
|
|
|
Stmt\Class_::verifyModifier($this->flags, $modifier);
|
|
|
|
$this->flags |= $modifier;
|
2012-03-10 23:25:26 +01:00
|
|
|
}
|
2014-12-19 18:48:21 +01:00
|
|
|
}
|