Skip to content

Commit

Permalink
Merge pull request #4 from rectorphp/tv-add-fluent
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Jul 22, 2023
2 parents 4b6abf2 + c570bc9 commit c3c5260
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 38 deletions.
67 changes: 54 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ CONSTANT_NAME = 'default'

* `$name` - `/** @var Identifier Name */`
* `$value` - `/** @var Expr Value */`
* `$namespacedName` - `/** @var Name|null Namespaced name (if using NameResolver) */`

<br>

Expand Down Expand Up @@ -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[] */`

Expand Down Expand Up @@ -937,7 +938,7 @@ func_call($someVariable)
### Public Properties

* `$name` - `/** @var Node\Name|Expr Function name */`
* `$args` - `/** @var Node\Arg[] Arguments */`
* `$args` - `/** @var array<Node\Arg|Node\VariadicPlaceholder> Arguments */`

<br>

Expand Down Expand Up @@ -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()
```

<br>

```php
<?php

declare(strict_types=1);

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
Expand Down Expand Up @@ -1214,7 +1245,7 @@ $someObject->methodName('yes', 'maybe')

* `$var` - `/** @var Expr Variable holding object */`
* `$name` - `/** @var Identifier|Expr Method name */`
* `$args` - `/** @var Arg[] Arguments */`
* `$args` - `/** @var array<Arg|VariadicPlaceholder> Arguments */`

<br>

Expand Down Expand Up @@ -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<Arg|VariadicPlaceholder> Arguments */`

<br>

Expand Down Expand Up @@ -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<Arg|VariadicPlaceholder> Arguments */`

<br>

Expand Down Expand Up @@ -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<Arg|VariadicPlaceholder> Arguments */`

<br>

Expand Down Expand Up @@ -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` - ``

<br>
Expand Down Expand Up @@ -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
*/`

<br>

Expand Down Expand Up @@ -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 */`
Expand Down Expand Up @@ -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` - ``
Expand Down Expand Up @@ -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) */`

<br>

Expand Down Expand Up @@ -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) */`

<br>

Expand Down Expand Up @@ -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) */`

<br>

Expand Down Expand Up @@ -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 */`

<br>
Expand Down Expand Up @@ -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) */`

<br>

Expand Down Expand Up @@ -2915,6 +2956,6 @@ string|int

### Public Properties

* `$types` - `/** @var (Identifier|Name)[] Types */`
* `$types` - `/** @var (Identifier|Name|IntersectionType)[] Types */`

<br>
5 changes: 0 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,3 @@ parameters:
paths:
- src
- tests

ignoreErrors:
-
message: '#Cannot cast (.*?) to string#'
path: src/Command/DumpNodesCommand.php
19 changes: 19 additions & 0 deletions snippet/method_call_fluent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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;
23 changes: 4 additions & 19 deletions src/Command/DumpNodesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@
use Rector\PhpParserNodesDocs\Printer\MarkdownNodeInfosPrinter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

final class DumpNodesCommand extends Command
{
/**
* @var string
*/
private const OUTPUT_FILE = 'output-file';

public function __construct(
private readonly MarkdownNodeInfosPrinter $markdownNodeInfosPrinter,
private readonly NodeInfosFactory $nodeInfosFactory,
Expand All @@ -29,29 +23,20 @@ protected function configure(): void
{
$this->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"',
'<info>Documentation for %d nodes was generated to README.md</info>' . PHP_EOL,
count($nodeInfos),
$outputFile
));

return self::SUCCESS;
Expand Down
3 changes: 2 additions & 1 deletion src/NodeCodeSampleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit c3c5260

Please sign in to comment.