Skip to content

Commit

Permalink
[CodeQuality] Skip no @Property doc on DynamicDocBlockPropertyToNativ…
Browse files Browse the repository at this point in the history
…ePropertyRector (#6380)
  • Loading branch information
samsonasik authored Oct 12, 2024
1 parent 684f23d commit d180b8d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Fixture;

use Rector\Tests\CodeQuality\Rector\Class_\DynamicDocBlockPropertyToNativePropertyRector\Source\SomeDependency;

/**
* @see SomeDependency
*/
#[\AllowDynamicProperties]
final class SkipNoPropertyDoc
{
public function run(): void
{
$this->someDependency = new SomeDependency();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,34 @@ public function refactor(Node $node): ?Node
return null;
}

// 1. remove dynamic attribute, most likely any
$node->attrGroups = [];

// 2. add defined @property explicitly
$classPhpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if ($classPhpDocInfo instanceof PhpDocInfo) {
$propertyPhpDocTagNodes = $classPhpDocInfo->getTagsByName('property');
if (! $classPhpDocInfo instanceof PhpDocInfo) {
return null;
}

$newProperties = $this->createNewPropertyFromPropertyTagValueNodes($propertyPhpDocTagNodes, $node);
$propertyPhpDocTagNodes = $classPhpDocInfo->getTagsByName('property');
if ($propertyPhpDocTagNodes === []) {
return null;
}

// remove property tags
foreach ($propertyPhpDocTagNodes as $propertyPhpDocTagNode) {
// remove from docblock
$this->phpDocTagRemover->removeTagValueFromNode($classPhpDocInfo, $propertyPhpDocTagNode);
}
// 1. remove dynamic attribute, most likely any
$node->attrGroups = [];

// merge new properties to start of the file
$node->stmts = array_merge($newProperties, $node->stmts);
$newProperties = $this->createNewPropertyFromPropertyTagValueNodes($propertyPhpDocTagNodes, $node);

// update doc info
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);
// remove property tags
foreach ($propertyPhpDocTagNodes as $propertyPhpDocTagNode) {
// remove from docblock
$this->phpDocTagRemover->removeTagValueFromNode($classPhpDocInfo, $propertyPhpDocTagNode);
}

// merge new properties to start of the file
$node->stmts = array_merge($newProperties, $node->stmts);

// update doc info
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node);

return $node;
}

Expand Down

0 comments on commit d180b8d

Please sign in to comment.