Add parameter builder

This commit is contained in:
nikic 2012-03-11 00:06:02 +01:00
parent 48f0322aef
commit 4c8351fa86
6 changed files with 234 additions and 16 deletions

View File

@ -134,19 +134,4 @@ class PHPParser_Builder_Class extends PHPParser_BuilderAbstract
'stmts' => array_merge($this->uses, $this->constants, $this->properties, $this->methods),
));
}
/**
* Normalizes a name: Converts plain string names to PHPParser_Node_Name.
*
* @param PHPParser_Node_Name|string $name The name to normalize
*
* @return PHPParser_Node_Name The normalized name
*/
protected function normalizeName($name) {
if ($name instanceof PHPParser_Node_Name) {
return $name;
} else {
return new PHPParser_Node_Name($name);
}
}
}

View File

@ -0,0 +1,75 @@
<?php
class PHPParser_Builder_Param extends PHPParser_BuilderAbstract
{
protected $name;
protected $default;
protected $type;
protected $byRef;
/**
* Creates a parameter builder.
*
* @param string $name Name of the parameter
*/
public function __construct($name) {
$this->name = $name;
$this->default = null;
$this->type = null;
$this->byRef = false;
}
/**
* Sets default value for the parameter.
*
* @param mixed $value Default value to use
*
* @return PHPParser_Builder_Param The builder instance (for fluid interface)
*/
public function setDefault($value) {
$this->default = $this->normalizeValue($value);
return $this;
}
/**
* Sets type hint for the parameter.
*
* @param string|PHPParser_Node_Name $type Type hint to use
*
* @return PHPParser_Builder_Param The builder instance (for fluid interface)
*/
public function setTypeHint($type) {
if ($type === 'array' || $type === 'callable') {
$this->type = $type;
} else {
$this->type = $this->normalizeName($type);
}
return $this;
}
/**
* Make the parameter accept the value by reference.
*
* @return PHPParser_Builder_Param The builder instance (for fluid interface)
*/
public function makeByRef() {
$this->byRef = true;
return $this;
}
/**
* Returns the built parameter node.
*
* @return PHPParser_Node_Param The built parameter node
*/
public function getNode() {
return new PHPParser_Node_Param(
$this->name, $this->default, $this->type, $this->byRef
);
}
}

View File

@ -18,6 +18,51 @@ abstract class PHPParser_BuilderAbstract implements PHPParser_Builder {
throw new LogicException('Expected node or builder object');
}
/**
* Normalizes a name: Converts plain string names to PHPParser_Node_Name.
*
* @param PHPParser_Node_Name|string $name The name to normalize
*
* @return PHPParser_Node_Name The normalized name
*/
protected function normalizeName($name) {
if ($name instanceof PHPParser_Node_Name) {
return $name;
} else {
return new PHPParser_Node_Name($name);
}
}
/**
* Normalizes a value: Converts nulls, booleans, integers,
* floats and strings into their respective nodes
*
* @param mixed $value The value to normalize
*
* @return PHPParser_Node The normalized value
*/
protected function normalizeValue($value) {
if ($value instanceof PHPParser_Node) {
return $value;
} elseif (is_null($value)) {
return new PHPParser_Node_Expr_ConstFetch(
new PHPParser_Node_Name('null')
);
} elseif (is_bool($value)) {
return new PHPParser_Node_Expr_ConstFetch(
new PHPParser_Node_Name($value ? 'true' : 'false')
);
} elseif (is_int($value)) {
return new PHPParser_Node_Scalar_LNumber($value);
} elseif (is_float($value)) {
return new PHPParser_Node_Scalar_DNumber($value);
} elseif (is_string($value)) {
return new PHPParser_Node_Scalar_String($value);
} else {
throw new LogicException('Invalid value');
}
}
/**
* Sets a modifier in the $this->type property.
*

View File

@ -18,12 +18,23 @@ class PHPParser_BuilderFactory
*
* @param string $name Name of the method
*
* @return PHPParser_Builder_Method The created method class builder
* @return PHPParser_Builder_Method The created method builder
*/
public function method($name) {
return new PHPParser_Builder_Method($name);
}
/**
* Creates a parameter builder.
*
* @param string $name Name of the parameter
*
* @return PHPParser_Builder_Param The created parameter builder
*/
public function param($name) {
return new PHPParser_Builder_Param($name);
}
public function __call($name, array $args) {
if ('class' === $name) {
return call_user_func_array(array($this, '_class'), $args);

View File

@ -0,0 +1,97 @@
<?php
class PHPParser_Tests_Builder_ParamTest extends PHPUnit_Framework_TestCase
{
public function createParamBuilder($name) {
return new PHPParser_Builder_Param($name);
}
/**
* @dataProvider provideTestDefaultValues
*/
public function testDefaultValues($value, $expectedValueNode) {
$node = $this->createParamBuilder('test')
->setDefault($value)
->getNode()
;
$this->assertEquals($expectedValueNode, $node->default);
}
public function provideTestDefaultValues() {
return array(
array(
null,
new PHPParser_Node_Expr_ConstFetch(new PHPParser_Node_Name('null'))
),
array(
true,
new PHPParser_Node_Expr_ConstFetch(new PHPParser_Node_Name('true'))
),
array(
false,
new PHPParser_Node_Expr_ConstFetch(new PHPParser_Node_Name('false'))
),
array(
31415,
new PHPParser_Node_Scalar_LNumber(31415)
),
array(
3.1415,
new PHPParser_Node_Scalar_DNumber(3.1415)
),
array(
'Hallo World',
new PHPParser_Node_Scalar_String('Hallo World')
),
array(
new PHPParser_Node_Scalar_DirConst,
new PHPParser_Node_Scalar_DirConst
)
);
}
public function testTypeHints() {
$node = $this->createParamBuilder('test')
->setTypeHint('array')
->getNode()
;
$this->assertEquals(
new PHPParser_Node_Param('test', null, 'array'),
$node
);
$node = $this->createParamBuilder('test')
->setTypeHint('callable')
->getNode()
;
$this->assertEquals(
new PHPParser_Node_Param('test', null, 'callable'),
$node
);
$node = $this->createParamBuilder('test')
->setTypeHint('Some\Class')
->getNode()
;
$this->assertEquals(
new PHPParser_Node_Param('test', null, new PHPParser_Node_Name('Some\Class')),
$node
);
}
public function testByRef() {
$node = $this->createParamBuilder('test')
->makeByRef()
->getNode()
;
$this->assertEquals(
new PHPParser_Node_Param('test', null, null, true),
$node
);
}
}

View File

@ -11,4 +11,9 @@ class PHPParser_Tests_BuilderFactoryTest extends PHPUnit_Framework_TestCase
$factory = new PHPParser_BuilderFactory;
$this->assertInstanceOf('PHPParser_Builder_Method', $factory->method('test'));
}
public function testCreateParamBuilder() {
$factory = new PHPParser_BuilderFactory;
$this->assertInstanceOf('PHPParser_Builder_Param', $factory->param('test'));
}
}