Skip to content

Commit

Permalink
remove only failing fixture, to finalize in-rule doc block printer
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Sep 11, 2023
1 parent 6df215d commit b2bf4b4
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 100 deletions.
25 changes: 25 additions & 0 deletions e2e/wildcards-path-config/rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradeAttributeToAnnotationRector;
use Rector\DowngradePhp80\ValueObject\DowngradeAttributeToAnnotation;
use Rector\Renaming\Rector\Name\RenameClassRector;

return static function (RectorConfig $rectorConfig): void {
// @todo test in simpler way
$rectorConfig->paths([
__DIR__ . '/packages/*/src/*',
]);

$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
'DateTime' => 'DateTimeInterface'
]);
$rectorConfig->ruleWithConfiguration(DowngradeAttributeToAnnotationRector::class, [
new DowngradeAttributeToAnnotation('Symfony\Component\Routing\Annotation\Route')
]);

$rectorConfig->rule(RemoveUselessVarTagRector::class);
};
26 changes: 2 additions & 24 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\Type\ShortenedIdentifierTypeNode;
Expand All @@ -54,8 +53,6 @@ final class PhpDocInfo

private readonly PhpDocNode $originalPhpDocNode;

private bool $hasChanged = false;

public function __construct(
private readonly PhpDocNode $phpDocNode,
private readonly BetterTokenIterator $betterTokenIterator,
Expand Down Expand Up @@ -374,30 +371,11 @@ public function getTemplateTagValueNodes(): array
* @deprecated Change doc block and print directly in the node instead
* @internal
* 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
*
* @api
*/
public function markAsChanged(): void
{
$this->hasChanged = true;
}

public function hasChanged(): bool
{
if ($this->isNewNode()) {
return true;
}

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();
}

public function makeMultiLined(): void
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,4 @@ parameters:
# remove in next step
- '#Call to deprecated method markAsChanged\(\) of class Rector\\BetterPhpDocParser\\PhpDocInfo\\PhpDocInfo#'
- '#Method "(importNames|renamePhpDocType)\(\)" returns bool type, so the name should start with is/has/was#'
- '#Call to an undefined method Rector\\BetterPhpDocParser\\PhpDocInfo\\PhpDocInfo\:\:markAsChanged\(\)#'

This file was deleted.

17 changes: 5 additions & 12 deletions rules/CodingStyle/Rector/Stmt/NewlineAfterStatementRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private function processAddNewLine(
}

$totalKeys = array_key_last($node->stmts);

for ($key = $jumpToKey; $key < $totalKeys; ++$key) {
if (! isset($node->stmts[$key], $node->stmts[$key + 1])) {
break;
Expand Down Expand Up @@ -147,7 +148,6 @@ private function processAddNewLine(
array_splice($node->stmts, $key + 1, 0, [new Nop()]);

$hasChanged = true;

return $this->processAddNewLine($node, $hasChanged, $key + 2);
}

Expand All @@ -171,13 +171,10 @@ private function resolveRangeLineFromComment(
return $rangeLine;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($nextStmt);
if ($phpDocInfo->hasChanged()) {
return $rangeLine;
}

/** @var Comment[] $comments */
$line = $comments[0]->getStartLine();
$firstComment = $comments[0];

$line = $firstComment->getStartLine();
return $line - $endLine;
}

Expand All @@ -186,11 +183,7 @@ private function resolveRangeLineFromComment(
*/
private function hasNoComment(?array $comments): bool
{
if ($comments === null) {
return true;
}

return ! isset($comments[0]);
return $comments === null || $comments === [];
}

private function shouldSkip(Stmt $stmt): bool
Expand Down
18 changes: 11 additions & 7 deletions rules/Renaming/NodeManipulator/ClassRenamer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public function renameNode(Node $node, array $oldToNewClasses, ?Scope $scope): ?
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
$hasPhpDocChanged = false;
if ($phpDocInfo instanceof PhpDocInfo) {
$this->refactorPhpDoc($node, $oldToNewTypes, $oldToNewClasses, $phpDocInfo);
$hasPhpDocChanged = $this->refactorPhpDoc($node, $oldToNewTypes, $oldToNewClasses, $phpDocInfo);
}

if ($node instanceof Namespace_) {
Expand All @@ -81,9 +82,9 @@ public function renameNode(Node $node, array $oldToNewClasses, ?Scope $scope): ?
return $this->refactorClassLike($node, $oldToNewClasses, $scope);
}

// if ($phpDocInfo->hasChanged()) {
// return $node;
// }
if ($hasPhpDocChanged) {
return $node;
}

return null;
}
Expand All @@ -97,23 +98,26 @@ private function refactorPhpDoc(
array $oldToNewTypes,
array $oldToNewClasses,
PhpDocInfo $phpDocInfo
): void {
): bool {
if (! $phpDocInfo->hasByTypes(NodeTypes::TYPE_AWARE_NODES) && ! $phpDocInfo->hasByAnnotationClasses(
NodeTypes::TYPE_AWARE_DOCTRINE_ANNOTATION_CLASSES
)) {
return;
return false;
}

if ($node instanceof AttributeGroup) {
return;
return false;
}

$hasChanged = $this->docBlockClassRenamer->renamePhpDocType($phpDocInfo, $oldToNewTypes);
$this->phpDocClassRenamer->changeTypeInAnnotationTypes($node, $phpDocInfo, $oldToNewClasses);

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

return false;
}

private function shouldSkip(string $newName, Name $name): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -31,7 +32,8 @@ final class AddParamTypeSplFixedArrayRector extends AbstractRector
];

public function __construct(
private readonly PhpDocTypeChanger $phpDocTypeChanger
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly DocBlockUpdater $docBlockUpdater,
) {
}

Expand Down Expand Up @@ -89,6 +91,7 @@ public function refactor(Node $node): ?Node
}

$functionLikePhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$hasChanged = false;

foreach ($node->getParams() as $param) {
if ($param->type === null) {
Expand Down Expand Up @@ -121,9 +124,12 @@ public function refactor(Node $node): ?Node
$param,
$paramName
);

$hasChanged = true;
}

if ($functionLikePhpDocInfo->hasChanged()) {
if ($hasChanged) {
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
return $node;
}

Expand Down
18 changes: 0 additions & 18 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,7 @@ protected function pArray(
// reindex positions for printer
$nodes = array_values($nodes);

// $this->moveCommentsFromAttributeObjectToCommentsAttribute($nodes);

$content = parent::pArray($nodes, $origNodes, $pos, $indentAdjustment, $parentNodeType, $subNodeName, $fixup);

if ($content === null) {
return $content;
}
Expand Down Expand Up @@ -515,21 +512,6 @@ private function resolveNewStmts(array $stmts): array
return $stmts;
}

// /**
// * @param array<Node|null> $nodes
// */
//// private function moveCommentsFromAttributeObjectToCommentsAttribute(array $nodes): void
// {
// // move phpdoc from node to "comment" attribute
// foreach ($nodes as $node) {
// if (! $node instanceof Stmt && ! $node instanceof Param) {
// continue;
// }
//
// // $this->docBlockUpdater->updateNodeWithPhpDocInfo($node);
// }
// }

/**
* @param Node[] $nodes
*/
Expand Down

0 comments on commit b2bf4b4

Please sign in to comment.