diff --git a/README.md b/README.md index 530c779..b875f53 100644 --- a/README.md +++ b/README.md @@ -112,10 +112,10 @@ declare(strict_types=1); 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); ``` @@ -181,9 +181,9 @@ return new Array_([$arrayItem]); 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); ``` @@ -275,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); ``` @@ -308,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); ``` @@ -341,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); ``` @@ -374,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); ``` @@ -407,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); ``` @@ -440,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); ``` @@ -473,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); ``` @@ -506,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); ``` @@ -539,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); ``` @@ -572,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); ``` @@ -605,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); ``` @@ -1107,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]); @@ -1596,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); @@ -1750,7 +1750,7 @@ $variableName
-## `PhpParser\Node\PropertyItem` +## `PhpParser\Node\Scalar\Float_` ### Example PHP Code @@ -1759,36 +1759,26 @@ $variableName 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; +use PhpParser\Node\Scalar\Float_; -return $propertyProperty; +return new Float_(100.5); ``` ↓ ```php -$someProperty +100.5 ```
### Public Properties - * `$name` - `/** @var Node\VarLikeIdentifier Name */` - * `$default` - `/** @var null|Node\Expr Default */` + * `$value` - `/** @var float Number value */`
-## `PhpParser\Node\Scalar\Float_` +## `PhpParser\Node\Scalar\Int_` ### Example PHP Code @@ -1797,26 +1787,26 @@ $someProperty declare(strict_types=1); -use PhpParser\Node\Scalar\DNumber; +use PhpParser\Node\Scalar\Int_; -return new DNumber(10.5); +return new Int_(100); ``` ↓ ```php -10.5 +100 ```
### Public Properties - * `$value` - `/** @var float Number value */` + * `$value` - `/** @var int Number value */`
-## `PhpParser\Node\Scalar\Int_` +## `PhpParser\Node\Scalar\InterpolatedString` ### Example PHP Code @@ -1825,26 +1815,27 @@ return new DNumber(10.5); declare(strict_types=1); -use PhpParser\Node\Scalar\LNumber; +use PhpParser\Node\Expr\Variable; +use PhpParser\Node\Scalar\Encapsed; -return new LNumber(1000); +return new Encapsed([new Variable('variableName')]); ``` ↓ ```php -1000 +"{$variableName}" ```
### Public Properties - * `$value` - `/** @var int Number value */` + * `$parts` - `/** @var (Expr|InterpolatedStringPart)[] list of string parts */`
-## `PhpParser\Node\Scalar\InterpolatedString` +## `PhpParser\Node\Scalar\MagicConst\Property` ### Example PHP Code @@ -1853,23 +1844,20 @@ return new LNumber(1000); declare(strict_types=1); -use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Scalar\Encapsed; +use PhpParser\Node\Scalar\MagicConst\Property; -return new Encapsed([new Variable('variableName')]); +return new Property(); ``` ↓ ```php -"{$variableName}" +__PROPERTY__ ```
-### Public Properties - * `$parts` - `/** @var (Expr|InterpolatedStringPart)[] list of string parts */`
@@ -1934,6 +1922,42 @@ $variableName
+## `PhpParser\Node\Stmt\Block` + +### Example PHP Code + +```php + + +### Public Properties + + * `$stmts` - `/** @var Stmt[] Statements */` + +
+ ## `PhpParser\Node\Stmt\ClassConst` ### Example PHP Code @@ -1943,15 +1967,15 @@ $variableName 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); ``` ↓ @@ -1980,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; ``` @@ -2004,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]; @@ -2035,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); @@ -2107,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; ``` @@ -2130,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; @@ -2205,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]); ``` @@ -2541,6 +2599,7 @@ labelName: declare(strict_types=1); +use PhpParser\Node\Identifier; use PhpParser\Modifiers; use PhpParser\Node\Stmt\Property; use PhpParser\Node\Stmt\PropertyProperty; @@ -2548,7 +2607,7 @@ use PhpParser\Node\VarLikeIdentifier; $propertyProperty = new PropertyProperty(new VarLikeIdentifier('propertyName')); -return new Property(Modifiers::PUBLIC, [$propertyProperty], [], new \PhpParser\Node\Identifier('string')); +return new Property(Modifiers::PUBLIC, [$propertyProperty], [], new Identifier('string')); ``` ↓ @@ -2564,14 +2623,52 @@ public string $propertyName; declare(strict_types=1); -use PhpParser\Node\Stmt\Class_; +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; +``` + +↓ + +```php +public $someProperty { + getProperty => $variable + 100; +} +``` + +
+ +```php +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, count($nodeInfos)); +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 30f3991..cd697c9 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ "require": { "php": "^8.3", "nikic/php-parser": "^5.6", - "webmozart/assert": "^1.11" + "webmozart/assert": "^1.11", + "symfony/finder": "^7.3" }, "require-dev": { "symplify/easy-coding-standard": "^12.6", diff --git a/ecs.php b/ecs.php index 4629829..5900c54 100644 --- a/ecs.php +++ b/ecs.php @@ -9,5 +9,6 @@ __DIR__ . '/bin', __DIR__ . '/src', __DIR__ . '/tests', + __DIR__ . '/snippet' ]) ->withPreparedSets(symplify: true, common: true, psr12: true); diff --git a/phpstan.neon b/phpstan.neon index ce4501d..7b46b98 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -5,10 +5,6 @@ parameters: - bin - src - tests + - snippet treatPhpDocTypesAsCertain: false - - ignoreErrors: - - - path: src/Finder/PhpFilesFinder.php - identifier: varTag.nativeType \ No newline at end of file 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/src/Finder/PhpFilesFinder.php b/src/Finder/PhpFilesFinder.php index dbf14f7..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 list $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 05b4e26..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,23 +24,23 @@ 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; - - /** @var string $fileContents */ - $fileContents = file_get_contents($phpFilePath); + $node = include $phpFileInfo->getRealPath(); - $errorMessage = sprintf('The "%s" file must return "%s" instance ', $phpFilePath, Node::class); + $errorMessage = sprintf('The "%s" file must return "%s" instance ', $phpFileInfo, Node::class); Assert::isInstanceOf($node, Node::class, $errorMessage); + /** @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); } 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/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 08921be..3e0e16f 100644 --- a/tests/NodeInfosFactoryTest.php +++ b/tests/NodeInfosFactoryTest.php @@ -4,9 +4,7 @@ namespace Rector\PhpParserNodesDocs\Tests; -use PhpParser\PrettyPrinter\Standard; use PHPUnit\Framework\TestCase; -use Rector\PhpParserNodesDocs\Finder\PhpFilesFinder; use Rector\PhpParserNodesDocs\NodeCodeSampleProvider; use Rector\PhpParserNodesDocs\NodeInfosFactory; use Rector\PhpParserNodesDocs\Sorter\NodeInfoSorter; @@ -15,10 +13,7 @@ final class NodeInfosFactoryTest extends TestCase { public function test(): void { - $nodeInfosFactory = new NodeInfosFactory( - new NodeCodeSampleProvider(new Standard(), new PhpFilesFinder()), - new NodeInfoSorter() - ); + $nodeInfosFactory = new NodeInfosFactory(new NodeCodeSampleProvider(), new NodeInfoSorter()); $nodeInfos = $nodeInfosFactory->create();