Add constFetch() and classConstFetch() builders

This commit is contained in:
Nikita Popov 2018-03-03 22:14:42 +01:00
parent b998d1e9b5
commit ff2d85dc6b
5 changed files with 100 additions and 21 deletions

View File

@ -8,8 +8,12 @@ Version 4.0.1-dev
### Added
* Added `funcCall()`, `methodCall()` and `staticCall()` methods to `BuilderFactory`, to simplify
creation of call nodes.
* Added the following method to `BuilderFactory`, to simplify creation of expressions:
* `funcCall()`
* `methodCall()`
* `staticCall()`
* `constFetch()`
* `classConstFetch()`
Version 4.0.0 (2018-02-28)
--------------------------

View File

@ -106,6 +106,9 @@ nodes. The following methods are currently available:
an `Identifier` node and normalizes arguments.
* `staticCall($class, $name, array $args = [])`: Create a static method call node. Converts
`$class` to a `Name` node, `$name` to an `Identifier` node and normalizes arguments.
* `constFetch($name)`: Create a constant fetch node. Converts `$name` to a `Name` node.
* `classConstFetch($class, $name)`: Create a class constant fetch node. Converts `$class` to a
`Name` node and `$name` to an `Identifier` node.
* `concat(...$exprs)`: Create a tree of `BinaryOp\Concat` nodes for the given expressions.
These methods may be expanded on an as-needed basis. Please open an issue or PR if a common

View File

@ -170,7 +170,7 @@ class BuilderFactory
public function methodCall(Expr $var, $name, array $args = []) : Expr\MethodCall {
return new Expr\MethodCall(
$var,
$this->normalizeIdentifierOrExpr($name),
BuilderHelpers::normalizeIdentifierOrExpr($name),
$this->args($args)
);
}
@ -187,11 +187,37 @@ class BuilderFactory
public function staticCall($class, $name, array $args = []) : Expr\StaticCall {
return new Expr\StaticCall(
BuilderHelpers::normalizeNameOrExpr($class),
$this->normalizeIdentifierOrExpr($name),
BuilderHelpers::normalizeIdentifierOrExpr($name),
$this->args($args)
);
}
/**
* Creates a constant fetch node.
*
* @param string|Name $name Constant name
*
* @return Expr\ConstFetch
*/
public function constFetch($name) : Expr\ConstFetch {
return new Expr\ConstFetch(BuilderHelpers::normalizeName($name));
}
/**
* Creates a class constant fetch node.
*
* @param string|Name|Expr $class Class name
* @param string|Identifier $name Constant name
*
* @return Expr\ClassConstFetch
*/
public function classConstFetch($class, $name): Expr\ClassConstFetch {
return new Expr\ClassConstFetch(
BuilderHelpers::normalizeNameOrExpr($class),
BuilderHelpers::normalizeIdentifier($name)
);
}
/**
* Creates nested Concat nodes from a list of expressions.
*
@ -227,20 +253,4 @@ class BuilderFactory
throw new \LogicException('Expected string or Expr');
}
/**
* @param string|Identifier|Expr $name
* @return Identifier|Expr
*/
private function normalizeIdentifierOrExpr($name) {
if ($name instanceof Identifier || $name instanceof Expr) {
return $name;
}
if (\is_string($name)) {
return new Identifier($name);
}
throw new \LogicException('Expected string or instance of Node\Identifier or Node\Expr');
}
}

View File

@ -55,6 +55,44 @@ final class BuilderHelpers
throw new \LogicException('Expected statement or expression node');
}
/**
* Normalizes strings to Identifier.
*
* @param string|Identifier $name The identifier to normalize
*
* @return Identifier The normalized identifier
*/
public static function normalizeIdentifier($name) : Identifier {
if ($name instanceof Identifier) {
return $name;
}
if (\is_string($name)) {
return new Identifier($name);
}
throw new \LogicException('Expected string or instance of Node\Identifier or Node\Expr');
}
/**
* Normalizes strings to Identifier, also allowing expressions.
*
* @param string|Identifier|Expr $name The identifier to normalize
*
* @return Identifier|Expr The normalized identifier or expression
*/
public static function normalizeIdentifierOrExpr($name) {
if ($name instanceof Identifier || $name instanceof Expr) {
return $name;
}
if (\is_string($name)) {
return new Identifier($name);
}
throw new \LogicException('Expected string or instance of Node\Identifier or Node\Expr');
}
/**
* Normalizes a name: Converts string names to Name nodes.
*

View File

@ -158,11 +158,35 @@ class BuilderFactoryTest extends TestCase
);
}
public function testConstFetches() {
$factory = new BuilderFactory();
$this->assertEquals(
new Expr\ConstFetch(new Name('FOO')),
$factory->constFetch('FOO')
);
$this->assertEquals(
new Expr\ClassConstFetch(new Name('Foo'), new Identifier('BAR')),
$factory->classConstFetch('Foo', 'BAR')
);
$this->assertEquals(
new Expr\ClassConstFetch(new Expr\Variable('foo'), new Identifier('BAR')),
$factory->classConstFetch(new Expr\Variable('foo'), 'BAR')
);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected string or instance of Node\Identifier
*/
public function testInvalidIdentifier() {
(new BuilderFactory())->classConstFetch('Foo', new Expr\Variable('foo'));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected string or instance of Node\Identifier or Node\Expr
*/
public function testInvalidIdentifier() {
public function testInvalidIdentifierOrExpr() {
(new BuilderFactory())->staticCall('Foo', new Name('bar'));
}