phpstan / phpdoc-parser

Next-gen phpDoc parser with support for intersection types and generics
MIT License
1.3k stars 62 forks source link

Add README #19

Open jasny opened 5 years ago

jasny commented 5 years ago

Please add a README.md showing how the parser can be used.

ondrejmirtes commented 5 years ago

Check out PHPStan’s codebase, namely the PHPStan\PhpDoc namespace.

On Sun, 9 Sep 2018 at 20:52, Arnold Daniels notifications@github.com wrote:

Please add a README.md showing how the parser can be used.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/phpstan/phpdoc-parser/issues/19, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGZuHnhoXLJeuEeeHo-mwN2OjP9efSYks5uZWNPgaJpZM4WgZmY .

--

Ondřej Mirtes

jasny commented 5 years ago

I'm more interested in how to use the parser outside of PHPStan. To be precise; Can it be used to collect metadata as an alternative to Doctrine\Annotations?

I have a hard time deducing that from the code. It only seems to be concerned with variable types at first glance.

ondrejmirtes commented 5 years ago

What exactly do you need it to parse and what output do you expect? Show a few examples.

On Mon, 10 Sep 2018 at 10:39, Arnold Daniels notifications@github.com wrote:

I'm more interested in how to use the parser outside of PHPStan. To be precise; Can it be used to collect metadata as an alternative to Doctrine\Annotations?

I have a hard time deducing that from the code. It only seems to be concerned with variable types at first glance.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/phpstan/phpdoc-parser/issues/19#issuecomment-419833822, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGZuEPcFEv56fT3OD_Nt0-5yOnd_T-Gks5uZiVVgaJpZM4WgZmY .

--

Ondřej Mirtes

JanTvrdik commented 5 years ago

Can it be used to collect metadata as an alternative to Doctrine\Annotations?

@jasny Probably not. Depends on what metadata do you have in mind. Currently it for example does not allow backslash in annotation name.

Even if we allow backslash in annotation name, it will not understand doctrine value syntax, i.e. the following code

$input = '/**
 * @ORM\Table(name="product")
 */';

$lexer = new Lexer();
$typeParser = new TypeParser();
$constExprParser = new ConstExprParser();
$phpDocParser = new PhpDocParser($typeParser, $constExprParser);

$tokens = $lexer>tokenize($input);
$tokenIterator = new TokenIterator($tokens);
$phpDocNode = $phpDocParser ->parse($tokens);
var_dump($phpDocNode);

will result in

object(PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode)#10 (1) {
  ["children"]=>
  array(1) {
    [0]=>
    object(PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode)#9 (2) {
      ["name"]=>
      string(10) "@ORM\Table"
      ["value"]=>
      object(PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode)#8 (1) {
        ["value"]=>
        string(16) "(name="product")"
      }
    }
  }
}
jasny commented 5 years ago

I don't use the doctrine style notation, from the looks of it, it may be useful as I'm currently just using regular expressions to extract tags and information.

This example helps a bit. Still, a good README with such examples would help.

VincentLanglet commented 4 years ago

Where can we find all the phpDoc supported syntax ? Wouldn't the README perfect for this ?

Union types, intersections type, @method types, array types (since [] is actually not alway supported), closure types (is there a way to type the param and the return), value type (is there a way to type 'A'|'B', int(0...10), ...)... A full list would be great

JanTvrdik commented 4 years ago

@VincentLanglet https://github.com/phpstan/phpdoc-parser/blob/master/doc/grammars/type.abnf may help (but is not fully up to date i think)

VincentLanglet commented 4 years ago

@VincentLanglet https://github.com/phpstan/phpdoc-parser/blob/master/doc/grammars/type.abnf may help (but is not fully up to date i think)

It helps indeed, but the reader need to know the abnf syntax. I think giving example for Type and Constant would be great.

icanhazstring commented 4 years ago

README.md added with #43. Should be improved tho :)