1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-27 04:14:44 +01:00
PHP-Parser/lib/PhpParser/BuilderAbstract.php

132 lines
4.1 KiB
PHP
Raw Normal View History

2012-03-10 23:25:26 +01:00
<?php
namespace PhpParser;
use PhpParser\Node\Name;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\Node\Scalar;
use PhpParser\Comment;
abstract class BuilderAbstract implements Builder {
2012-03-10 23:25:26 +01:00
/**
* Normalizes a node: Converts builder objects to nodes.
*
* @param Node|Builder $node The node to normalize
2012-03-10 23:25:26 +01:00
*
* @return Node The normalized node
2012-03-10 23:25:26 +01:00
*/
protected function normalizeNode($node) {
if ($node instanceof Builder) {
2012-03-10 23:25:26 +01:00
return $node->getNode();
} elseif ($node instanceof Node) {
2012-03-10 23:25:26 +01:00
return $node;
}
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
*
* @param Name|string $name The name to normalize
2012-03-11 00:06:02 +01:00
*
* @return Name The normalized name
2012-03-11 00:06:02 +01:00
*/
protected function normalizeName($name) {
if ($name instanceof Name) {
2012-03-11 00:06:02 +01:00
return $name;
} 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
}
throw new \LogicException('Name must be a string or an instance of PhpParser\Node\Name');
2012-03-11 00:06:02 +01:00
}
/**
* Normalizes a value: Converts nulls, booleans, integers,
* floats, strings and arrays into their respective nodes
2012-03-11 00:06:02 +01:00
*
* @param mixed $value The value to normalize
*
* @return Expr The normalized value
2012-03-11 00:06:02 +01:00
*/
protected function normalizeValue($value) {
if ($value instanceof Node) {
2012-03-11 00:06:02 +01:00
return $value;
} elseif (is_null($value)) {
return new Expr\ConstFetch(
new Name('null')
2012-03-11 00:06:02 +01:00
);
} elseif (is_bool($value)) {
return new Expr\ConstFetch(
new Name($value ? 'true' : 'false')
2012-03-11 00:06:02 +01:00
);
} elseif (is_int($value)) {
return new Scalar\LNumber($value);
2012-03-11 00:06:02 +01:00
} elseif (is_float($value)) {
return new Scalar\DNumber($value);
2012-03-11 00:06:02 +01:00
} elseif (is_string($value)) {
return new Scalar\String_($value);
} 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) {
$items[] = new Expr\ArrayItem(
$this->normalizeValue($itemValue)
);
} else {
$lastKey = null;
$items[] = new Expr\ArrayItem(
$this->normalizeValue($itemValue),
$this->normalizeValue($itemKey)
);
}
}
return new Expr\Array_($items);
2012-03-11 00:06:02 +01:00
} else {
throw new \LogicException('Invalid value');
2012-03-11 00:06:02 +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) {
Stmt\Class_::verifyModifier($this->type, $modifier);
2012-03-10 23:25:26 +01:00
$this->type |= $modifier;
}
}