Skip to content
This repository was archived by the owner on Dec 3, 2023. It is now read-only.

Commit 3354f27

Browse files
authored
[SimplePhpDocParser] Merge to Astral package, as close related purpose (#3950)
1 parent fc72eab commit 3354f27

67 files changed

Lines changed: 171 additions & 351 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/code_analysis.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ jobs:
6969
packages/easy-ci/bin/easy-ci check-active-class packages/phpstan-rules --ansi
7070
packages/easy-ci/bin/easy-ci check-active-class packages/rule-doc-generator --ansi
7171
packages/easy-ci/bin/easy-ci check-active-class packages/rule-doc-generator-contracts --ansi
72-
packages/easy-ci/bin/easy-ci check-active-class packages/simple-php-doc-parser --ansi
7372
packages/easy-ci/bin/easy-ci check-active-class packages/skipper --ansi
7473
packages/easy-ci/bin/easy-ci check-active-class packages/smart-file-system --ansi
7574
packages/easy-ci/bin/easy-ci check-active-class packages/symfony-static-dumper --ansi

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ You'll find all packages in [`/packages`](/packages) directory. Here is a brief
2323

2424
- [Easy Coding Standard](https://github.com/symplify/easy-coding-standard)
2525
- [Coding Standard](https://github.com/symplify/coding-standard)
26-
- [Simple PHP Doc Parser](https://github.com/symplify/simple-php-doc-parser)
2726

2827
## For Symfony
2928

composer.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
"packages/rule-doc-generator-contracts/src",
109109
"packages/rule-doc-generator/src"
110110
],
111-
"Symplify\\SimplePhpDocParser\\": "packages/simple-php-doc-parser/src",
112111
"Symplify\\Skipper\\": "packages/skipper/src",
113112
"Symplify\\SmartFileSystem\\": "packages/smart-file-system/src",
114113
"Symplify\\SymfonyStaticDumper\\": "packages/symfony-static-dumper/src",
@@ -164,7 +163,6 @@
164163
"Symplify\\PackageBuilder\\Tests\\": "packages/package-builder/tests",
165164
"Symplify\\PhpConfigPrinter\\Tests\\": "packages/php-config-printer/tests",
166165
"Symplify\\RuleDocGenerator\\Tests\\": "packages/rule-doc-generator/tests",
167-
"Symplify\\SimplePhpDocParser\\Tests\\": "packages/simple-php-doc-parser/tests",
168166
"Symplify\\Skipper\\Tests\\": "packages/skipper/tests",
169167
"Symplify\\SmartFileSystem\\Tests\\": "packages/smart-file-system/tests",
170168
"Symplify\\SymfonyStaticDumper\\Tests\\": "packages/symfony-static-dumper/tests",
@@ -197,7 +195,6 @@
197195
"symplify/phpstan-rules": "10.0.25",
198196
"symplify/rule-doc-generator": "10.0.25",
199197
"symplify/rule-doc-generator-contracts": "10.0.25",
200-
"symplify/simple-php-doc-parser": "10.0.25",
201198
"symplify/skipper": "10.0.25",
202199
"symplify/smart-file-system": "10.0.25",
203200
"symplify/symfony-static-dumper": "10.0.25",

packages/amnesia/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"symplify/phpstan-rules": "<10.0.25",
2626
"symplify/easy-testing": "<10.0.25",
2727
"symplify/rule-doc-generator-contracts": "<10.0.25",
28-
"symplify/simple-php-doc-parser": "<10.0.25",
2928
"symplify/php-config-printer": "<10.0.25",
3029
"symplify/autowire-array-parameter": "<10.0.25",
3130
"symplify/markdown-diff": "<10.0.25",

packages/astral/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,106 @@ $simpleCallableNodeTraverser->traverseNodesWithCallable($classMethod, function (
179179
});
180180
```
181181

182+
### 5. Register Config
183+
184+
Register config in your `config/config.php`:
185+
186+
```php
187+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
188+
use Symplify\Astral\ValueObject\AstralConfig;
189+
190+
return static function (ContainerConfigurator $containerConfigurator): void {
191+
$containerConfigurator->import(AstralConfig::FILE_PATH);
192+
};
193+
```
194+
195+
### 6. Usage of `SimplePhpDocParser`
196+
197+
Required services `Symplify\Astral\PhpDocParser\SimplePhpDocParser` in constructor, where you need it, and use it:
198+
199+
```php
200+
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
201+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
202+
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
203+
use Symplify\Astral\PhpDocParser\SimplePhpDocParser;
204+
205+
final class SomeClass
206+
{
207+
public function __construct(
208+
private SimplePhpDocParser $simplePhpDocParser
209+
) {
210+
}
211+
212+
public function some(): void
213+
{
214+
$docBlock = '/** @param int $name */';
215+
216+
/** @var PhpDocNode $phpDocNode */
217+
$simplePhpDocNode = $this->simplePhpDocParser->parseDocBlock($docBlock);
218+
219+
// param extras
220+
221+
/** @var TypeNode $nameParamType */
222+
$nameParamType = $simplePhpDocNode->getParamType('name');
223+
224+
/** @var ParamTagValueNode $nameParamTagValueNode */
225+
$nameParamTagValueNode = $simplePhpDocNode->getParam('name');
226+
}
227+
}
228+
```
229+
230+
## 4. Traverse Nodes with `PhpDocNodeTraverser`
231+
232+
```php
233+
use PHPStan\PhpDocParser\Ast\Node;
234+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
235+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
236+
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
237+
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
238+
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;
239+
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
240+
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\CallablePhpDocNodeVisitor;
241+
242+
$phpDocNodeTraverser = new PhpDocNodeTraverser();
243+
$phpDocNode = new PhpDocNode([new PhpDocTagNode('@var', new VarTagValueNode(new IdentifierTypeNode('string')))]);
244+
245+
// A. you can use callable to traverse
246+
$callable = function (Node $node): Node {
247+
if (! $node instanceof VarTagValueNode) {
248+
return $node;
249+
}
250+
251+
$node->type = new IdentifierTypeNode('int');
252+
return $node;
253+
};
254+
255+
$callablePhpDocNodeVisitor = new CallablePhpDocNodeVisitor($callable, null);
256+
$phpDocNodeTraverser->addPhpDocNodeVisitor($callablePhpDocNodeVisitor);
257+
258+
// B. or class that extends AbstractPhpDocNodeVisitor
259+
final class IntegerPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
260+
{
261+
/**
262+
* @return Node|int|null
263+
*/
264+
public function enterNode(Node $node)
265+
{
266+
if (! $node instanceof VarTagValueNode) {
267+
return $node;
268+
}
269+
270+
$node->type = new IdentifierTypeNode('int');
271+
return $node;
272+
}
273+
}
274+
275+
$integerPhpDocNodeVisitor = new IntegerPhpDocNodeVisitor();
276+
$phpDocNodeTraverser->addPhpDocNodeVisitor($integerPhpDocNodeVisitor);
277+
278+
// then traverse the main node
279+
$phpDocNodeTraverser->traverse($phpDocNode);
280+
```
281+
182282
## Report Issues
183283

184284
In case you are experiencing a bug or want to request a new feature head over to the [Symplify monorepo issue tracker](https://github.com/symplify/symplify/issues)

packages/astral/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"symfony/dependency-injection": "^5.4|^6.0",
1010
"symplify/smart-file-system": "^10.1",
1111
"phpstan/phpstan": "^1.4.6",
12+
"phpstan/phpdoc-parser": "^1.2",
13+
"symfony/config": "^5.4|^6.0",
1214
"nikic/php-parser": "^4.13.2",
1315
"symplify/package-builder": "^10.1",
1416
"symplify/symplify-kernel": "^10.1"
@@ -43,7 +45,6 @@
4345
"symplify/phpstan-rules": "<10.0.25",
4446
"symplify/easy-testing": "<10.0.25",
4547
"symplify/rule-doc-generator-contracts": "<10.0.25",
46-
"symplify/simple-php-doc-parser": "<10.0.25",
4748
"symplify/php-config-printer": "<10.0.25",
4849
"symplify/markdown-diff": "<10.0.25",
4950
"symplify/amnesia": "<10.0.25",

packages/astral/config/config.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use PhpParser\ConstExprEvaluator;
66
use PhpParser\NodeFinder;
7+
use PHPStan\PhpDocParser\Lexer\Lexer;
8+
use PHPStan\PhpDocParser\Parser\ConstExprParser;
9+
use PHPStan\PhpDocParser\Parser\PhpDocParser;
10+
use PHPStan\PhpDocParser\Parser\TypeParser;
711
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
812
use Symplify\Astral\PhpParser\SmartPhpParser;
913
use Symplify\Astral\PhpParser\SmartPhpParserFactory;
@@ -24,6 +28,7 @@
2428
__DIR__ . '/../src/ValueObject',
2529
__DIR__ . '/../src/NodeVisitor',
2630
__DIR__ . '/../src/PhpParser/SmartPhpParser.php',
31+
__DIR__ . '/../src/PhpDocParser/PhpDocNodeVisitor/CallablePhpDocNodeVisitor.php',
2732
]);
2833

2934
$services->set(SmartPhpParser::class)
@@ -32,4 +37,10 @@
3237
$services->set(ConstExprEvaluator::class);
3338
$services->set(TypeChecker::class);
3439
$services->set(NodeFinder::class);
40+
41+
// phpdoc parser
42+
$services->set(PhpDocParser::class);
43+
$services->set(Lexer::class);
44+
$services->set(TypeParser::class);
45+
$services->set(ConstExprParser::class);
3546
};

packages/simple-php-doc-parser/src/Contract/PhpDocNodeVisitorInterface.php renamed to packages/astral/src/PhpDocParser/Contract/PhpDocNodeVisitorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Symplify\SimplePhpDocParser\Contract;
5+
namespace Symplify\Astral\PhpDocParser\Contract;
66

77
use PHPStan\PhpDocParser\Ast\Node;
88

packages/simple-php-doc-parser/src/Exception/InvalidTraverseException.php renamed to packages/astral/src/PhpDocParser/Exception/InvalidTraverseException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Symplify\SimplePhpDocParser\Exception;
5+
namespace Symplify\Astral\PhpDocParser\Exception;
66

77
use Exception;
88

packages/simple-php-doc-parser/src/PhpDocNodeTraverser.php renamed to packages/astral/src/PhpDocParser/PhpDocNodeTraverser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
declare(strict_types=1);
44

5-
namespace Symplify\SimplePhpDocParser;
5+
namespace Symplify\Astral\PhpDocParser;
66

77
use PHPStan\PhpDocParser\Ast\Node;
8-
use Symplify\SimplePhpDocParser\Contract\PhpDocNodeVisitorInterface;
9-
use Symplify\SimplePhpDocParser\Exception\InvalidTraverseException;
10-
use Symplify\SimplePhpDocParser\PhpDocNodeVisitor\CallablePhpDocNodeVisitor;
8+
use Symplify\Astral\PhpDocParser\Contract\PhpDocNodeVisitorInterface;
9+
use Symplify\Astral\PhpDocParser\Exception\InvalidTraverseException;
10+
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\CallablePhpDocNodeVisitor;
1111

1212
/**
1313
* @api
1414
*
1515
* Mimics
1616
* https://github.com/nikic/PHP-Parser/blob/4abdcde5f16269959a834e4e58ea0ba0938ab133/lib/PhpParser/NodeTraverser.php
1717
*
18-
* @see \Symplify\SimplePhpDocParser\Tests\SimplePhpDocNodeTraverser\PhpDocNodeTraverserTest
18+
* @see \Symplify\Astral\Tests\PhpDocParser\SimplePhpDocNodeTraverser\PhpDocNodeTraverserTest
1919
*/
2020
final class PhpDocNodeTraverser
2121
{

0 commit comments

Comments
 (0)