Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traverse phpdoc with use of PhpDocNodeVisitor #6045

Merged
merged 6 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,35 @@
namespace PHPSTORM_META;

// $container->get(Type::class) → instance of "Type"
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;

override(\Psr\Container\ContainerInterface::get(0), type(0));

expectedArguments(
\PHPStan\PhpDocParser\Ast\Node::getAttribute(),
0,
PhpDocAttributeKey::START_AND_END,
PhpDocAttributeKey::LAST_TOKEN_POSITION,
PhpDocAttributeKey::PARENT,
);

expectedArguments(
\PHPStan\PhpDocParser\Ast\Node::setAttribute(),
0,
PhpDocAttributeKey::START_AND_END,
PhpDocAttributeKey::LAST_TOKEN_POSITION,
PhpDocAttributeKey::PARENT,
);

expectedArguments(
\PHPStan\PhpDocParser\Ast\Node::hasAttribute(),
0,
PhpDocAttributeKey::START_AND_END,
PhpDocAttributeKey::LAST_TOKEN_POSITION,
PhpDocAttributeKey::PARENT,
);


// PhpStorm 2019.1 - add argument autocomplete
// https://blog.jetbrains.com/phpstorm/2019/02/new-phpstorm-meta-php-features/
expectedArguments(
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"symplify/package-builder": "^9.2.12",
"symplify/rule-doc-generator-contracts": "^9.2.12",
"symplify/set-config-resolver": "^9.2.12",
"symplify/simple-php-doc-parser": "^9.2.12",
"symplify/simple-php-doc-parser": "^9.3",
"symplify/skipper": "^9.2.12",
"symplify/smart-file-system": "^9.2.12",
"symplify/symfony-php-config": "^9.2.12",
Expand Down Expand Up @@ -154,5 +154,7 @@
"config": {
"sort-packages": true,
"platform-check": false
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
3 changes: 0 additions & 3 deletions config/services-packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@

$services->load('Rector\\', __DIR__ . '/../packages')
->exclude([
// @todo move to value object
__DIR__ . '/../packages/*/{ValueObject,Contract,Exception}',
__DIR__ . '/../packages/BetterPhpDocParser/Attributes/Ast/PhpDoc',
__DIR__ . '/../packages/BetterPhpDocParser/Attributes/Attribute',
__DIR__ . '/../packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php',
__DIR__ . '/../packages/Testing/PHPUnit',
__DIR__ . '/../packages/BetterPhpDocParser/PhpDoc',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function provideData(): Iterator
public function provideDataClass(): Iterator
{
yield [__DIR__ . '/Source/Class_/some_entity_class.txt', new Class_(SomeEntityClass::class)];

yield [__DIR__ . '/Source/Multiline/table.txt', new Class_(TableClass::class)];
}

Expand Down
7 changes: 4 additions & 3 deletions packages-tests/BetterPhpDocParser/PhpDocNodeMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode;
use Rector\BetterPhpDocParser\PhpDocNodeMapper;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\VariadicAwareParamTagValueNode;
use Rector\Core\HttpKernel\RectorKernel;
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;
Expand All @@ -27,13 +28,13 @@ protected function setUp(): void
$this->phpDocNodeMapper = $this->getService(PhpDocNodeMapper::class);
}

public function testPropertyTag(): void
public function testParamTag(): void
{
$phpDocNode = $this->createParamDocNode();

$reprintedPhpDocNode = $this->phpDocNodeMapper->transform($phpDocNode, '');
$this->phpDocNodeMapper->transform($phpDocNode, new BetterTokenIterator([]));

$childNode = $reprintedPhpDocNode->children[0];
$childNode = $phpDocNode->children[0];
$this->assertInstanceOf(PhpDocTagNode::class, $childNode);

// test param tag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,6 @@ private function parseFileAndGetFirstNodeOfType(SmartFileInfo $smartFileInfo, st
private function printNodePhpDocInfoToString(Node $node): string
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$phpDocInfo->markAsChanged();

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

Expand Down
31 changes: 0 additions & 31 deletions packages/BetterPhpDocParser/Attributes/Attribute/Attribute.php

This file was deleted.

32 changes: 32 additions & 0 deletions packages/BetterPhpDocParser/Attributes/AttributeMirrorer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\Attributes;

use PHPStan\PhpDocParser\Ast\Node;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;

final class AttributeMirrorer
{
/**
* @var string[]
*/
private const ATTRIBUTES_TO_MIRROR = [
PhpDocAttributeKey::PARENT,
PhpDocAttributeKey::START_AND_END,
PhpDocAttributeKey::ORIG_NODE,
];

public function mirror(Node $oldNode, Node $newNode): void
{
foreach (self::ATTRIBUTES_TO_MIRROR as $attributeToMirror) {
if (! $oldNode->hasAttribute($attributeToMirror)) {
continue;
}

$attributeValue = $oldNode->getAttribute($attributeToMirror);
$newNode->setAttribute($attributeToMirror, $attributeValue);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\Contract;

use Symplify\SimplePhpDocParser\Contract\PhpDocNodeVisitorInterface;

interface BasePhpDocNodeVisitorInterface extends PhpDocNodeVisitorInterface
{
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\DataProvider;

use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;

final class CurrentTokenIteratorProvider
{
/**
* @var BetterTokenIterator
*/
private $betterTokenIterator;

public function setBetterTokenIterator(BetterTokenIterator $betterTokenIterator): void
{
$this->betterTokenIterator = $betterTokenIterator;
}

public function provide(): BetterTokenIterator
{
return $this->betterTokenIterator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc;
namespace Rector\BetterPhpDocParser\PhpDoc;

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;

Expand Down
29 changes: 19 additions & 10 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\SpacelessPhpDocTagNode;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\ShortNameAwareTagInterface;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Exception\NotImplementedYetException;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\SimplePhpDocParser\PhpDocNodeTraverser;

/**
* @template TNode as \PHPStan\PhpDocParser\Ast\Node
Expand Down Expand Up @@ -138,13 +139,6 @@ public function addPhpDocTagNode(PhpDocChildNode $phpDocChildNode): void
$this->phpDocNode->children[] = $phpDocChildNode;
// to give node more space
$this->makeMultiLined();
$this->markAsChanged();
}

public function addTagValueNodeWithShortName(ShortNameAwareTagInterface $shortNameAwareTag): void
{
$spacelessPhpDocTagNode = new SpacelessPhpDocTagNode($shortNameAwareTag->getShortName(), $shortNameAwareTag);
$this->addPhpDocTagNode($spacelessPhpDocTagNode);
}

public function getPhpDocNode(): PhpDocNode
Expand Down Expand Up @@ -470,6 +464,11 @@ public function hasInheritDoc(): bool
return $this->hasByNames(['inheritdoc', 'inheritDoc']);
}

/**
* @deprecated
* Should be handled by attributes of phpdoc node - if stard_and_end is missing in one of nodes, it has been changed
* Similar to missing original node in php-aprser
*/
public function markAsChanged(): void
{
$this->hasChanged = true;
Expand All @@ -486,7 +485,17 @@ public function hasChanged(): bool
return true;
}

return $this->hasChanged;
if ($this->hasChanged) {
return true;
}

// has a single node with missing start_end
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$changedPhpDocNodeVisitor = new ChangedPhpDocNodeVisitor();
$phpDocNodeTraverser->addPhpDocNodeVisitor($changedPhpDocNodeVisitor);
$phpDocNodeTraverser->traverse($this->phpDocNode);

return $changedPhpDocNodeVisitor->hasChanged();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
use Rector\BetterPhpDocParser\Attributes\Attribute\Attribute;
use Rector\BetterPhpDocParser\Contract\PhpDocNodeFactoryInterface;
use Rector\BetterPhpDocParser\PhpDocNodeMapper;
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\Core\Configuration\CurrentNodeProvider;
Expand Down Expand Up @@ -141,6 +141,7 @@ public function createEmpty(Node $node): PhpDocInfo

// multiline by default
$phpDocInfo->makeMultiLined();

return $phpDocInfo;
}

Expand All @@ -166,10 +167,10 @@ private function setPositionOfLastToken(PhpDocNode $phpDocNode): void
$lastChildNode = array_pop($phpDocChildNodes);

/** @var StartAndEnd $startAndEnd */
$startAndEnd = $lastChildNode->getAttribute(Attribute::START_END);
$startAndEnd = $lastChildNode->getAttribute(PhpDocAttributeKey::START_AND_END);

if ($startAndEnd !== null) {
$phpDocNode->setAttribute(Attribute::LAST_TOKEN_POSITION, $startAndEnd->getEnd());
$phpDocNode->setAttribute(PhpDocAttributeKey::LAST_TOKEN_POSITION, $startAndEnd->getEnd());
}
}

Expand All @@ -182,8 +183,7 @@ private function createFromPhpDocNode(
array $tokens,
Node $node
): PhpDocInfo {
/** @var PhpDocNode $phpDocNode */
$phpDocNode = $this->phpDocNodeMapper->transform($phpDocNode, $content);
$this->phpDocNodeMapper->transform($phpDocNode, new BetterTokenIterator($tokens));

$phpDocInfo = new PhpDocInfo(
$phpDocNode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

final class TokenIteratorFactory
{
/**
* @var string
*/
private const INDEX = 'index';

/**
* @var Lexer
*/
Expand Down Expand Up @@ -44,7 +49,7 @@ public function createFromTokenIterator(TokenIterator $tokenIterator): BetterTok

// keep original position
$currentIndex = $this->privatesAccessor->getPrivateProperty($tokenIterator, 'index');
$this->privatesAccessor->setPrivateProperty($betterTokenIterator, 'index', $currentIndex);
$this->privatesAccessor->setPrivateProperty($betterTokenIterator, self::INDEX, $currentIndex);

return $betterTokenIterator;
}
Expand Down
Loading