1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-30 04:19:30 +01:00

Add BuilderFactors->args(Add BuilderFactors->args())

This commit is contained in:
Nikita Popov 2017-04-28 18:13:06 +02:00
parent 7f6477ed83
commit 3b4abbfc97
2 changed files with 36 additions and 0 deletions

View File

@ -3,6 +3,7 @@
namespace PhpParser;
use PhpParser\Builder;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
@ -131,6 +132,27 @@ class BuilderFactory
return BuilderHelpers::normalizeValue($value);
}
/**
* Normalizes an argument list.
*
* Creates Arg nodes for all arguments and converts literal values to expressions.
*
* @param array $args List of arguments to normalize
*
* @return Arg[]
*/
public function args(array $args) {
$normalizedArgs = [];
foreach ($args as $arg) {
if ($arg instanceof Arg) {
$normalizedArgs[] = $arg;
} else {
$normalizedArgs[] = new Arg(BuilderHelpers::normalizeValue($arg));
}
}
return $normalizedArgs;
}
/**
* Creates nested Concat nodes from a list of expressions.
*

View File

@ -2,6 +2,7 @@
namespace PhpParser;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\String_;
@ -84,6 +85,19 @@ class BuilderFactoryTest extends TestCase
(new BuilderFactory())->concat("a", 42);
}
public function testArgs() {
$factory = new BuilderFactory();
$unpack = new Arg(new Expr\Variable('c'), false, true);
$this->assertEquals(
[
new Arg(new Expr\Variable('a')),
new Arg(new String_('b')),
$unpack
],
$factory->args([new Expr\Variable('a'), 'b', $unpack])
);
}
public function testIntegration() {
$factory = new BuilderFactory;
$node = $factory->namespace('Name\Space')