php-parser/lib/PhpParser/BuilderHelpers.php

314 lines
9.0 KiB
PHP
Raw Permalink Normal View History

2017-08-18 22:57:27 +02:00
<?php declare(strict_types=1);
2012-03-10 23:25:26 +01:00
namespace PhpParser;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
2016-11-23 22:58:18 +01:00
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
use PhpParser\Node\Scalar;
2016-11-23 22:58:18 +01:00
use PhpParser\Node\Stmt;
/**
* This class defines helpers used in the implementation of builders. Don't use it directly.
*
* @internal
*/
2018-01-10 17:24:26 +01:00
final class BuilderHelpers
{
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
*/
2017-04-28 21:40:59 +02:00
public static function normalizeNode($node) : Node {
if ($node instanceof Builder) {
2012-03-10 23:25:26 +01:00
return $node->getNode();
}
if ($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
}
/**
* Normalizes a node to a statement.
*
* Expressions are wrapped in a Stmt\Expression node.
*
* @param Node|Builder $node The node to normalize
*
* @return Stmt The normalized statement node
*/
2017-04-28 21:40:59 +02:00
public static function normalizeStmt($node) : Stmt {
$node = self::normalizeNode($node);
if ($node instanceof Stmt) {
return $node;
}
if ($node instanceof Expr) {
return new Stmt\Expression($node);
}
throw new \LogicException('Expected statement or expression node');
}
/**
* Normalizes strings to Identifier.
*
* @param string|Identifier $name The identifier to normalize
*
* @return Identifier The normalized identifier
*/
public static function normalizeIdentifier($name) : Identifier {
if ($name instanceof Identifier) {
return $name;
}
if (\is_string($name)) {
return new Identifier($name);
}
throw new \LogicException('Expected string or instance of Node\Identifier');
}
/**
* Normalizes strings to Identifier, also allowing expressions.
*
* @param string|Identifier|Expr $name The identifier to normalize
*
* @return Identifier|Expr The normalized identifier or expression
*/
public static function normalizeIdentifierOrExpr($name) {
if ($name instanceof Identifier || $name instanceof Expr) {
return $name;
}
if (\is_string($name)) {
return new Identifier($name);
}
throw new \LogicException('Expected string or instance of Node\Identifier or Node\Expr');
}
2012-03-11 00:06:02 +01:00
/**
* Normalizes a name: Converts string names to Name nodes.
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
*/
2017-04-28 21:40:59 +02:00
public static function normalizeName($name) : Name {
if ($name instanceof Name) {
2012-03-11 00:06:02 +01:00
return $name;
}
if (is_string($name)) {
if (!$name) {
throw new \LogicException('Name cannot be empty');
}
if ($name[0] === '\\') {
return new Name\FullyQualified(substr($name, 1));
}
if (0 === strpos($name, 'namespace\\')) {
return new Name\Relative(substr($name, strlen('namespace\\')));
}
return new Name($name);
2012-03-11 00:06:02 +01:00
}
throw new \LogicException('Name must be a string or an instance of Node\Name');
}
/**
* Normalizes a name: Converts string names to Name nodes, while also allowing expressions.
*
* @param Expr|Name|string $name The name to normalize
*
* @return Name|Expr The normalized name or expression
*/
public static function normalizeNameOrExpr($name) {
if ($name instanceof Expr) {
return $name;
}
if (!is_string($name) && !($name instanceof Name)) {
throw new \LogicException(
'Name must be a string or an instance of Node\Name or Node\Expr'
);
}
return self::normalizeName($name);
2012-03-11 00:06:02 +01:00
}
/**
* Normalizes a type: Converts plain-text type names into proper AST representation.
*
* In particular, builtin types become Identifiers, custom types become Names and nullables
* are wrapped in NullableType nodes.
*
* @param string|Name|Identifier|ComplexType $type The type to normalize
*
* @return Name|Identifier|ComplexType The normalized type
*/
public static function normalizeType($type) {
if (!is_string($type)) {
if (
!$type instanceof Name && !$type instanceof Identifier &&
!$type instanceof ComplexType
) {
throw new \LogicException(
'Type must be a string, or an instance of Name, Identifier or ComplexType'
);
}
return $type;
}
$nullable = false;
if (strlen($type) > 0 && $type[0] === '?') {
$nullable = true;
$type = substr($type, 1);
}
$builtinTypes = [
'array', 'callable', 'string', 'int', 'float', 'bool', 'iterable', 'void', 'object', 'mixed', 'never',
];
$lowerType = strtolower($type);
if (in_array($lowerType, $builtinTypes)) {
$type = new Identifier($lowerType);
} else {
$type = self::normalizeName($type);
}
$notNullableTypes = [
'void', 'mixed', 'never',
];
if ($nullable && in_array((string) $type, $notNullableTypes)) {
throw new \LogicException(sprintf('%s type cannot be nullable', $type));
2020-05-28 22:50:32 +02:00
}
return $nullable ? new NullableType($type) : $type;
}
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
*
2016-12-10 21:35:32 +01:00
* @param Node\Expr|bool|null|int|float|string|array $value The value to normalize
2012-03-11 00:06:02 +01:00
*
* @return Expr The normalized value
2012-03-11 00:06:02 +01:00
*/
2017-04-28 21:40:59 +02:00
public static function normalizeValue($value) : Expr {
2016-12-10 21:35:32 +01:00
if ($value instanceof Node\Expr) {
2012-03-11 00:06:02 +01:00
return $value;
}
if (is_null($value)) {
return new Expr\ConstFetch(
new Name('null')
2012-03-11 00:06:02 +01:00
);
}
if (is_bool($value)) {
return new Expr\ConstFetch(
new Name($value ? 'true' : 'false')
2012-03-11 00:06:02 +01:00
);
}
if (is_int($value)) {
return new Scalar\LNumber($value);
}
if (is_float($value)) {
return new Scalar\DNumber($value);
}
if (is_string($value)) {
return new Scalar\String_($value);
}
if (is_array($value)) {
$items = [];
$lastKey = -1;
foreach ($value as $itemKey => $itemValue) {
// for consecutive, numeric keys don't generate keys
if (null !== $lastKey && ++$lastKey === $itemKey) {
$items[] = new Expr\ArrayItem(
self::normalizeValue($itemValue)
);
} else {
$lastKey = null;
$items[] = new Expr\ArrayItem(
self::normalizeValue($itemValue),
self::normalizeValue($itemKey)
);
}
}
return new Expr\Array_($items);
2012-03-11 00:06:02 +01:00
}
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
*/
2017-04-28 21:40:59 +02:00
public static function normalizeDocComment($docComment) : Comment\Doc {
if ($docComment instanceof Comment\Doc) {
return $docComment;
}
if (is_string($docComment)) {
return new Comment\Doc($docComment);
}
throw new \LogicException('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
}
/**
* Normalizes a attribute: Converts attribute to the Attribute Group if needed.
*
* @param Node\Attribute|Node\AttributeGroup $attribute
*
* @return Node\AttributeGroup The Attribute Group
*/
public static function normalizeAttribute($attribute) : Node\AttributeGroup
{
if ($attribute instanceof Node\AttributeGroup) {
return $attribute;
}
if (!($attribute instanceof Node\Attribute)) {
throw new \LogicException('Attribute must be an instance of PhpParser\Node\Attribute or PhpParser\Node\AttributeGroup');
}
return new Node\AttributeGroup([$attribute]);
}
2012-03-10 23:25:26 +01:00
/**
* Adds a modifier and returns new modifier bitmask.
*
* @param int $modifiers Existing modifiers
* @param int $modifier Modifier to set
2012-03-10 23:25:26 +01:00
*
* @return int New modifiers
2012-03-10 23:25:26 +01:00
*/
2017-04-28 21:40:59 +02:00
public static function addModifier(int $modifiers, int $modifier) : int {
Stmt\Class_::verifyModifier($modifiers, $modifier);
return $modifiers | $modifier;
2012-03-10 23:25:26 +01:00
}
}