From c570bc940358fd215878d80c3aa4244301066a8c Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 22 Jul 2023 14:20:36 +0200 Subject: [PATCH] add fluent call --- README.md | 67 +++++++++++++++++++++++++------- phpstan.neon | 5 --- snippet/method_call_fluent.php | 19 +++++++++ src/Command/DumpNodesCommand.php | 23 ++--------- src/NodeCodeSampleProvider.php | 3 +- 5 files changed, 79 insertions(+), 38 deletions(-) create mode 100644 snippet/method_call_fluent.php diff --git a/README.md b/README.md index ab1dce5..99de4d3 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ CONSTANT_NAME = 'default' * `$name` - `/** @var Identifier Name */` * `$value` - `/** @var Expr Value */` + * `$namespacedName` - `/** @var Name|null Namespaced name (if using NameResolver) */`
@@ -168,7 +169,7 @@ fn() => 1 * `$static` - `/** @var bool */` * `$byRef` - `/** @var bool */` * `$params` - `/** @var Node\Param[] */` - * `$returnType` - `/** @var null|Node\Identifier|Node\Name|Node\NullableType|Node\UnionType */` + * `$returnType` - `/** @var null|Node\Identifier|Node\Name|Node\ComplexType */` * `$expr` - `/** @var Expr */` * `$attrGroups` - `/** @var Node\AttributeGroup[] */` @@ -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 */`
@@ -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 */`
@@ -1592,7 +1623,10 @@ shortName ### Public Properties - * `$parts` - `/** @var string[] Parts of the name */` + * `$parts` - `/** + * @var string[] Parts of the name + * @deprecated Use getParts() instead + */` * `$specialClassNames` - ``
@@ -1621,7 +1655,10 @@ return new FullyQualified('SomeNamespace\ShortName'); ### Public Properties - * `$parts` - `/** @var string[] Parts of the name */` + * `$parts` - `/** + * @var string[] Parts of the name + * @deprecated Use getParts() instead + */`
@@ -1680,7 +1717,7 @@ $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 */` @@ -1940,7 +1977,7 @@ 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` - `` @@ -2028,6 +2065,7 @@ final class ClassName extends \ParentClass * `$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) */`
@@ -2264,9 +2302,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 +2405,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) */`
@@ -2473,7 +2513,7 @@ public static $firstProperty, $secondProperty; * `$flags` - `/** @var int Modifiers */` * `$props` - `/** @var PropertyProperty[] Properties */` - * `$type` - `/** @var null|Identifier|Name|NullableType|UnionType Type declaration */` + * `$type` - `/** @var null|Identifier|Name|ComplexType Type declaration */` * `$attrGroups` - `/** @var Node\AttributeGroup[] PHP attribute groups */`
@@ -2742,6 +2782,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) */`
@@ -2915,6 +2956,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/phpstan.neon b/phpstan.neon index 74e0b1b..028820a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,8 +4,3 @@ parameters: paths: - src - tests - - ignoreErrors: - - - message: '#Cannot cast (.*?) to string#' - path: src/Command/DumpNodesCommand.php diff --git a/snippet/method_call_fluent.php b/snippet/method_call_fluent.php new file mode 100644 index 0000000..e31cfec --- /dev/null +++ b/snippet/method_call_fluent.php @@ -0,0 +1,19 @@ +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); + file_put_contents(getcwd() . '/README.md', $printedContent); + + $output->write(PHP_EOL); $output->writeln(sprintf( - 'Documentation for "%d" PhpParser Nodes was generated to "%s"', + 'Documentation for %d nodes was generated to README.md' . PHP_EOL, count($nodeInfos), - $outputFile )); return self::SUCCESS; diff --git a/src/NodeCodeSampleProvider.php b/src/NodeCodeSampleProvider.php index d5ccb8c..c4ab3c9 100644 --- a/src/NodeCodeSampleProvider.php +++ b/src/NodeCodeSampleProvider.php @@ -34,7 +34,8 @@ public function provide(): array /** @var string $fileContents */ $fileContents = file_get_contents($phpFilePath); - Assert::isInstanceOf($node, Node::class, $phpFilePath); + $errorMessage = sprintf('The "%s" file must return "%s" instance ', $phpFilePath, Node::class); + Assert::isInstanceOf($node, Node::class, $errorMessage); $nodeClass = $node::class;