Skip to content

Commit

Permalink
[DX] Inline symplify/astral to use only active classes (#2851)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 29, 2022
1 parent ad7cdc7 commit bb59a7c
Show file tree
Hide file tree
Showing 110 changed files with 1,690 additions and 163 deletions.
1 change: 0 additions & 1 deletion composer.json
Expand Up @@ -37,7 +37,6 @@
"symfony/dependency-injection": "^6.1",
"symfony/finder": "^6.0",
"symfony/string": "^6.0",
"symplify/astral": "^11.1",
"symplify/autowire-array-parameter": "^11.1",
"symplify/composer-json-manipulator": "^11.1",
"symplify/easy-parallel": "^11.1",
Expand Down
25 changes: 24 additions & 1 deletion config/config.php
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Inflector\Rules\English\InflectorFactory;
use OndraM\CiDetector\CiDetector;
use PhpParser\BuilderFactory;
use PhpParser\ConstExprEvaluator;
use PhpParser\Lexer;
use PhpParser\NodeFinder;
use PhpParser\NodeVisitor\CloningVisitor;
Expand All @@ -17,6 +18,7 @@
use PHPStan\File\FileHelper;
use PHPStan\Parser\Parser;
use PHPStan\PhpDoc\TypeNodeResolver;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\Reflection\ReflectionProvider;
Expand All @@ -34,12 +36,14 @@
use Rector\NodeTypeResolver\DependencyInjection\PHPStanServicesFactory;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocator\IntermediateSourceLocator;
use Rector\NodeTypeResolver\Reflection\BetterReflection\SourceLocatorProvider\DynamicSourceLocatorProvider;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpDocParser\PhpParser\SmartPhpParser;
use Rector\PhpDocParser\PhpParser\SmartPhpParserFactory;
use Rector\PSR4\Composer\PSR4NamespaceMatcher;
use Rector\PSR4\Contract\PSR4AutoloadNamespaceMatcherInterface;
use Rector\Utils\Command\MissingInSetCommand;
use Symfony\Component\Console\Application;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
use Symplify\EasyParallel\ValueObject\EasyParallelConfig;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\PackageBuilder\Php\TypeChecker;
Expand Down Expand Up @@ -85,6 +89,12 @@
__DIR__ . '/../packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php',
__DIR__ . '/../packages/Testing/PHPUnit',
__DIR__ . '/../packages/BetterPhpDocParser/PhpDoc',

__DIR__ . '/../packages/PhpDocParser/NodeVisitor',
__DIR__ . '/../packages/PhpDocParser/PhpParser/SmartPhpParser.php',
__DIR__ . '/../packages/PhpDocParser/ValueObject',
__DIR__ . '/../packages/PhpDocParser/PhpDocParser/PhpDocNodeVisitor/CallablePhpDocNodeVisitor.php',

__DIR__ . '/../packages/PHPStanStaticTypeMapper/Enum',
__DIR__ . '/../packages/Caching/Cache.php',
__DIR__ . '/../packages/NodeTypeResolver/PhpDocNodeVisitor/UnderscoreRenamePhpDocNodeVisitor.php',
Expand Down Expand Up @@ -216,4 +226,17 @@
->factory([service(PHPStanServicesFactory::class), 'createDynamicSourceLocatorProvider']);

$services->set(MissingInSetCommand::class);

// phpdoc parser
$services->set(SmartPhpParser::class)
->factory([service(SmartPhpParserFactory::class), 'create']);

$services->set(ConstExprEvaluator::class);
$services->set(NodeFinder::class);

// phpdoc parser
$services->set(PhpDocParser::class);
$services->set(\PHPStan\PhpDocParser\Lexer\Lexer::class);
$services->set(TypeParser::class);
$services->set(ConstExprParser::class);
};
2 changes: 1 addition & 1 deletion easy-ci.php
Expand Up @@ -33,6 +33,7 @@
use Rector\Php81\NodeFactory\ClassFromEnumFactory;
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
use Rector\PhpAttribute\NodeFactory\DoctrineAnnotationFactory;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\ReadWrite\Contract\ParentNodeReadAnalyzerInterface;
use Rector\ReadWrite\Contract\ReadNodeAnalyzerInterface;
Expand All @@ -45,7 +46,6 @@
use Rector\TypeDeclaration\Contract\TypeInferer\ReturnTypeInfererInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
use Symplify\EasyCI\Config\EasyCIConfig;

return static function (EasyCIConfig $easyCiConfig): void {
Expand Down
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\PhpDocParser\NodeValue\Fixture;

final class SomeClassWithConstant
{
public const NAME = 'value';
}
57 changes: 57 additions & 0 deletions packages-tests/PhpDocParser/NodeValue/NodeValueResolverTest.php
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\PhpDocParser\NodeValue;

use Iterator;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\MagicConst\Dir;
use PhpParser\Node\Scalar\MagicConst\File;
use PhpParser\Node\Scalar\String_;
use Rector\PhpDocParser\NodeValue\NodeValueResolver;
use Rector\Testing\PHPUnit\AbstractTestCase;
use Rector\Tests\PhpDocParser\NodeValue\Fixture\SomeClassWithConstant;

final class NodeValueResolverTest extends AbstractTestCase
{
private NodeValueResolver $nodeValueResolver;

protected function setUp(): void
{
$this->boot();
$this->nodeValueResolver = $this->getService(NodeValueResolver::class);
}

/**
* @dataProvider provideData()
* @param string|class-string<\Rector\Tests\PhpDocParser\NodeValue\NodeValueResolverTest>|bool $expectedValue
*/
public function test(Expr $expr, string|bool $expectedValue): void
{
$resolvedValue = $this->nodeValueResolver->resolve($expr, __FILE__);
$this->assertSame($expectedValue, $resolvedValue);
}

/**
* @return Iterator<mixed[]|Expr[]>
*/
public function provideData(): Iterator
{
yield [new String_('value'), 'value'];
yield [new ClassConstFetch(new FullyQualified(self::class), 'class'), self::class];
yield [new ClassConstFetch(new FullyQualified(SomeClassWithConstant::class), 'NAME'), 'value'];
yield [new Dir(), __DIR__];
yield [new ConstFetch(new Name('true')), true];
yield [new Concat(new Dir(), new String_('/example.latte')), __DIR__ . '/example.latte'];
$args = [new Arg(new String_('.php')), new Arg(new String_('.latte')), new Arg(new File())];
yield [new FuncCall(new Name('str_replace'), $args), __DIR__ . '/NodeValueResolverTest.latte'];
}
}
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\PhpDocParser\PhpDocParser\PhpDocNodeVisitor;

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeVisitor\ParentConnectingPhpDocNodeVisitor;
use Rector\PhpDocParser\PhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Testing\PHPUnit\AbstractTestCase;

final class ParentConnectingPhpDocNodeVisitorTest extends AbstractTestCase
{
private PhpDocNodeTraverser $phpDocNodeTraverser;

protected function setUp(): void
{
$this->boot();

$this->phpDocNodeTraverser = $this->getService(PhpDocNodeTraverser::class);

/** @var ParentConnectingPhpDocNodeVisitor $parentConnectingPhpDocNodeVisitor */
$parentConnectingPhpDocNodeVisitor = $this->getService(ParentConnectingPhpDocNodeVisitor::class);
$this->phpDocNodeTraverser->addPhpDocNodeVisitor($parentConnectingPhpDocNodeVisitor);
}

public function testTypeNode(): void
{
$phpDocNode = $this->createPhpDocNode();
$this->phpDocNodeTraverser->traverse($phpDocNode);

/** @var PhpDocTagNode $phpDocChildNode */
$phpDocChildNode = $phpDocNode->children[0];

$returnTagValueNode = $phpDocChildNode->value;

$this->assertInstanceOf(ReturnTagValueNode::class, $returnTagValueNode);

/** @var ReturnTagValueNode $returnTagValueNode */
$returnParent = $returnTagValueNode->getAttribute(PhpDocAttributeKey::PARENT);
$this->assertSame($phpDocChildNode, $returnParent);

$returnTypeParent = $returnTagValueNode->type->getAttribute(PhpDocAttributeKey::PARENT);
$this->assertSame($returnTagValueNode, $returnTypeParent);

// test child + parent node
$phpDocChildNode = $phpDocNode->children[0];
$this->assertInstanceOf(PhpDocTagNode::class, $phpDocChildNode);

$childParent = $phpDocChildNode->getAttribute(PhpDocAttributeKey::PARENT);
$this->assertSame($phpDocNode, $childParent);
}

private function createPhpDocNode(): PhpDocNode
{
$returnTagValueNode = new ReturnTagValueNode(new IdentifierTypeNode('string'), '');

return new PhpDocNode([
new PhpDocTagNode('@return', $returnTagValueNode),
new PhpDocTextNode('some text'),
]);
}
}
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\PhpDocParser\PhpDocParser\SimplePhpDocNodeTraverser;

use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\Testing\PHPUnit\AbstractTestCase;

final class PhpDocNodeTraverserTest extends AbstractTestCase
{
/**
* @var string
*/
private const SOME_DESCRIPTION = 'some description';

private PhpDocNodeTraverser $phpDocNodeTraverser;

protected function setUp(): void
{
$this->boot();
$this->phpDocNodeTraverser = $this->getService(PhpDocNodeTraverser::class);
}

public function test(): void
{
$varTagValueNode = new VarTagValueNode(new IdentifierTypeNode('string'), '', '');
$phpDocNode = new PhpDocNode([new PhpDocTagNode('@var', $varTagValueNode)]);

$this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', static function (Node $node): Node {
if (! $node instanceof VarTagValueNode) {
return $node;
}

$node->description = self::SOME_DESCRIPTION;
return $node;
});

$varTagValueNodes = $phpDocNode->getVarTagValues();
$this->assertSame(self::SOME_DESCRIPTION, $varTagValueNodes[0]->description);
}
}
@@ -0,0 +1,3 @@
/**
* @param string $name
*/
@@ -0,0 +1,3 @@
/**
* @var int
*/
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\PhpDocParser\PhpDocParser\SimplePhpDocParser;

use Nette\Utils\FileSystem;
use Rector\PhpDocParser\PhpDocParser\SimplePhpDocParser;
use Rector\PhpDocParser\PhpDocParser\ValueObject\Ast\PhpDoc\SimplePhpDocNode;
use Rector\Testing\PHPUnit\AbstractTestCase;

final class SimplePhpDocParserTest extends AbstractTestCase
{
private SimplePhpDocParser $simplePhpDocParser;

protected function setUp(): void
{
$this->boot();

$this->simplePhpDocParser = $this->getService(SimplePhpDocParser::class);
}

public function testVar(): void
{
$fileContents = FileSystem::read(__DIR__ . '/Fixture/var_int.txt');

$simplePhpDocNode = $this->simplePhpDocParser->parseDocBlock($fileContents);
$this->assertInstanceOf(SimplePhpDocNode::class, $simplePhpDocNode);

$varTagValues = $simplePhpDocNode->getVarTagValues();
$this->assertCount(1, $varTagValues);
}

public function testParam(): void
{
$fileContents = FileSystem::read(__DIR__ . '/Fixture/param_string_name.txt');

$simplePhpDocNode = $this->simplePhpDocParser->parseDocBlock($fileContents);
$this->assertInstanceOf(SimplePhpDocNode::class, $simplePhpDocNode);

// DX friendly
$paramType = $simplePhpDocNode->getParamType('name');
$withDollarParamType = $simplePhpDocNode->getParamType('$name');

$this->assertSame($paramType, $withDollarParamType);
}
}
2 changes: 1 addition & 1 deletion packages/BetterPhpDocParser/Comment/CommentsMerger.php
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Comment;
use PhpParser\Node;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;

final class CommentsMerger
{
Expand Down
Expand Up @@ -4,7 +4,7 @@

namespace Rector\BetterPhpDocParser\Contract;

use Symplify\Astral\PhpDocParser\Contract\PhpDocNodeVisitorInterface;
use Rector\PhpDocParser\PhpDocParser\Contract\PhpDocNodeVisitorInterface;

interface BasePhpDocNodeVisitorInterface extends PhpDocNodeVisitorInterface
{
Expand Down
2 changes: 1 addition & 1 deletion packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Expand Up @@ -29,8 +29,8 @@
use Rector\BetterPhpDocParser\ValueObject\Type\ShortenedIdentifierTypeNode;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;

/**
* @see \Rector\Tests\BetterPhpDocParser\PhpDocInfo\PhpDocInfo\PhpDocInfoTest
Expand Down
Expand Up @@ -8,7 +8,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;

final class PhpDocTagRemover
{
Expand Down
Expand Up @@ -7,7 +7,7 @@
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;

/**
* @template TNode as \PHPStan\PhpDocParser\Ast\Node
Expand Down
6 changes: 3 additions & 3 deletions packages/BetterPhpDocParser/PhpDocNodeMapper.php
Expand Up @@ -8,9 +8,9 @@
use Rector\BetterPhpDocParser\Contract\BasePhpDocNodeVisitorInterface;
use Rector\BetterPhpDocParser\DataProvider\CurrentTokenIteratorProvider;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Symplify\Astral\PhpDocParser\PhpDocNodeTraverser;
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\CloningPhpDocNodeVisitor;
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\ParentConnectingPhpDocNodeVisitor;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeVisitor\CloningPhpDocNodeVisitor;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeVisitor\ParentConnectingPhpDocNodeVisitor;

/**
* @see \Rector\Tests\BetterPhpDocParser\PhpDocNodeMapperTest
Expand Down
Expand Up @@ -9,7 +9,7 @@
use Rector\BetterPhpDocParser\Attributes\AttributeMirrorer;
use Rector\BetterPhpDocParser\Contract\BasePhpDocNodeVisitorInterface;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode;
use Symplify\Astral\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;

final class ArrayTypePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor implements BasePhpDocNodeVisitorInterface
{
Expand Down

0 comments on commit bb59a7c

Please sign in to comment.