mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-08 12:20:52 +00:00
use PhpDocTagRemoer as a service (#5242)
Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
parent
66d0b61a9d
commit
4b7f6f4a72
|
@ -40,9 +40,15 @@ var_dump($returnType);
|
|||
|
||||
```php
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
private PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
}
|
||||
|
||||
/** @var PhpDocInfo $phpDocInfo */
|
||||
$phpDocInfo->removeByName('return');
|
||||
$this->phpDocTagRemover->removeByName($phpDocInfo, 'return');
|
||||
```
|
||||
|
||||
## How create PhpDocInfo for a new node?
|
||||
|
|
|
@ -15,17 +15,16 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
|
|||
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
||||
use PHPStan\Type\MixedType;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwareParamTagValueNode;
|
||||
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwarePhpDocNode;
|
||||
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwarePhpDocTagNode;
|
||||
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwareReturnTagValueNode;
|
||||
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
|
||||
use Rector\BetterPhpDocParser\Attributes\Ast\AttributeAwareNodeFactory;
|
||||
use Rector\BetterPhpDocParser\Attributes\Ast\PhpDoc\SpacelessPhpDocTagNode;
|
||||
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
|
||||
use Rector\BetterPhpDocParser\Contract\PhpDocNode\ShortNameAwareTagInterface;
|
||||
use Rector\BetterPhpDocParser\Contract\PhpDocNode\TypeAwareTagValueNodeInterface;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocRemover;
|
||||
use Rector\ChangesReporting\Collector\RectorChangeCollector;
|
||||
use Rector\Core\Configuration\CurrentNodeProvider;
|
||||
use Rector\Core\Exception\NotImplementedException;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\Util\StaticInstanceOf;
|
||||
|
@ -74,16 +73,6 @@ final class PhpDocInfo
|
|||
*/
|
||||
private $node;
|
||||
|
||||
/**
|
||||
* @var PhpDocRemover
|
||||
*/
|
||||
private $phpDocRemover;
|
||||
|
||||
/**
|
||||
* @var AttributeAwareNodeFactory
|
||||
*/
|
||||
private $attributeAwareNodeFactory;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
|
@ -94,6 +83,16 @@ final class PhpDocInfo
|
|||
*/
|
||||
private $annotationNaming;
|
||||
|
||||
/**
|
||||
* @var CurrentNodeProvider
|
||||
*/
|
||||
private $currentNodeProvider;
|
||||
|
||||
/**
|
||||
* @var RectorChangeCollector
|
||||
*/
|
||||
private $rectorChangeCollector;
|
||||
|
||||
/**
|
||||
* @param mixed[] $tokens
|
||||
*/
|
||||
|
@ -103,9 +102,9 @@ final class PhpDocInfo
|
|||
string $originalContent,
|
||||
StaticTypeMapper $staticTypeMapper,
|
||||
Node $node,
|
||||
PhpDocRemover $phpDocRemover,
|
||||
AttributeAwareNodeFactory $attributeAwareNodeFactory,
|
||||
AnnotationNaming $annotationNaming
|
||||
AnnotationNaming $annotationNaming,
|
||||
CurrentNodeProvider $currentNodeProvider,
|
||||
RectorChangeCollector $rectorChangeCollector
|
||||
) {
|
||||
$this->phpDocNode = $attributeAwarePhpDocNode;
|
||||
$this->tokens = $tokens;
|
||||
|
@ -113,9 +112,9 @@ final class PhpDocInfo
|
|||
$this->originalContent = $originalContent;
|
||||
$this->staticTypeMapper = $staticTypeMapper;
|
||||
$this->node = $node;
|
||||
$this->phpDocRemover = $phpDocRemover;
|
||||
$this->attributeAwareNodeFactory = $attributeAwareNodeFactory;
|
||||
$this->annotationNaming = $annotationNaming;
|
||||
$this->currentNodeProvider = $currentNodeProvider;
|
||||
$this->rectorChangeCollector = $rectorChangeCollector;
|
||||
}
|
||||
|
||||
public function getOriginalContent(): string
|
||||
|
@ -217,11 +216,6 @@ final class PhpDocInfo
|
|||
return $this->getTypeOrMixed($this->getReturnTagValue());
|
||||
}
|
||||
|
||||
public function removeTagValueNodeFromNode(PhpDocTagValueNode $phpDocTagValueNode): void
|
||||
{
|
||||
$this->phpDocRemover->removeTagValueFromNode($this, $phpDocTagValueNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @template T as \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode
|
||||
* @param class-string<T> $type
|
||||
|
@ -322,11 +316,6 @@ final class PhpDocInfo
|
|||
}
|
||||
}
|
||||
|
||||
public function removeByName(string $name): void
|
||||
{
|
||||
$this->phpDocRemover->removeByName($this, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, Type>
|
||||
*/
|
||||
|
@ -411,19 +400,9 @@ final class PhpDocInfo
|
|||
return $returnTagValueNodes[0] ?? null;
|
||||
}
|
||||
|
||||
public function getParamTagValueByName(string $name): ?AttributeAwareParamTagValueNode
|
||||
public function getParamTagValueByName(string $name): ?ParamTagValueNode
|
||||
{
|
||||
$paramTagValueNode = $this->phpDocNode->getParam($name);
|
||||
if ($paramTagValueNode === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$attributeAwareParamTagValueNode = $this->attributeAwareNodeFactory->createFromNode($paramTagValueNode, '');
|
||||
if (! $attributeAwareParamTagValueNode instanceof AttributeAwareParamTagValueNode) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
|
||||
return $attributeAwareParamTagValueNode;
|
||||
return $this->phpDocNode->getParam($name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -442,6 +421,11 @@ final class PhpDocInfo
|
|||
public function markAsChanged(): void
|
||||
{
|
||||
$this->hasChanged = true;
|
||||
|
||||
$node = $this->currentNodeProvider->getNode();
|
||||
if ($node !== null) {
|
||||
$this->rectorChangeCollector->notifyNodeFileInfo($node);
|
||||
}
|
||||
}
|
||||
|
||||
public function hasChanged(): bool
|
||||
|
|
|
@ -14,9 +14,9 @@ use Rector\BetterPhpDocParser\Attributes\Ast\AttributeAwareNodeFactory;
|
|||
use Rector\BetterPhpDocParser\Attributes\Attribute\Attribute;
|
||||
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
|
||||
use Rector\BetterPhpDocParser\Contract\PhpDocNodeFactoryInterface;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocRemover;
|
||||
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
|
||||
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
|
||||
use Rector\ChangesReporting\Collector\RectorChangeCollector;
|
||||
use Rector\Core\Configuration\CurrentNodeProvider;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\StaticTypeMapper\StaticTypeMapper;
|
||||
|
@ -48,32 +48,32 @@ final class PhpDocInfoFactory
|
|||
*/
|
||||
private $attributeAwareNodeFactory;
|
||||
|
||||
/**
|
||||
* @var PhpDocRemover
|
||||
*/
|
||||
private $phpDocRemover;
|
||||
|
||||
/**
|
||||
* @var AnnotationNaming
|
||||
*/
|
||||
private $annotationNaming;
|
||||
|
||||
/**
|
||||
* @var RectorChangeCollector
|
||||
*/
|
||||
private $rectorChangeCollector;
|
||||
|
||||
public function __construct(
|
||||
AttributeAwareNodeFactory $attributeAwareNodeFactory,
|
||||
CurrentNodeProvider $currentNodeProvider,
|
||||
Lexer $lexer,
|
||||
BetterPhpDocParser $betterPhpDocParser,
|
||||
PhpDocRemover $phpDocRemover,
|
||||
StaticTypeMapper $staticTypeMapper,
|
||||
AnnotationNaming $annotationNaming
|
||||
AnnotationNaming $annotationNaming,
|
||||
RectorChangeCollector $rectorChangeCollector
|
||||
) {
|
||||
$this->betterPhpDocParser = $betterPhpDocParser;
|
||||
$this->lexer = $lexer;
|
||||
$this->currentNodeProvider = $currentNodeProvider;
|
||||
$this->staticTypeMapper = $staticTypeMapper;
|
||||
$this->attributeAwareNodeFactory = $attributeAwareNodeFactory;
|
||||
$this->phpDocRemover = $phpDocRemover;
|
||||
$this->annotationNaming = $annotationNaming;
|
||||
$this->rectorChangeCollector = $rectorChangeCollector;
|
||||
}
|
||||
|
||||
public function createFromNodeOrEmpty(Node $node): PhpDocInfo
|
||||
|
@ -179,9 +179,9 @@ final class PhpDocInfoFactory
|
|||
$content,
|
||||
$this->staticTypeMapper,
|
||||
$node,
|
||||
$this->phpDocRemover,
|
||||
$this->attributeAwareNodeFactory,
|
||||
$this->annotationNaming
|
||||
$this->annotationNaming,
|
||||
$this->currentNodeProvider,
|
||||
$this->rectorChangeCollector
|
||||
);
|
||||
|
||||
$node->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
|
||||
|
|
|
@ -8,7 +8,7 @@ use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
|||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
|
||||
final class PhpDocRemover
|
||||
final class PhpDocTagRemover
|
||||
{
|
||||
public function removeByName(PhpDocInfo $phpDocInfo, string $name): void
|
||||
{
|
|
@ -12,8 +12,8 @@ use PhpParser\Node\Stmt\Class_;
|
|||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\PhpAttribute\Contract\PhpAttributableTagNodeInterface;
|
||||
use Rector\PhpAttribute\Printer\PhpAttributteGroupFactory;
|
||||
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
|
||||
|
@ -30,12 +30,19 @@ final class AnnotationToAttributeConverter
|
|||
*/
|
||||
private $phpDocInfoFactory;
|
||||
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
PhpAttributteGroupFactory $phpAttributteGroupFactory,
|
||||
PhpDocInfoFactory $phpDocInfoFactory
|
||||
PhpDocInfoFactory $phpDocInfoFactory,
|
||||
PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
$this->phpAttributteGroupFactory = $phpAttributteGroupFactory;
|
||||
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,8 +69,7 @@ final class AnnotationToAttributeConverter
|
|||
|
||||
// 2. remove tags
|
||||
foreach ($phpAttributableTagNodes as $phpAttributableTagNode) {
|
||||
/** @var PhpDocTagValueNode $phpAttributableTagNode */
|
||||
$phpDocInfo->removeTagValueNodeFromNode($phpAttributableTagNode);
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $phpAttributableTagNode);
|
||||
}
|
||||
|
||||
// 3. convert annotations to attributes
|
||||
|
|
|
@ -536,3 +536,4 @@ parameters:
|
|||
- '#Parameter \#1 \$type of method Rector\\BetterPhpDocParser\\PhpDocInfo\\PhpDocInfo\:\:hasByType\(\) expects class\-string<PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagValueNode\>, string given#'
|
||||
- '#Method Rector\\BetterPhpDocParser\\PhpDocInfo\\PhpDocInfo\:\:findAllByType\(\) should return array<T of PHPStan\\PhpDocParser\\Ast\\Node\> but returns array<int, PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagNode\|\(PHPStan\\PhpDocParser\\Ast\\PhpDoc\\PhpDocTagValueNode&T of PHPStan\\PhpDocParser\\Ast\\Node\)\>#'
|
||||
- '#Content of method "inferFunctionLike\(\)" is duplicated with method "getNodeReturnPhpDocType\(\)" in "Rector\\TypeDeclaration\\Rector\\ClassMethod\\AddArrayReturnDocTypeRector" class\. Use unique content or abstract service instead#'
|
||||
- '#Method Rector\\BetterPhpDocParser\\PhpDocInfo\\PhpDocInfo\:\:getParamTagValueByName\(\) should return Rector\\AttributeAwarePhpDoc\\Ast\\PhpDoc\\AttributeAwareParamTagValueNode\|null but returns PHPStan\\PhpDocParser\\Ast\\PhpDoc\\ParamTagValueNode\|null#'
|
||||
|
|
|
@ -8,6 +8,7 @@ use PhpParser\Node;
|
|||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Caching\Contract\Rector\ZeroCacheRectorInterface;
|
||||
use Rector\Core\PhpParser\Node\Manipulator\ClassManipulator;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
|
@ -46,16 +47,23 @@ final class RemoveUnusedParameterRector extends AbstractRector implements ZeroCa
|
|||
*/
|
||||
private $unusedParameterResolver;
|
||||
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
ClassManipulator $classManipulator,
|
||||
MagicMethodDetector $magicMethodDetector,
|
||||
VariadicFunctionLikeDetector $variadicFunctionLikeDetector,
|
||||
UnusedParameterResolver $unusedParameterResolver
|
||||
UnusedParameterResolver $unusedParameterResolver,
|
||||
PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
$this->classManipulator = $classManipulator;
|
||||
$this->magicMethodDetector = $magicMethodDetector;
|
||||
$this->variadicFunctionLikeDetector = $variadicFunctionLikeDetector;
|
||||
$this->unusedParameterResolver = $unusedParameterResolver;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -196,7 +204,7 @@ CODE_SAMPLE
|
|||
continue;
|
||||
}
|
||||
|
||||
$phpDocInfo->removeTagValueNodeFromNode($paramTagValueNode);
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $paramTagValueNode);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace Rector\DeadDocBlock\Rector\ClassMethod;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DeadDocBlock\DeadParamTagValueNodeAnalyzer;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -21,9 +22,17 @@ final class RemoveUselessParamTagRector extends AbstractRector
|
|||
*/
|
||||
private $deadParamTagValueNodeAnalyzer;
|
||||
|
||||
public function __construct(DeadParamTagValueNodeAnalyzer $deadParamTagValueNodeAnalyzer)
|
||||
{
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
DeadParamTagValueNodeAnalyzer $deadParamTagValueNodeAnalyzer,
|
||||
PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
$this->deadParamTagValueNodeAnalyzer = $deadParamTagValueNodeAnalyzer;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -76,20 +85,18 @@ CODE_SAMPLE
|
|||
{
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
||||
|
||||
$hasChanged = false;
|
||||
foreach ($phpDocInfo->getParamTagValueNodes() as $paramTagValueNode) {
|
||||
if (! $this->deadParamTagValueNodeAnalyzer->isDead($paramTagValueNode, $node)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$phpDocInfo->removeTagValueNodeFromNode($paramTagValueNode);
|
||||
$hasChanged = true;
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $paramTagValueNode);
|
||||
}
|
||||
|
||||
if (! $hasChanged) {
|
||||
return null;
|
||||
if ($phpDocInfo->hasChanged()) {
|
||||
return $node;
|
||||
}
|
||||
|
||||
return $node;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace Rector\DeadDocBlock\Rector\ClassMethod;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\DeadDocBlock\DeadReturnTagValueNodeAnalyzer;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -21,9 +22,17 @@ final class RemoveUselessReturnTagRector extends AbstractRector
|
|||
*/
|
||||
private $deadReturnTagValueNodeAnalyzer;
|
||||
|
||||
public function __construct(DeadReturnTagValueNodeAnalyzer $deadReturnTagValueNodeAnalyzer)
|
||||
{
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
DeadReturnTagValueNodeAnalyzer $deadReturnTagValueNodeAnalyzer,
|
||||
PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
$this->deadReturnTagValueNodeAnalyzer = $deadReturnTagValueNodeAnalyzer;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -85,7 +94,7 @@ CODE_SAMPLE
|
|||
return null;
|
||||
}
|
||||
|
||||
$phpDocInfo->removeTagValueNodeFromNode($attributeAwareReturnTagValueNode);
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $attributeAwareReturnTagValueNode);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace Rector\DeadDocBlock\TagRemover;
|
|||
|
||||
use PhpParser\Node\FunctionLike;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\DeadDocBlock\DeadParamTagValueNodeAnalyzer;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
|
@ -16,9 +17,17 @@ final class ParamTagRemover
|
|||
*/
|
||||
private $deadParamTagValueNodeAnalyzer;
|
||||
|
||||
public function __construct(DeadParamTagValueNodeAnalyzer $deadParamTagValueNodeAnalyzer)
|
||||
{
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
DeadParamTagValueNodeAnalyzer $deadParamTagValueNodeAnalyzer,
|
||||
PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
$this->deadParamTagValueNodeAnalyzer = $deadParamTagValueNodeAnalyzer;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function removeParamTagsIfUseless(FunctionLike $functionLike): void
|
||||
|
@ -43,7 +52,7 @@ final class ParamTagRemover
|
|||
continue;
|
||||
}
|
||||
|
||||
$phpDocInfo->removeTagValueNodeFromNode($paramTagValueNode);
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $paramTagValueNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ namespace Rector\DeadDocBlock\TagRemover;
|
|||
use PhpParser\Node\FunctionLike;
|
||||
use PHPStan\Type\Type;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\DeadDocBlock\DeadReturnTagValueNodeAnalyzer;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
|
@ -17,9 +18,17 @@ final class ReturnTagRemover
|
|||
*/
|
||||
private $deadReturnTagValueNodeAnalyzer;
|
||||
|
||||
public function __construct(DeadReturnTagValueNodeAnalyzer $deadReturnTagValueNodeAnalyzer)
|
||||
{
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
DeadReturnTagValueNodeAnalyzer $deadReturnTagValueNodeAnalyzer,
|
||||
PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
$this->deadReturnTagValueNodeAnalyzer = $deadReturnTagValueNodeAnalyzer;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function removeReturnTagIfUseless(FunctionLike $functionLike): void
|
||||
|
@ -43,6 +52,6 @@ final class ReturnTagRemover
|
|||
return;
|
||||
}
|
||||
|
||||
$phpDocInfo->removeTagValueNodeFromNode($attributeAwareReturnTagValueNode);
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $attributeAwareReturnTagValueNode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ use Rector\BetterPhpDocParser\Contract\Doctrine\DoctrineRelationTagValueNodeInte
|
|||
use Rector\BetterPhpDocParser\Contract\Doctrine\ToManyTagNodeInterface;
|
||||
use Rector\BetterPhpDocParser\Contract\Doctrine\ToOneTagNodeInterface;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Doctrine\Property_\JoinColumnTagValueNode;
|
||||
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Doctrine\Property_\OneToOneTagValueNode;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
|
@ -39,12 +40,19 @@ final class AddUuidMirrorForRelationPropertyRector extends AbstractRector
|
|||
*/
|
||||
private $uuidMigrationDataCollector;
|
||||
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
PhpDocTagNodeFactory $phpDocTagNodeFactory,
|
||||
UuidMigrationDataCollector $uuidMigrationDataCollector
|
||||
UuidMigrationDataCollector $uuidMigrationDataCollector,
|
||||
PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
$this->phpDocTagNodeFactory = $phpDocTagNodeFactory;
|
||||
$this->uuidMigrationDataCollector = $uuidMigrationDataCollector;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -258,16 +266,16 @@ CODE_SAMPLE
|
|||
);
|
||||
}
|
||||
|
||||
private function refactorToManyPropertyPhpDocInfo(PhpDocInfo $propertyPhpDocInfo, Property $property): void
|
||||
private function refactorToManyPropertyPhpDocInfo(PhpDocInfo $phpDocInfo, Property $property): void
|
||||
{
|
||||
$doctrineJoinColumnTagValueNode = $propertyPhpDocInfo->getByType(JoinColumnTagValueNode::class);
|
||||
$doctrineJoinColumnTagValueNode = $phpDocInfo->getByType(JoinColumnTagValueNode::class);
|
||||
if ($doctrineJoinColumnTagValueNode !== null) {
|
||||
// replace @ORM\JoinColumn with @ORM\JoinTable
|
||||
$propertyPhpDocInfo->removeTagValueNodeFromNode($doctrineJoinColumnTagValueNode);
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $doctrineJoinColumnTagValueNode);
|
||||
}
|
||||
|
||||
$joinTableTagNode = $this->phpDocTagNodeFactory->createJoinTableTagNode($property);
|
||||
$propertyPhpDocInfo->addPhpDocTagNode($joinTableTagNode);
|
||||
$phpDocInfo->addPhpDocTagNode($joinTableTagNode);
|
||||
}
|
||||
|
||||
private function refactorToOnePropertyPhpDocInfo(PhpDocInfo $propertyPhpDocInfo): void
|
||||
|
|
|
@ -9,6 +9,7 @@ use PhpParser\Node\FunctionLike;
|
|||
use PhpParser\Node\Stmt\ClassConst;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
||||
|
@ -30,6 +31,16 @@ final class RemoveAnnotationRector extends AbstractRector implements Configurabl
|
|||
*/
|
||||
private $annotationsToRemove = [];
|
||||
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(PhpDocTagRemover $phpDocTagRemover)
|
||||
{
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition('Remove annotation by names', [
|
||||
|
@ -75,17 +86,15 @@ CODE_SAMPLE
|
|||
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
||||
|
||||
$hasChanged = false;
|
||||
foreach ($this->annotationsToRemove as $annotationToRemove) {
|
||||
if (! $phpDocInfo->hasByName($annotationToRemove)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$phpDocInfo->removeByName($annotationToRemove);
|
||||
$hasChanged = true;
|
||||
$this->phpDocTagRemover->removeByName($phpDocInfo, $annotationToRemove);
|
||||
}
|
||||
|
||||
if (! $hasChanged) {
|
||||
if ($phpDocInfo->hasChanged()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ namespace Rector\Generic\Rector\Property;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
use Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer;
|
||||
|
@ -37,10 +38,19 @@ final class AnnotatedPropertyInjectToConstructorInjectionRector extends Abstract
|
|||
*/
|
||||
private $classChildAnalyzer;
|
||||
|
||||
public function __construct(ClassChildAnalyzer $classChildAnalyzer, PropertyUsageAnalyzer $propertyUsageAnalyzer)
|
||||
{
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
ClassChildAnalyzer $classChildAnalyzer,
|
||||
PropertyUsageAnalyzer $propertyUsageAnalyzer,
|
||||
PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
$this->propertyUsageAnalyzer = $propertyUsageAnalyzer;
|
||||
$this->classChildAnalyzer = $classChildAnalyzer;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -91,7 +101,7 @@ CODE_SAMPLE
|
|||
}
|
||||
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
|
||||
$phpDocInfo->removeByName(TagName::INJECT);
|
||||
$this->phpDocTagRemover->removeByName($phpDocInfo, TagName::INJECT);
|
||||
|
||||
if ($this->propertyUsageAnalyzer->isPropertyFetchedInChildClass($node)) {
|
||||
$this->makeProtected($node);
|
||||
|
|
|
@ -7,6 +7,7 @@ namespace Rector\NetteCodeQuality\Rector\Class_;
|
|||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\MethodName;
|
||||
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||
|
@ -27,9 +28,15 @@ final class MoveInjectToExistingConstructorRector extends AbstractRector
|
|||
*/
|
||||
private $propertyUsageAnalyzer;
|
||||
|
||||
public function __construct(PropertyUsageAnalyzer $propertyUsageAnalyzer)
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(PropertyUsageAnalyzer $propertyUsageAnalyzer, PhpDocTagRemover $phpDocTagRemover)
|
||||
{
|
||||
$this->propertyUsageAnalyzer = $propertyUsageAnalyzer;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -133,7 +140,7 @@ CODE_SAMPLE
|
|||
private function removeInjectAnnotation(Property $property): void
|
||||
{
|
||||
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
|
||||
$phpDocInfo->removeByName(TagName::INJECT);
|
||||
$this->phpDocTagRemover->removeByName($phpDocInfo, TagName::INJECT);
|
||||
}
|
||||
|
||||
private function changePropertyVisibility(Property $injectProperty): void
|
||||
|
|
|
@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\Class_;
|
|||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\ValueObject\MethodName;
|
||||
use Rector\DeadDocBlock\TagRemover\VarTagRemover;
|
||||
|
@ -42,14 +43,21 @@ final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRect
|
|||
*/
|
||||
private $varTagRemover;
|
||||
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
PromotedPropertyResolver $promotedPropertyResolver,
|
||||
VariableRenamer $variableRenamer,
|
||||
VarTagRemover $varTagRemover
|
||||
VarTagRemover $varTagRemover,
|
||||
PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
$this->promotedPropertyResolver = $promotedPropertyResolver;
|
||||
$this->variableRenamer = $variableRenamer;
|
||||
$this->varTagRemover = $varTagRemover;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -167,6 +175,6 @@ CODE_SAMPLE
|
|||
return;
|
||||
}
|
||||
|
||||
$phpDocInfo->removeTagValueNodeFromNode($attributeAwareParamTagValueNode);
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $attributeAwareParamTagValueNode);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace Rector\PHPUnit\Rector\ClassMethod;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Core\Rector\AbstractPHPUnitRector;
|
||||
use Rector\PHPUnit\NodeFactory\ExpectExceptionMethodCallFactory;
|
||||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||||
|
@ -36,9 +37,17 @@ final class ExceptionAnnotationRector extends AbstractPHPUnitRector
|
|||
*/
|
||||
private $expectExceptionMethodCallFactory;
|
||||
|
||||
public function __construct(ExpectExceptionMethodCallFactory $expectExceptionMethodCallFactory)
|
||||
{
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(
|
||||
ExpectExceptionMethodCallFactory $expectExceptionMethodCallFactory,
|
||||
PhpDocTagRemover $phpDocTagRemover
|
||||
) {
|
||||
$this->expectExceptionMethodCallFactory = $expectExceptionMethodCallFactory;
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
|
@ -100,7 +109,7 @@ CODE_SAMPLE
|
|||
);
|
||||
$node->stmts = array_merge($methodCallExpressions, (array) $node->stmts);
|
||||
|
||||
$phpDocInfo->removeByName($annotationName);
|
||||
$this->phpDocTagRemover->removeByName($phpDocInfo, $annotationName);
|
||||
}
|
||||
|
||||
return $node;
|
||||
|
|
|
@ -10,6 +10,7 @@ use PhpParser\Node\Stmt\Class_;
|
|||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Sensio\SensioRouteTagValueNode;
|
||||
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Symfony\SymfonyRouteTagValueNode;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
|
@ -24,6 +25,16 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
*/
|
||||
final class ReplaceSensioRouteAnnotationWithSymfonyRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(PhpDocTagRemover $phpDocTagRemover)
|
||||
{
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
|
@ -95,7 +106,8 @@ CODE_SAMPLE
|
|||
|
||||
/** @var SensioRouteTagValueNode $sensioRouteTagValueNode */
|
||||
$sensioRouteTagValueNode = $phpDocInfo->getByType(SensioRouteTagValueNode::class);
|
||||
$phpDocInfo->removeTagValueNodeFromNode($sensioRouteTagValueNode);
|
||||
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $sensioRouteTagValueNode);
|
||||
|
||||
// unset service, that is deprecated
|
||||
$items = $sensioRouteTagValueNode->getItems();
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace Rector\Symfony3\Rector\ClassMethod;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Sensio\SensioMethodTagValueNode;
|
||||
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Symfony\SymfonyRouteTagValueNode;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
|
@ -21,6 +22,16 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
*/
|
||||
final class MergeMethodAnnotationToRouteAnnotationRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(PhpDocTagRemover $phpDocTagRemover)
|
||||
{
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
|
@ -99,7 +110,7 @@ CODE_SAMPLE
|
|||
|
||||
$symfonyRouteTagValueNode->changeMethods($methods);
|
||||
|
||||
$phpDocInfo->removeTagValueNodeFromNode($sensioMethodTagValueNode);
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $sensioMethodTagValueNode);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ use PHPStan\Type\NullType;
|
|||
use PHPStan\Type\Type;
|
||||
use PHPStan\Type\UnionType;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
|
||||
|
@ -28,6 +29,16 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|||
*/
|
||||
final class FlipTypeControlToUseExclusiveTypeRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var PhpDocTagRemover
|
||||
*/
|
||||
private $phpDocTagRemover;
|
||||
|
||||
public function __construct(PhpDocTagRemover $phpDocTagRemover)
|
||||
{
|
||||
$this->phpDocTagRemover = $phpDocTagRemover;
|
||||
}
|
||||
|
||||
public function getRuleDefinition(): RuleDefinition
|
||||
{
|
||||
return new RuleDefinition(
|
||||
|
@ -127,7 +138,8 @@ CODE_SAMPLE
|
|||
|
||||
/** @var VarTagValueNode $tagValueNode */
|
||||
$tagValueNode = $phpDocInfo->getVarTagValueNode();
|
||||
$phpDocInfo->removeTagValueNodeFromNode($tagValueNode);
|
||||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $tagValueNode);
|
||||
|
||||
return new BooleanNot(new Instanceof_($expr, new FullyQualified($type->getClassName())));
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
|
|||
use Rector\Core\Util\StaticInstanceOf;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
use Symplify\PackageBuilder\Php\TypeChecker;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
|
@ -40,14 +41,21 @@ final class BetterNodeFinder
|
|||
*/
|
||||
private $betterStandardPrinter;
|
||||
|
||||
/**
|
||||
* @var TypeChecker
|
||||
*/
|
||||
private $typeChecker;
|
||||
|
||||
public function __construct(
|
||||
BetterStandardPrinter $betterStandardPrinter,
|
||||
NodeFinder $nodeFinder,
|
||||
NodeNameResolver $nodeNameResolver
|
||||
NodeNameResolver $nodeNameResolver,
|
||||
TypeChecker $typeChecker
|
||||
) {
|
||||
$this->nodeFinder = $nodeFinder;
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->betterStandardPrinter = $betterStandardPrinter;
|
||||
$this->typeChecker = $typeChecker;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -329,15 +337,7 @@ final class BetterNodeFinder
|
|||
public function findFirstPreviousOfTypes(Node $mainNode, array $types): ?Node
|
||||
{
|
||||
return $this->findFirstPrevious($mainNode, function (Node $node) use ($types): bool {
|
||||
foreach ($types as $type) {
|
||||
if (! is_a($node, $type, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return $this->typeChecker->isInstanceOf($node, $types);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user