Skip to content

Commit

Permalink
update few more references, rename 'Phpdoc' namespace to 'PhpDoce
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Apr 24, 2018
0 parents commit fc812d7
Show file tree
Hide file tree
Showing 26 changed files with 921 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
@@ -0,0 +1,6 @@
/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
LICENSE export-ignore
phpunit.xml export-ignore
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/vendor
composer.lock
14 changes: 14 additions & 0 deletions .travis.yml
@@ -0,0 +1,14 @@
language: php

php:
- 7.1
- 7.2

install:
- composer install --prefer-source

script:
- vendor/bin/phpunit

notifications:
email: never
25 changes: 25 additions & 0 deletions LICENSE
@@ -0,0 +1,25 @@
The MIT License
---------------

Copyright (c) 2017-present Tomáš Votruba (http://tomasvotruba.cz)

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
@@ -0,0 +1,56 @@
# Better Reflection DocBlock

[![Build Status](https://img.shields.io/travis/Symplify/BetterPhpDocParser/master.svg?style=flat-square)](https://travis-ci.org/Symplify/BetterPhpDocParser)
[![Subscribe](https://img.shields.io/badge/subscribe-to--releases-green.svg?style=flat-square)](https://libraries.io/packagist/symplify%2Fbetter-phpdoc-parser)

Wrapper around [phpstan/phpdoc-parser](https://github.com/phpstan/phpdoc-parser) that adds **format preserving printer**.

## Install

```bash
composer require symplify/better-phpdoc-parser
```

## Usage

Register services in your Symfony config:

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

or register the needed services from `services.yml` in config of your other framework.
And require services in the constructor.

### Why do we need Format Preserving Printer?

[Symplify\CodingStandard](https://github.com/symplify/codingstandard) and [Rector](github.com/rectorphp/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`](https://github.com/nikic/PHP-Parser/issues/487).

```php
use Symplify\BetterPhpDocParser\PhpDocParser\PhpDocInfoFactory;
use Symplify\BetterPhpDocParser\PhpDocParser\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));
}
}
```
30 changes: 30 additions & 0 deletions composer.json
@@ -0,0 +1,30 @@
{
"name": "symplify/better-phpdoc-parser",
"description": "Slim wrapper around phpstan/phpdoc-parser with format preserving printer",
"license": "MIT",
"require": {
"php": "^7.1",
"phpstan/phpdoc-parser": "^0.2",
"symplify/package-builder": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": {
"Symplify\\BetterPhpDocParser\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Symplify\\BetterPhpDocParser\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "4.2-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
9 changes: 9 additions & 0 deletions phpunit.xml
@@ -0,0 +1,9 @@
<phpunit
bootstrap="vendor/autoload.php"
verbose="true"
>
<!-- where tests are located -->
<testsuite>
<directory>tests</directory>
</testsuite>
</phpunit>
40 changes: 40 additions & 0 deletions src/DependencyInjection/BetterPhpDocParserKernel.php
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Symplify\BetterPhpDocParser\DependencyInjection;

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symplify\PackageBuilder\DependencyInjection\CompilerPass\PublicForTestsCompilerPass;

final class BetterPhpDocParserKernel extends Kernel
{
public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__ . '/../config/services.yml');
}

public function getCacheDir(): string
{
return sys_get_temp_dir() . '/_better_reflection_doc_block';
}

public function getLogDir(): string
{
return sys_get_temp_dir() . '/_better_reflection_doc_block_logs';
}

/**
* @return BundleInterface[]
*/
public function registerBundles(): array
{
return [];
}

protected function build(ContainerBuilder $containerBuilder): void
{
$containerBuilder->addCompilerPass(new PublicForTestsCompilerPass());
}
}
21 changes: 21 additions & 0 deletions src/DependencyInjection/ContainerFactory.php
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Symplify\BetterPhpDocParser\DependencyInjection;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;

final class ContainerFactory
{
/**
* @return ContainerInterface|SymfonyContainerInterface|Container
*/
public function create(): ContainerInterface
{
$appKernel = new BetterPhpDocParserKernel('dev', true);
$appKernel->boot();

return $appKernel->getContainer();
}
}
9 changes: 9 additions & 0 deletions src/Exception/NotImplementedYetException.php
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Symplify\BetterPhpDocParser\Exception;

use Exception;

final class NotImplementedYetException extends Exception
{
}
13 changes: 13 additions & 0 deletions src/PhpDocParser/Ast/Type/FormatPreservingUnionTypeNode.php
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Symplify\BetterPhpDocParser\PhpDocParser\Ast\Type;

use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;

final class FormatPreservingUnionTypeNode extends UnionTypeNode
{
public function __toString(): string
{
return implode('|', $this->types);
}
}
97 changes: 97 additions & 0 deletions src/PhpDocParser/PhpDocInfo.php
@@ -0,0 +1,97 @@
<?php declare(strict_types=1);

namespace Symplify\BetterPhpDocParser\PhpDocParser;

use Nette\Utils\Strings;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;

final class PhpDocInfo
{
/**
* @var PhpDocNode
*/
private $phpDocNode;

/**
* @var mixed[]
*/
private $tokens = [];

/**
* @var PhpDocNode
*/
private $originalPhpDocNode;

/**
* @var string
*/
private $originalContent;

/**
* @param mixed[] $tokens
*/
public function __construct(PhpDocNode $phpDocNode, array $tokens, string $originalContent)
{
$this->phpDocNode = $phpDocNode;
$this->tokens = $tokens;
$this->originalPhpDocNode = clone $phpDocNode;
$this->originalContent = $originalContent;
}

public function getOriginalContent(): string
{
return $this->originalContent;
}

public function getPhpDocNode(): PhpDocNode
{
return $this->phpDocNode;
}

public function getOriginalPhpDocNode(): PhpDocNode
{
return $this->originalPhpDocNode;
}

/**
* @return mixed[]
*/
public function getTokens(): array
{
return $this->tokens;
}

public function getParamTagValueByName(string $name): ?ParamTagValueNode
{
$phpDocNode = $this->getPhpDocNode();

foreach ($phpDocNode->getParamTagValues() as $paramTagValue) {
if (Strings::match($paramTagValue->parameterName, '#^(\$)?' . $name . '$#')) {
return $paramTagValue;
}
}

return null;
}

public function getVarTagValue(): ?VarTagValueNode
{
return $this->getPhpDocNode()->getVarTagValues()[0] ?? null;
}

public function getReturnTagValue(): ?ReturnTagValueNode
{
return $this->getPhpDocNode()->getReturnTagValues()[0] ?? null;
}

/**
* @return ParamTagValueNode[]
*/
public function getParamTagValues(): array
{
return $this->getPhpDocNode()->getParamTagValues();
}
}
35 changes: 35 additions & 0 deletions src/PhpDocParser/PhpDocInfoFactory.php
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace Symplify\BetterPhpDocParser\PhpDocParser;

use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;

final class PhpDocInfoFactory
{
/**
* @var PhpDocParser
*/
private $phpDocParser;

/**
* @var Lexer
*/
private $lexer;

public function __construct(PhpDocParser $phpDocParser, Lexer $lexer)
{
$this->phpDocParser = $phpDocParser;
$this->lexer = $lexer;
}

public function createFrom(string $content): PhpDocInfo
{
$tokens = $this->lexer->tokenize($content);
$tokenIterator = new TokenIterator($tokens);
$phpDocNode = $this->phpDocParser->parse($tokenIterator);

return new PhpDocInfo($phpDocNode, $tokens, $content);
}
}

0 comments on commit fc812d7

Please sign in to comment.