diff --git a/.github/workflows/code_analysis.yaml b/.github/workflows/code_analysis.yaml index 167ad19..10d6763 100644 --- a/.github/workflows/code_analysis.yaml +++ b/.github/workflows/code_analysis.yaml @@ -15,15 +15,15 @@ jobs: - name: 'Bare Run' - run: bin/console + run: php bin/dump-nodes.php - name: 'ECS' - run: composer check-cs + run: vendor/bin/ecs - name: 'PHPStan' - run: composer phpstan + run: vendor/bin/phpstan name: ${{ matrix.actions.name }} runs-on: ubuntu-latest @@ -34,7 +34,7 @@ jobs: - uses: shivammathur/setup-php@v2 with: - php-version: 8.1 + php-version: 8.3 coverage: none - uses: "ramsey/composer-install@v1" diff --git a/README.md b/README.md index ab1dce5..b875f53 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# Node Overview +# Node Overview for PHP-Parser 5.6 (2025) Here you can find overview of commonly used nodes and how to build PHP code from them. For all nodes, [check php-parser code](https://github.com/nikic/PHP-Parser/tree/master/lib/PhpParser/Node). -## `PhpParser\Node\Const_` +## `PhpParser\Node\ArrayItem` ### Example PHP Code @@ -11,28 +11,34 @@ Here you can find overview of commonly used nodes and how to build PHP code from declare(strict_types=1); -use PhpParser\Node\Const_; +use PhpParser\Node\Expr\ArrayItem; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Scalar\String_; -return new Const_('CONSTANT_NAME', new String_('default')); +$value = new Variable('Tom'); +$key = new String_('name'); + +return new ArrayItem($value, $key); ``` ↓ ```php -CONSTANT_NAME = 'default' +'name' => $Tom ```
### Public Properties - * `$name` - `/** @var Identifier Name */` + * `$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 */`
-## `PhpParser\Node\Expr\ArrayDimFetch` +## `PhpParser\Node\ClosureUse` ### Example PHP Code @@ -41,32 +47,30 @@ CONSTANT_NAME = 'default' declare(strict_types=1); -use PhpParser\Node\Expr\ArrayDimFetch; +use PhpParser\Node\Expr\ClosureUse; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\LNumber; $variable = new Variable('variableName'); -$dimension = new LNumber(0); -return new ArrayDimFetch($variable, $dimension); +return new ClosureUse($variable); ``` ↓ ```php -$variableName[0] +$variableName ```
### Public Properties - * `$var` - `/** @var Expr Variable */` - * `$dim` - `/** @var null|Expr Array index / dim */` + * `$var` - `/** @var Expr\Variable Variable to use */` + * `$byRef` - `/** @var bool Whether to use by reference */`
-## `PhpParser\Node\Expr\ArrayItem` +## `PhpParser\Node\Const_` ### Example PHP Code @@ -75,30 +79,59 @@ $variableName[0] declare(strict_types=1); -use PhpParser\Node\Expr\ArrayItem; -use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Const_; use PhpParser\Node\Scalar\String_; -$value = new Variable('Tom'); -$key = new String_('name'); - -return new ArrayItem($value, $key); +return new Const_('CONSTANT_NAME', new String_('default')); ``` ↓ ```php -'name' => $Tom +CONSTANT_NAME = 'default' ```
### Public Properties - * `$key` - `/** @var null|Expr Key */` + * `$name` - `/** @var Identifier Name */` * `$value` - `/** @var Expr Value */` - * `$byRef` - `/** @var bool Whether to assign by reference */` - * `$unpack` - `/** @var bool Whether to unpack the argument */` + * `$namespacedName` - `/** @var Name|null Namespaced name (if using NameResolver) */` + +
+ +## `PhpParser\Node\Expr\ArrayDimFetch` + +### Example PHP Code + +```php + + +### Public Properties + + * `$var` - `/** @var Expr Variable */` + * `$dim` - `/** @var null|Expr Array index / dim */`
@@ -127,14 +160,14 @@ return new Array_([$arrayItem]); ↓ ```php -array('name' => $Tom) +['name' => $Tom] ```
### Public Properties - * `$items` - `/** @var (ArrayItem|null)[] Items */` + * `$items` - `/** @var ArrayItem[] Items */`
@@ -148,9 +181,9 @@ array('name' => $Tom) declare(strict_types=1); use PhpParser\Node\Expr\ArrowFunction; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$subNodes['expr'] = new LNumber(1); +$subNodes['expr'] = new Int_(1); return new ArrowFunction($subNodes); ``` @@ -165,11 +198,11 @@ fn() => 1 ### Public Properties - * `$static` - `/** @var bool */` - * `$byRef` - `/** @var bool */` + * `$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\NullableType|Node\UnionType */` - * `$expr` - `/** @var Expr */` + * `$returnType` - `/** @var null|Node\Identifier|Node\Name|Node\ComplexType */` + * `$expr` - `/** @var Expr Expression body */` * `$attrGroups` - `/** @var Node\AttributeGroup[] */`
@@ -242,10 +275,10 @@ $someObject->someProperty = 'some value' declare(strict_types=1); use PhpParser\Node\Expr\AssignOp\Coalesce; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Coalesce($left, $right); ``` @@ -275,10 +308,10 @@ return new Coalesce($left, $right); declare(strict_types=1); use PhpParser\Node\Expr\AssignOp\Concat; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Concat($left, $right); ``` @@ -308,10 +341,10 @@ return new Concat($left, $right); declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\BooleanAnd; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new BooleanAnd($left, $right); ``` @@ -341,10 +374,10 @@ return new BooleanAnd($left, $right); declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Coalesce; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Coalesce($left, $right); ``` @@ -374,10 +407,10 @@ return new Coalesce($left, $right); declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Concat; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Concat($left, $right); ``` @@ -407,10 +440,10 @@ return new Concat($left, $right); declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Equal; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Equal($left, $right); ``` @@ -440,10 +473,10 @@ return new Equal($left, $right); declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Identical; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Identical($left, $right); ``` @@ -473,10 +506,10 @@ return new Identical($left, $right); declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Minus; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Minus($left, $right); ``` @@ -506,10 +539,10 @@ return new Minus($left, $right); declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\NotEqual; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new NotEqual($left, $right); ``` @@ -539,10 +572,10 @@ return new NotEqual($left, $right); declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\NotIdentical; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new NotIdentical($left, $right); ``` @@ -572,10 +605,10 @@ return new NotIdentical($left, $right); declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Spaceship; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Spaceship($left, $right); ``` @@ -778,39 +811,7 @@ return new ClassConstFetch($class, 'SOME_CONSTANT'); ### Public Properties * `$class` - `/** @var Name|Expr Class name */` - * `$name` - `/** @var Identifier|Error Constant name */` - -
- -## `PhpParser\Node\Expr\ClosureUse` - -### Example PHP Code - -```php - - -### Public Properties - - * `$var` - `/** @var Expr\Variable Variable to use */` - * `$byRef` - `/** @var bool Whether to use by reference */` + * `$name` - `/** @var Identifier|Expr|Error Constant name */`
@@ -937,7 +938,7 @@ func_call($someVariable) ### Public Properties * `$name` - `/** @var Node\Name|Expr Function name */` - * `$args` - `/** @var Node\Arg[] Arguments */` + * `$args` - `/** @var array Arguments */`
@@ -1073,7 +1074,7 @@ use PhpParser\Node\Expr\List_; use PhpParser\Node\Expr\Variable; $variable = new Variable('variableName'); -$anotherVariable = new Variable('anoterVariableName'); +$anotherVariable = new Variable('anotherVariableName'); $arrayItems = [new ArrayItem($variable), new ArrayItem($anotherVariable)]; @@ -1083,7 +1084,7 @@ return new List_($arrayItems); ↓ ```php -list($variableName, $anoterVariableName) +[$variableName, $anotherVariableName] ```
@@ -1106,13 +1107,13 @@ 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\Int_; use PhpParser\Node\Scalar\String_; $variable = new Variable('variableName'); $body = new String_('yes'); -$cond = new LNumber(1); +$cond = new Int_(1); $matchArm = new MatchArm([$cond], $body); return new Match_($variable, [$matchArm]); @@ -1130,7 +1131,7 @@ match ($variableName) { ### Public Properties - * `$cond` - `/** @var Node\Expr */` + * `$cond` - `/** @var Node\Expr Condition */` * `$arms` - `/** @var MatchArm[] */`
@@ -1165,6 +1166,36 @@ $someObject->methodName() 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; +``` + +↓ + +```php +$someObject->methodName('yes')->nextMethodName() +``` + +
+ +```php +methodName('yes', 'maybe') * `$var` - `/** @var Expr Variable holding object */` * `$name` - `/** @var Identifier|Expr Method name */` - * `$args` - `/** @var Arg[] Arguments */` + * `$args` - `/** @var array Arguments */`
@@ -1271,7 +1302,7 @@ new SomeClass() ### Public Properties * `$class` - `/** @var Node\Name|Expr|Node\Stmt\Class_ Class name */` - * `$args` - `/** @var Node\Arg[] Arguments */` + * `$args` - `/** @var array Arguments */`
@@ -1304,7 +1335,7 @@ $variableName?->methodName() * `$var` - `/** @var Expr Variable holding object */` * `$name` - `/** @var Identifier|Expr Method name */` - * `$args` - `/** @var Arg[] Arguments */` + * `$args` - `/** @var array Arguments */`
@@ -1401,7 +1432,7 @@ return new StaticCall($fullyQualified, 'methodName'); * `$class` - `/** @var Node\Name|Expr Class name */` * `$name` - `/** @var Identifier|Expr Method name */` - * `$args` - `/** @var Node\Arg[] Arguments */` + * `$args` - `/** @var array Arguments */`
@@ -1500,6 +1531,27 @@ throw 'some string'
+```php + + ### Public Properties * `$expr` - `/** @var Node\Expr Expression */` @@ -1544,10 +1596,10 @@ $variableName declare(strict_types=1); use PhpParser\Node\MatchArm; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Scalar\String_; -$conds = [new LNumber(1)]; +$conds = [new Int_(1)]; $body = new String_('yes'); return new MatchArm($conds, $body); @@ -1563,8 +1615,8 @@ return new MatchArm($conds, $body); ### Public Properties - * `$conds` - `/** @var null|Node\Expr[] */` - * `$body` - `/** @var Node\Expr */` + * `$conds` - `/** @var null|list */` + * `$body` - ``
@@ -1592,8 +1644,11 @@ shortName ### Public Properties - * `$parts` - `/** @var string[] Parts of the name */` - * `$specialClassNames` - `` + * `$name` - `/** + * @psalm-var non-empty-string + * @var string Name as string + */` + * `$specialClassNames` - `/** @var array */`
@@ -1621,7 +1676,10 @@ return new FullyQualified('SomeNamespace\ShortName'); ### Public Properties - * `$parts` - `/** @var string[] Parts of the name */` + * `$name` - `/** + * @psalm-var non-empty-string + * @var string Name as string + */`
@@ -1634,9 +1692,10 @@ return new FullyQualified('SomeNamespace\ShortName'); declare(strict_types=1); +use PhpParser\Node\Name; use PhpParser\Node\NullableType; -return new NullableType('SomeType'); +return new NullableType(new Name('SomeType')); ``` ↓ @@ -1680,17 +1739,18 @@ $variableName ### Public Properties - * `$type` - `/** @var null|Identifier|Name|NullableType|UnionType Type declaration */` + * `$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 */` + * `$flags` - `/** @var int Optional visibility flags */` * `$attrGroups` - `/** @var AttributeGroup[] PHP attribute groups */` + * `$hooks` - `/** @var PropertyHook[] Property hooks for promoted properties */`
-## `PhpParser\Node\Scalar\DNumber` +## `PhpParser\Node\Scalar\Float_` ### Example PHP Code @@ -1699,15 +1759,15 @@ $variableName declare(strict_types=1); -use PhpParser\Node\Scalar\DNumber; +use PhpParser\Node\Scalar\Float_; -return new DNumber(10.5); +return new Float_(100.5); ``` ↓ ```php -10.5 +100.5 ```
@@ -1718,7 +1778,35 @@ return new DNumber(10.5);
-## `PhpParser\Node\Scalar\Encapsed` +## `PhpParser\Node\Scalar\Int_` + +### Example PHP Code + +```php + + +### Public Properties + + * `$value` - `/** @var int Number value */` + +
+ +## `PhpParser\Node\Scalar\InterpolatedString` ### Example PHP Code @@ -1743,11 +1831,11 @@ return new Encapsed([new Variable('variableName')]); ### Public Properties - * `$parts` - `/** @var Expr[] list of string parts */` + * `$parts` - `/** @var (Expr|InterpolatedStringPart)[] list of string parts */`
-## `PhpParser\Node\Scalar\LNumber` +## `PhpParser\Node\Scalar\MagicConst\Property` ### Example PHP Code @@ -1756,22 +1844,20 @@ return new Encapsed([new Variable('variableName')]); declare(strict_types=1); -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\MagicConst\Property; -return new LNumber(1000); +return new Property(); ``` ↓ ```php -1000 +__PROPERTY__ ```
-### Public Properties - * `$value` - `/** @var int Number value */`
@@ -1800,7 +1886,75 @@ return new String_('some string'); ### Public Properties * `$value` - `/** @var string String value */` - * `$replacements` - `` + * `$replacements` - `/** @var array Escaped character to its decoded value */` + +
+ +## `PhpParser\Node\StaticVar` + +### Example PHP Code + +```php + + +### Public Properties + + * `$var` - `/** @var Expr\Variable Variable */` + * `$default` - `/** @var null|Node\Expr Default value */` + +
+ +## `PhpParser\Node\Stmt\Block` + +### Example PHP Code + +```php + + +### Public Properties + + * `$stmts` - `/** @var Stmt[] Statements */`
@@ -1813,15 +1967,15 @@ return new String_('some string'); declare(strict_types=1); +use PhpParser\Modifiers; 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); +return new ClassConst([$const], Modifiers::PUBLIC); ``` ↓ @@ -1836,7 +1990,8 @@ public const SOME_CLASS_CONSTANT = 'default value'; * `$flags` - `/** @var int Modifiers */` * `$consts` - `/** @var Node\Const_[] Constant declarations */` - * `$attrGroups` - `/** @var Node\AttributeGroup[] */` + * `$attrGroups` - `/** @var Node\AttributeGroup[] PHP attribute groups */` + * `$type` - `/** @var Node\Identifier|Node\Name|Node\ComplexType|null Type declaration */`
@@ -1849,11 +2004,11 @@ public const SOME_CLASS_CONSTANT = 'default value'; declare(strict_types=1); -use PhpParser\Node\Stmt\Class_; +use PhpParser\Modifiers; use PhpParser\Node\Stmt\ClassMethod; $classMethod = new ClassMethod('methodName'); -$classMethod->flags = Class_::MODIFIER_PUBLIC; +$classMethod->flags = Modifiers::PUBLIC; return $classMethod; ``` @@ -1873,14 +2028,14 @@ public function methodName() declare(strict_types=1); +use PhpParser\Modifiers; 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; +$classMethod->flags = Modifiers::PRIVATE; $param = new Param(new Variable('paramName')); $classMethod->params = [$param]; @@ -1892,7 +2047,7 @@ return $classMethod; ↓ ```php -private function methodName($paramName) : string +private function methodName($paramName): string { } ``` @@ -1904,18 +2059,18 @@ private function methodName($paramName) : string declare(strict_types=1); +use PhpParser\Modifiers; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\LNumber; -use PhpParser\Node\Stmt\Class_; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; $classMethod = new ClassMethod('methodName'); -$classMethod->flags = Class_::MODIFIER_PUBLIC; +$classMethod->flags = Modifiers::PUBLIC; $variable = new Variable('some'); -$number = new LNumber(10000); +$number = new Int_(10000); $assign = new Assign($variable, $number); $classMethod->stmts[] = new Expression($assign); @@ -1940,10 +2095,10 @@ public function methodName() * `$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\NullableType|Node\UnionType Return type */` + * `$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` - `` + * `$magicNames` - `/** @var array */`
@@ -1976,10 +2131,11 @@ class ClassName declare(strict_types=1); +use PhpParser\Modifiers; use PhpParser\Node\Stmt\Class_; $class = new Class_('ClassName'); -$class->flags |= Class_::MODIFIER_FINAL; +$class->flags |= Modifiers::FINAL; return $class; ``` @@ -1999,12 +2155,45 @@ final class ClassName 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; +``` + +↓ + +```php +class ClassName +{ + private $someProperty; +} +``` + +
+ +```php +flags = Class_::MODIFIER_FINAL; +$class->flags = Modifiers::FINAL; $class->extends = new FullyQualified('ParentClass'); return $class; @@ -2022,12 +2211,13 @@ final class ClassName extends \ParentClass ### Public Properties - * `$flags` - `/** @var int Type */` + * `$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) */`
@@ -2060,6 +2250,7 @@ const CONSTANT_IN_CLASS = 'default value'; ### Public Properties * `$consts` - `/** @var Node\Const_[] Constant declarations */` + * `$attrGroups` - `/** @var Node\AttributeGroup[] PHP attribute groups */`
@@ -2072,11 +2263,11 @@ const CONSTANT_IN_CLASS = 'default value'; declare(strict_types=1); -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt\Declare_; use PhpParser\Node\Stmt\DeclareDeclare; -$declareDeclare = new DeclareDeclare('strict_types', new LNumber(1)); +$declareDeclare = new DeclareDeclare('strict_types', new Int_(1)); return new Declare_([$declareDeclare]); ``` @@ -2091,7 +2282,7 @@ declare (strict_types=1); ### Public Properties - * `$declares` - `/** @var DeclareDeclare[] List of declares */` + * `$declares` - `/** @var DeclareItem[] List of declares */` * `$stmts` - `/** @var Node\Stmt[]|null Statements */`
@@ -2264,9 +2455,10 @@ 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\NullableType|Node\UnionType Return type */` + * `$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) */`
@@ -2366,6 +2558,7 @@ interface InterfaceName * `$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) */`
@@ -2406,14 +2599,15 @@ labelName: declare(strict_types=1); -use PhpParser\Node\Stmt\Class_; +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(Class_::MODIFIER_PUBLIC, [$propertyProperty], [], 'string'); +return new Property(Modifiers::PUBLIC, [$propertyProperty], [], new Identifier('string')); ``` ↓ @@ -2429,122 +2623,91 @@ public string $propertyName; declare(strict_types=1); -use PhpParser\Node\Stmt\Class_; +use PhpParser\Node\PropertyItem; 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]); -``` - -↓ - -```php -public $propertyName; -``` - -
+use PhpParser\Node\Expr\BinaryOp\Plus; +use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Scalar\Int_; +use PhpParser\Node\PropertyHook; +use PhpParser\Modifiers; -```php -hooks[] = $getPropertyHook; -return new Property(Class_::MODIFIER_STATIC | Class_::MODIFIER_PUBLIC, $propertyProperties); +return $property; ``` ↓ ```php -public static $firstProperty, $secondProperty; +public $someProperty { + getProperty => $variable + 100; +} ```
-### Public Properties - - * `$flags` - `/** @var int Modifiers */` - * `$props` - `/** @var PropertyProperty[] Properties */` - * `$type` - `/** @var null|Identifier|Name|NullableType|UnionType Type declaration */` - * `$attrGroups` - `/** @var Node\AttributeGroup[] PHP attribute groups */` - -
- -## `PhpParser\Node\Stmt\PropertyProperty` - -### Example PHP Code - ```php stmts[] = $property; +$propertyProperty = new PropertyProperty(new VarLikeIdentifier('propertyName')); -return $propertyProperty; +return new Property(Modifiers::PUBLIC, [$propertyProperty]); ``` ↓ ```php -$someProperty +public $propertyName; ```
-### Public Properties - - * `$name` - `/** @var Node\VarLikeIdentifier Name */` - * `$default` - `/** @var null|Node\Expr Default */` - -
- -## `PhpParser\Node\Stmt\StaticVar` - -### Example PHP Code - ```php ### Public Properties - * `$var` - `/** @var Expr\Variable Variable */` - * `$default` - `/** @var null|Node\Expr Default value */` + * `$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 */`
@@ -2590,12 +2753,12 @@ static $static; declare(strict_types=1); use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt\Case_; use PhpParser\Node\Stmt\Switch_; $cond = new Variable('variableName'); -$cases = [new Case_(new LNumber(1))]; +$cases = [new Case_(new Int_(1))]; return new Switch_($cond, $cases); ``` @@ -2617,37 +2780,6 @@ switch ($variableName) {
-## `PhpParser\Node\Stmt\Throw_` - -### Example PHP Code - -```php - - -### Public Properties - - * `$expr` - `/** @var Node\Expr Expression */` - -
- ## `PhpParser\Node\Stmt\TraitUse` ### Example PHP Code @@ -2687,13 +2819,13 @@ use \TraitName; declare(strict_types=1); +use PhpParser\Modifiers; 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'); +return new Alias($traitFullyQualified, 'method', Modifiers::PUBLIC, 'aliasedMethod'); ``` ↓ @@ -2742,6 +2874,7 @@ 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) */`
@@ -2765,7 +2898,7 @@ $echo = new Echo_([new String_('one')]); $tryStmts = [$echo]; $echo2 = new Echo_([new String_('two')]); -$catch = new Catch_([new FullyQualified('CatchedType')], null, [$echo2]); +$catch = new Catch_([new FullyQualified('SomeType')], null, [$echo2]); $echo3 = new Echo_([new String_('three')]); $finally = new Finally_([$echo3]); @@ -2778,7 +2911,7 @@ return new TryCatch($tryStmts, [$catch]); ```php try { echo 'one'; -} catch (\CatchedType) { +} catch (\SomeType) { echo 'two'; } ``` @@ -2852,8 +2985,8 @@ use UsedNamespace; ### Public Properties - * `$type` - `/** @var int Type of alias */` - * `$uses` - `/** @var UseUse[] Aliases */` + * `$type` - `/** @var self::TYPE_* Type of alias */` + * `$uses` - `/** @var UseItem[] Aliases */`
@@ -2915,6 +3048,6 @@ string|int ### Public Properties - * `$types` - `/** @var (Identifier|Name)[] Types */` + * `$types` - `/** @var (Identifier|Name|IntersectionType)[] Types */`
\ No newline at end of file diff --git a/bin/console b/bin/console deleted file mode 100755 index e7f0299..0000000 --- a/bin/console +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env php -create(); - -/** @var Application $application */ -$application = $container->make(Application::class); - -$input = new ArgvInput(); -$output = new ConsoleOutput(); - -$exitCode = $application->run($input, $output); -exit($exitCode); diff --git a/bin/dump-nodes.php b/bin/dump-nodes.php new file mode 100755 index 0000000..a8ecb86 --- /dev/null +++ b/bin/dump-nodes.php @@ -0,0 +1,24 @@ +create(); +$printedContent = $markdownNodeInfosPrinter->print($nodeInfos); + +file_put_contents(getcwd() . '/README.md', $printedContent); + +echo sprintf('Documentation for %d nodes was generated to README.md' . PHP_EOL . PHP_EOL, count($nodeInfos)); + +// success +exit(0); diff --git a/composer.json b/composer.json index 5bb2dab..cd697c9 100644 --- a/composer.json +++ b/composer.json @@ -7,17 +7,18 @@ "bin/console.php" ], "require": { - "php": "^8.1", - "symfony/console": "^6.3", - "nikic/php-parser": "^4.16", - "illuminate/container": "^10.15", - "webmozart/assert": "^1.11" + "php": "^8.3", + "nikic/php-parser": "^5.6", + "webmozart/assert": "^1.11", + "symfony/finder": "^7.3" }, "require-dev": { - "symplify/easy-coding-standard": "^11.5", - "phpstan/phpstan": "^1.10", - "phpunit/phpunit": "^10.2", - "rector/rector": "^0.17.6" + "symplify/easy-coding-standard": "^12.6", + "phpstan/phpstan": "^2.1", + "phpunit/phpunit": "^12.4", + "rector/rector": "^2.2.2", + "phpstan/phpstan-deprecation-rules": "^2.0", + "phpstan/extension-installer": "^1.4" }, "autoload": { "psr-4": { @@ -34,5 +35,10 @@ "fix-cs": "vendor/bin/ecs check --fix --ansi", "phpstan": "vendor/bin/phpstan analyse --ansi", "build": "bin/console dump-nodes" + }, + "config": { + "allow-plugins": { + "phpstan/extension-installer": true + } } } diff --git a/ecs.php b/ecs.php index ce3a451..5900c54 100644 --- a/ecs.php +++ b/ecs.php @@ -3,17 +3,12 @@ declare(strict_types=1); use Symplify\EasyCodingStandard\Config\ECSConfig; -use Symplify\EasyCodingStandard\ValueObject\Set\SetList; -return static function (ECSConfig $ecsConfig): void { - $ecsConfig->paths([ +return ECSConfig::configure() + ->withPaths([ + __DIR__ . '/bin', __DIR__ . '/src', __DIR__ . '/tests', - ]); - - $ecsConfig->sets([ - SetList::STRICT, - SetList::COMMON, - SetList::PSR_12, - ]); -}; + __DIR__ . '/snippet' + ]) + ->withPreparedSets(symplify: true, common: true, psr12: true); diff --git a/phpstan.neon b/phpstan.neon index 74e0b1b..7b46b98 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,11 +1,10 @@ parameters: - level: max + level: 8 paths: + - bin - src - tests + - snippet - ignoreErrors: - - - message: '#Cannot cast (.*?) to string#' - path: src/Command/DumpNodesCommand.php + treatPhpDocTypesAsCertain: false diff --git a/rector.php b/rector.php index bd90c28..e694c74 100644 --- a/rector.php +++ b/rector.php @@ -6,15 +6,11 @@ use Rector\Config\RectorConfig; use Rector\Set\ValueObject\LevelSetList; -return static function (RectorConfig $rectorConfig): void { - $rectorConfig->importNames(); - - $rectorConfig->paths([ +return RectorConfig::configure() + ->withImportNames() + ->withPaths([ + __DIR__ . '/snippet', __DIR__ . '/src', __DIR__ . '/tests', - ]); - - $rectorConfig->sets([ - LevelSetList::UP_TO_PHP_81 - ]); -}; + ]) + ->withPhpSets(); diff --git a/snippet/alias.php b/snippet/alias.php index e315ff9..a978ca6 100644 --- a/snippet/alias.php +++ b/snippet/alias.php @@ -2,10 +2,10 @@ declare(strict_types=1); +use PhpParser\Modifiers; 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'); +return new Alias($traitFullyQualified, 'method', Modifiers::PUBLIC, 'aliasedMethod'); diff --git a/snippet/array_dim_fetch.php b/snippet/array_dim_fetch.php index 36413c9..4bf725b 100644 --- a/snippet/array_dim_fetch.php +++ b/snippet/array_dim_fetch.php @@ -4,9 +4,9 @@ use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; $variable = new Variable('variableName'); -$dimension = new LNumber(0); +$dimension = new Int_(0); return new ArrayDimFetch($variable, $dimension); diff --git a/snippet/assign_op/assign_op_coalesce.php b/snippet/assign_op/assign_op_coalesce.php index 35d778e..7b17257 100644 --- a/snippet/assign_op/assign_op_coalesce.php +++ b/snippet/assign_op/assign_op_coalesce.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\AssignOp\Coalesce; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Coalesce($left, $right); diff --git a/snippet/assign_op/assign_op_concat.php b/snippet/assign_op/assign_op_concat.php index d9b7999..b4353e8 100644 --- a/snippet/assign_op/assign_op_concat.php +++ b/snippet/assign_op/assign_op_concat.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\AssignOp\Concat; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Concat($left, $right); diff --git a/snippet/binary_op/binary_op_boolean_and.php b/snippet/binary_op/binary_op_boolean_and.php index e51a54e..0d46a66 100644 --- a/snippet/binary_op/binary_op_boolean_and.php +++ b/snippet/binary_op/binary_op_boolean_and.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\BooleanAnd; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new BooleanAnd($left, $right); diff --git a/snippet/binary_op/binary_op_coalesce.php b/snippet/binary_op/binary_op_coalesce.php index 863a063..b277b75 100644 --- a/snippet/binary_op/binary_op_coalesce.php +++ b/snippet/binary_op/binary_op_coalesce.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Coalesce; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Coalesce($left, $right); diff --git a/snippet/binary_op/binary_op_concat.php b/snippet/binary_op/binary_op_concat.php index f8e852b..17b878b 100644 --- a/snippet/binary_op/binary_op_concat.php +++ b/snippet/binary_op/binary_op_concat.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Concat; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Concat($left, $right); diff --git a/snippet/binary_op/binary_op_equal.php b/snippet/binary_op/binary_op_equal.php index c7b4fe2..a7af1c6 100644 --- a/snippet/binary_op/binary_op_equal.php +++ b/snippet/binary_op/binary_op_equal.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Equal; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Equal($left, $right); diff --git a/snippet/binary_op/binary_op_identical.php b/snippet/binary_op/binary_op_identical.php index 76273c2..63a5ff3 100644 --- a/snippet/binary_op/binary_op_identical.php +++ b/snippet/binary_op/binary_op_identical.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Identical; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Identical($left, $right); diff --git a/snippet/binary_op/binary_op_minus.php b/snippet/binary_op/binary_op_minus.php index 587732c..d23c37f 100644 --- a/snippet/binary_op/binary_op_minus.php +++ b/snippet/binary_op/binary_op_minus.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Minus; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Minus($left, $right); diff --git a/snippet/binary_op/binary_op_not_equal.php b/snippet/binary_op/binary_op_not_equal.php index bb20c6c..f84e931 100644 --- a/snippet/binary_op/binary_op_not_equal.php +++ b/snippet/binary_op/binary_op_not_equal.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\NotEqual; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new NotEqual($left, $right); diff --git a/snippet/binary_op/binary_op_not_identical.php b/snippet/binary_op/binary_op_not_identical.php index 068e001..db3da4f 100644 --- a/snippet/binary_op/binary_op_not_identical.php +++ b/snippet/binary_op/binary_op_not_identical.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\NotIdentical; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new NotIdentical($left, $right); diff --git a/snippet/binary_op/binary_op_spaceship.php b/snippet/binary_op/binary_op_spaceship.php index 5c2b09e..f0a77ae 100644 --- a/snippet/binary_op/binary_op_spaceship.php +++ b/snippet/binary_op/binary_op_spaceship.php @@ -3,9 +3,9 @@ declare(strict_types=1); use PhpParser\Node\Expr\BinaryOp\Spaceship; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; -$left = new LNumber(5); -$right = new LNumber(10); +$left = new Int_(5); +$right = new Int_(10); return new Spaceship($left, $right); diff --git a/snippet/block.php b/snippet/block.php new file mode 100644 index 0000000..7852218 --- /dev/null +++ b/snippet/block.php @@ -0,0 +1,13 @@ +flags |= Class_::MODIFIER_FINAL; +$class->flags |= Modifiers::FINAL; return $class; diff --git a/snippet/class_method.php b/snippet/class_method.php index 863937c..5e7df90 100644 --- a/snippet/class_method.php +++ b/snippet/class_method.php @@ -2,10 +2,10 @@ declare(strict_types=1); -use PhpParser\Node\Stmt\Class_; +use PhpParser\Modifiers; use PhpParser\Node\Stmt\ClassMethod; $classMethod = new ClassMethod('methodName'); -$classMethod->flags = Class_::MODIFIER_PUBLIC; +$classMethod->flags = Modifiers::PUBLIC; return $classMethod; diff --git a/snippet/class_method_with_param_and_return_type.php b/snippet/class_method_with_param_and_return_type.php index 50597df..d2edef1 100644 --- a/snippet/class_method_with_param_and_return_type.php +++ b/snippet/class_method_with_param_and_return_type.php @@ -2,14 +2,14 @@ declare(strict_types=1); +use PhpParser\Modifiers; 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; +$classMethod->flags = Modifiers::PRIVATE; $param = new Param(new Variable('paramName')); $classMethod->params = [$param]; diff --git a/snippet/class_method_with_stmts.php b/snippet/class_method_with_stmts.php index 9f6513b..a09bb44 100644 --- a/snippet/class_method_with_stmts.php +++ b/snippet/class_method_with_stmts.php @@ -2,18 +2,18 @@ declare(strict_types=1); +use PhpParser\Modifiers; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\LNumber; -use PhpParser\Node\Stmt\Class_; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Expression; $classMethod = new ClassMethod('methodName'); -$classMethod->flags = Class_::MODIFIER_PUBLIC; +$classMethod->flags = Modifiers::PUBLIC; $variable = new Variable('some'); -$number = new LNumber(10000); +$number = new Int_(10000); $assign = new Assign($variable, $number); $classMethod->stmts[] = new Expression($assign); diff --git a/snippet/class_with_property.php b/snippet/class_with_property.php index 82d58d1..02bc830 100644 --- a/snippet/class_with_property.php +++ b/snippet/class_with_property.php @@ -2,15 +2,17 @@ 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_('ClassName'); +$class = new Class_(new Identifier('ClassName')); $propertyProperty = new PropertyProperty('someProperty'); -$property = new Property(Class_::MODIFIER_PRIVATE, [$propertyProperty]); +$property = new Property(Modifiers::PRIVATE, [$propertyProperty]); $class->stmts[] = $property; -return $propertyProperty; +return $class; diff --git a/snippet/d_number.php b/snippet/d_number.php deleted file mode 100644 index bce611a..0000000 --- a/snippet/d_number.php +++ /dev/null @@ -1,7 +0,0 @@ -flags = Class_::MODIFIER_FINAL; +$class->flags = Modifiers::FINAL; $class->extends = new FullyQualified('ParentClass'); return $class; diff --git a/snippet/float_.php b/snippet/float_.php new file mode 100644 index 0000000..8a6a172 --- /dev/null +++ b/snippet/float_.php @@ -0,0 +1,7 @@ +hooks[] = $getPropertyHook; + +return $property; diff --git a/snippet/property.php b/snippet/property.php index ed760db..4588481 100644 --- a/snippet/property.php +++ b/snippet/property.php @@ -2,11 +2,11 @@ declare(strict_types=1); -use PhpParser\Node\Stmt\Class_; +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(Class_::MODIFIER_PUBLIC, [$propertyProperty]); +return new Property(Modifiers::PUBLIC, [$propertyProperty]); diff --git a/snippet/property_double.php b/snippet/property_double.php index 35803f2..3e7f7ac 100644 --- a/snippet/property_double.php +++ b/snippet/property_double.php @@ -2,10 +2,10 @@ declare(strict_types=1); -use PhpParser\Node\Stmt\Class_; +use PhpParser\Modifiers; 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); +return new Property(Modifiers::STATIC | Modifiers::PUBLIC, $propertyProperties); diff --git a/snippet/switch.php b/snippet/switch.php index 61bb24d..a81422b 100644 --- a/snippet/switch.php +++ b/snippet/switch.php @@ -3,11 +3,11 @@ declare(strict_types=1); use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Stmt\Case_; use PhpParser\Node\Stmt\Switch_; $cond = new Variable('variableName'); -$cases = [new Case_(new LNumber(1))]; +$cases = [new Case_(new Int_(1))]; return new Switch_($cond, $cases); diff --git a/snippet/throw_stmt.php b/snippet/throw_stmt.php index 299fa06..0942c05 100644 --- a/snippet/throw_stmt.php +++ b/snippet/throw_stmt.php @@ -2,8 +2,8 @@ declare(strict_types=1); +use PhpParser\Node\Expr\Throw_; use PhpParser\Node\Scalar\String_; -use PhpParser\Node\Stmt\Throw_; $string = new String_('some string'); diff --git a/snippet/try_catch.php b/snippet/try_catch.php index bfeb6c6..737c673 100644 --- a/snippet/try_catch.php +++ b/snippet/try_catch.php @@ -13,7 +13,7 @@ $tryStmts = [$echo]; $echo2 = new Echo_([new String_('two')]); -$catch = new Catch_([new FullyQualified('CatchedType')], null, [$echo2]); +$catch = new Catch_([new FullyQualified('SomeType')], null, [$echo2]); $echo3 = new Echo_([new String_('three')]); $finally = new Finally_([$echo3]); diff --git a/src/Command/DumpNodesCommand.php b/src/Command/DumpNodesCommand.php deleted file mode 100644 index 44c2e9a..0000000 --- a/src/Command/DumpNodesCommand.php +++ /dev/null @@ -1,59 +0,0 @@ -setName('dump-nodes'); - $this->setDescription('Dump nodes overview'); - - $this->addOption( - self::OUTPUT_FILE, - null, - InputOption::VALUE_REQUIRED, - 'Where to output the file', - getcwd() . '/docs/nodes_overview.md' - ); - } - - protected function execute(InputInterface $input, OutputInterface $output): int - { - $outputFile = (string) $input->getOption(self::OUTPUT_FILE); - - $nodeInfos = $this->nodeInfosFactory->create(); - $printedContent = $this->markdownNodeInfosPrinter->print($nodeInfos); - - file_put_contents($outputFile, $printedContent); - - $output->writeln(sprintf( - 'Documentation for "%d" PhpParser Nodes was generated to "%s"', - count($nodeInfos), - $outputFile - )); - - return self::SUCCESS; - } -} diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php deleted file mode 100644 index 6e9412f..0000000 --- a/src/DependencyInjection/ContainerFactory.php +++ /dev/null @@ -1,29 +0,0 @@ -singleton(Application::class, function (Container $container) { - $application = new Application(); - - /** @var DumpNodesCommand $dumpNodesCommand */ - $dumpNodesCommand = $container->make(DumpNodesCommand::class); - $application->add($dumpNodesCommand); - - return $application; - }); - - return $container; - } -} diff --git a/src/Finder/PhpFilesFinder.php b/src/Finder/PhpFilesFinder.php index 4305cd0..af4b032 100644 --- a/src/Finder/PhpFilesFinder.php +++ b/src/Finder/PhpFilesFinder.php @@ -4,57 +4,25 @@ namespace Rector\PhpParserNodesDocs\Finder; +use Symfony\Component\Finder\Finder; +use Symfony\Component\Finder\SplFileInfo; use Webmozart\Assert\Assert; final class PhpFilesFinder { /** * @param string[] $paths - * @return string[] + * @return SplFileInfo[] */ - public function findPhpFiles(array $paths): array + public static function find(array $paths): array { - Assert::allFileExists($paths); + Assert::allString($paths); - // fallback to config paths - $filePaths = []; + $finder = Finder::create() + ->name('*.php') + ->sortByName() + ->in($paths); - foreach ($paths as $path) { - if (is_file($path)) { - $filePaths[] = $path; - } else { - $currentFilePaths = $this->findFilesUsingGlob($path); - $filePaths = array_merge($filePaths, $currentFilePaths); - } - } - - sort($filePaths); - - Assert::allString($filePaths); - Assert::allFileExists($filePaths); - - return $filePaths; - } - - /** - * @return string[] - */ - private function findFilesUsingGlob(string $directory): array - { - // Search for php files in the current directory - /** @var string[] $phpFiles */ - $phpFiles = glob($directory . '/*.php'); - - // recursively search in subdirectories - - /** @var string[] $subdirectories */ - $subdirectories = glob($directory . '/*', GLOB_ONLYDIR); - - foreach ($subdirectories as $subdirectory) { - // Merge the results from subdirectories - $phpFiles = array_merge($phpFiles, $this->findFilesUsingGlob($subdirectory)); - } - - return $phpFiles; + return iterator_to_array($finder->getIterator()); } } diff --git a/src/NodeCodeSampleProvider.php b/src/NodeCodeSampleProvider.php index d5ccb8c..1a050a6 100644 --- a/src/NodeCodeSampleProvider.php +++ b/src/NodeCodeSampleProvider.php @@ -10,12 +10,13 @@ use Rector\PhpParserNodesDocs\ValueObject\NodeCodeSample; use Webmozart\Assert\Assert; -final class NodeCodeSampleProvider +final readonly class NodeCodeSampleProvider { + private Standard $standardPrinter; + public function __construct( - private readonly Standard $standard, - private readonly PhpFilesFinder $phpFilesFinder, ) { + $this->standardPrinter = new Standard(); } /** @@ -23,27 +24,25 @@ public function __construct( */ public function provide(): array { - $phpFilePaths = $this->phpFilesFinder->findPhpFiles([__DIR__ . '/../snippet']); + $phpFileInfos = PhpFilesFinder::find([__DIR__ . '/../snippet']); $nodeCodeSamplesByNodeClass = []; - foreach ($phpFilePaths as $phpFilePath) { + foreach ($phpFileInfos as $phpFileInfo) { /** @var Node $node */ - $node = include $phpFilePath; + $node = include $phpFileInfo->getRealPath(); - /** @var string $fileContents */ - $fileContents = file_get_contents($phpFilePath); + $errorMessage = sprintf('The "%s" file must return "%s" instance ', $phpFileInfo, Node::class); + Assert::isInstanceOf($node, Node::class, $errorMessage); - Assert::isInstanceOf($node, Node::class, $phpFilePath); + /** @var string $fileContents */ + $fileContents = $phpFileInfo->getContents(); $nodeClass = $node::class; - $printedContent = $this->standard->prettyPrint([$node]); + $printedContent = $this->standardPrinter->prettyPrint([$node]); - $nodeCodeSamplesByNodeClass[$nodeClass][] = new NodeCodeSample( - $fileContents, - $printedContent - ); + $nodeCodeSamplesByNodeClass[$nodeClass][] = new NodeCodeSample($fileContents, $printedContent); } ksort($nodeCodeSamplesByNodeClass); diff --git a/src/NodeInfosFactory.php b/src/NodeInfosFactory.php index 015aeb1..a424288 100644 --- a/src/NodeInfosFactory.php +++ b/src/NodeInfosFactory.php @@ -10,11 +10,11 @@ /** * @see \Rector\PhpParserNodesDocs\Tests\NodeInfosFactoryTest */ -final class NodeInfosFactory +final readonly class NodeInfosFactory { public function __construct( - private readonly NodeCodeSampleProvider $nodeCodeSampleProvider, - private readonly NodeInfoSorter $nodeInfoSorter + private NodeCodeSampleProvider $nodeCodeSampleProvider, + private NodeInfoSorter $nodeInfoSorter ) { } diff --git a/src/Printer/MarkdownNodeInfosPrinter.php b/src/Printer/MarkdownNodeInfosPrinter.php index a49f6c2..245edae 100644 --- a/src/Printer/MarkdownNodeInfosPrinter.php +++ b/src/Printer/MarkdownNodeInfosPrinter.php @@ -15,7 +15,7 @@ final class MarkdownNodeInfosPrinter public function print(array $nodeInfos): string { $contentLines = []; - $contentLines[] = '# Node Overview'; + $contentLines[] = '# Node Overview for PHP-Parser 5.6 (2025)'; $contentLines[] = 'Here you can find overview of commonly used nodes and how to build PHP code from them. For all nodes, [check php-parser code](https://github.com/nikic/PHP-Parser/tree/master/lib/PhpParser/Node).'; foreach ($nodeInfos as $nodeInfo) { diff --git a/src/Sorter/NodeInfoSorter.php b/src/Sorter/NodeInfoSorter.php index f933bd4..4c74983 100644 --- a/src/Sorter/NodeInfoSorter.php +++ b/src/Sorter/NodeInfoSorter.php @@ -14,7 +14,10 @@ final class NodeInfoSorter */ public function sortNodeInfosByClass(array $nodeInfos): array { - usort($nodeInfos, fn (NodeInfo $firstNodeInfo, NodeInfo $secondNodeInfo): int => $firstNodeInfo->getClass() <=> $secondNodeInfo->getClass()); + usort( + $nodeInfos, + fn (NodeInfo $firstNodeInfo, NodeInfo $secondNodeInfo): int => $firstNodeInfo->getClass() <=> $secondNodeInfo->getClass() + ); return $nodeInfos; } diff --git a/src/ValueObject/NodeCodeSample.php b/src/ValueObject/NodeCodeSample.php index cc33bbf..9fea207 100644 --- a/src/ValueObject/NodeCodeSample.php +++ b/src/ValueObject/NodeCodeSample.php @@ -4,11 +4,11 @@ namespace Rector\PhpParserNodesDocs\ValueObject; -final class NodeCodeSample +final readonly class NodeCodeSample { - private readonly string $phpCode; + private string $phpCode; - private readonly string $printedContent; + private string $printedContent; public function __construct(string $phpCode, string $printedContent) { diff --git a/tests/NodeInfosFactoryTest.php b/tests/NodeInfosFactoryTest.php index b818de1..3e0e16f 100644 --- a/tests/NodeInfosFactoryTest.php +++ b/tests/NodeInfosFactoryTest.php @@ -5,22 +5,19 @@ namespace Rector\PhpParserNodesDocs\Tests; use PHPUnit\Framework\TestCase; -use Rector\PhpParserNodesDocs\DependencyInjection\ContainerFactory; +use Rector\PhpParserNodesDocs\NodeCodeSampleProvider; use Rector\PhpParserNodesDocs\NodeInfosFactory; +use Rector\PhpParserNodesDocs\Sorter\NodeInfoSorter; final class NodeInfosFactoryTest extends TestCase { public function test(): void { - $containerFactory = new ContainerFactory(); - $container = $containerFactory->create(); - - /** @var NodeInfosFactory $nodeInfosFactory */ - $nodeInfosFactory = $container->make(NodeInfosFactory::class); + $nodeInfosFactory = new NodeInfosFactory(new NodeCodeSampleProvider(), new NodeInfoSorter()); $nodeInfos = $nodeInfosFactory->create(); $nodeInfoCount = count($nodeInfos); - $this->assertGreaterThan(50, $nodeInfoCount); + $this->assertGreaterThan(78, $nodeInfoCount); } }