-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PhpStan based tag parsing #343
Merged
Merged
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7192e67
First POC steps
jaapio d6c050a
WIP
jaapio ece4f5a
Introduce a Simple implementation of the TagFactory interface
jaapio c6f6e2d
Improve naming for tag factory
jaapio b66b6dc
Fix some php8+ issues
jaapio 25d6965
Cleanup and var implementation
jaapio b12a339
Add deprecations
jaapio 1dd491c
Add return tag factory
jaapio cfe9845
Add property support
jaapio e4ac07b
Add method support
jaapio 81553b5
Add default types to method tag arguments
jaapio 8d57d3d
Add deprecation to getArguments
jaapio 50bf167
Add support for constant types.
jaapio d6e90a3
Add callable parameter support
jaapio 6a92bc3
Remove type logic from this package
jaapio 1f95f3b
Codestyle fixes and static analysis
jaapio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
/* | ||
* This file is part of phpDocumentor. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @link http://phpdoc.org | ||
* | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory; | ||
|
||
use phpDocumentor\Reflection\DocBlock\Tag; | ||
use phpDocumentor\Reflection\DocBlock\TagFactory; | ||
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag; | ||
use phpDocumentor\Reflection\Types\Context as TypeContext; | ||
use PHPStan\PhpDocParser\Lexer\Lexer; | ||
use PHPStan\PhpDocParser\Parser\ConstExprParser; | ||
use PHPStan\PhpDocParser\Parser\PhpDocParser; | ||
use PHPStan\PhpDocParser\Parser\TokenIterator; | ||
use PHPStan\PhpDocParser\Parser\TypeParser; | ||
|
||
/** | ||
* Factory class creating tags using phpstan's parser | ||
* | ||
* This class uses {@see PHPStanFactory} implementations to create tags | ||
* from the ast of the phpstan docblock parser. | ||
* | ||
* @internal This class is not part of the BC promise of this library. | ||
*/ | ||
class AbstractPHPStanFactory implements TagFactory | ||
{ | ||
private PhpDocParser $parser; | ||
private Lexer $lexer; | ||
private array $factories; | ||
|
||
public function __construct(PHPStanFactory ...$factories) | ||
{ | ||
$this->lexer = new Lexer(); | ||
$constParser = new ConstExprParser(); | ||
$this->parser = new PhpDocParser(new TypeParser($constParser), $constParser); | ||
$this->factories = $factories; | ||
} | ||
|
||
public function addParameter(string $name, $value): void | ||
{ | ||
// TODO: Implement addParameter() method. | ||
} | ||
|
||
public function create(string $tagLine, ?TypeContext $context = null): Tag | ||
{ | ||
$tokens = $this->lexer->tokenize($tagLine); | ||
$ast = $this->parser->parseTag(new TokenIterator($tokens)); | ||
|
||
foreach ($this->factories as $factory) { | ||
if ($factory->supports($ast, $context)) { | ||
return $factory->create($ast, $context); | ||
} | ||
} | ||
|
||
return InvalidTag::create( | ||
$ast->name, | ||
(string) $ast->value | ||
); | ||
} | ||
|
||
public function addService(object $service): void | ||
{ | ||
// TODO: Implement addService() method. | ||
} | ||
|
||
public function registerTagHandler(string $tagName, string $handler): void | ||
{ | ||
// TODO: Implement registerTagHandler() method. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory; | ||
|
||
use phpDocumentor\Reflection\DocBlock\Tag; | ||
use phpDocumentor\Reflection\Types\Context; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; | ||
|
||
interface PHPStanFactory | ||
{ | ||
public function create(PhpDocTagNode $node, Context $context): Tag; | ||
|
||
public function supports(PhpDocTagNode $node, ?Context $context): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Reflection\DocBlock\Tags\Factory; | ||
|
||
use phpDocumentor\Reflection\DocBlock\DescriptionFactory; | ||
use phpDocumentor\Reflection\DocBlock\Tag; | ||
use phpDocumentor\Reflection\DocBlock\Tags\Param; | ||
use phpDocumentor\Reflection\Types\Context; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; | ||
use Webmozart\Assert\Assert; | ||
|
||
use function trim; | ||
|
||
/** | ||
* @internal This class is not part of the BC promise of this library. | ||
*/ | ||
final class ParamFactory implements PHPStanFactory | ||
{ | ||
private TypeFactory $typeFactory; | ||
private DescriptionFactory $descriptionFactory; | ||
|
||
public function __construct(TypeFactory $typeFactory, DescriptionFactory $descriptionFactory) | ||
{ | ||
$this->typeFactory = $typeFactory; | ||
$this->descriptionFactory = $descriptionFactory; | ||
} | ||
|
||
public function create(PhpDocTagNode $node, Context $context): Tag | ||
{ | ||
$tagValue = $node->value; | ||
Assert::isInstanceOf($tagValue, ParamTagValueNode::class); | ||
|
||
return new Param( | ||
trim($tagValue->parameterName, '$'), | ||
$this->typeFactory->createType($tagValue->type, $context), | ||
$tagValue->isVariadic, | ||
$this->descriptionFactory->create($tagValue->description, $context), | ||
$tagValue->isReference | ||
); | ||
} | ||
|
||
public function supports(PhpDocTagNode $node, ?Context $context): bool | ||
{ | ||
return $node->value instanceof ParamTagValueNode; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the version drop required ? could we drop less of them ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would you like to support end-of-life versions of PHP? I think for people using old versions of PHP the older library versions will work.
Tools using this library can easily run on newer versions while the applications being processed can be on old PHP versions.
But I'm open to see if we can support PHP 7.2 as well. however phpstan requires > 7.4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because that would mean that I also need to drop PHP versions. I am not saying this is bad but if it can be avoided because x.y is supported by the lib you add then let's not drop them ?