From 8f40eec1229c800f5d090c7ce2c651722e83c19b Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 24 Apr 2017 21:28:03 +0200 Subject: [PATCH] Add BuilderFactory->val() method --- lib/PhpParser/BuilderFactory.php | 11 +++++++++++ test/PhpParser/BuilderFactoryTest.php | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/PhpParser/BuilderFactory.php b/lib/PhpParser/BuilderFactory.php index 9cc2fce..1975572 100644 --- a/lib/PhpParser/BuilderFactory.php +++ b/lib/PhpParser/BuilderFactory.php @@ -117,6 +117,17 @@ class BuilderFactory return new Builder\Use_($name, Use_::TYPE_NORMAL); } + /** + * Creates node a for a literal value. + * + * @param Node\Expr|bool|null|int|float|string|array $value $value + * + * @return Node\Expr + */ + public function val($value) { + return BuilderHelpers::normalizeValue($value); + } + public function __call($name, array $args) { if (method_exists($this, '_' . $name)) { return $this->{'_' . $name}(...$args); diff --git a/test/PhpParser/BuilderFactoryTest.php b/test/PhpParser/BuilderFactoryTest.php index 1c3ef18..e0ab32e 100644 --- a/test/PhpParser/BuilderFactoryTest.php +++ b/test/PhpParser/BuilderFactoryTest.php @@ -3,6 +3,7 @@ namespace PhpParser; use PhpParser\Node\Expr; +use PhpParser\Node\Scalar\String_; class BuilderFactoryTest extends \PHPUnit_Framework_TestCase { @@ -34,6 +35,16 @@ class BuilderFactoryTest extends \PHPUnit_Framework_TestCase $factory->foo(); } + public function testVal() { + // This method is a wrapper around BuilderHelpers::normalizeValue(), + // which is already tested elsewhere + $factory = new BuilderFactory(); + $this->assertEquals( + new String_("foo"), + $factory->val("foo") + ); + } + public function testIntegration() { $factory = new BuilderFactory; $node = $factory->namespace('Name\Space')