1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-20 12:46:47 +01:00

Extract common builder methods to abstract classes

Declaration for fns/classes in general and FunctionLike for
functions/methods in particular.
This commit is contained in:
Nikita Popov 2014-12-19 17:21:46 +01:00
parent e69c9ee38c
commit 55fdbc6dbc
8 changed files with 150 additions and 298 deletions

View File

@ -6,20 +6,18 @@ use PhpParser;
use PhpParser\Node\Name; use PhpParser\Node\Name;
use PhpParser\Node\Stmt; use PhpParser\Node\Stmt;
class Class_ extends PhpParser\BuilderAbstract class Class_ extends Declaration
{ {
protected $name; protected $name;
protected $extends; protected $extends = null;
protected $implements; protected $implements = array();
protected $type; protected $type = 0;
protected $uses; protected $uses = array();
protected $constants; protected $constants = array();
protected $properties; protected $properties = array();
protected $methods; protected $methods = array();
protected $attributes;
/** /**
* Creates a class builder. * Creates a class builder.
@ -28,13 +26,6 @@ class Class_ extends PhpParser\BuilderAbstract
*/ */
public function __construct($name) { public function __construct($name) {
$this->name = $name; $this->name = $name;
$this->type = 0;
$this->extends = null;
$this->implements = array();
$this->uses = $this->constants = $this->properties = $this->methods = array();
$this->attributes = array();
} }
/** /**
@ -42,7 +33,7 @@ class Class_ extends PhpParser\BuilderAbstract
* *
* @param Name|string $class Name of class to extend * @param Name|string $class Name of class to extend
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function extend($class) { public function extend($class) {
$this->extends = $this->normalizeName($class); $this->extends = $this->normalizeName($class);
@ -56,7 +47,7 @@ class Class_ extends PhpParser\BuilderAbstract
* @param Name|string $interface Name of interface to implement * @param Name|string $interface Name of interface to implement
* @param Name|string $... More interfaces to implement * @param Name|string $... More interfaces to implement
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function implement() { public function implement() {
foreach (func_get_args() as $interface) { foreach (func_get_args() as $interface) {
@ -69,7 +60,7 @@ class Class_ extends PhpParser\BuilderAbstract
/** /**
* Makes the class abstract. * Makes the class abstract.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makeAbstract() { public function makeAbstract() {
$this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT); $this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT);
@ -80,7 +71,7 @@ class Class_ extends PhpParser\BuilderAbstract
/** /**
* Makes the class final. * Makes the class final.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makeFinal() { public function makeFinal() {
$this->setModifier(Stmt\Class_::MODIFIER_FINAL); $this->setModifier(Stmt\Class_::MODIFIER_FINAL);
@ -93,7 +84,7 @@ class Class_ extends PhpParser\BuilderAbstract
* *
* @param Stmt|PhpParser\Builder $stmt The statement to add * @param Stmt|PhpParser\Builder $stmt The statement to add
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function addStmt($stmt) { public function addStmt($stmt) {
$stmt = $this->normalizeNode($stmt); $stmt = $this->normalizeNode($stmt);
@ -115,36 +106,6 @@ class Class_ extends PhpParser\BuilderAbstract
return $this; return $this;
} }
/**
* Adds multiple statements.
*
* @param array $stmts The statements to add
*
* @return self The builder instance (for fluid interface)
*/
public function addStmts(array $stmts) {
foreach ($stmts as $stmt) {
$this->addStmt($stmt);
}
return $this;
}
/**
* Sets doc comment for the property.
*
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
*
* @return self The builder instance (for fluid interface)
*/
public function setDocComment($docComment) {
$this->attributes = array(
'comments' => array($this->normalizeDocComment($docComment))
);
return $this;
}
/** /**
* Returns the built class node. * Returns the built class node.
* *

View File

@ -0,0 +1,44 @@
<?php
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\Node;
use PhpParser\Node\Stmt;
abstract class Declaration extends PhpParser\BuilderAbstract
{
protected $attributes = array();
abstract public function addStmt($stmt);
/**
* Adds multiple statements.
*
* @param array $stmts The statements to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addStmts(array $stmts) {
foreach ($stmts as $stmt) {
$this->addStmt($stmt);
}
return $this;
}
/**
* Sets doc comment for the declaration.
*
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
*
* @return $this The builder instance (for fluid interface)
*/
public function setDocComment($docComment) {
$this->attributes['comments'] = array(
$this->normalizeDocComment($docComment)
);
return $this;
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace PhpParser\Builder;
use PhpParser;
use PhpParser\Node;
use PhpParser\Node\Stmt;
abstract class FunctionLike extends Declaration
{
protected $returnByRef = false;
protected $params = array();
/**
* Make the function return by reference.
*
* @return $this The builder instance (for fluid interface)
*/
public function makeReturnByRef() {
$this->returnByRef = true;
return $this;
}
/**
* Adds a parameter.
*
* @param Node\Param|Param $param The parameter to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addParam($param) {
$param = $this->normalizeNode($param);
if (!$param instanceof Node\Param) {
throw new \LogicException(sprintf('Expected parameter node, got "%s"', $param->getType()));
}
$this->params[] = $param;
return $this;
}
/**
* Adds multiple parameters.
*
* @param array $params The parameters to add
*
* @return $this The builder instance (for fluid interface)
*/
public function addParams(array $params) {
foreach ($params as $param) {
$this->addParam($param);
}
return $this;
}
}

View File

@ -6,14 +6,10 @@ use PhpParser;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Stmt; use PhpParser\Node\Stmt;
class Function_ extends PhpParser\BuilderAbstract class Function_ extends FunctionLike
{ {
protected $name; protected $name;
protected $stmts = array();
protected $returnByRef;
protected $params;
protected $stmts;
protected $attributes;
/** /**
* Creates a function builder. * Creates a function builder.
@ -22,56 +18,6 @@ class Function_ extends PhpParser\BuilderAbstract
*/ */
public function __construct($name) { public function __construct($name) {
$this->name = $name; $this->name = $name;
$this->returnByRef = false;
$this->params = array();
$this->stmts = array();
$this->attributes = array();
}
/**
* Make the function return by reference.
*
* @return self The builder instance (for fluid interface)
*/
public function makeReturnByRef() {
$this->returnByRef = true;
return $this;
}
/**
* Adds a parameter.
*
* @param Node\Param|Param $param The parameter to add
*
* @return self The builder instance (for fluid interface)
*/
public function addParam($param) {
$param = $this->normalizeNode($param);
if (!$param instanceof Node\Param) {
throw new \LogicException(sprintf('Expected parameter node, got "%s"', $param->getType()));
}
$this->params[] = $param;
return $this;
}
/**
* Adds multiple parameters.
*
* @param array $params The parameters to add
*
* @return self The builder instance (for fluid interface)
*/
public function addParams(array $params) {
foreach ($params as $param) {
$this->addParam($param);
}
return $this;
} }
/** /**
@ -79,7 +25,7 @@ class Function_ extends PhpParser\BuilderAbstract
* *
* @param Node|PhpParser\Builder $stmt The statement to add * @param Node|PhpParser\Builder $stmt The statement to add
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function addStmt($stmt) { public function addStmt($stmt) {
$this->stmts[] = $this->normalizeNode($stmt); $this->stmts[] = $this->normalizeNode($stmt);
@ -87,36 +33,6 @@ class Function_ extends PhpParser\BuilderAbstract
return $this; return $this;
} }
/**
* Adds multiple statements.
*
* @param array $stmts The statements to add
*
* @return self The builder instance (for fluid interface)
*/
public function addStmts(array $stmts) {
foreach ($stmts as $stmt) {
$this->addStmt($stmt);
}
return $this;
}
/**
* Sets doc comment for the function.
*
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
*
* @return self The builder instance (for fluid interface)
*/
public function setDocComment($docComment) {
$this->attributes = array(
'comments' => array($this->normalizeDocComment($docComment))
);
return $this;
}
/** /**
* Returns the built function node. * Returns the built function node.
* *

View File

@ -6,13 +6,12 @@ use PhpParser;
use PhpParser\Node\Name; use PhpParser\Node\Name;
use PhpParser\Node\Stmt; use PhpParser\Node\Stmt;
class Interface_ extends PhpParser\BuilderAbstract class Interface_ extends Declaration
{ {
protected $name; protected $name;
protected $extends; protected $extends = array();
protected $constants; protected $constants = array();
protected $methods; protected $methods = array();
protected $attributes;
/** /**
* Creates an interface builder. * Creates an interface builder.
@ -21,9 +20,6 @@ class Interface_ extends PhpParser\BuilderAbstract
*/ */
public function __construct($name) { public function __construct($name) {
$this->name = $name; $this->name = $name;
$this->extends = array();
$this->constants = $this->methods = array();
$this->attributes = array();
} }
/** /**
@ -32,7 +28,7 @@ class Interface_ extends PhpParser\BuilderAbstract
* @param Name|string $interface Name of interface to extend * @param Name|string $interface Name of interface to extend
* @param Name|string $... More interfaces to extend * @param Name|string $... More interfaces to extend
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function extend() { public function extend() {
foreach (func_get_args() as $interface) { foreach (func_get_args() as $interface) {
@ -47,7 +43,7 @@ class Interface_ extends PhpParser\BuilderAbstract
* *
* @param Stmt|PhpParser\Builder $stmt The statement to add * @param Stmt|PhpParser\Builder $stmt The statement to add
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function addStmt($stmt) { public function addStmt($stmt) {
$stmt = $this->normalizeNode($stmt); $stmt = $this->normalizeNode($stmt);
@ -72,37 +68,7 @@ class Interface_ extends PhpParser\BuilderAbstract
} }
/** /**
* Adds multiple statements. * Returns the built interface node.
*
* @param array $stmts The statements to add
*
* @return self The builder instance (for fluid interface)
*/
public function addStmts(array $stmts) {
foreach ($stmts as $stmt) {
$this->addStmt($stmt);
}
return $this;
}
/**
* Sets doc comment for the property.
*
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
*
* @return self The builder instance (for fluid interface)
*/
public function setDocComment($docComment) {
$this->attributes = array(
'comments' => array($this->normalizeDocComment($docComment))
);
return $this;
}
/**
* Returns the built class node.
* *
* @return Stmt\Interface_ The built interface node * @return Stmt\Interface_ The built interface node
*/ */

View File

@ -6,15 +6,11 @@ use PhpParser;
use PhpParser\Node; use PhpParser\Node;
use PhpParser\Node\Stmt; use PhpParser\Node\Stmt;
class Method extends PhpParser\BuilderAbstract class Method extends FunctionLike
{ {
protected $name; protected $name;
protected $type = 0;
protected $type; protected $stmts = array();
protected $returnByRef;
protected $params;
protected $stmts;
protected $attributes;
/** /**
* Creates a method builder. * Creates a method builder.
@ -23,18 +19,12 @@ class Method extends PhpParser\BuilderAbstract
*/ */
public function __construct($name) { public function __construct($name) {
$this->name = $name; $this->name = $name;
$this->type = 0;
$this->returnByRef = false;
$this->params = array();
$this->stmts = array();
$this->attributes = array();
} }
/** /**
* Makes the method public. * Makes the method public.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makePublic() { public function makePublic() {
$this->setModifier(Stmt\Class_::MODIFIER_PUBLIC); $this->setModifier(Stmt\Class_::MODIFIER_PUBLIC);
@ -45,7 +35,7 @@ class Method extends PhpParser\BuilderAbstract
/** /**
* Makes the method protected. * Makes the method protected.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makeProtected() { public function makeProtected() {
$this->setModifier(Stmt\Class_::MODIFIER_PROTECTED); $this->setModifier(Stmt\Class_::MODIFIER_PROTECTED);
@ -56,7 +46,7 @@ class Method extends PhpParser\BuilderAbstract
/** /**
* Makes the method private. * Makes the method private.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makePrivate() { public function makePrivate() {
$this->setModifier(Stmt\Class_::MODIFIER_PRIVATE); $this->setModifier(Stmt\Class_::MODIFIER_PRIVATE);
@ -67,7 +57,7 @@ class Method extends PhpParser\BuilderAbstract
/** /**
* Makes the method static. * Makes the method static.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makeStatic() { public function makeStatic() {
$this->setModifier(Stmt\Class_::MODIFIER_STATIC); $this->setModifier(Stmt\Class_::MODIFIER_STATIC);
@ -78,7 +68,7 @@ class Method extends PhpParser\BuilderAbstract
/** /**
* Makes the method abstract. * Makes the method abstract.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makeAbstract() { public function makeAbstract() {
if (!empty($this->stmts)) { if (!empty($this->stmts)) {
@ -94,7 +84,7 @@ class Method extends PhpParser\BuilderAbstract
/** /**
* Makes the method final. * Makes the method final.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makeFinal() { public function makeFinal() {
$this->setModifier(Stmt\Class_::MODIFIER_FINAL); $this->setModifier(Stmt\Class_::MODIFIER_FINAL);
@ -102,57 +92,12 @@ class Method extends PhpParser\BuilderAbstract
return $this; return $this;
} }
/**
* Make the method return by reference.
*
* @return self The builder instance (for fluid interface)
*/
public function makeReturnByRef() {
$this->returnByRef = true;
return $this;
}
/**
* Adds a parameter.
*
* @param Node\Param|Param $param The parameter to add
*
* @return self The builder instance (for fluid interface)
*/
public function addParam($param) {
$param = $this->normalizeNode($param);
if (!$param instanceof Node\Param) {
throw new \LogicException(sprintf('Expected parameter node, got "%s"', $param->getType()));
}
$this->params[] = $param;
return $this;
}
/**
* Adds multiple parameters.
*
* @param array $params The parameters to add
*
* @return self The builder instance (for fluid interface)
*/
public function addParams(array $params) {
foreach ($params as $param) {
$this->addParam($param);
}
return $this;
}
/** /**
* Adds a statement. * Adds a statement.
* *
* @param Node|PhpParser\Builder $stmt The statement to add * @param Node|PhpParser\Builder $stmt The statement to add
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function addStmt($stmt) { public function addStmt($stmt) {
if (null === $this->stmts) { if (null === $this->stmts) {
@ -164,36 +109,6 @@ class Method extends PhpParser\BuilderAbstract
return $this; return $this;
} }
/**
* Adds multiple statements.
*
* @param array $stmts The statements to add
*
* @return self The builder instance (for fluid interface)
*/
public function addStmts(array $stmts) {
foreach ($stmts as $stmt) {
$this->addStmt($stmt);
}
return $this;
}
/**
* Sets doc comment for the method.
*
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
*
* @return self The builder instance (for fluid interface)
*/
public function setDocComment($docComment) {
$this->attributes = array(
'comments' => array($this->normalizeDocComment($docComment))
);
return $this;
}
/** /**
* Returns the built method node. * Returns the built method node.
* *

View File

@ -9,9 +9,9 @@ class Param extends PhpParser\BuilderAbstract
{ {
protected $name; protected $name;
protected $default; protected $default = null;
protected $type; protected $type = null;
protected $byRef; protected $byRef = false;
/** /**
* Creates a parameter builder. * Creates a parameter builder.
@ -20,10 +20,6 @@ class Param extends PhpParser\BuilderAbstract
*/ */
public function __construct($name) { public function __construct($name) {
$this->name = $name; $this->name = $name;
$this->default = null;
$this->type = null;
$this->byRef = false;
} }
/** /**
@ -31,7 +27,7 @@ class Param extends PhpParser\BuilderAbstract
* *
* @param mixed $value Default value to use * @param mixed $value Default value to use
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function setDefault($value) { public function setDefault($value) {
$this->default = $this->normalizeValue($value); $this->default = $this->normalizeValue($value);
@ -44,7 +40,7 @@ class Param extends PhpParser\BuilderAbstract
* *
* @param string|Node\Name $type Type hint to use * @param string|Node\Name $type Type hint to use
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function setTypeHint($type) { public function setTypeHint($type) {
if ($type === 'array' || $type === 'callable') { if ($type === 'array' || $type === 'callable') {
@ -59,7 +55,7 @@ class Param extends PhpParser\BuilderAbstract
/** /**
* Make the parameter accept the value by reference. * Make the parameter accept the value by reference.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makeByRef() { public function makeByRef() {
$this->byRef = true; $this->byRef = true;

View File

@ -9,9 +9,9 @@ class Property extends PhpParser\BuilderAbstract
{ {
protected $name; protected $name;
protected $type; protected $type = 0;
protected $default; protected $default = null;
protected $attributes; protected $attributes = array();
/** /**
* Creates a property builder. * Creates a property builder.
@ -20,16 +20,12 @@ class Property extends PhpParser\BuilderAbstract
*/ */
public function __construct($name) { public function __construct($name) {
$this->name = $name; $this->name = $name;
$this->type = 0;
$this->default = null;
$this->attributes = array();
} }
/** /**
* Makes the property public. * Makes the property public.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makePublic() { public function makePublic() {
$this->setModifier(Stmt\Class_::MODIFIER_PUBLIC); $this->setModifier(Stmt\Class_::MODIFIER_PUBLIC);
@ -40,7 +36,7 @@ class Property extends PhpParser\BuilderAbstract
/** /**
* Makes the property protected. * Makes the property protected.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makeProtected() { public function makeProtected() {
$this->setModifier(Stmt\Class_::MODIFIER_PROTECTED); $this->setModifier(Stmt\Class_::MODIFIER_PROTECTED);
@ -51,7 +47,7 @@ class Property extends PhpParser\BuilderAbstract
/** /**
* Makes the property private. * Makes the property private.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makePrivate() { public function makePrivate() {
$this->setModifier(Stmt\Class_::MODIFIER_PRIVATE); $this->setModifier(Stmt\Class_::MODIFIER_PRIVATE);
@ -62,7 +58,7 @@ class Property extends PhpParser\BuilderAbstract
/** /**
* Makes the property static. * Makes the property static.
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function makeStatic() { public function makeStatic() {
$this->setModifier(Stmt\Class_::MODIFIER_STATIC); $this->setModifier(Stmt\Class_::MODIFIER_STATIC);
@ -75,7 +71,7 @@ class Property extends PhpParser\BuilderAbstract
* *
* @param mixed $value Default value to use * @param mixed $value Default value to use
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function setDefault($value) { public function setDefault($value) {
$this->default = $this->normalizeValue($value); $this->default = $this->normalizeValue($value);
@ -88,7 +84,7 @@ class Property extends PhpParser\BuilderAbstract
* *
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set * @param PhpParser\Comment\Doc|string $docComment Doc comment to set
* *
* @return self The builder instance (for fluid interface) * @return $this The builder instance (for fluid interface)
*/ */
public function setDocComment($docComment) { public function setDocComment($docComment) {
$this->attributes = array( $this->attributes = array(