Skip to content
This repository has been archived by the owner on Mar 20, 2022. It is now read-only.

New Implementation #14

Open
wants to merge 63 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
f2a94a0
Start again
dantleech Jan 20, 2021
a816265
Initial
dantleech Jan 20, 2021
7c803ed
Progress
dantleech Jan 20, 2021
817a5ee
open, close and leading phpdoc
dantleech Jan 20, 2021
afe8a54
Return collection
dantleech Jan 20, 2021
1e9b704
Parser test
dantleech Jan 20, 2021
efa1e2a
Updated README
dantleech Jan 20, 2021
6c5ea45
Parse lists
dantleech Jan 21, 2021
11cb6c1
Printer tests OK, docblock start / end broken
dantleech Jan 22, 2021
a36dae5
Fixed tests
dantleech Jan 22, 2021
a507778
Refactored
dantleech Jan 22, 2021
28459e8
Remove dump
dantleech Jan 23, 2021
38e0a6e
Add bench
dantleech Jan 23, 2021
97f060d
Use dev-master
dantleech Jan 23, 2021
f54f646
Experiment with preg_match_all lexer
dantleech Jan 23, 2021
8e8ea0b
Added phpstan comparison benchmark
dantleech Jan 23, 2021
339ce22
Revert "Experiment with preg_match_all lexer"
dantleech Jan 23, 2021
9cf4db7
Fixed parser test
dantleech Jan 23, 2021
c7797e5
Optimisations
dantleech Jan 23, 2021
61d6dbf
Performance optimization
dantleech Jan 23, 2021
ba68f54
Use token properties
dantleech Jan 23, 2021
a7e599b
Access current as property
dantleech Jan 23, 2021
9da77bf
Add type hint
dantleech Jan 23, 2021
3aae081
Create pattern in constructor
dantleech Jan 23, 2021
27cf88b
Support param text
dantleech Jan 23, 2021
4670c87
Remove skip docblock tokens
dantleech Jan 23, 2021
d01f5e9
Add ignored tests
dantleech Jan 23, 2021
b9258c9
support unknown tag
dantleech Jan 23, 2021
f1b65b4
Support deprecated tag
dantleech Jan 23, 2021
c6bdf9e
Support method node
dantleech Jan 23, 2021
4b02882
Support nullable
dantleech Jan 23, 2021
38ee727
Support property
dantleech Jan 23, 2021
034f18b
Support return node
dantleech Jan 23, 2021
657f344
Support fully qualfiied names
dantleech Jan 23, 2021
a264866
Remove duplicate check
dantleech Jan 23, 2021
15382fb
Use switch (doesn't make any perf diff)
dantleech Jan 23, 2021
3c0abd6
Support NULL and return text
dantleech Jan 23, 2021
8cf1de2
Support method parameters
dantleech Jan 23, 2021
0e38ec9
Support for default method values
dantleech Jan 23, 2021
28df9f5
Updated phpcs
dantleech Jan 24, 2021
eccad5b
Apply CS fixes
dantleech Jan 24, 2021
c3ffc05
Add lexer bench
dantleech Jan 24, 2021
0bfc821
Adding traversal API
dantleech Jan 25, 2021
3f6fdee
Added test for method node
dantleech Jan 25, 2021
9d1e711
Aiming towards 100% isomorphism
dantleech Jan 25, 2021
104e91f
Method node is isomorphic
dantleech Jan 25, 2021
0baa83b
Param node
dantleech Jan 25, 2021
5cd1571
Return node
dantleech Jan 25, 2021
9e6a733
Finish adding node tests
dantleech Jan 26, 2021
c616bcb
Namespaced tag nodes
dantleech Jan 26, 2021
eaec94d
Migrate printer tests
dantleech Jan 26, 2021
3bc0fbf
Add property traversable support
dantleech Jan 27, 2021
013368c
Improving API
dantleech Jan 27, 2021
1384d0b
Apply CS fix
dantleech Jan 27, 2021
aa7e948
Added more API methods
dantleech Jan 27, 2021
878745c
Updated composer, removed baseline
dantleech Jan 27, 2021
0ffbbe0
Render text
dantleech Jan 27, 2021
59af98e
Add API for tags to docblock
dantleech Jan 27, 2021
eb60d2c
FIx docblock get tag
dantleech Jan 31, 2021
91d2d92
Accessor for parameters
dantleech Jan 31, 2021
6006420
Removed core PHP docs from benchmark
dantleech Jan 27, 2021
beae269
Prop assertion
dantleech Jan 31, 2021
6d4b18e
Support for return $this
dantleech Jan 31, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ $finder = PhpCsFixer\Finder::create()
->in('lib')
->in('tests')
->exclude([
'tests/Workspace'
'tests/Workspace',
])
;

Expand All @@ -13,6 +13,11 @@ return PhpCsFixer\Config::create()
'@PSR2' => true,
'no_unused_imports' => true,
'array_syntax' => ['syntax' => 'short'],
'void_return' => true,
'ordered_class_elements' => true,
'single_quote' => true,
'heredoc_indentation' => true,
'global_namespace_import' => true,
])
->setFinder($finder)
;
Expand Down
23 changes: 4 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
Docblock Parser
===============

Sub-standard docblock parser.
PHP Docblock parser with the following aims:

```php
$docblock = (new DocblockFactory())->create('/** @var Foobar */');
$vars = $docblock->tags()->byName('var');

foreach ($vars as $var) {
$var->type();
$var->varName();
}
```

Why?
----

There is already a [standards-compliant
library](https://github.com/phpDocumentor/ReflectionDocBlock) for
PHP-Documentor, however it is coupled to the PHPDocumentor type reflection
library. This library only cares about parsing docblocks badly for
[Phpactor](https://github.com/phpactor/phpactor).
- Fast: has to be fast enough for IDE analysis.
- Isomorphic: can be transformed back to the original text representation.
- Positional: the positions of nodes are captured.

Contributing
------------
Expand Down
15 changes: 13 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
"php": "^7.3"
},
"require-dev": {
"phpbench/phpbench": "dev-master",
"ergebnis/composer-normalize": "^2.0",
"friendsofphp/php-cs-fixer": "^2.17",
"phpstan/phpstan": "~0.12.0",
"phpunit/phpunit": "^9.0",
"phpspec/prophecy-phpunit": "^2.0"
"phpspec/prophecy-phpunit": "^2.0",
"symfony/var-dumper": "^5.2",
"phpstan/phpdoc-parser": "^0.4.10",
"jetbrains/phpstorm-stubs": "^2020.2"
},
"extra": {
"branch-alias": {
Expand All @@ -35,5 +39,12 @@
}
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"scripts": {
"integrate": [
"vendor/bin/phpunit",
"vendor/bin/php-cs-fixer fix --allow-risky=yes",
"vendor/bin/phpstan analyse"
]
}
}
75 changes: 75 additions & 0 deletions lib/Ast/Docblock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Phpactor\Docblock\Ast;

use Generator;

class Docblock extends Node
{
protected const CHILD_NAMES = [
'children'
];

/**
* @var ElementList
*/
public $children = [];

/**
* @param Element[] $children
*/
public function __construct(array $children)
{
$this->children = new ElementList($children);
}

/**
* @param class-string $tagFqn
*/
public function hasTag(string $tagFqn): bool
{
foreach ($this->tags() as $tag) {
if ($tag instanceof $tagFqn) {
return true;
}
}

return false;
}

/**
* @template T
* @param class-string<T>|null $tagFqn
* @return Generator<T>
*/
public function tags(?string $tagFqn = null): Generator
{
foreach ($this->children as $child) {
if ($tagFqn && $child instanceof $tagFqn) {
yield $child;
continue;
}
if (!$tagFqn && $child instanceof TagNode) {
yield $child;
continue;
}
}
}

public function prose(): string
{
return trim(implode('', array_map(function (Element $token): string {
if ($token instanceof Token) {
if (in_array($token->type, [
Token::T_PHPDOC_OPEN,
Token::T_PHPDOC_CLOSE,
Token::T_PHPDOC_LEADING
])) {
return '';
}
return $token->value;
}
return '';
}, iterator_to_array($this->children, false))));
}
}
21 changes: 21 additions & 0 deletions lib/Ast/Element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Phpactor\Docblock\Ast;

interface Element
{
/**
* Return the string aggregation of all tokens in this element
*/
public function toString(): string;

/**
* Return the start byte offset, starting at 0
*/
public function start(): int;

/**
* Return the end byte offset, starting at 0
*/
public function end(): int;
}
47 changes: 47 additions & 0 deletions lib/Ast/ElementList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Phpactor\Docblock\Ast;

use ArrayIterator;
use Iterator;
use IteratorAggregate;

/**
* @template T of Element
* @implements IteratorAggregate<int,T>
*/
class ElementList extends Node implements IteratorAggregate
{
protected const CHILD_NAMES = [
'elements',
];

/**
* @var T[]
*/
public $elements;

/**
* @param T[] $elements
*/
public function __construct(array $elements)
{
$this->elements = $elements;
}

/**
* @return ArrayIterator<int,T>
*/
public function getIterator(): Iterator
{
return new ArrayIterator($this->elements);
}

/**
* @return Element[]
*/
public function toArray(): array
{
return $this->elements;
}
}
Loading