[READ-ONLY] Slim wrapper around phpstan/phpdoc-parser with format preserving printer
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config move /config to packages root Jan 5, 2019
src TYPO: In 'TypeNodeToStringsConvertor' refactored word 'convertor' to … Jan 21, 2019
tests TYPO: In 'TypeNodeToStringsConvertor' refactored word 'convertor' to … Jan 21, 2019
.gitattributes update few more references, rename 'Phpdoc' namespace to 'PhpDoce Apr 24, 2018
.gitignore update few more references, rename 'Phpdoc' namespace to 'PhpDoce Apr 24, 2018
.travis.yml travis: drop unused package runs Jan 18, 2019
LICENSE update few more references, rename 'Phpdoc' namespace to 'PhpDoce Apr 24, 2018
README.md
composer.json
phpunit.xml phpunit.xml: fix missing suite name Jun 2, 2018

README.md

Better PhpDoc Parser

Build Status Downloads

Wrapper around phpstan/phpdoc-parser that adds format preserving printer.

When do We Need Format Preserving Printer?

Symplify\CodingStandard and Rector need to modify docblock and put it back in correct format. Packages on open-source market often put own spacing, or formats of specific tags. Goal of this package is preserve origin docblock format.

Thanks for inspiration in Format Preserving Printer feature in nikic/php-parser.

Install

composer require symplify/better-phpdoc-parser

Usage

Register services in your Symfony config:

# services.yml
imports:
    - { resource: 'vendor/symplify/better-phpdoc-parser/config/services.yml' }

or register the needed services from services.yml in config of your other framework.

<?php

use Symplify\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Symplify\BetterPhpDocParser\Printer\PhpDocInfoPrinter;

class SomeClass
{
    public function __construct(PhpDocInfoFactory $phpDocInfoFactory, PhpDocInfoPrinter $phpDocInfoPrinter)
    {
        $this->phpDocInfoFactory = $phpDocInfoFactory;
        $this->phpDocInfoPrinter = $phpDocInfoPrinter;
    }

    public function changeDocBlockAndPrintItBack(): string
    {
        $docComment = '/**    @var Type $variable    */';

        $phpDocInfo = $this->phpDocInfoFactory->createFrom($docComment);

        // modify `$phpDocInfo` using its methods

        return $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo);
    }
}