1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-20 04:36:57 +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\Stmt;
class Class_ extends PhpParser\BuilderAbstract
class Class_ extends Declaration
{
protected $name;
protected $extends;
protected $implements;
protected $type;
protected $extends = null;
protected $implements = array();
protected $type = 0;
protected $uses;
protected $constants;
protected $properties;
protected $methods;
protected $attributes;
protected $uses = array();
protected $constants = array();
protected $properties = array();
protected $methods = array();
/**
* Creates a class builder.
@ -28,13 +26,6 @@ class Class_ extends PhpParser\BuilderAbstract
*/
public function __construct($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
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function extend($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 $... More interfaces to implement
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function implement() {
foreach (func_get_args() as $interface) {
@ -69,7 +60,7 @@ class Class_ extends PhpParser\BuilderAbstract
/**
* Makes the class abstract.
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function makeAbstract() {
$this->setModifier(Stmt\Class_::MODIFIER_ABSTRACT);
@ -80,7 +71,7 @@ class Class_ extends PhpParser\BuilderAbstract
/**
* Makes the class final.
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function makeFinal() {
$this->setModifier(Stmt\Class_::MODIFIER_FINAL);
@ -93,7 +84,7 @@ class Class_ extends PhpParser\BuilderAbstract
*
* @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) {
$stmt = $this->normalizeNode($stmt);
@ -115,36 +106,6 @@ class Class_ extends PhpParser\BuilderAbstract
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.
*

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\Stmt;
class Function_ extends PhpParser\BuilderAbstract
class Function_ extends FunctionLike
{
protected $name;
protected $returnByRef;
protected $params;
protected $stmts;
protected $attributes;
protected $stmts = array();
/**
* Creates a function builder.
@ -22,56 +18,6 @@ class Function_ extends PhpParser\BuilderAbstract
*/
public function __construct($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
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function addStmt($stmt) {
$this->stmts[] = $this->normalizeNode($stmt);
@ -87,36 +33,6 @@ class Function_ extends PhpParser\BuilderAbstract
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.
*

View File

@ -6,13 +6,12 @@ use PhpParser;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
class Interface_ extends PhpParser\BuilderAbstract
class Interface_ extends Declaration
{
protected $name;
protected $extends;
protected $constants;
protected $methods;
protected $attributes;
protected $extends = array();
protected $constants = array();
protected $methods = array();
/**
* Creates an interface builder.
@ -21,9 +20,6 @@ class Interface_ extends PhpParser\BuilderAbstract
*/
public function __construct($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 $... More interfaces to extend
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function extend() {
foreach (func_get_args() as $interface) {
@ -47,7 +43,7 @@ class Interface_ extends PhpParser\BuilderAbstract
*
* @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) {
$stmt = $this->normalizeNode($stmt);
@ -72,37 +68,7 @@ class Interface_ extends PhpParser\BuilderAbstract
}
/**
* 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 interface node.
*
* @return Stmt\Interface_ The built interface node
*/

View File

@ -6,15 +6,11 @@ use PhpParser;
use PhpParser\Node;
use PhpParser\Node\Stmt;
class Method extends PhpParser\BuilderAbstract
class Method extends FunctionLike
{
protected $name;
protected $type;
protected $returnByRef;
protected $params;
protected $stmts;
protected $attributes;
protected $type = 0;
protected $stmts = array();
/**
* Creates a method builder.
@ -23,18 +19,12 @@ class Method extends PhpParser\BuilderAbstract
*/
public function __construct($name) {
$this->name = $name;
$this->type = 0;
$this->returnByRef = false;
$this->params = array();
$this->stmts = array();
$this->attributes = array();
}
/**
* Makes the method public.
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function makePublic() {
$this->setModifier(Stmt\Class_::MODIFIER_PUBLIC);
@ -45,7 +35,7 @@ class Method extends PhpParser\BuilderAbstract
/**
* Makes the method protected.
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function makeProtected() {
$this->setModifier(Stmt\Class_::MODIFIER_PROTECTED);
@ -56,7 +46,7 @@ class Method extends PhpParser\BuilderAbstract
/**
* Makes the method private.
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function makePrivate() {
$this->setModifier(Stmt\Class_::MODIFIER_PRIVATE);
@ -67,7 +57,7 @@ class Method extends PhpParser\BuilderAbstract
/**
* Makes the method static.
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function makeStatic() {
$this->setModifier(Stmt\Class_::MODIFIER_STATIC);
@ -78,7 +68,7 @@ class Method extends PhpParser\BuilderAbstract
/**
* Makes the method abstract.
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function makeAbstract() {
if (!empty($this->stmts)) {
@ -94,7 +84,7 @@ class Method extends PhpParser\BuilderAbstract
/**
* Makes the method final.
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function makeFinal() {
$this->setModifier(Stmt\Class_::MODIFIER_FINAL);
@ -102,57 +92,12 @@ class Method extends PhpParser\BuilderAbstract
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.
*
* @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) {
if (null === $this->stmts) {
@ -164,36 +109,6 @@ class Method extends PhpParser\BuilderAbstract
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.
*

View File

@ -9,9 +9,9 @@ class Param extends PhpParser\BuilderAbstract
{
protected $name;
protected $default;
protected $type;
protected $byRef;
protected $default = null;
protected $type = null;
protected $byRef = false;
/**
* Creates a parameter builder.
@ -20,10 +20,6 @@ class Param extends PhpParser\BuilderAbstract
*/
public function __construct($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
*
* @return self The builder instance (for fluid interface)
* @return $this The builder instance (for fluid interface)
*/
public function setDefault($value) {
$this->default = $this->normalizeValue($value);
@ -44,7 +40,7 @@ class Param extends PhpParser\BuilderAbstract
*
* @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) {
if ($type === 'array' || $type === 'callable') {
@ -59,7 +55,7 @@ class Param extends PhpParser\BuilderAbstract
/**
* 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() {
$this->byRef = true;

View File

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