symplify / astral
Toolking for smart daily work with AST
Fund package maintenance!
tomasvotruba
www.paypal.me/rectorphp
Installs: 2 777 352
Dependents: 6
Suggesters: 0
Security: 0
Stars: 25
Watchers: 2
Forks: 1
Type:phpstan-extension
Requires
- php: >=8.0
- nette/utils: ^3.2
- nikic/php-parser: ^4.14.0
- phpstan/phpdoc-parser: ^1.7.0
- phpstan/phpstan: ^1.8.1
- symplify/package-builder: ^11.1.5
Requires (Dev)
- phpunit/phpunit: ^9.5.21
- symfony/config: ^6.0
- symfony/dependency-injection: ^6.0
- symplify/easy-testing: ^11.1.5
- symplify/symplify-kernel: ^11.1.5
Conflicts
- symplify/autowire-array-parameter: <11.1.5
- symplify/coding-standard: <11.1.5
- symplify/composer-json-manipulator: <11.1.5
- symplify/config-transformer: <11.1.5
- symplify/easy-ci: <11.1.5
- symplify/easy-coding-standard: <11.1.5
- symplify/easy-parallel: <11.1.5
- symplify/easy-testing: <11.1.5
- symplify/monorepo-builder: <11.1.5
- symplify/php-config-printer: <11.1.5
- symplify/phpstan-extensions: <11.1.5
- symplify/phpstan-rules: <11.1.5
- symplify/rule-doc-generator: <11.1.5
- symplify/rule-doc-generator-contracts: <11.1.5
- symplify/skipper: <11.1.5
- symplify/smart-file-system: <11.1.5
- symplify/symfony-static-dumper: <11.1.5
- symplify/symplify-kernel: <11.1.5
- symplify/vendor-patches: <11.1.5
- dev-main / 11.2.x-dev
- 11.1.5
- 11.1.4
- 11.1.3
- 11.1.2
- 11.1.1
- 11.1.0
- 11.0.9
- 11.0.8
- 11.0.7
- 11.0.6
- 11.0.5
- 11.0.4
- 11.0.3
- 11.0.2
- 11.0.1
- 11.0.0
- 10.3.3
- 10.3.2
- 10.3.1
- 10.3.0
- 10.2.11
- 10.2.10
- 10.2.9
- 10.2.8
- 10.2.7
- 10.2.6
- 10.2.5
- 10.2.4
- 10.2.3
- 10.2.2
- 10.2.1
- 10.2.0
- 10.1.4
- 10.1.3
- 10.1.2
- 10.1.1
- 10.1.0
- 10.0.25
- 10.0.24
- 10.0.23
- 10.0.22
- 10.0.21
- 10.0.20
- 10.0.19
- 10.0.18
- 10.0.17
- 10.0.16
- 10.0.15
- 10.0.14
- 10.0.13
- 10.0.12
- 10.0.11
- 10.0.10
- 10.0.9
- 10.0.8
- 10.0.7
- 10.0.6
- 10.0.5
- 10.0.4
- 10.0.3
- 10.0.2
- 10.0.1
- 10.0.0
- 10.0.0-beta17
- 10.0.0-beta16
- 10.0.0-beta15
- 10.0.0-beta14
- 10.0.0-beta13
- 10.0.0-beta12
- 10.0.0-beta11
- 10.0.0-beta10
- 10.0.0-beta9
- 10.0.0-beta8
- 10.0.0-beta7
- 10.0.0-beta6
- 10.0.0-beta5
- 10.0.0-beta4
- 10.0.0-beta3
- 10.0.0-beta2
- 10.0.0-beta1
- 9.4.70
- 9.4.69
- 9.4.68
- 9.4.67
- 9.4.66
- 9.4.65
- 9.4.64
- 9.4.63
- 9.4.62
- 9.4.61
- 9.4.60
- 9.4.59
- 9.4.58
- 9.4.57
- 9.4.56
- 9.4.55
- 9.4.54
- 9.4.53
- 9.4.52
- 9.4.51
- 9.4.50
- 9.4.49
- 9.4.48
- 9.4.47
- 9.4.46
- 9.4.45
- 9.4.44
- 9.4.43
- 9.4.42
- 9.4.41
- 9.4.40
- 9.4.39
- 9.4.38
- 9.4.37
- 9.4.36
- 9.4.35
- 9.4.34
- 9.4.33
- 9.4.32
- 9.4.31
- 9.4.30
- 9.4.29
- 9.4.28
- 9.4.27
- 9.4.26
- 9.4.25
- 9.4.24
- 9.4.23
- 9.4.22
- 9.4.21
- 9.4.20
- 9.4.19
- 9.4.18
- 9.4.17
- 9.4.16
- 9.4.15
- 9.4.14
- 9.4.13
- 9.4.12
- 9.4.11
- 9.4.10
- 9.4.9
- 9.4.8
- 9.4.7
- 9.4.6
- 9.4.5
- 9.4.4
- 9.4.3
- 9.4.2
- v9.4.1
- v9.4.0
- v9.3.27
- v9.3.26
- v9.3.25
- v9.3.24
- v9.3.23
- v9.3.22
- v9.3.21
- v9.3.20
- v9.3.19
- v9.3.18
- v9.3.17
- v9.3.16
- v9.3.15
- v9.3.14
- v9.3.13
- v9.3.12
- v9.3.11
- v9.3.10
- v9.3.8
- v9.3.6
- v9.3.5
- v9.3.4
- v9.3.3
- v9.3.1
- v9.3.0
- v9.2.24
- v9.2.23
- v9.2.22
- v9.2.21
- v9.2.20
- v9.2.19
- v9.2.18
- v9.2.17
- v9.2.16
- v9.2.15
- v9.2.14
- v9.2.13
- v9.2.12
- v9.2.11
- v9.2.10
- v9.2.9
- v9.2.8
- v9.2.7
- v9.2.6
- v9.2.5
- v9.2.4
- v9.2.3
- v9.2.2
- 9.2.1
- 9.2.0
- 9.1.9
- 9.1.8
- 9.1.7
- 9.1.6
- 9.1.5
- 9.1.4
- 9.1.3
- 9.1.1
- 9.1.0
- 9.0.50
- 9.0.49
- 9.0.48
- 9.0.47
- 9.0.46
- 9.0.45
- 9.0.44
- 9.0.43
- 9.0.42
- 9.0.41
- 9.0.40
- 9.0.39
- 9.0.38
- 9.0.37
- 9.0.36
- 9.0.35
- 9.0.34
- 9.0.33
- 9.0.32
- 9.0.31
- 9.0.30
- 9.0.29
- 9.0.28
- 9.0.27
- 9.0.26
- 9.0.25
- 9.0.24
- 9.0.23
- 9.0.22
- 9.0.21
This package is auto-updated.
Last update: 2022-08-29 21:45:23 UTC
README
Install
composer require symplify/astral
Add to Symfony Project
Register package in config/config.php
:
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symplify\Astral\ValueObject\AstralConfig::FILE_PATH; return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->import(AstralConfig::FILE_PATH); };
Add to PHPStan Rules
Include in your phpstan.neon
:
includes: - vendor/symplify/astral/config/services.neon
Usage
1. Resolve Value of Node
$value = 1000;
How can we get the 1000
value?
use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Scalar\LNumber; use PHPStan\Analyser\Scope; use Symplify\Astral\NodeValue\NodeValueResolver; final class SomeRule { public function __construct( // PHP 8.0 promoted property syntax private NodeValueResolver $nodeValueResolver ) { } public function process(Node $node, Scope $scope): void { if ($node instanceof Assign && $node->expr instanceof LNumber) { $resolvedValue = $this->nodeValueResolver->resolve($node->expr, $scope->getFile()); } } }
Work for static expressions like these:
$value = 'Hey'; // "Hey" SomeClass::class; // "SomeClass" class SomeClass { public const VALUE = 'different'; } SomeClass::VALUE; // "different" __DIR__; // realpath of the __DIR__ in its place
2. Unique *Builder
Classes
Native PhpParser node class and builder class share the same short class name.
use PhpParser\Builder\Class_; use PhpParser\Node\Stmt\Class_; $class = new Class_('ClassName'); $class = $class->getNode();
This confuses IDE and lead to wrong classes being used as type hints. To avoid that, this package provides *Builder
names:
use Symplify\Astral\ValueObject\NodeBuilder\ClassBuilder; $classBuilder = new ClassBuilder('some_class'); $class = $classBuilder->getNode();
3. Traverse Nodes with Simple Callback
Working with nodes is based on traversing each one of them. You can use native NodeVisitor
and NodeTraverses
. But that requires to create at least 2 objects, to connect them and call them.
What if we need just a small traverse right in this method? Service SimpleCallableNodeTraverser
to the rescue:
use PhpParser\Node; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\ClassMethod; use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser; /** @var ClassMethod $classMethod */ $classMethod = '...'; $simpleCallableNodeTraverser = new SimpleCallableNodeTraverser(); $simpleCallableNodeTraverser->traverseNodesWithCallable($classMethod, function (Node $node) { if (! $node instanceof String_) { return null; } $node->value = 'changed name'; return $node; });
4. Register Config
Register config in your config/config.php
:
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symplify\Astral\ValueObject\AstralConfig; return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->import(AstralConfig::FILE_PATH); };
5. Usage of SimplePhpDocParser
Required services Symplify\Astral\PhpDocParser\SimplePhpDocParser
in constructor, where you need it, and use it:
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'); } }
6. Traverse Nodes with PhpDocNodeTraverser
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 { public function enterNode(Node $node): Node|int|null { 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
Contribute
The sources of this package are contained in the Symplify monorepo. We welcome contributions for this package on symplify/symplify.