Skip to content
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
6 changes: 2 additions & 4 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ public function removeByType(string $typeToRemove): bool
$hasChanged = false;

$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->traverseWithCallable($this->phpDocNode, '', function (Node $node) use (
$phpDocNodeTraverser->traverseWithCallable($this->phpDocNode, '', static function (Node $node) use (
$typeToRemove,
&$hasChanged,
&$hasChanged
): ?int {
if ($node instanceof PhpDocTagNode && $node->value instanceof $typeToRemove) {
// keep special annotation for tools
Expand All @@ -269,7 +269,6 @@ public function removeByType(string $typeToRemove): bool
return null;
}

$this->markAsChanged();
$hasChanged = true;
return PhpDocNodeTraverser::NODE_REMOVE;
}
Expand All @@ -278,7 +277,6 @@ public function removeByType(string $typeToRemove): bool
return null;
}

$this->markAsChanged();
$hasChanged = true;
return PhpDocNodeTraverser::NODE_REMOVE;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
Expand All @@ -23,17 +21,13 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
use Rector\BetterPhpDocParser\Guard\NewPhpDocFromPHPStanTypeGuard;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareIntersectionTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode;
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareCallableTypeNode;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\PhpDocParser\ParamPhpDocNodeFactory;
Expand All @@ -59,9 +53,6 @@ public function __construct(
private readonly StaticTypeMapper $staticTypeMapper,
private readonly TypeComparator $typeComparator,
private readonly ParamPhpDocNodeFactory $paramPhpDocNodeFactory,
private readonly NodeNameResolver $nodeNameResolver,
private readonly CommentsMerger $commentsMerger,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly NewPhpDocFromPHPStanTypeGuard $newPhpDocFromPHPStanTypeGuard,
private readonly DocBlockUpdater $docBlockUpdater
) {
Expand Down Expand Up @@ -177,7 +168,6 @@ public function changeParamType(
}

$paramTagValueNode->type = $phpDocTypeNode;
$phpDocInfo->markAsChanged();
} else {
$paramTagValueNode = $this->paramPhpDocNodeFactory->create($phpDocTypeNode, $param);
$phpDocInfo->addTagValueNode($paramTagValueNode);
Expand Down Expand Up @@ -211,46 +201,8 @@ public function isAllowed(TypeNode $typeNode): bool
return in_array((string) $typeNode, self::ALLOWED_IDENTIFIER_TYPENODE_TYPES, true);
}

public function copyPropertyDocToParam(ClassMethod $classMethod, Property $property, Param $param): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($property);
if (! $phpDocInfo instanceof PhpDocInfo) {
return;
}

$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
$this->processKeepComments($classMethod, $property, $param);
return;
}

if ($varTagValueNode->description !== '') {
return;
}

$paramVarName = $this->nodeNameResolver->getName($param->var);
if (! $this->isAllowed($varTagValueNode->type)) {
return;
}

if (! is_string($paramVarName)) {
return;
}

$phpDocInfo->removeByType(VarTagValueNode::class);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property);

$param->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);

$phpDocInfo = $classMethod->getAttribute(AttributeKey::PHP_DOC_INFO);
$paramType = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($varTagValueNode, $property);

$this->changeParamType($classMethod, $phpDocInfo, $paramType, $param, $paramVarName);
$this->processKeepComments($classMethod, $property, $param);
}

/**
* @api doctrine
* @api downgrade
*/
public function changeVarTypeNode(Stmt $stmt, PhpDocInfo $phpDocInfo, TypeNode $typeNode): void
{
Expand All @@ -260,33 +212,4 @@ public function changeVarTypeNode(Stmt $stmt, PhpDocInfo $phpDocInfo, TypeNode $

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($stmt);
}

private function processKeepComments(ClassMethod $classMethod, Property $property, Param $param): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($param);
$varTagValueNode = $phpDocInfo->getVarTagValueNode();

$toBeRemoved = ! $varTagValueNode instanceof VarTagValueNode;
$this->commentsMerger->keepComments($param, [$property]);

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($param);
$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $toBeRemoved) {
return;
}

if (! $varTagValueNode instanceof VarTagValueNode) {
return;
}

if ($varTagValueNode->description !== '') {
return;
}

$phpDocInfo->removeByType(VarTagValueNode::class);

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
}
}
10 changes: 10 additions & 0 deletions packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\BetterPhpDocParser\Printer;

use Nette\Utils\Strings;
use PhpParser\Comment;
use PhpParser\Node\Stmt\InlineHTML;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
Expand Down Expand Up @@ -128,6 +129,15 @@ public function printFormatPreserving(PhpDocInfo $phpDocInfo): string
return Strings::replace($phpDocString, self::CALLABLE_REGEX, 'callable(');
}

/**
* @return Comment[]
*/
public function printToComments(PhpDocInfo $phpDocInfo): array
{
$printedPhpDocContents = $this->printFormatPreserving($phpDocInfo);
return [new Comment($printedPhpDocContents)];
}

private function getCurrentPhpDocInfo(): PhpDocInfo
{
if (! $this->phpDocInfo instanceof PhpDocInfo) {
Expand Down
1 change: 0 additions & 1 deletion packages/Comments/NodeDocBlock/DocBlockUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function updateRefactoredNodeWithPhpDocInfo(Node $node): void

$printedPhpDoc = $this->printPhpDocInfoToString($phpDocInfo);
$node->setDocComment(new Doc($printedPhpDoc));
// $node->setDocComment(new Doc((string) $phpDocNode));
}

private function setCommentsAttribute(Node $node): void
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,6 @@ parameters:
-
message: '#Parameters should use "PhpParser\\Node\\Identifier\|string" types as the only types passed to this method#'
path: packages/NodeNameResolver/NodeNameResolver.php

# remove in next step
- '#Call to deprecated method markAsChanged\(\) of class Rector\\BetterPhpDocParser\\PhpDocInfo\\PhpDocInfo#'
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector\Fixture;

class Comment
final class WithComment
{
public function accept()
{
Expand All @@ -23,7 +23,7 @@ class Comment

namespace Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector\Fixture;

class Comment
final class WithComment
{
public function accept()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromo

final class CopyArrayGenericDoc
{
/**
* @param array<string, string> $map
*/
public function __construct(
/**
* Comment
* @var array<string, string>
*/
private array $map
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,23 @@

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

use Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\ValidatedElement;
use Symfony\Component\Validator\Constraints as Assert;

class Y {}
class Z {}

final class CopyDoc
{
public Y $y;

/**
* @var Z[]
* @var ValidatedElement[]
* @Assert\Valid()
* @Assert\NotBlank()
*/
public array $z = [];

/**
* @param Z[] $z
* @param ValidatedElement[] $z
*/
public function __construct(Y $y, array $z = [])
public function __construct(array $z = [])
{
$this->y = $y;
$this->z = $z;
}
}
Expand All @@ -33,18 +28,15 @@ final class CopyDoc

namespace Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Fixture;

use Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source\ValidatedElement;
use Symfony\Component\Validator\Constraints as Assert;

class Y {}
class Z {}

final class CopyDoc
{
/**
* @param Z[] $z
* @param ValidatedElement[] $z
*/
public function __construct(
public Y $y,
/**
* @Assert\Valid()
* @Assert\NotBlank()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\Source;

final class ValidatedElement
{

}
10 changes: 7 additions & 3 deletions rules/DeadCode/PhpDoc/TagRemover/ParamTagRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\Type\UnionTypeNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\DeadCode\PhpDoc\DeadParamTagValueNodeAnalyzer;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;

final class ParamTagRemover
{
public function __construct(
private readonly DeadParamTagValueNodeAnalyzer $deadParamTagValueNodeAnalyzer
private readonly DeadParamTagValueNodeAnalyzer $deadParamTagValueNodeAnalyzer,
private readonly DocBlockUpdater $docBlockUpdater,
) {
}

Expand All @@ -27,7 +29,6 @@ public function removeParamTagsIfUseless(PhpDocInfo $phpDocInfo, FunctionLike $f
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$phpDocNodeTraverser->traverseWithCallable($phpDocInfo->getPhpDocNode(), '', function (Node $docNode) use (
$functionLike,
$phpDocInfo,
&$hasChanged
): ?int {
if (! $docNode instanceof PhpDocTagNode) {
Expand All @@ -52,11 +53,14 @@ public function removeParamTagsIfUseless(PhpDocInfo $phpDocInfo, FunctionLike $f
return null;
}

$phpDocInfo->markAsChanged();
$hasChanged = true;
return PhpDocNodeTraverser::NODE_REMOVE;
});

if ($hasChanged) {
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($functionLike);
}

return $hasChanged;
}
}
Loading