mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-30 04:29:15 +01:00
Change BuilderAbstract into BuilderHelpers static class
Used as poor man's function namespace here.
This commit is contained in:
parent
6b6c903585
commit
7419649eae
@ -3,6 +3,7 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
@ -36,7 +37,7 @@ class Class_ extends Declaration
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function extend($class) {
|
||||
$this->extends = $this->normalizeName($class);
|
||||
$this->extends = BuilderHelpers::normalizeName($class);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -50,7 +51,7 @@ class Class_ extends Declaration
|
||||
*/
|
||||
public function implement() {
|
||||
foreach (func_get_args() as $interface) {
|
||||
$this->implements[] = $this->normalizeName($interface);
|
||||
$this->implements[] = BuilderHelpers::normalizeName($interface);
|
||||
}
|
||||
|
||||
return $this;
|
||||
@ -62,7 +63,7 @@ class Class_ extends Declaration
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeAbstract() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -73,7 +74,7 @@ class Class_ extends Declaration
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeFinal() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -86,7 +87,7 @@ class Class_ extends Declaration
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmt($stmt) {
|
||||
$stmt = $this->normalizeNode($stmt);
|
||||
$stmt = BuilderHelpers::normalizeNode($stmt);
|
||||
|
||||
$targets = array(
|
||||
'Stmt_TraitUse' => &$this->uses,
|
||||
|
@ -3,8 +3,9 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
|
||||
abstract class Declaration extends PhpParser\BuilderAbstract
|
||||
abstract class Declaration implements PhpParser\Builder
|
||||
{
|
||||
protected $attributes = array();
|
||||
|
||||
@ -34,7 +35,7 @@ abstract class Declaration extends PhpParser\BuilderAbstract
|
||||
*/
|
||||
public function setDocComment($docComment) {
|
||||
$this->attributes['comments'] = array(
|
||||
$this->normalizeDocComment($docComment)
|
||||
BuilderHelpers::normalizeDocComment($docComment)
|
||||
);
|
||||
|
||||
return $this;
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
|
||||
abstract class FunctionLike extends Declaration
|
||||
@ -32,7 +33,7 @@ abstract class FunctionLike extends Declaration
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addParam($param) {
|
||||
$param = $this->normalizeNode($param);
|
||||
$param = BuilderHelpers::normalizeNode($param);
|
||||
|
||||
if (!$param instanceof Node\Param) {
|
||||
throw new \LogicException(sprintf('Expected parameter node, got "%s"', $param->getType()));
|
||||
@ -61,14 +62,13 @@ abstract class FunctionLike extends Declaration
|
||||
/**
|
||||
* Sets the return type for PHP 7.
|
||||
*
|
||||
* @param string|Node\Name|Node\NullableType $type One of array, callable, string, int, float, bool, iterable,
|
||||
* or a class/interface name.
|
||||
* @param string|Node\Name|Node\NullableType $type One of array, callable, string, int, float,
|
||||
* bool, iterable, or a class/interface name.
|
||||
*
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function setReturnType($type)
|
||||
{
|
||||
$this->returnType = $this->normalizeType($type);
|
||||
public function setReturnType($type) {
|
||||
$this->returnType = BuilderHelpers::normalizeType($type);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
@ -28,7 +29,7 @@ class Function_ extends FunctionLike
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmt($stmt) {
|
||||
$this->stmts[] = $this->normalizeStmt($stmt);
|
||||
$this->stmts[] = BuilderHelpers::normalizeStmt($stmt);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
@ -31,7 +32,7 @@ class Interface_ extends Declaration
|
||||
*/
|
||||
public function extend() {
|
||||
foreach (func_get_args() as $interface) {
|
||||
$this->extends[] = $this->normalizeName($interface);
|
||||
$this->extends[] = BuilderHelpers::normalizeName($interface);
|
||||
}
|
||||
|
||||
return $this;
|
||||
@ -45,7 +46,7 @@ class Interface_ extends Declaration
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmt($stmt) {
|
||||
$stmt = $this->normalizeNode($stmt);
|
||||
$stmt = BuilderHelpers::normalizeNode($stmt);
|
||||
|
||||
$type = $stmt->getType();
|
||||
switch ($type) {
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
@ -29,7 +30,7 @@ class Method extends FunctionLike
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makePublic() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -40,7 +41,7 @@ class Method extends FunctionLike
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeProtected() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -51,7 +52,7 @@ class Method extends FunctionLike
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makePrivate() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -62,7 +63,7 @@ class Method extends FunctionLike
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeStatic() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -77,7 +78,7 @@ class Method extends FunctionLike
|
||||
throw new \LogicException('Cannot make method with statements abstract');
|
||||
}
|
||||
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_ABSTRACT);
|
||||
$this->stmts = null; // abstract methods don't have statements
|
||||
|
||||
return $this;
|
||||
@ -89,7 +90,7 @@ class Method extends FunctionLike
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeFinal() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_FINAL);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -106,7 +107,7 @@ class Method extends FunctionLike
|
||||
throw new \LogicException('Cannot add statements to an abstract method');
|
||||
}
|
||||
|
||||
$this->stmts[] = $this->normalizeStmt($stmt);
|
||||
$this->stmts[] = BuilderHelpers::normalizeStmt($stmt);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -3,10 +3,11 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class Namespace_ extends PhpParser\BuilderAbstract
|
||||
class Namespace_ implements PhpParser\Builder
|
||||
{
|
||||
private $name;
|
||||
private $stmts = array();
|
||||
@ -17,7 +18,7 @@ class Namespace_ extends PhpParser\BuilderAbstract
|
||||
* @param Node\Name|string|null $name Name of the namespace
|
||||
*/
|
||||
public function __construct($name) {
|
||||
$this->name = null !== $name ? $this->normalizeName($name) : null;
|
||||
$this->name = null !== $name ? BuilderHelpers::normalizeName($name) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -28,7 +29,7 @@ class Namespace_ extends PhpParser\BuilderAbstract
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmt($stmt) {
|
||||
$this->stmts[] = $this->normalizeStmt($stmt);
|
||||
$this->stmts[] = BuilderHelpers::normalizeStmt($stmt);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -3,9 +3,10 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
|
||||
class Param extends PhpParser\BuilderAbstract
|
||||
class Param implements PhpParser\Builder
|
||||
{
|
||||
protected $name;
|
||||
|
||||
@ -35,7 +36,7 @@ class Param extends PhpParser\BuilderAbstract
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function setDefault($value) {
|
||||
$this->default = $this->normalizeValue($value);
|
||||
$this->default = BuilderHelpers::normalizeValue($value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -48,7 +49,7 @@ class Param extends PhpParser\BuilderAbstract
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function setTypeHint($type) {
|
||||
$this->type = $this->normalizeType($type);
|
||||
$this->type = BuilderHelpers::normalizeType($type);
|
||||
if ($this->type === 'void') {
|
||||
throw new \LogicException('Parameter type cannot be void');
|
||||
}
|
||||
|
@ -3,9 +3,10 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class Property extends PhpParser\BuilderAbstract
|
||||
class Property implements PhpParser\Builder
|
||||
{
|
||||
protected $name;
|
||||
|
||||
@ -28,7 +29,7 @@ class Property extends PhpParser\BuilderAbstract
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makePublic() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PUBLIC);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -39,7 +40,7 @@ class Property extends PhpParser\BuilderAbstract
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeProtected() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PROTECTED);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -50,7 +51,7 @@ class Property extends PhpParser\BuilderAbstract
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makePrivate() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_PRIVATE);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -61,7 +62,7 @@ class Property extends PhpParser\BuilderAbstract
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function makeStatic() {
|
||||
$this->flags = $this->addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC);
|
||||
$this->flags = BuilderHelpers::addModifier($this->flags, Stmt\Class_::MODIFIER_STATIC);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -74,7 +75,7 @@ class Property extends PhpParser\BuilderAbstract
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function setDefault($value) {
|
||||
$this->default = $this->normalizeValue($value);
|
||||
$this->default = BuilderHelpers::normalizeValue($value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -88,7 +89,7 @@ class Property extends PhpParser\BuilderAbstract
|
||||
*/
|
||||
public function setDocComment($docComment) {
|
||||
$this->attributes = array(
|
||||
'comments' => array($this->normalizeDocComment($docComment))
|
||||
'comments' => array(BuilderHelpers::normalizeDocComment($docComment))
|
||||
);
|
||||
|
||||
return $this;
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
class Trait_ extends Declaration
|
||||
@ -28,7 +29,7 @@ class Trait_ extends Declaration
|
||||
* @return $this The builder instance (for fluid interface)
|
||||
*/
|
||||
public function addStmt($stmt) {
|
||||
$stmt = $this->normalizeNode($stmt);
|
||||
$stmt = BuilderHelpers::normalizeNode($stmt);
|
||||
|
||||
if ($stmt instanceof Stmt\Property) {
|
||||
$this->properties[] = $stmt;
|
||||
|
@ -2,14 +2,15 @@
|
||||
|
||||
namespace PhpParser\Builder;
|
||||
|
||||
use PhpParser\BuilderAbstract;
|
||||
use PhpParser\Builder;
|
||||
use PhpParser\BuilderHelpers;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
/**
|
||||
* @method $this as(string $alias) Sets alias for used name.
|
||||
*/
|
||||
class Use_ extends BuilderAbstract {
|
||||
class Use_ implements Builder {
|
||||
protected $name;
|
||||
protected $type;
|
||||
protected $alias = null;
|
||||
@ -21,7 +22,7 @@ class Use_ extends BuilderAbstract {
|
||||
* @param int $type One of the Stmt\Use_::TYPE_* constants
|
||||
*/
|
||||
public function __construct($name, $type) {
|
||||
$this->name = $this->normalizeName($name);
|
||||
$this->name = BuilderHelpers::normalizeName($name);
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,12 @@ use PhpParser\Node\NullableType;
|
||||
use PhpParser\Node\Scalar;
|
||||
use PhpParser\Node\Stmt;
|
||||
|
||||
abstract class BuilderAbstract implements Builder {
|
||||
/**
|
||||
* This class defines helpers used in the implementation of builders. Don't use it directly.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class BuilderHelpers {
|
||||
/**
|
||||
* Normalizes a node: Converts builder objects to nodes.
|
||||
*
|
||||
@ -17,7 +22,7 @@ abstract class BuilderAbstract implements Builder {
|
||||
*
|
||||
* @return Node The normalized node
|
||||
*/
|
||||
protected function normalizeNode($node) {
|
||||
public static function normalizeNode($node) {
|
||||
if ($node instanceof Builder) {
|
||||
return $node->getNode();
|
||||
} elseif ($node instanceof Node) {
|
||||
@ -36,8 +41,8 @@ abstract class BuilderAbstract implements Builder {
|
||||
*
|
||||
* @return Stmt The normalized statement node
|
||||
*/
|
||||
protected function normalizeStmt($node) {
|
||||
$node = $this->normalizeNode($node);
|
||||
public static function normalizeStmt($node) {
|
||||
$node = self::normalizeNode($node);
|
||||
if ($node instanceof Stmt) {
|
||||
return $node;
|
||||
}
|
||||
@ -56,7 +61,7 @@ abstract class BuilderAbstract implements Builder {
|
||||
*
|
||||
* @return Name The normalized name
|
||||
*/
|
||||
protected function normalizeName($name) {
|
||||
public static function normalizeName($name) {
|
||||
if ($name instanceof Name) {
|
||||
return $name;
|
||||
} elseif (is_string($name)) {
|
||||
@ -86,7 +91,7 @@ abstract class BuilderAbstract implements Builder {
|
||||
*
|
||||
* @return Name|string|NullableType The normalized type
|
||||
*/
|
||||
protected function normalizeType($type) {
|
||||
public static function normalizeType($type) {
|
||||
if (!is_string($type)) {
|
||||
if (!$type instanceof Name && !$type instanceof NullableType) {
|
||||
throw new \LogicException(
|
||||
@ -109,7 +114,7 @@ abstract class BuilderAbstract implements Builder {
|
||||
if (in_array($lowerType, $builtinTypes)) {
|
||||
$type = $lowerType;
|
||||
} else {
|
||||
$type = $this->normalizeName($type);
|
||||
$type = self::normalizeName($type);
|
||||
}
|
||||
|
||||
if ($nullable && $type === 'void') {
|
||||
@ -127,7 +132,7 @@ abstract class BuilderAbstract implements Builder {
|
||||
*
|
||||
* @return Expr The normalized value
|
||||
*/
|
||||
protected function normalizeValue($value) {
|
||||
public static function normalizeValue($value) {
|
||||
if ($value instanceof Node\Expr) {
|
||||
return $value;
|
||||
} elseif (is_null($value)) {
|
||||
@ -151,13 +156,13 @@ abstract class BuilderAbstract implements Builder {
|
||||
// for consecutive, numeric keys don't generate keys
|
||||
if (null !== $lastKey && ++$lastKey === $itemKey) {
|
||||
$items[] = new Expr\ArrayItem(
|
||||
$this->normalizeValue($itemValue)
|
||||
self::normalizeValue($itemValue)
|
||||
);
|
||||
} else {
|
||||
$lastKey = null;
|
||||
$items[] = new Expr\ArrayItem(
|
||||
$this->normalizeValue($itemValue),
|
||||
$this->normalizeValue($itemKey)
|
||||
self::normalizeValue($itemValue),
|
||||
self::normalizeValue($itemKey)
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -175,7 +180,7 @@ abstract class BuilderAbstract implements Builder {
|
||||
*
|
||||
* @return Comment\Doc The normalized doc comment
|
||||
*/
|
||||
protected function normalizeDocComment($docComment) {
|
||||
public static function normalizeDocComment($docComment) {
|
||||
if ($docComment instanceof Comment\Doc) {
|
||||
return $docComment;
|
||||
} else if (is_string($docComment)) {
|
||||
@ -193,7 +198,7 @@ abstract class BuilderAbstract implements Builder {
|
||||
*
|
||||
* @return int New modifiers
|
||||
*/
|
||||
protected function addModifier($modifiers, $modifier) {
|
||||
public static function addModifier($modifiers, $modifier) {
|
||||
Stmt\Class_::verifyModifier($modifiers, $modifier);
|
||||
return $modifiers | $modifier;
|
||||
}
|
Loading…
Reference in New Issue
Block a user