Skip to content

Commit

Permalink
Add test case for #3222
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 4, 2020
1 parent 56529e4 commit c3b6931
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 70 deletions.
48 changes: 35 additions & 13 deletions packages/better-php-doc-parser/src/PhpDocInfo/PhpDocInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,20 @@ public function createFromNode(Node $node): ?PhpDocInfo
$this->setPositionOfLastToken($phpDocNode);
}

/** @var AttributeAwarePhpDocNode $phpDocNode */
$phpDocNode = $this->attributeAwareNodeFactory->createFromNode($phpDocNode, $content);
return $this->createFromPhpDocNode($phpDocNode, $content, $tokens, $node);
}

$phpDocInfo = new PhpDocInfo(
$phpDocNode,
$tokens,
$content,
$this->staticTypeMapper,
$node,
$this->typeComparator,
$this->paramPhpDocNodeFactory
);
$node->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
public function createEmpty(Node $node): PhpDocInfo
{
/** needed for @see PhpDocNodeFactoryInterface */
$this->currentNodeProvider->setNode($node);

return $phpDocInfo;
// create empty node
$content = '';
$tokens = [];
$phpDocNode = new AttributeAwarePhpDocNode([]);

return $this->createFromPhpDocNode($phpDocNode, $content, $tokens, $node);
}

private function parseTokensToPhpDocNode(array $tokens): AttributeAwarePhpDocNode
Expand Down Expand Up @@ -140,4 +139,27 @@ private function setPositionOfLastToken(AttributeAwarePhpDocNode $attributeAware
$attributeAwarePhpDocNode->setAttribute(Attribute::LAST_TOKEN_POSITION, $startEndValueObject->getEnd());
}
}

private function createFromPhpDocNode(
AttributeAwarePhpDocNode $phpDocNode,
string $content,
array $tokens,
Node $node
): PhpDocInfo {
/** @var AttributeAwarePhpDocNode $phpDocNode */
$phpDocNode = $this->attributeAwareNodeFactory->createFromNode($phpDocNode, $content);

$phpDocInfo = new PhpDocInfo(
$phpDocNode,
$tokens,
$content,
$this->staticTypeMapper,
$node,
$this->typeComparator,
$this->paramPhpDocNodeFactory
);
$node->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);

return $phpDocInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public function updateNodeWithPhpDocInfo(Node $node): void

$phpDoc = $this->printPhpDocInfoToString($phpDocInfo);

if ($phpDoc === '' && count($node->getComments()) > 1) {
foreach ($node->getComments() as $comment) {
$phpDoc .= $comment->getText() . PHP_EOL;
}
}

if ($phpDoc === '') {
if ($phpDocInfo->getOriginalPhpDocNode()->children !== []) {
// all comments were removed → null
Expand Down
11 changes: 0 additions & 11 deletions rules/dead-code/src/Rector/Stmt/RemoveDeadStmtRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
Expand All @@ -19,16 +18,6 @@
*/
final class RemoveDeadStmtRector extends AbstractRector
{
/**
* @var PhpDocInfoFactory
*/
private $phpDocInfoFactory;

public function __construct(PhpDocInfoFactory $phpDocInfoFactory)
{
$this->phpDocInfoFactory = $phpDocInfoFactory;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Removes dead code statements', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use PHPStan\Type\StringType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocNode\Gedmo\SlugTagValueNode;
use Rector\Core\PhpParser\Node\Manipulator\ClassInsertManipulator;
use Rector\Core\Rector\AbstractRector;
Expand All @@ -28,21 +27,13 @@
*/
final class SluggableBehaviorRector extends AbstractRector
{
/**
* @var PhpDocInfoFactory
*/
private $phpDocInfoFactory;

/**
* @var ClassInsertManipulator
*/
private $classInsertManipulator;

public function __construct(
PhpDocInfoFactory $phpDocInfoFactory,
ClassInsertManipulator $classInsertManipulator
) {
$this->phpDocInfoFactory = $phpDocInfoFactory;
public function __construct(ClassInsertManipulator $classInsertManipulator)
{
$this->classInsertManipulator = $classInsertManipulator;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Namespace_;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\EntityTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Gedmo\LocaleTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Gedmo\TranslatableTagValueNode;
Expand Down Expand Up @@ -37,23 +36,16 @@ final class TranslationBehaviorRector extends AbstractRector
*/
private $classManipulator;

/**
* @var PhpDocInfoFactory
*/
private $phpDocInfoFactory;

/**
* @var ClassInsertManipulator
*/
private $classInsertManipulator;

public function __construct(
ClassManipulator $classManipulator,
PhpDocInfoFactory $phpDocInfoFactory,
ClassInsertManipulator $classInsertManipulator
) {
$this->classManipulator = $classManipulator;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->classInsertManipulator = $classInsertManipulator;
}

Expand Down
11 changes: 0 additions & 11 deletions rules/phpstan/src/Rector/Assign/PHPStormVarAnnotationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
Expand All @@ -23,16 +22,6 @@
*/
final class PHPStormVarAnnotationRector extends AbstractRector
{
/**
* @var PhpDocInfoFactory
*/
private $phpDocInfoFactory;

public function __construct(PhpDocInfoFactory $phpDocInfoFactory)
{
$this->phpDocInfoFactory = $phpDocInfoFactory;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Change various @var annotation formats to one PHPStorm understands', [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ private function replaceTemplateAnnotation(ClassMethod $classMethod): ?Node
private function classHasTemplateAnnotations(Class_ $node): bool
{
foreach ($node->getMethods() as $classMethod) {
/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $classMethod->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo === null) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ private function decorateWithVarAnnotation(ClassConst $classConst): void
/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $classConst->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo === null) {
// prevent comments override
return;
$phpDocInfo = $this->phpDocInfoFactory->createEmpty($classConst);
}

$phpDocInfo->changeVarType($constStaticType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ use Nette\Utils\Strings;
class ReplaceInArgs
{
// this prevents miss matching like "aMethoda"
/**
* @var string[]
*/
private const POSSIBLE_DELIMITERS = ['#', '~', '/'];
public function isRegexPattern(string $name): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ArrayType;
use PHPStan\Type\MixedType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -83,15 +82,9 @@ final class EventListenerToEventSubscriberRector extends AbstractRector
*/
private $applicationServiceMapProvider;

/**
* @var PhpDocInfoFactory
*/
private $phpDocInfoFactory;

public function __construct(ServiceMapProvider $applicationServiceMapProvider, PhpDocInfoFactory $phpDocInfoFactory)
public function __construct(ServiceMapProvider $applicationServiceMapProvider)
{
$this->applicationServiceMapProvider = $applicationServiceMapProvider;
$this->phpDocInfoFactory = $phpDocInfoFactory;
}

public function getDefinition(): RectorDefinition
Expand Down
20 changes: 14 additions & 6 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpParser\Node\Stmt\Return_;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\Scope;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
use Rector\Core\Configuration\Option;
use Rector\Core\Contract\Rector\PhpRectorInterface;
Expand Down Expand Up @@ -73,6 +74,11 @@ abstract class AbstractRector extends NodeVisitorAbstract implements PhpRectorIn
*/
protected $phpDocInfoPrinter;

/**
* @var PhpDocInfoFactory
*/
protected $phpDocInfoFactory;

/**
* @var DocBlockManipulator
*/
Expand Down Expand Up @@ -107,6 +113,7 @@ public function autowireAbstractRectorDependencies(
BuilderFactory $builderFactory,
ExclusionManager $exclusionManager,
PhpDocInfoPrinter $phpDocInfoPrinter,
PhpDocInfoFactory $phpDocInfoFactory,
DocBlockManipulator $docBlockManipulator,
StaticTypeMapper $staticTypeMapper,
ParameterProvider $parameterProvider,
Expand All @@ -117,6 +124,7 @@ public function autowireAbstractRectorDependencies(
$this->builderFactory = $builderFactory;
$this->exclusionManager = $exclusionManager;
$this->phpDocInfoPrinter = $phpDocInfoPrinter;
$this->phpDocInfoFactory = $phpDocInfoFactory;
$this->docBlockManipulator = $docBlockManipulator;
$this->staticTypeMapper = $staticTypeMapper;
$this->parameterProvider = $parameterProvider;
Expand All @@ -134,12 +142,6 @@ final public function enterNode(Node $node)

$this->currentRectorProvider->changeCurrentRector($this);

// show current Rector class on --debug
if ($this->symfonyStyle->isDebug()) {
// indented on purpose to improve log nesting under [refactoring]
$this->symfonyStyle->writeln(' [applying] ' . static::class);
}

// already removed
if ($this->isNodeRemoved($node)) {
return null;
Expand All @@ -149,6 +151,12 @@ final public function enterNode(Node $node)
return null;
}

// show current Rector class on --debug
if ($this->symfonyStyle->isDebug()) {
// indented on purpose to improve log nesting under [refactoring]
$this->symfonyStyle->writeln(' [applying] ' . static::class);
}

$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? clone $node;
$originalNodeWithAttributes = clone $node;
$node = $this->refactor($node);
Expand Down
47 changes: 47 additions & 0 deletions tests/Issues/Issue3222/Fixture/fixture3222.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\Core\Tests\Issues\Issue3222\Fixture;

/**
* This docblock is being deleted when there's an empty docblock after it.
*/

/**
*
*/

final class Fixture3222
{
private $eventDispatcher;

public function __construct(EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\Issue3222\Fixture;

/**
* This docblock is being deleted when there's an empty docblock after it.
*/

/**
*
*/

final class Fixture3222
{
private \Rector\Core\Tests\Issues\Issue3222\Fixture\EventDispatcher $eventDispatcher;

public function __construct(EventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
}

?>
29 changes: 29 additions & 0 deletions tests/Issues/Issue3222/Issue3222Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\Issue3222;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;

final class Issue3222Test extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function provideConfig(): string
{
return __DIR__ . '/config3222.yaml';
}
}
2 changes: 2 additions & 0 deletions tests/Issues/Issue3222/config3222.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
services:
Rector\Php74\Rector\Property\TypedPropertyRector: null

0 comments on commit c3b6931

Please sign in to comment.