From ff2d85dc6b0e565ef0e237aec6cb73be8c5c3b28 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Sat, 3 Mar 2018 22:14:42 +0100 Subject: [PATCH] Add constFetch() and classConstFetch() builders --- CHANGELOG.md | 8 +++-- doc/component/AST_builders.markdown | 3 ++ lib/PhpParser/BuilderFactory.php | 46 ++++++++++++++++----------- lib/PhpParser/BuilderHelpers.php | 38 ++++++++++++++++++++++ test/PhpParser/BuilderFactoryTest.php | 26 ++++++++++++++- 5 files changed, 100 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f20c9fa..655694a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) -------------------------- diff --git a/doc/component/AST_builders.markdown b/doc/component/AST_builders.markdown index 9158818..ed4a20a 100644 --- a/doc/component/AST_builders.markdown +++ b/doc/component/AST_builders.markdown @@ -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 diff --git a/lib/PhpParser/BuilderFactory.php b/lib/PhpParser/BuilderFactory.php index 0318753..0d1b023 100644 --- a/lib/PhpParser/BuilderFactory.php +++ b/lib/PhpParser/BuilderFactory.php @@ -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'); - } } diff --git a/lib/PhpParser/BuilderHelpers.php b/lib/PhpParser/BuilderHelpers.php index d3ced90..d5ae179 100644 --- a/lib/PhpParser/BuilderHelpers.php +++ b/lib/PhpParser/BuilderHelpers.php @@ -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. * diff --git a/test/PhpParser/BuilderFactoryTest.php b/test/PhpParser/BuilderFactoryTest.php index 1bbfec4..79bc5cf 100644 --- a/test/PhpParser/BuilderFactoryTest.php +++ b/test/PhpParser/BuilderFactoryTest.php @@ -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')); }