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\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\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\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\Int_;
$variable = new Variable('variableName');
$dimension = new Int_(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\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]);↓
['name' => $Tom]$items-/** @var ArrayItem[] Items */
<?php
declare(strict_types=1);
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Scalar\Int_;
$subNodes['expr'] = new Int_(1);
return new ArrowFunction($subNodes);↓
fn() => 1$static-/** @var bool Whether the closure is static */$byRef-/** @var bool Whether to return by reference */$params-/** @var Node\Param[] */$returnType-/** @var null|Node\Identifier|Node\Name|Node\ComplexType */$expr-/** @var Expr Expression body */$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\Int_;
$left = new Int_(5);
$right = new Int_(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\Int_;
$left = new Int_(5);
$right = new Int_(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\Int_;
$left = new Int_(5);
$right = new Int_(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\Int_;
$left = new Int_(5);
$right = new Int_(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\Int_;
$left = new Int_(5);
$right = new Int_(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\Int_;
$left = new Int_(5);
$right = new Int_(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\Int_;
$left = new Int_(5);
$right = new Int_(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\Int_;
$left = new Int_(5);
$right = new Int_(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\Int_;
$left = new Int_(5);
$right = new Int_(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\Int_;
$left = new Int_(5);
$right = new Int_(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\Int_;
$left = new Int_(5);
$right = new Int_(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|Expr|Error Constant name */
<?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);↓
[$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\Int_;
use PhpParser\Node\Scalar\String_;
$variable = new Variable('variableName');
$body = new String_('yes');
$cond = new Int_(1);
$matchArm = new MatchArm([$cond], $body);
return new Match_($variable, [$matchArm]);↓
match ($variableName) {
1 => 'yes',
}$cond-/** @var Node\Expr Condition */$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'<?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\Int_;
use PhpParser\Node\Scalar\String_;
$conds = [new Int_(1)];
$body = new String_('yes');
return new MatchArm($conds, $body);↓
1 => 'yes'$conds-/** @var null|list<Node\Expr> */$body- ``
<?php
declare(strict_types=1);
use PhpParser\Node\Name;
return new Name('shortName');↓
shortName$name- `/**- @psalm-var non-empty-string
- @var string Name as string */`
$specialClassNames-/** @var array<string, bool> */
<?php
declare(strict_types=1);
use PhpParser\Node\Name\FullyQualified;
return new FullyQualified('SomeNamespace\ShortName');↓
\SomeNamespace\ShortName$name- `/**- @psalm-var non-empty-string
- @var string Name as string */`
<?php
declare(strict_types=1);
use PhpParser\Node\Name;
use PhpParser\Node\NullableType;
return new NullableType(new Name('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 Optional visibility flags */$attrGroups-/** @var AttributeGroup[] PHP attribute groups */$hooks-/** @var PropertyHook[] Property hooks for promoted properties */
<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\Float_;
return new Float_(100.5);↓
100.5$value-/** @var float Number value */
<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\Int_;
return new Int_(100);↓
100$value-/** @var int 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|InterpolatedStringPart)[] list of string parts */
<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\MagicConst\Property;
return new Property();↓
__PROPERTY__<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\String_;
return new String_('some string');↓
'some string'$value-/** @var string String value */$replacements-/** @var array<string, string> Escaped character to its decoded value */
<?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\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\Block;
use PhpParser\Node\Stmt\Expression;
$assign = new Assign(new Variable('someValue'), new Int_(10000));
return new Block([new Expression($assign)]);↓
{
$someValue = 10000;
}$stmts-/** @var Stmt[] Statements */
<?php
declare(strict_types=1);
use PhpParser\Modifiers;
use PhpParser\Node\Const_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\ClassConst;
$defaultValue = new String_('default value');
$const = new Const_('SOME_CLASS_CONSTANT', $defaultValue);
return new ClassConst([$const], Modifiers::PUBLIC);↓
public const SOME_CLASS_CONSTANT = 'default value';$flags-/** @var int Modifiers */$consts-/** @var Node\Const_[] Constant declarations */$attrGroups-/** @var Node\AttributeGroup[] PHP attribute groups */$type-/** @var Node\Identifier|Node\Name|Node\ComplexType|null Type declaration */
<?php
declare(strict_types=1);
use PhpParser\Modifiers;
use PhpParser\Node\Stmt\ClassMethod;
$classMethod = new ClassMethod('methodName');
$classMethod->flags = Modifiers::PUBLIC;
return $classMethod;↓
public function methodName()
{
}<?php
declare(strict_types=1);
use PhpParser\Modifiers;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
$classMethod = new ClassMethod('methodName');
$classMethod->flags = Modifiers::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\Modifiers;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
$classMethod = new ClassMethod('methodName');
$classMethod->flags = Modifiers::PUBLIC;
$variable = new Variable('some');
$number = new Int_(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-/** @var array<string, bool> */
<?php
declare(strict_types=1);
use PhpParser\Node\Stmt\Class_;
return new Class_('ClassName');↓
class ClassName
{
}<?php
declare(strict_types=1);
use PhpParser\Modifiers;
use PhpParser\Node\Stmt\Class_;
$class = new Class_('ClassName');
$class->flags |= Modifiers::FINAL;
return $class;↓
final class ClassName
{
}<?php
declare(strict_types=1);
use PhpParser\Modifiers;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
$class = new Class_(new Identifier('ClassName'));
$propertyProperty = new PropertyProperty('someProperty');
$property = new Property(Modifiers::PRIVATE, [$propertyProperty]);
$class->stmts[] = $property;
return $class;↓
class ClassName
{
private $someProperty;
}<?php
declare(strict_types=1);
use PhpParser\Modifiers;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
$class = new Class_('ClassName');
$class->flags = Modifiers::FINAL;
$class->extends = new FullyQualified('ParentClass');
return $class;↓
final class ClassName extends \ParentClass
{
}$flags-/** @var int Modifiers */$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 */$attrGroups-/** @var Node\AttributeGroup[] PHP attribute groups */
<?php
declare(strict_types=1);
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\DeclareDeclare;
$declareDeclare = new DeclareDeclare('strict_types', new Int_(1));
return new Declare_([$declareDeclare]);↓
declare (strict_types=1);$declares-/** @var DeclareItem[] 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\Identifier;
use PhpParser\Modifiers;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\VarLikeIdentifier;
$propertyProperty = new PropertyProperty(new VarLikeIdentifier('propertyName'));
return new Property(Modifiers::PUBLIC, [$propertyProperty], [], new Identifier('string'));↓
public string $propertyName;<?php
declare(strict_types=1);
use PhpParser\Node\PropertyItem;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Expr\BinaryOp\Plus;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\PropertyHook;
use PhpParser\Modifiers;
$propertyItem = new PropertyItem('someProperty');
$property = new Property(Modifiers::PUBLIC, [$propertyItem]);
$plus = new Plus(
new Variable('variable'),
new Int_(100)
);
$getPropertyHook = new PropertyHook('getProperty', $plus);
$property->hooks[] = $getPropertyHook;
return $property;↓
public $someProperty {
getProperty => $variable + 100;
}<?php
declare(strict_types=1);
use PhpParser\Modifiers;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PhpParser\Node\VarLikeIdentifier;
$propertyProperty = new PropertyProperty(new VarLikeIdentifier('propertyName'));
return new Property(Modifiers::PUBLIC, [$propertyProperty]);↓
public $propertyName;<?php
declare(strict_types=1);
use PhpParser\Modifiers;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
$propertyProperties = [new PropertyProperty('firstProperty'), new PropertyProperty('secondProperty')];
return new Property(Modifiers::STATIC | Modifiers::PUBLIC, $propertyProperties);↓
public static $firstProperty, $secondProperty;$flags-/** @var int Modifiers */$props-/** @var PropertyItem[] Properties */$type-/** @var null|Identifier|Name|ComplexType Type declaration */$attrGroups-/** @var Node\AttributeGroup[] PHP attribute groups */$hooks-/** @var Node\PropertyHook[] Property hooks */
<?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\Int_;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\Switch_;
$cond = new Variable('variableName');
$cases = [new Case_(new Int_(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\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\Modifiers;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\TraitUseAdaptation\Alias;
$traitFullyQualified = new FullyQualified('TraitName');
return new Alias($traitFullyQualified, 'method', Modifiers::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 self::TYPE_* Type of alias */$uses-/** @var UseItem[] 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 */