Here you can find overview of commonly used nodes and how to build PHP code from them. For all nodes, check php-parser code.
<?php
declare(strict_types=1);
use PhpParser\Node\Const_;
use PhpParser\Node\Scalar\String_;
return new Const_('CONSTANT_NAME', new String_('default'));
↓
CONSTANT_NAME = 'default'
$name
-/** @var Identifier Name */
$value
-/** @var Expr Value */
$namespacedName
-/** @var Name|null Namespaced name (if using NameResolver) */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;
$variable = new Variable('variableName');
$dimension = new LNumber(0);
return new ArrayDimFetch($variable, $dimension);
↓
$variableName[0]
$var
-/** @var Expr Variable */
$dim
-/** @var null|Expr Array index / dim */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
$value = new Variable('Tom');
$key = new String_('name');
return new ArrayItem($value, $key);
↓
'name' => $Tom
$key
-/** @var null|Expr Key */
$value
-/** @var Expr Value */
$byRef
-/** @var bool Whether to assign by reference */
$unpack
-/** @var bool Whether to unpack the argument */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
$value = new Variable('Tom');
$key = new String_('name');
$arrayItem = new ArrayItem($value, $key);
return new Array_([$arrayItem]);
↓
array('name' => $Tom)
$items
-/** @var (ArrayItem|null)[] Items */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Scalar\LNumber;
$subNodes['expr'] = new LNumber(1);
return new ArrowFunction($subNodes);
↓
fn() => 1
$static
-/** @var bool */
$byRef
-/** @var bool */
$params
-/** @var Node\Param[] */
$returnType
-/** @var null|Node\Identifier|Node\Name|Node\ComplexType */
$expr
-/** @var Expr */
$attrGroups
-/** @var Node\AttributeGroup[] */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
$variable = new Variable('variableName');
$value = new String_('some value');
return new Assign($variable, $value);
↓
$variableName = 'some value'
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
$propertyFetch = new PropertyFetch(new Variable('someObject'), 'someProperty');
$value = new String_('some value');
return new Assign($propertyFetch, $value);
↓
$someObject->someProperty = 'some value'
$var
-/** @var Expr Variable */
$expr
-/** @var Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\AssignOp\Coalesce;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new Coalesce($left, $right);
↓
5 ??= 10
$var
-/** @var Expr Variable */
$expr
-/** @var Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\AssignOp\Concat;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new Concat($left, $right);
↓
5 .= 10
$var
-/** @var Expr Variable */
$expr
-/** @var Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new BooleanAnd($left, $right);
↓
5 && 10
$left
-/** @var Expr The left hand side expression */
$right
-/** @var Expr The right hand side expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new Coalesce($left, $right);
↓
5 ?? 10
$left
-/** @var Expr The left hand side expression */
$right
-/** @var Expr The right hand side expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new Concat($left, $right);
↓
5 . 10
$left
-/** @var Expr The left hand side expression */
$right
-/** @var Expr The right hand side expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new Equal($left, $right);
↓
5 == 10
$left
-/** @var Expr The left hand side expression */
$right
-/** @var Expr The right hand side expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new Identical($left, $right);
↓
5 === 10
$left
-/** @var Expr The left hand side expression */
$right
-/** @var Expr The right hand side expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\BinaryOp\Minus;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new Minus($left, $right);
↓
5 - 10
$left
-/** @var Expr The left hand side expression */
$right
-/** @var Expr The right hand side expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new NotEqual($left, $right);
↓
5 != 10
$left
-/** @var Expr The left hand side expression */
$right
-/** @var Expr The right hand side expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new NotIdentical($left, $right);
↓
5 !== 10
$left
-/** @var Expr The left hand side expression */
$right
-/** @var Expr The right hand side expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\BinaryOp\Spaceship;
use PhpParser\Node\Scalar\LNumber;
$left = new LNumber(5);
$right = new LNumber(10);
return new Spaceship($left, $right);
↓
5 <=> 10
$left
-/** @var Expr The left hand side expression */
$right
-/** @var Expr The right hand side expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('isEligible');
return new BooleanNot($variable);
↓
!$isEligible
$expr
-/** @var Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\Variable;
$expr = new Variable('variableName');
return new Array_($expr);
↓
(array) $variableName
$expr
-/** @var Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\Variable;
$expr = new Variable('variableName');
return new Bool_($expr);
↓
(bool) $variableName
$expr
-/** @var Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Cast\Int_;
use PhpParser\Node\Expr\Variable;
$expr = new Variable('variableName');
return new Int_($expr);
↓
(int) $variableName
$expr
-/** @var Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Cast\String_;
use PhpParser\Node\Expr\Variable;
$expr = new Variable('variableName');
return new String_($expr);
↓
(string) $variableName
$expr
-/** @var Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;
$class = new FullyQualified('SomeClassName');
return new ClassConstFetch($class, 'SOME_CONSTANT');
↓
\SomeClassName::SOME_CONSTANT
$class
-/** @var Name|Expr Class name */
$name
-/** @var Identifier|Error Constant name */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('variableName');
return new ClosureUse($variable);
↓
$variableName
$var
-/** @var Expr\Variable Variable to use */
$byRef
-/** @var bool Whether to use by reference */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
$name = new Name('true');
return new ConstFetch($name);
↓
true
$name
-/** @var Name Constant name */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('variableName');
return new Empty_($variable);
↓
empty($variableName)
$expr
-/** @var Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Eval_;
use PhpParser\Node\Scalar\String_;
$string = new String_('Some php code');
return new Eval_(new String_('Some php code'));
↓
eval('Some php code')
$expr
-/** @var Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
$args = [new Arg(new Variable('someVariable'))];
return new FuncCall(new Name('func_call'), $args);
↓
func_call($someVariable)
$name
-/** @var Node\Name|Expr Function name */
$args
-/** @var array<Node\Arg|Node\VariadicPlaceholder> Arguments */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('variableName');
return new Include_($variable, Include_::TYPE_INCLUDE);
↓
include $variableName
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('variableName');
return new Include_($variable, Include_::TYPE_REQUIRE_ONCE);
↓
require_once $variableName
$expr
-/** @var Expr Expression */
$type
-/** @var int Type of include */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
$variable = new Variable('variableName');
$class = new FullyQualified('SomeClassName');
return new Instanceof_($variable, $class);
↓
$variableName instanceof \SomeClassName
$expr
-/** @var Expr Expression */
$class
-/** @var Name|Expr Class name */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('variableName');
return new Isset_([$variable]);
↓
isset($variableName)
$vars
-/** @var Expr[] Variables */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('variableName');
$anotherVariable = new Variable('anotherVariableName');
$arrayItems = [new ArrayItem($variable), new ArrayItem($anotherVariable)];
return new List_($arrayItems);
↓
list($variableName, $anotherVariableName)
$items
-/** @var (ArrayItem|null)[] List of items to assign to */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
$variable = new Variable('variableName');
$body = new String_('yes');
$cond = new LNumber(1);
$matchArm = new MatchArm([$cond], $body);
return new Match_($variable, [$matchArm]);
↓
match ($variableName) {
1 => 'yes',
}
$cond
-/** @var Node\Expr */
$arms
-/** @var MatchArm[] */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('someObject');
return new MethodCall($variable, 'methodName');
↓
$someObject->methodName()
<?php
declare(strict_types=1);
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
$variable = new Variable('someObject');
$args = [];
$args[] = new Arg(new String_('yes'));
$methodCall = new MethodCall($variable, 'methodName', $args);
$nestedMethodCall = new MethodCall($methodCall, 'nextMethodName');
return $nestedMethodCall;
↓
$someObject->methodName('yes')->nextMethodName()
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
$thisVariable = new Variable('this');
$propertyFetch = new PropertyFetch($thisVariable, 'someProperty');
return new MethodCall($propertyFetch, 'methodName');
↓
$this->someProperty->methodName()
<?php
declare(strict_types=1);
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
$variable = new Variable('someObject');
$args = [];
$args[] = new Arg(new String_('yes'));
$args[] = new Arg(new String_('maybe'));
return new MethodCall($variable, 'methodName', $args);
↓
$someObject->methodName('yes', 'maybe')
$var
-/** @var Expr Variable holding object */
$name
-/** @var Identifier|Expr Method name */
$args
-/** @var array<Arg|VariadicPlaceholder> Arguments */
<?php
declare(strict_types=1);
// anonymous class
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Stmt\Class_;
$class = new Class_(null);
return new New_($class);
↓
new class
{
}
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
$class = new Name('SomeClass');
return new New_($class);
↓
new SomeClass()
$class
-/** @var Node\Name|Expr|Node\Stmt\Class_ Class name */
$args
-/** @var array<Arg|VariadicPlaceholder> Arguments */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('variableName');
return new NullsafeMethodCall($variable, 'methodName');
↓
$variableName?->methodName()
$var
-/** @var Expr Variable holding object */
$name
-/** @var Identifier|Expr Method name */
$args
-/** @var array<Arg|VariadicPlaceholder> Arguments */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\NullsafePropertyFetch;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('variableName');
return new NullsafePropertyFetch($variable, 'someProperty');
↓
$variableName?->someProperty
$var
-/** @var Expr Variable holding object */
$name
-/** @var Identifier|Expr Property name */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
$variable = new Variable('variableName');
return new PropertyFetch($variable, 'propertyName');
↓
$variableName->propertyName
$var
-/** @var Expr Variable holding object */
$name
-/** @var Identifier|Expr Property name */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name\FullyQualified;
$fullyQualified = new FullyQualified('ClassName');
return new StaticCall($fullyQualified, 'methodName');
↓
\ClassName::methodName()
$class
-/** @var Node\Name|Expr Class name */
$name
-/** @var Identifier|Expr Method name */
$args
-/** @var array<Arg|VariadicPlaceholder> Arguments */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Name\FullyQualified;
$class = new FullyQualified('StaticClassName');
return new StaticPropertyFetch($class, 'someProperty');
↓
\StaticClassName::$someProperty
$class
-/** @var Name|Expr Class name */
$name
-/** @var VarLikeIdentifier|Expr Property name */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
$variable = new Variable('variableName');
$trueConstFetch = new ConstFetch(new Name('true'));
$falseConstFetch = new ConstFetch(new Name('false'));
return new Ternary($variable, $trueConstFetch, $falseConstFetch);
↓
$variableName ? true : false
$cond
-/** @var Expr Condition */
$if
-/** @var null|Expr Expression for true */
$else
-/** @var Expr Expression for false */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Throw_;
use PhpParser\Node\Scalar\String_;
$string = new String_('some string');
return new Throw_($string);
↓
throw 'some string'
$expr
-/** @var Node\Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
return new Variable('variableName');
↓
$variableName
$name
-/** @var string|Expr Name */
<?php
declare(strict_types=1);
use PhpParser\Node\MatchArm;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
$conds = [new LNumber(1)];
$body = new String_('yes');
return new MatchArm($conds, $body);
↓
1 => 'yes'
$conds
-/** @var null|Node\Expr[] */
$body
-/** @var Node\Expr */
<?php
declare(strict_types=1);
use PhpParser\Node\Name;
return new Name('shortName');
↓
shortName
$parts
- `/**- @var string[] Parts of the name
- @deprecated Use getParts() instead */`
$specialClassNames
- ``
<?php
declare(strict_types=1);
use PhpParser\Node\Name\FullyQualified;
return new FullyQualified('SomeNamespace\ShortName');
↓
\SomeNamespace\ShortName
$parts
- `/**- @var string[] Parts of the name
- @deprecated Use getParts() instead */`
<?php
declare(strict_types=1);
use PhpParser\Node\NullableType;
return new NullableType('SomeType');
↓
?SomeType
$type
-/** @var Identifier|Name Type */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
$variable = new Variable('variableName');
return new Param($variable);
↓
$variableName
$type
-/** @var null|Identifier|Name|ComplexType Type declaration */
$byRef
-/** @var bool Whether parameter is passed by reference */
$variadic
-/** @var bool Whether this is a variadic argument */
$var
-/** @var Expr\Variable|Expr\Error Parameter variable */
$default
-/** @var null|Expr Default value */
$flags
-/** @var int */
$attrGroups
-/** @var AttributeGroup[] PHP attribute groups */
<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\DNumber;
return new DNumber(10.5);
↓
10.5
$value
-/** @var float Number value */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\Encapsed;
return new Encapsed([new Variable('variableName')]);
↓
"{$variableName}"
$parts
-/** @var Expr[] list of string parts */
<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\LNumber;
return new LNumber(1000);
↓
1000
$value
-/** @var int Number value */
<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\String_;
return new String_('some string');
↓
'some string'
$value
-/** @var string String value */
$replacements
- ``
<?php
declare(strict_types=1);
use PhpParser\Node\Const_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
$defaultValue = new String_('default value');
$const = new Const_('SOME_CLASS_CONSTANT', $defaultValue);
return new ClassConst([$const], Class_::MODIFIER_PUBLIC);
↓
public const SOME_CLASS_CONSTANT = 'default value';
$flags
-/** @var int Modifiers */
$consts
-/** @var Node\Const_[] Constant declarations */
$attrGroups
-/** @var Node\AttributeGroup[] */
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
$classMethod = new ClassMethod('methodName');
$classMethod->flags = Class_::MODIFIER_PUBLIC;
return $classMethod;
↓
public function methodName()
{
}
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
$classMethod = new ClassMethod('methodName');
$classMethod->flags = Class_::MODIFIER_PRIVATE;
$param = new Param(new Variable('paramName'));
$classMethod->params = [$param];
$classMethod->returnType = new Identifier('string');
return $classMethod;
↓
private function methodName($paramName) : string
{
}
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
$classMethod = new ClassMethod('methodName');
$classMethod->flags = Class_::MODIFIER_PUBLIC;
$variable = new Variable('some');
$number = new LNumber(10000);
$assign = new Assign($variable, $number);
$classMethod->stmts[] = new Expression($assign);
return $classMethod;
↓
public function methodName()
{
$some = 10000;
}
$flags
-/** @var int Flags */
$byRef
-/** @var bool Whether to return by reference */
$name
-/** @var Node\Identifier Name */
$params
-/** @var Node\Param[] Parameters */
$returnType
-/** @var null|Node\Identifier|Node\Name|Node\ComplexType Return type */
$stmts
-/** @var Node\Stmt[]|null Statements */
$attrGroups
-/** @var Node\AttributeGroup[] PHP attribute groups */
$magicNames
- ``
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Class_;
return new Class_('ClassName');
↓
class ClassName
{
}
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Class_;
$class = new Class_('ClassName');
$class->flags |= Class_::MODIFIER_FINAL;
return $class;
↓
final class ClassName
{
}
<?php
declare(strict_types=1);
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
$class = new Class_('ClassName');
$class->flags = Class_::MODIFIER_FINAL;
$class->extends = new FullyQualified('ParentClass');
return $class;
↓
final class ClassName extends \ParentClass
{
}
$flags
-/** @var int Type */
$extends
-/** @var null|Node\Name Name of extended class */
$implements
-/** @var Node\Name[] Names of implemented interfaces */
$name
-/** @var Node\Identifier|null Name */
$stmts
-/** @var Node\Stmt[] Statements */
$attrGroups
-/** @var Node\AttributeGroup[] PHP attribute groups */
$namespacedName
-/** @var Node\Name|null Namespaced name (if using NameResolver) */
<?php
declare(strict_types=1);
use PhpParser\Node\Const_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Const_ as ConstStmt;
$consts = [new Const_('CONSTANT_IN_CLASS', new String_('default value'))];
return new ConstStmt($consts);
↓
const CONSTANT_IN_CLASS = 'default value';
$consts
-/** @var Node\Const_[] Constant declarations */
<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\DeclareDeclare;
$declareDeclare = new DeclareDeclare('strict_types', new LNumber(1));
return new Declare_([$declareDeclare]);
↓
declare (strict_types=1);
$declares
-/** @var DeclareDeclare[] List of declares */
$stmts
-/** @var Node\Stmt[]|null Statements */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Do_;
$variable = new Variable('variableName');
return new Do_($variable);
↓
do {
} while ($variableName);
$stmts
-/** @var Node\Stmt[] Statements */
$cond
-/** @var Node\Expr Condition */
<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Echo_;
$string = new String_('hello');
return new Echo_([$string]);
↓
echo 'hello';
$exprs
-/** @var Node\Expr[] Expressions */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\Return_;
$name = new Name('true');
$constFetch = new ConstFetch($name);
$stmt = new Return_();
return new ElseIf_($constFetch, [$stmt]);
↓
elseif (true) {
return;
}
$cond
-/** @var Node\Expr Condition */
$stmts
-/** @var Node\Stmt[] Statements */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Foreach_;
$foreachedVariable = new Variable('foreachedVariableName');
$asVariable = new Variable('asVariable');
return new Foreach_($foreachedVariable, $asVariable);
↓
foreach ($foreachedVariableName as $asVariable) {
}
$expr
-/** @var Node\Expr Expression to iterate */
$keyVar
-/** @var null|Node\Expr Variable to assign key to */
$byRef
-/** @var bool Whether to assign value by reference */
$valueVar
-/** @var Node\Expr Variable to assign value to */
$stmts
-/** @var Node\Stmt[] Statements */
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Function_;
return new Function_('some_function');
↓
function some_function()
{
}
$byRef
-/** @var bool Whether function returns by reference */
$name
-/** @var Node\Identifier Name */
$params
-/** @var Node\Param[] Parameters */
$returnType
-/** @var null|Node\Identifier|Node\Name|Node\ComplexType Return type */
$stmts
-/** @var Node\Stmt[] Statements */
$attrGroups
-/** @var Node\AttributeGroup[] PHP attribute groups */
$namespacedName
-/** @var Node\Name|null Namespaced name (if using NameResolver) */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\If_;
$cond = new ConstFetch(new Name('true'));
return new If_($cond);
↓
if (true) {
}
$cond
-/** @var Node\Expr Condition expression */
$stmts
-/** @var Node\Stmt[] Statements */
$elseifs
-/** @var ElseIf_[] Elseif clauses */
$else
-/** @var null|Else_ Else clause */
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\InlineHTML;
return new InlineHTML('<strong>feel</strong>');
↓
?>
<strong>feel</strong><?php
$value
-/** @var string String */
<?php
declare(strict_types=1);
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Interface_;
return new Interface_(new Identifier('InterfaceName'));
↓
interface InterfaceName
{
}
$extends
-/** @var Node\Name[] Extended interfaces */
$name
-/** @var Node\Identifier|null Name */
$stmts
-/** @var Node\Stmt[] Statements */
$attrGroups
-/** @var Node\AttributeGroup[] PHP attribute groups */
$namespacedName
-/** @var Node\Name|null Namespaced name (if using NameResolver) */
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Label;
return new Label('labelName');
↓
labelName:
$name
-/** @var Identifier Name */
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\VarLikeIdentifier;
$propertyProperty = new PropertyProperty(new VarLikeIdentifier('propertyName'));
return new Property(Class_::MODIFIER_PUBLIC, [$propertyProperty], [], 'string');
↓
public string $propertyName;
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\VarLikeIdentifier;
$propertyProperty = new PropertyProperty(new VarLikeIdentifier('propertyName'));
return new Property(Class_::MODIFIER_PUBLIC, [$propertyProperty]);
↓
public $propertyName;
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
$propertyProperties = [new PropertyProperty('firstProperty'), new PropertyProperty('secondProperty')];
return new Property(Class_::MODIFIER_STATIC | Class_::MODIFIER_PUBLIC, $propertyProperties);
↓
public static $firstProperty, $secondProperty;
$flags
-/** @var int Modifiers */
$props
-/** @var PropertyProperty[] Properties */
$type
-/** @var null|Identifier|Name|ComplexType Type declaration */
$attrGroups
-/** @var Node\AttributeGroup[] PHP attribute groups */
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
$class = new Class_('ClassName');
$propertyProperty = new PropertyProperty('someProperty');
$property = new Property(Class_::MODIFIER_PRIVATE, [$propertyProperty]);
$class->stmts[] = $property;
return $propertyProperty;
↓
$someProperty
$name
-/** @var Node\VarLikeIdentifier Name */
$default
-/** @var null|Node\Expr Default */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\StaticVar;
$variable = new Variable('variableName');
return new StaticVar($variable);
↓
$variableName
$var
-/** @var Expr\Variable Variable */
$default
-/** @var null|Node\Expr Default value */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Static_;
use PhpParser\Node\Stmt\StaticVar;
$staticVars = [new StaticVar(new Variable('static'))];
return new Static_($staticVars);
↓
static $static;
$vars
-/** @var StaticVar[] Variable definitions */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\Switch_;
$cond = new Variable('variableName');
$cases = [new Case_(new LNumber(1))];
return new Switch_($cond, $cases);
↓
switch ($variableName) {
case 1:
}
$cond
-/** @var Node\Expr Condition */
$cases
-/** @var Case_[] Case list */
<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Throw_;
$string = new String_('some string');
return new Throw_($string);
↓
throw 'some string';
$expr
-/** @var Node\Expr Expression */
<?php
declare(strict_types=1);
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\TraitUse;
return new TraitUse([new FullyQualified('TraitName')]);
↓
use \TraitName;
$traits
-/** @var Node\Name[] Traits */
$adaptations
-/** @var TraitUseAdaptation[] Adaptations */
<?php
declare(strict_types=1);
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\TraitUseAdaptation\Alias;
$traitFullyQualified = new FullyQualified('TraitName');
return new Alias($traitFullyQualified, 'method', Class_::MODIFIER_PUBLIC, 'aliasedMethod');
↓
\TraitName::method as public aliasedMethod;
$newModifier
-/** @var null|int New modifier */
$newName
-/** @var null|Node\Identifier New name */
$trait
-/** @var Node\Name|null Trait name */
$method
-/** @var Node\Identifier Method name */
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Trait_;
return new Trait_('TraitName');
↓
trait TraitName
{
}
$name
-/** @var Node\Identifier|null Name */
$stmts
-/** @var Node\Stmt[] Statements */
$attrGroups
-/** @var Node\AttributeGroup[] PHP attribute groups */
$namespacedName
-/** @var Node\Name|null Namespaced name (if using NameResolver) */
<?php
declare(strict_types=1);
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\Finally_;
use PhpParser\Node\Stmt\TryCatch;
$echo = new Echo_([new String_('one')]);
$tryStmts = [$echo];
$echo2 = new Echo_([new String_('two')]);
$catch = new Catch_([new FullyQualified('SomeType')], null, [$echo2]);
$echo3 = new Echo_([new String_('three')]);
$finally = new Finally_([$echo3]);
return new TryCatch($tryStmts, [$catch]);
↓
try {
echo 'one';
} catch (\SomeType) {
echo 'two';
}
$stmts
-/** @var Node\Stmt[] Statements */
$catches
-/** @var Catch_[] Catches */
$finally
-/** @var null|Finally_ Optional finally node */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Unset_;
$variable = new Variable('variableName');
return new Unset_([$variable]);
↓
unset($variableName);
$vars
-/** @var Node\Expr[] Variables to unset */
<?php
declare(strict_types=1);
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
$useUse = new UseUse(new Name('UsedNamespace'));
return new Use_([$useUse]);
↓
use UsedNamespace;
$type
-/** @var int Type of alias */
$uses
-/** @var UseUse[] Aliases */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\While_;
return new While_(new Variable('variableName'));
↓
while ($variableName) {
}
$cond
-/** @var Node\Expr Condition */
$stmts
-/** @var Node\Stmt[] Statements */
<?php
declare(strict_types=1);
use PhpParser\Node\Identifier;
use PhpParser\Node\UnionType;
$unionedTypes = [new Identifier('string'), new Identifier('int')];
return new UnionType($unionedTypes);
↓
string|int
$types
-/** @var (Identifier|Name|IntersectionType)[] Types */