Skip to content

Commit

Permalink
[SimplePhpDocParser] Init (#4391)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Oct 11, 2020
1 parent 874baec commit a4b4b47
Show file tree
Hide file tree
Showing 21 changed files with 282 additions and 186 deletions.
202 changes: 106 additions & 96 deletions composer.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion monorepo-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::DIRECTORIES_TO_REPOSITORIES, [
__DIR__ . '/packages/symfony-php-config' => 'git@github.com:rectorphp/symfony-php-config.git',
// __DIR__ . '/packages/symfony-php-config' => 'git@github.com:rectorphp/symfony-php-config.git',
__DIR__ . '/packages/simple-php-doc-parser' => 'git@github.com:rectorphp/simple-php-doc-parser.git',
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwarePhpDocNode;
use Rector\AttributeAwarePhpDoc\Contract\AttributeNodeAwareFactory\AttributeAwareNodeFactoryAwareInterface;
use Rector\AttributeAwarePhpDoc\Contract\AttributeNodeAwareFactory\AttributeNodeAwareFactoryInterface;
use Rector\BetterPhpDocParser\Ast\PhpDocNodeTraverser;
use Rector\BetterPhpDocParser\Attributes\Ast\AttributeAwareNodeFactory;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
use Rector\SimplePhpDocParser\PhpDocNodeTraverser;

final class AttributeAwarePhpDocNodeFactory implements AttributeNodeAwareFactoryInterface, AttributeAwareNodeFactoryAwareInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Rector\BetterPhpDocParser\Contract\PhpDocNodeFactoryInterface;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocRemover;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -25,7 +26,7 @@ final class PhpDocInfoFactory
/**
* @var PhpDocParser
*/
private $phpDocParser;
private $betterPhpDocParser;

/**
* @var Lexer
Expand Down Expand Up @@ -61,12 +62,12 @@ public function __construct(
AttributeAwareNodeFactory $attributeAwareNodeFactory,
CurrentNodeProvider $currentNodeProvider,
Lexer $lexer,
PhpDocParser $phpDocParser,
BetterPhpDocParser $betterPhpDocParser,
PhpDocRemover $phpDocRemover,
PhpDocTypeChanger $phpDocTypeChanger,
StaticTypeMapper $staticTypeMapper
) {
$this->phpDocParser = $phpDocParser;
$this->betterPhpDocParser = $betterPhpDocParser;
$this->lexer = $lexer;
$this->currentNodeProvider = $currentNodeProvider;
$this->staticTypeMapper = $staticTypeMapper;
Expand Down Expand Up @@ -114,7 +115,7 @@ private function parseTokensToPhpDocNode(array $tokens): AttributeAwarePhpDocNod
{
$tokenIterator = new TokenIterator($tokens);

return $this->phpDocParser->parse($tokenIterator);
return $this->betterPhpDocParser->parse($tokenIterator);
}

/**
Expand Down
7 changes: 2 additions & 5 deletions packages/file-system-rector/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set('project_directory', null);

$services = $containerConfigurator->services();

$services->defaults()
->public()
->autowire();
->autowire()
->autoconfigure();

$services->load('Rector\FileSystemRector\\', __DIR__ . '/../src')
->exclude([__DIR__ . '/../src/Rector']);
Expand Down
13 changes: 0 additions & 13 deletions packages/file-system-rector/src/Configuration/Option.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
namespace Rector\FileSystemRector\Rector\Removing;

use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\RectorDefinition\ConfiguredCodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
use Rector\FileSystemRector\Configuration\Option;
use Rector\FileSystemRector\Rector\AbstractFileSystemRector;
use Symplify\SmartFileSystem\SmartFileInfo;

Expand All @@ -31,7 +29,7 @@ public function refactor(SmartFileInfo $smartFileInfo): void
return;
}

$projectDirectory = $this->getProjectDirectory();
$projectDirectory = getcwd();

$relativePathInProject = $smartFileInfo->getRelativeFilePathFromDirectory($projectDirectory);

Expand Down Expand Up @@ -66,25 +64,4 @@ public function configure(array $configuration): void
{
$this->filePathsToRemove = $configuration[self::FILE_PATHS_TO_REMOVE] ?? [];
}

private function getProjectDirectory(): string
{
$this->ensureProjectDirectoryIsSet();

return (string) $this->parameterProvider->provideParameter(Option::PROJECT_DIRECTORY_PARAMETER);
}

private function ensureProjectDirectoryIsSet(): void
{
$projectDirectory = $this->parameterProvider->provideParameter(Option::PROJECT_DIRECTORY_PARAMETER);

// has value? → skip
if ($projectDirectory) {
return;
}

throw new ShouldNotHappenException(
'Complete "$parameters->setParameter(Option::PROJECT_DIRECTORY_PARAMETER, ...);" in rector.php, so Rector can found your directory'
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\Ast\PhpDocNodeTraverser;
use Rector\PHPStan\Type\ShortenedObjectType;
use Rector\SimplePhpDocParser\PhpDocNodeTraverser;
use Rector\StaticTypeMapper\StaticTypeMapper;

final class DocBlockClassRenamer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Stmt\ClassLike;
use PHPStan\PhpDocParser\Ast\Node as PhpDocParserNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\BetterPhpDocParser\Ast\PhpDocNodeTraverser;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\CodingStyle\Imports\ImportSkipper;
use Rector\Core\Configuration\Option;
Expand All @@ -20,6 +19,7 @@
use Rector\PHPStan\Type\FullyQualifiedObjectType;
use Rector\PHPStan\Type\ShortenedObjectType;
use Rector\PostRector\Collector\UseNodesToAddCollector;
use Rector\SimplePhpDocParser\PhpDocNodeTraverser;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\PackageBuilder\Parameter\ParameterProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use PHPStan\PhpDocParser\Ast\Node as PhpDocParserNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\Ast\PhpDocNodeTraverser;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Generic\ValueObject\PseudoNamespaceToNamespace;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\SimplePhpDocParser\PhpDocNodeTraverser;
use Rector\StaticTypeMapper\StaticTypeMapper;

final class PhpDocTypeRenamer
Expand Down
26 changes: 26 additions & 0 deletions packages/simple-php-doc-parser/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "rector/simple-php-doc-parser",
"description": "Simple service integration of phpstan/phpdoc-parser",
"license": "MIT",
"require": {
"php": "^7.2.4|^8.0",
"symfony/dependency-injection": "^4.4.8|^5.1",
"symfony/config": "^4.4.8|^5.1",
"symfony/http-kernel": "^4.4.8|^5.1",
"symplify/package-builder": "^8.3.33"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.2",
"symplify/easy-testing": "^8.3.33"
},
"autoload": {
"psr-4": {
"Rector\\SymfonyPhpDocParser\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Rector\\SymfonyPhpDocParser\\Tests\\": "tests"
}
}
}
21 changes: 21 additions & 0 deletions packages/simple-php-doc-parser/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->defaults()
->public()
->autowire()
->autoconfigure();

$services->load('Rector\SimplePhpDocParser\\', __DIR__ . '/../src');

$services->set(PhpDocParser::class);
$services->set(Lexer::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Rector\SimplePhpDocParser\Bundle\DependencyInjection\Extension;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

final class SimplePhpDocParserExtension extends Extension
{
public function load(array $configs, ContainerBuilder $containerBuilder): void
{
$phpFileLoader = new PhpFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../../../../config'));
$phpFileLoader->load('config.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Rector\SimplePhpDocParser\Bundle;

use Rector\SimplePhpDocParser\Bundle\DependencyInjection\Extension\SimplePhpDocParserExtension;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

final class SimplePhpDocParserBundle extends Bundle
{
public function getContainerExtension(): ?ExtensionInterface
{
return new SimplePhpDocParserExtension();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\Ast;
namespace Rector\SimplePhpDocParser;

use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
Expand Down
40 changes: 40 additions & 0 deletions packages/simple-php-doc-parser/src/SimplePhpDocParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Rector\SimplePhpDocParser;

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;

/**
* @see \Rector\SimplePhpDocParser\Tests\SimplePhpDocParser\SimplePhpDocParserTest
*/
final class SimplePhpDocParser
{
/**
* @var PhpDocParser
*/
private $phpDocParser;

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

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

public function parseDocBlock(string $docBlock): PhpDocNode
{
$tokens = $this->lexer->tokenize($docBlock);
$tokenIterator = new TokenIterator($tokens);

return $this->phpDocParser->parse($tokenIterator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/**
* @var int
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Rector\SimplePhpDocParser\Tests\SimplePhpDocParser;

use Rector\Core\HttpKernel\RectorKernel;
use Rector\SimplePhpDocParser\SimplePhpDocParser;
use Symplify\PackageBuilder\Tests\AbstractKernelTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class SimplePhpDocParserTest extends AbstractKernelTestCase
{
/**
* @var SimplePhpDocParser
*/
private $simplePhpDocParser;

protected function setUp(): void
{
$this->bootKernel(RectorKernel::class);
$this->simplePhpDocParser = self::$container->get(SimplePhpDocParser::class);
}

public function test(): void
{
$fileInfo = new SmartFileInfo(__DIR__ . '/Fixture/basic_doc.txt');

$phpDocNode = $this->simplePhpDocParser->parseDocBlock($fileInfo->getContents());

$varTagValues = $phpDocNode->getVarTagValues();
$this->assertCount(1, $varTagValues);
}
}
9 changes: 1 addition & 8 deletions packages/symfony-php-config/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@
"name": "rector/symfony-php-config",
"description": "Tools that easy work with Symfony PHP Configs",
"license": "MIT",
"authors": [
{
"name": "Tomas Votruba",
"email": "tomas.vot@gmail.com",
"homepage": "https://tomasvotruba.com"
}
],
"require": {
"php": "^7.2.4|^8.0",
"symfony/dependency-injection": "^4.4.8|^5.1",
"symfony/http-kernel": "^4.4.8|^5.0.6",
"symfony/http-kernel": "^4.4.8|^5.1",
"symplify/package-builder": "^8.3.33"
},
"require-dev": {
Expand Down
Loading

0 comments on commit a4b4b47

Please sign in to comment.