-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added builders for enum and enum case
- Loading branch information
Showing
6 changed files
with
470 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpParser\Builder; | ||
|
||
use PhpParser; | ||
use PhpParser\BuilderHelpers; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Stmt; | ||
|
||
class EnumCase implements PhpParser\Builder | ||
{ | ||
protected $name; | ||
protected $value = null; | ||
protected $attributes = []; | ||
|
||
/** @var Node\AttributeGroup[] */ | ||
protected $attributeGroups = []; | ||
|
||
/** | ||
* Creates an enum case builder. | ||
* | ||
* @param string|Identifier $name Name | ||
*/ | ||
public function __construct($name) { | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* Sets the value. | ||
* | ||
* @param Node\Expr|string|int $value | ||
* | ||
* @return $this | ||
*/ | ||
public function setValue($value) { | ||
$this->value = BuilderHelpers::normalizeValue($value); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Sets doc comment for the constant. | ||
* | ||
* @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' => [BuilderHelpers::normalizeDocComment($docComment)] | ||
]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Adds an attribute group. | ||
* | ||
* @param Node\Attribute|Node\AttributeGroup $attribute | ||
* | ||
* @return $this The builder instance (for fluid interface) | ||
*/ | ||
public function addAttribute($attribute) { | ||
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the built enum case node. | ||
* | ||
* @return Stmt\EnumCase The built constant node | ||
*/ | ||
public function getNode(): PhpParser\Node { | ||
return new Stmt\EnumCase( | ||
$this->name, | ||
$this->value, | ||
$this->attributes, | ||
$this->attributeGroups | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PhpParser\Builder; | ||
|
||
use PhpParser; | ||
use PhpParser\BuilderHelpers; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt; | ||
|
||
class Enum_ extends Declaration | ||
{ | ||
protected $name; | ||
protected $scalarType = null; | ||
|
||
protected $implements = []; | ||
|
||
protected $uses = []; | ||
protected $enumCases = []; | ||
protected $constants = []; | ||
protected $methods = []; | ||
|
||
/** @var Node\AttributeGroup[] */ | ||
protected $attributeGroups = []; | ||
|
||
/** | ||
* Creates an enum builder. | ||
* | ||
* @param string $name Name of the enum | ||
*/ | ||
public function __construct(string $name) { | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* Sets the scalar type. | ||
* | ||
* @param string|Identifier $type | ||
* | ||
* @return $this | ||
*/ | ||
public function setScalarType($scalarType) { | ||
$this->scalarType = BuilderHelpers::normalizeType($scalarType); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Implements one or more interfaces. | ||
* | ||
* @param Name|string ...$interfaces Names of interfaces to implement | ||
* | ||
* @return $this The builder instance (for fluid interface) | ||
*/ | ||
public function implement(...$interfaces) { | ||
foreach ($interfaces as $interface) { | ||
$this->implements[] = BuilderHelpers::normalizeName($interface); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Adds a statement. | ||
* | ||
* @param Stmt|PhpParser\Builder $stmt The statement to add | ||
* | ||
* @return $this The builder instance (for fluid interface) | ||
*/ | ||
public function addStmt($stmt) { | ||
$stmt = BuilderHelpers::normalizeNode($stmt); | ||
|
||
$targets = [ | ||
Stmt\TraitUse::class => &$this->uses, | ||
Stmt\EnumCase::class => &$this->enumCases, | ||
Stmt\ClassConst::class => &$this->constants, | ||
Stmt\ClassMethod::class => &$this->methods, | ||
]; | ||
|
||
$class = \get_class($stmt); | ||
if (!isset($targets[$class])) { | ||
throw new \LogicException(sprintf('Unexpected node of type "%s"', $stmt->getType())); | ||
} | ||
|
||
$targets[$class][] = $stmt; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Adds an attribute group. | ||
* | ||
* @param Node\Attribute|Node\AttributeGroup $attribute | ||
* | ||
* @return $this The builder instance (for fluid interface) | ||
*/ | ||
public function addAttribute($attribute) { | ||
$this->attributeGroups[] = BuilderHelpers::normalizeAttribute($attribute); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the built class node. | ||
* | ||
* @return Stmt\Enum_ The built enum node | ||
*/ | ||
public function getNode() : PhpParser\Node { | ||
return new Stmt\Enum_($this->name, [ | ||
'scalarType' => $this->scalarType, | ||
'implements' => $this->implements, | ||
'stmts' => array_merge($this->uses, $this->enumCases, $this->constants, $this->methods), | ||
'attrGroups' => $this->attributeGroups, | ||
], $this->attributes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PhpParser\Builder; | ||
|
||
use PhpParser\Comment; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Attribute; | ||
use PhpParser\Node\AttributeGroup; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Scalar; | ||
use PhpParser\Node\Scalar\LNumber; | ||
use PhpParser\Node\Stmt; | ||
|
||
class EnumCaseTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function createEnumCaseBuilder($name) { | ||
return new EnumCase($name); | ||
} | ||
|
||
public function testDocComment() { | ||
$node = $this->createEnumCaseBuilder('TEST') | ||
->setDocComment('/** Test */') | ||
->getNode(); | ||
|
||
$this->assertEquals( | ||
new Stmt\EnumCase( | ||
"TEST", | ||
null, | ||
[ | ||
'comments' => [new Comment\Doc('/** Test */')] | ||
] | ||
), | ||
$node | ||
); | ||
} | ||
|
||
public function testAddAttribute() { | ||
$attribute = new Attribute( | ||
new Name('Attr'), | ||
[new Arg(new LNumber(1), false, false, [], new Identifier('name'))] | ||
); | ||
$attributeGroup = new AttributeGroup([$attribute]); | ||
|
||
$node = $this->createEnumCaseBuilder('ATTR_GROUP') | ||
->addAttribute($attributeGroup) | ||
->getNode(); | ||
|
||
$this->assertEquals( | ||
new Stmt\EnumCase( | ||
"ATTR_GROUP", | ||
null, | ||
[], | ||
[$attributeGroup] | ||
), | ||
$node | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider provideTestDefaultValues | ||
*/ | ||
public function testValues($value, $expectedValueNode) { | ||
$node = $this->createEnumCaseBuilder('TEST') | ||
->setValue($value) | ||
->getNode() | ||
; | ||
|
||
$this->assertEquals($expectedValueNode, $node->expr); | ||
} | ||
|
||
public function provideTestDefaultValues() { | ||
return [ | ||
[ | ||
31415, | ||
new Scalar\LNumber(31415) | ||
], | ||
[ | ||
'Hallo World', | ||
new Scalar\String_('Hallo World') | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.