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

Commit

Permalink
[SimplePhpDocParser] Merge to Astral package, as close related purpose (
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Feb 21, 2022
1 parent fc72eab commit 3354f27
Show file tree
Hide file tree
Showing 67 changed files with 171 additions and 351 deletions.
1 change: 0 additions & 1 deletion .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ jobs:
packages/easy-ci/bin/easy-ci check-active-class packages/phpstan-rules --ansi
packages/easy-ci/bin/easy-ci check-active-class packages/rule-doc-generator --ansi
packages/easy-ci/bin/easy-ci check-active-class packages/rule-doc-generator-contracts --ansi
packages/easy-ci/bin/easy-ci check-active-class packages/simple-php-doc-parser --ansi
packages/easy-ci/bin/easy-ci check-active-class packages/skipper --ansi
packages/easy-ci/bin/easy-ci check-active-class packages/smart-file-system --ansi
packages/easy-ci/bin/easy-ci check-active-class packages/symfony-static-dumper --ansi
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ You'll find all packages in [`/packages`](/packages) directory. Here is a brief

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

## For Symfony

Expand Down
3 changes: 0 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@
"packages/rule-doc-generator-contracts/src",
"packages/rule-doc-generator/src"
],
"Symplify\\SimplePhpDocParser\\": "packages/simple-php-doc-parser/src",
"Symplify\\Skipper\\": "packages/skipper/src",
"Symplify\\SmartFileSystem\\": "packages/smart-file-system/src",
"Symplify\\SymfonyStaticDumper\\": "packages/symfony-static-dumper/src",
Expand Down Expand Up @@ -164,7 +163,6 @@
"Symplify\\PackageBuilder\\Tests\\": "packages/package-builder/tests",
"Symplify\\PhpConfigPrinter\\Tests\\": "packages/php-config-printer/tests",
"Symplify\\RuleDocGenerator\\Tests\\": "packages/rule-doc-generator/tests",
"Symplify\\SimplePhpDocParser\\Tests\\": "packages/simple-php-doc-parser/tests",
"Symplify\\Skipper\\Tests\\": "packages/skipper/tests",
"Symplify\\SmartFileSystem\\Tests\\": "packages/smart-file-system/tests",
"Symplify\\SymfonyStaticDumper\\Tests\\": "packages/symfony-static-dumper/tests",
Expand Down Expand Up @@ -197,7 +195,6 @@
"symplify/phpstan-rules": "10.0.25",
"symplify/rule-doc-generator": "10.0.25",
"symplify/rule-doc-generator-contracts": "10.0.25",
"symplify/simple-php-doc-parser": "10.0.25",
"symplify/skipper": "10.0.25",
"symplify/smart-file-system": "10.0.25",
"symplify/symfony-static-dumper": "10.0.25",
Expand Down
1 change: 0 additions & 1 deletion packages/amnesia/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"symplify/phpstan-rules": "<10.0.25",
"symplify/easy-testing": "<10.0.25",
"symplify/rule-doc-generator-contracts": "<10.0.25",
"symplify/simple-php-doc-parser": "<10.0.25",
"symplify/php-config-printer": "<10.0.25",
"symplify/autowire-array-parameter": "<10.0.25",
"symplify/markdown-diff": "<10.0.25",
Expand Down
100 changes: 100 additions & 0 deletions packages/astral/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,106 @@ $simpleCallableNodeTraverser->traverseNodesWithCallable($classMethod, function (
});
```

### 5. Register Config

Register config in your `config/config.php`:

```php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\Astral\ValueObject\AstralConfig;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(AstralConfig::FILE_PATH);
};
```

### 6. Usage of `SimplePhpDocParser`

Required services `Symplify\Astral\PhpDocParser\SimplePhpDocParser` in constructor, where you need it, and use it:

```php
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use Symplify\Astral\PhpDocParser\SimplePhpDocParser;

final class SomeClass
{
public function __construct(
private SimplePhpDocParser $simplePhpDocParser
) {
}

public function some(): void
{
$docBlock = '/** @param int $name */';

/** @var PhpDocNode $phpDocNode */
$simplePhpDocNode = $this->simplePhpDocParser->parseDocBlock($docBlock);

// param extras

/** @var TypeNode $nameParamType */
$nameParamType = $simplePhpDocNode->getParamType('name');

/** @var ParamTagValueNode $nameParamTagValueNode */
$nameParamTagValueNode = $simplePhpDocNode->getParam('name');
}
}
```

## 4. Traverse Nodes with `PhpDocNodeTraverser`

```php
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\CallablePhpDocNodeVisitor;

$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNode = new PhpDocNode([new PhpDocTagNode('@var', new VarTagValueNode(new IdentifierTypeNode('string')))]);

// A. you can use callable to traverse
$callable = function (Node $node): Node {
if (! $node instanceof VarTagValueNode) {
return $node;
}

$node->type = new IdentifierTypeNode('int');
return $node;
};

$callablePhpDocNodeVisitor = new CallablePhpDocNodeVisitor($callable, null);
$phpDocNodeTraverser->addPhpDocNodeVisitor($callablePhpDocNodeVisitor);

// B. or class that extends AbstractPhpDocNodeVisitor
final class IntegerPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
{
/**
* @return Node|int|null
*/
public function enterNode(Node $node)
{
if (! $node instanceof VarTagValueNode) {
return $node;
}

$node->type = new IdentifierTypeNode('int');
return $node;
}
}

$integerPhpDocNodeVisitor = new IntegerPhpDocNodeVisitor();
$phpDocNodeTraverser->addPhpDocNodeVisitor($integerPhpDocNodeVisitor);

// then traverse the main node
$phpDocNodeTraverser->traverse($phpDocNode);
```

## Report Issues

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)
Expand Down
3 changes: 2 additions & 1 deletion packages/astral/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"symfony/dependency-injection": "^5.4|^6.0",
"symplify/smart-file-system": "^10.1",
"phpstan/phpstan": "^1.4.6",
"phpstan/phpdoc-parser": "^1.2",
"symfony/config": "^5.4|^6.0",
"nikic/php-parser": "^4.13.2",
"symplify/package-builder": "^10.1",
"symplify/symplify-kernel": "^10.1"
Expand Down Expand Up @@ -43,7 +45,6 @@
"symplify/phpstan-rules": "<10.0.25",
"symplify/easy-testing": "<10.0.25",
"symplify/rule-doc-generator-contracts": "<10.0.25",
"symplify/simple-php-doc-parser": "<10.0.25",
"symplify/php-config-printer": "<10.0.25",
"symplify/markdown-diff": "<10.0.25",
"symplify/amnesia": "<10.0.25",
Expand Down
11 changes: 11 additions & 0 deletions packages/astral/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use PhpParser\ConstExprEvaluator;
use PhpParser\NodeFinder;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TypeParser;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\Astral\PhpParser\SmartPhpParser;
use Symplify\Astral\PhpParser\SmartPhpParserFactory;
Expand All @@ -24,6 +28,7 @@
__DIR__ . '/../src/ValueObject',
__DIR__ . '/../src/NodeVisitor',
__DIR__ . '/../src/PhpParser/SmartPhpParser.php',
__DIR__ . '/../src/PhpDocParser/PhpDocNodeVisitor/CallablePhpDocNodeVisitor.php',
]);

$services->set(SmartPhpParser::class)
Expand All @@ -32,4 +37,10 @@
$services->set(ConstExprEvaluator::class);
$services->set(TypeChecker::class);
$services->set(NodeFinder::class);

// phpdoc parser
$services->set(PhpDocParser::class);
$services->set(Lexer::class);
$services->set(TypeParser::class);
$services->set(ConstExprParser::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser\Contract;
namespace Symplify\Astral\PhpDocParser\Contract;

use PHPStan\PhpDocParser\Ast\Node;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser\Exception;
namespace Symplify\Astral\PhpDocParser\Exception;

use Exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser;
namespace Symplify\Astral\PhpDocParser;

use PHPStan\PhpDocParser\Ast\Node;
use Symplify\SimplePhpDocParser\Contract\PhpDocNodeVisitorInterface;
use Symplify\SimplePhpDocParser\Exception\InvalidTraverseException;
use Symplify\SimplePhpDocParser\PhpDocNodeVisitor\CallablePhpDocNodeVisitor;
use Symplify\Astral\PhpDocParser\Contract\PhpDocNodeVisitorInterface;
use Symplify\Astral\PhpDocParser\Exception\InvalidTraverseException;
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\CallablePhpDocNodeVisitor;

/**
* @api
*
* Mimics
* https://github.com/nikic/PHP-Parser/blob/4abdcde5f16269959a834e4e58ea0ba0938ab133/lib/PhpParser/NodeTraverser.php
*
* @see \Symplify\SimplePhpDocParser\Tests\SimplePhpDocNodeTraverser\PhpDocNodeTraverserTest
* @see \Symplify\Astral\Tests\PhpDocParser\SimplePhpDocNodeTraverser\PhpDocNodeTraverserTest
*/
final class PhpDocNodeTraverser
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser\PhpDocNodeVisitor;
namespace Symplify\Astral\PhpDocParser\PhpDocNodeVisitor;

use PHPStan\PhpDocParser\Ast\Node;
use Symplify\SimplePhpDocParser\Contract\PhpDocNodeVisitorInterface;
use Symplify\Astral\PhpDocParser\Contract\PhpDocNodeVisitorInterface;

/**
* Inspired by https://github.com/nikic/PHP-Parser/blob/master/lib/PhpParser/NodeVisitorAbstract.php
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser\PhpDocNodeVisitor;
namespace Symplify\Astral\PhpDocParser\PhpDocNodeVisitor;

use PHPStan\PhpDocParser\Ast\Node;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser\PhpDocNodeVisitor;
namespace Symplify\Astral\PhpDocParser\PhpDocNodeVisitor;

use PHPStan\PhpDocParser\Ast\Node;
use Symplify\SimplePhpDocParser\ValueObject\PhpDocAttributeKey;
use Symplify\Astral\PhpDocParser\ValueObject\PhpDocAttributeKey;

/**
* @api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser\PhpDocNodeVisitor;
namespace Symplify\Astral\PhpDocParser\PhpDocNodeVisitor;

use PHPStan\PhpDocParser\Ast\Node;
use Symplify\SimplePhpDocParser\ValueObject\PhpDocAttributeKey;
use Symplify\Astral\PhpDocParser\ValueObject\PhpDocAttributeKey;

/**
* @api
*
* Mimics https://github.com/nikic/PHP-Parser/blob/master/lib/PhpParser/NodeVisitor/ParentConnectingVisitor.php
*
* @see \Symplify\SimplePhpDocParser\Tests\PhpDocNodeVisitor\ParentConnectingPhpDocNodeVisitorTest
* @see \Symplify\Astral\Tests\PhpDocParser\PhpDocNodeVisitor\ParentConnectingPhpDocNodeVisitorTest
*/
final class ParentConnectingPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser;
namespace Symplify\Astral\PhpDocParser;

use PhpParser\Comment\Doc;
use PhpParser\Node;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use Symplify\SimplePhpDocParser\ValueObject\Ast\PhpDoc\SimplePhpDocNode;
use Symplify\Astral\PhpDocParser\ValueObject\Ast\PhpDoc\SimplePhpDocNode;

/**
* @see \Symplify\SimplePhpDocParser\Tests\SimplePhpDocParser\SimplePhpDocParserTest
* @see \Symplify\Astral\Tests\PhpDocParser\SimplePhpDocParser\SimplePhpDocParserTest
*/
final class SimplePhpDocParser
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser\StaticFactory;
namespace Symplify\Astral\PhpDocParser\StaticFactory;

use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TypeParser;
use Symplify\SimplePhpDocParser\SimplePhpDocParser;
use Symplify\Astral\PhpDocParser\SimplePhpDocParser;

/**
* @api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser\ValueObject\Ast\PhpDoc;
namespace Symplify\Astral\PhpDocParser\ValueObject\Ast\PhpDoc;

use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser\ValueObject;
namespace Symplify\Astral\PhpDocParser\ValueObject;

final class PhpDocAttributeKey
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

declare(strict_types=1);

namespace Symplify\SimplePhpDocParser\Tests\HttpKernel;
namespace Symplify\Astral\Tests\PhpDocParser\HttpKernel;

use Psr\Container\ContainerInterface;
use Symplify\SimplePhpDocParser\ValueObject\SimplePhpDocParserConfig;
use Symplify\Astral\ValueObject\AstralConfig;
use Symplify\SymplifyKernel\HttpKernel\AbstractSymplifyKernel;

final class SimplePhpDocParserKernel extends AbstractSymplifyKernel
{
public function createFromConfigs(array $configFiles): ContainerInterface
{
$configFiles[] = SimplePhpDocParserConfig::FILE_PATH;
$configFiles[] = AstralConfig::FILE_PATH;
return $this->create($configFiles);
}
}
Loading

0 comments on commit 3354f27

Please sign in to comment.