Add property builder

This commit is contained in:
nikic 2012-03-11 08:42:13 +01:00
parent 4c8351fa86
commit 3ce3542032
4 changed files with 210 additions and 0 deletions

View File

@ -0,0 +1,92 @@
<?php
class PHPParser_Builder_Property extends PHPParser_BuilderAbstract
{
protected $name;
protected $type;
protected $default;
/**
* Creates a property builder.
*
* @param string $name Name of the property
*/
public function __construct($name) {
$this->name = $name;
$this->type = 0;
$this->default = null;
}
/**
* Makes the property public.
*
* @return PHPParser_Builder_Property The builder instance (for fluid interface)
*/
public function makePublic() {
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC);
return $this;
}
/**
* Makes the property protected.
*
* @return PHPParser_Builder_Property The builder instance (for fluid interface)
*/
public function makeProtected() {
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PROTECTED);
return $this;
}
/**
* Makes the property private.
*
* @return PHPParser_Builder_Property The builder instance (for fluid interface)
*/
public function makePrivate() {
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_PRIVATE);
return $this;
}
/**
* Makes the property static.
*
* @return PHPParser_Builder_Property The builder instance (for fluid interface)
*/
public function makeStatic() {
$this->setModifier(PHPParser_Node_Stmt_Class::MODIFIER_STATIC);
return $this;
}
/**
* Sets default value for the property.
*
* @param mixed $value Default value to use
*
* @return PHPParser_Builder_Property The builder instance (for fluid interface)
*/
public function setDefault($value) {
$this->default = $this->normalizeValue($value);
return $this;
}
/**
* Returns the built class node.
*
* @return PHPParser_Node_Stmt_Property The built property node
*/
public function getNode() {
return new PHPParser_Node_Stmt_Property(
$this->type !== 0 ? $this->type : PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC,
array(
new PHPParser_Node_Stmt_PropertyProperty($this->name, $this->default)
)
);
}
}

View File

@ -35,6 +35,17 @@ class PHPParser_BuilderFactory
return new PHPParser_Builder_Param($name);
}
/**
* Creates a property builder.
*
* @param string $name Name of the property
*
* @return PHPParser_Builder_Property The created property builder
*/
public function property($name) {
return new PHPParser_Builder_Property($name);
}
public function __call($name, array $args) {
if ('class' === $name) {
return call_user_func_array(array($this, '_class'), $args);

View File

@ -0,0 +1,102 @@
<?php
class PHPParser_Tests_Builder_PropertyTest extends PHPUnit_Framework_TestCase
{
public function createPropertyBuilder($name) {
return new PHPParser_Builder_Property($name);
}
public function testModifiers() {
$node = $this->createPropertyBuilder('test')
->makePrivate()
->makeStatic()
->getNode()
;
$this->assertEquals(
new PHPParser_Node_Stmt_Property(
PHPParser_Node_Stmt_Class::MODIFIER_PRIVATE
| PHPParser_Node_Stmt_Class::MODIFIER_STATIC,
array(
new PHPParser_Node_Stmt_PropertyProperty('test')
)
),
$node
);
$node = $this->createPropertyBuilder('test')
->makeProtected()
->getNode()
;
$this->assertEquals(
new PHPParser_Node_Stmt_Property(
PHPParser_Node_Stmt_Class::MODIFIER_PROTECTED,
array(
new PHPParser_Node_Stmt_PropertyProperty('test')
)
),
$node
);
$node = $this->createPropertyBuilder('test')
->makePublic()
->getNode()
;
$this->assertEquals(
new PHPParser_Node_Stmt_Property(
PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC,
array(
new PHPParser_Node_Stmt_PropertyProperty('test')
)
),
$node
);
}
/**
* @dataProvider provideTestDefaultValues
*/
public function testDefaultValues($value, $expectedValueNode) {
$node = $this->createPropertyBuilder('test')
->setDefault($value)
->getNode()
;
$this->assertEquals($expectedValueNode, $node->props[0]->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
)
);
}
}

View File

@ -16,4 +16,9 @@ class PHPParser_Tests_BuilderFactoryTest extends PHPUnit_Framework_TestCase
$factory = new PHPParser_BuilderFactory;
$this->assertInstanceOf('PHPParser_Builder_Param', $factory->param('test'));
}
public function testCreatePropertyBuilder() {
$factory = new PHPParser_BuilderFactory;
$this->assertInstanceOf('PHPParser_Builder_Property', $factory->property('test'));
}
}