2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2019-02-28 21:50:53 +00:00
|
|
|
namespace Rector\BetterPhpDocParser\PhpDocInfo;
|
|
|
|
|
2021-03-20 15:27:18 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\Node;
|
2021-04-09 17:36:06 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode;
|
2021-01-29 16:24:20 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
|
2020-02-04 08:39:57 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
|
2020-01-29 13:36:09 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
|
2021-03-20 15:27:18 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
|
2019-02-28 21:50:53 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
|
2021-01-29 16:24:20 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
|
2020-02-02 18:15:36 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
|
2020-12-28 13:01:27 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
|
2020-02-02 18:15:36 +00:00
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
|
2021-04-06 18:36:50 +00:00
|
|
|
use PHPStan\PhpDocParser\Lexer\Lexer;
|
2019-09-06 10:30:58 +00:00
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
use PHPStan\Type\Type;
|
2021-01-19 15:03:26 +00:00
|
|
|
use Rector\BetterPhpDocParser\Annotation\AnnotationNaming;
|
2021-04-04 09:01:11 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
|
2021-04-06 17:33:09 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode;
|
2021-07-01 14:10:40 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder;
|
2021-04-06 17:33:09 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor;
|
2021-04-06 18:36:50 +00:00
|
|
|
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
|
2021-01-19 19:45:30 +00:00
|
|
|
use Rector\ChangesReporting\Collector\RectorChangeCollector;
|
|
|
|
use Rector\Core\Configuration\CurrentNodeProvider;
|
2021-01-31 00:14:13 +00:00
|
|
|
use Rector\Core\Exception\NotImplementedYetException;
|
2020-02-10 09:17:05 +00:00
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2021-08-08 00:26:40 +00:00
|
|
|
use RectorPrefix20210808\Symplify\SimplePhpDocParser\PhpDocNodeTraverser;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-02-07 21:22:03 +00:00
|
|
|
* @template TNode as \PHPStan\PhpDocParser\Ast\Node
|
2021-03-12 19:13:27 +00:00
|
|
|
* @see \Rector\Tests\BetterPhpDocParser\PhpDocInfo\PhpDocInfo\PhpDocInfoTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2019-02-28 21:50:53 +00:00
|
|
|
final class PhpDocInfo
|
|
|
|
{
|
2021-01-21 20:58:26 +00:00
|
|
|
/**
|
2021-01-29 16:24:20 +00:00
|
|
|
* @var array<class-string<PhpDocTagValueNode>, string>
|
2021-01-21 20:58:26 +00:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
private const TAGS_TYPES_TO_NAMES = [\PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode::class => '@return', \PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode::class => '@param', \PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode::class => '@var', \PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode::class => '@method', \PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode::class => '@property'];
|
2020-07-31 20:20:45 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
private $isSingleLine = \false;
|
2019-02-28 21:50:53 +00:00
|
|
|
/**
|
2021-05-12 13:15:45 +00:00
|
|
|
* @var \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode
|
2019-02-28 21:50:53 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $originalPhpDocNode;
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $hasChanged = \false;
|
|
|
|
/**
|
|
|
|
* @var \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode
|
|
|
|
*/
|
2019-02-28 21:50:53 +00:00
|
|
|
private $phpDocNode;
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator
|
2019-02-28 21:50:53 +00:00
|
|
|
*/
|
2021-05-10 23:39:21 +00:00
|
|
|
private $betterTokenIterator;
|
2019-09-06 10:30:58 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
2019-09-06 10:30:58 +00:00
|
|
|
*/
|
|
|
|
private $staticTypeMapper;
|
|
|
|
/**
|
2021-03-20 15:27:18 +00:00
|
|
|
* @var \PhpParser\Node
|
2019-09-06 10:30:58 +00:00
|
|
|
*/
|
|
|
|
private $node;
|
2020-07-28 23:41:20 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\Annotation\AnnotationNaming
|
2020-10-11 14:17:43 +00:00
|
|
|
*/
|
2021-01-19 19:45:30 +00:00
|
|
|
private $annotationNaming;
|
2021-01-19 15:03:26 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\Configuration\CurrentNodeProvider
|
2021-01-19 15:03:26 +00:00
|
|
|
*/
|
2021-01-19 19:45:30 +00:00
|
|
|
private $currentNodeProvider;
|
2021-01-19 15:03:26 +00:00
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\ChangesReporting\Collector\RectorChangeCollector
|
2021-01-19 15:03:26 +00:00
|
|
|
*/
|
2021-01-19 19:45:30 +00:00
|
|
|
private $rectorChangeCollector;
|
2021-07-01 14:10:40 +00:00
|
|
|
/**
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder
|
|
|
|
*/
|
|
|
|
private $phpDocNodeByTypeFinder;
|
2021-07-01 14:46:27 +00:00
|
|
|
public function __construct(\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode $phpDocNode, \Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator $betterTokenIterator, \Rector\StaticTypeMapper\StaticTypeMapper $staticTypeMapper, \PhpParser\Node $node, \Rector\BetterPhpDocParser\Annotation\AnnotationNaming $annotationNaming, \Rector\Core\Configuration\CurrentNodeProvider $currentNodeProvider, \Rector\ChangesReporting\Collector\RectorChangeCollector $rectorChangeCollector, \Rector\BetterPhpDocParser\PhpDocNodeFinder\PhpDocNodeByTypeFinder $phpDocNodeByTypeFinder)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-03-20 15:27:18 +00:00
|
|
|
$this->phpDocNode = $phpDocNode;
|
2021-04-06 18:36:50 +00:00
|
|
|
$this->betterTokenIterator = $betterTokenIterator;
|
2019-09-06 10:30:58 +00:00
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
|
|
|
$this->node = $node;
|
2021-01-19 15:03:26 +00:00
|
|
|
$this->annotationNaming = $annotationNaming;
|
2021-01-19 19:45:30 +00:00
|
|
|
$this->currentNodeProvider = $currentNodeProvider;
|
|
|
|
$this->rectorChangeCollector = $rectorChangeCollector;
|
2021-07-01 14:10:40 +00:00
|
|
|
$this->phpDocNodeByTypeFinder = $phpDocNodeByTypeFinder;
|
2021-05-10 23:39:21 +00:00
|
|
|
$this->originalPhpDocNode = clone $phpDocNode;
|
|
|
|
if (!$betterTokenIterator->containsTokenType(\PHPStan\PhpDocParser\Lexer\Lexer::TOKEN_PHPDOC_EOL)) {
|
|
|
|
$this->isSingleLine = \true;
|
|
|
|
}
|
2019-02-28 21:50:53 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function addPhpDocTagNode(\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode $phpDocChildNode) : void
|
2020-01-29 13:36:09 +00:00
|
|
|
{
|
|
|
|
$this->phpDocNode->children[] = $phpDocChildNode;
|
2021-04-04 15:37:02 +00:00
|
|
|
// to give node more space
|
|
|
|
$this->makeMultiLined();
|
2021-05-07 23:34:29 +00:00
|
|
|
$this->markAsChanged();
|
2020-01-29 13:36:09 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getPhpDocNode() : \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
|
|
|
return $this->phpDocNode;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getOriginalPhpDocNode() : \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
|
|
|
return $this->originalPhpDocNode;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getTokens() : array
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
2021-04-06 18:36:50 +00:00
|
|
|
return $this->betterTokenIterator->getTokens();
|
2019-02-28 21:50:53 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getTokenCount() : int
|
2020-03-29 11:08:25 +00:00
|
|
|
{
|
2021-04-06 18:36:50 +00:00
|
|
|
return $this->betterTokenIterator->count();
|
2020-03-29 11:08:25 +00:00
|
|
|
}
|
2021-06-25 15:32:16 +00:00
|
|
|
public function getVarTagValueNode(string $tagName = '@var') : ?\PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
2021-06-25 15:32:16 +00:00
|
|
|
return $this->phpDocNode->getVarTagValues($tagName)[0] ?? null;
|
2019-02-28 21:50:53 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-03-19 14:33:58 +00:00
|
|
|
* @return array<PhpDocTagNode>
|
2019-02-28 21:50:53 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getTagsByName(string $name) : array
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
2021-06-30 21:52:31 +00:00
|
|
|
// for simple tag names only
|
|
|
|
if (\strpos($name, '\\') !== \false) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-02-28 21:50:53 +00:00
|
|
|
$tags = $this->phpDocNode->getTags();
|
2021-06-30 21:52:31 +00:00
|
|
|
$name = $this->annotationNaming->normalizeName($name);
|
2021-07-31 11:40:13 +00:00
|
|
|
$tags = \array_filter($tags, function (\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode $tag) use($name) : bool {
|
2019-09-06 10:30:58 +00:00
|
|
|
return $tag->name === $name;
|
2019-02-28 21:50:53 +00:00
|
|
|
});
|
2021-05-09 20:15:43 +00:00
|
|
|
$tags = \array_values($tags);
|
|
|
|
return \array_values($tags);
|
2019-02-28 21:50:53 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getParamType(string $name) : \PHPStan\Type\Type
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
2021-03-20 15:27:18 +00:00
|
|
|
$paramTagValueNodes = $this->getParamTagValueByName($name);
|
|
|
|
return $this->getTypeOrMixed($paramTagValueNodes);
|
2019-02-28 21:50:53 +00:00
|
|
|
}
|
2019-10-19 12:17:15 +00:00
|
|
|
/**
|
2020-12-04 23:22:12 +00:00
|
|
|
* @return ParamTagValueNode[]
|
2019-10-19 12:17:15 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getParamTagValueNodes() : array
|
2019-10-19 12:17:15 +00:00
|
|
|
{
|
2020-12-04 23:22:12 +00:00
|
|
|
return $this->phpDocNode->getParamTagValues();
|
2019-10-19 12:17:15 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getParamTagValueNodeByName(string $parameterName) : ?\PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode
|
2020-07-27 09:16:36 +00:00
|
|
|
{
|
2020-10-29 21:25:35 +00:00
|
|
|
foreach ($this->phpDocNode->getParamTagValues() as $paramTagValueNode) {
|
|
|
|
if ($paramTagValueNode->parameterName !== '$' . $parameterName) {
|
2020-07-27 09:16:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-10-29 21:25:35 +00:00
|
|
|
return $paramTagValueNode;
|
2020-07-27 09:16:36 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2021-06-25 15:32:16 +00:00
|
|
|
public function getVarType(string $tagName = '@var') : \PHPStan\Type\Type
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
2021-06-25 15:32:16 +00:00
|
|
|
return $this->getTypeOrMixed($this->getVarTagValueNode($tagName));
|
2019-02-28 21:50:53 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getReturnType() : \PHPStan\Type\Type
|
2019-02-28 21:50:53 +00:00
|
|
|
{
|
2020-04-08 11:48:33 +00:00
|
|
|
return $this->getTypeOrMixed($this->getReturnTagValue());
|
2019-08-26 22:15:56 +00:00
|
|
|
}
|
2021-01-19 01:15:32 +00:00
|
|
|
/**
|
2021-02-07 21:22:03 +00:00
|
|
|
* @param class-string<TNode> $type
|
2021-07-02 20:55:14 +00:00
|
|
|
* @return TNode[]
|
|
|
|
*/
|
|
|
|
public function getByType(string $type) : array
|
|
|
|
{
|
|
|
|
return $this->phpDocNodeByTypeFinder->findByType($this->phpDocNode, $type);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param class-string<TNode> $type
|
2021-01-19 01:15:32 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function hasByType(string $type) : bool
|
2019-12-27 12:44:45 +00:00
|
|
|
{
|
2021-07-01 14:46:27 +00:00
|
|
|
return $this->phpDocNodeByTypeFinder->findByType($this->phpDocNode, $type) !== [];
|
2019-12-27 12:44:45 +00:00
|
|
|
}
|
2021-01-19 21:32:28 +00:00
|
|
|
/**
|
2021-04-04 09:01:11 +00:00
|
|
|
* @param array<class-string<TNode>> $types
|
2021-01-19 21:32:28 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function hasByTypes(array $types) : bool
|
2021-01-19 21:32:28 +00:00
|
|
|
{
|
|
|
|
foreach ($types as $type) {
|
2021-02-07 21:22:03 +00:00
|
|
|
if ($this->hasByType($type)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-01-19 21:32:28 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-01-19 21:32:28 +00:00
|
|
|
}
|
2020-10-11 14:17:43 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $names
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function hasByNames(array $names) : bool
|
2020-04-08 11:48:33 +00:00
|
|
|
{
|
|
|
|
foreach ($names as $name) {
|
|
|
|
if ($this->hasByName($name)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-04-08 11:48:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-04-08 11:48:33 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function hasByName(string $name) : bool
|
2020-01-31 08:01:07 +00:00
|
|
|
{
|
|
|
|
return (bool) $this->getTagsByName($name);
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getByName(string $name) : ?\PHPStan\PhpDocParser\Ast\Node
|
2021-04-04 09:01:11 +00:00
|
|
|
{
|
|
|
|
return $this->getTagsByName($name)[0] ?? null;
|
|
|
|
}
|
2021-01-19 01:15:32 +00:00
|
|
|
/**
|
2021-06-30 21:52:31 +00:00
|
|
|
* @param class-string[] $classes
|
2021-01-19 01:15:32 +00:00
|
|
|
*/
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getByAnnotationClasses(array $classes) : ?\Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode
|
2019-08-30 06:27:03 +00:00
|
|
|
{
|
2021-07-01 14:46:27 +00:00
|
|
|
$doctrineAnnotationTagValueNodes = $this->phpDocNodeByTypeFinder->findDoctrineAnnotationsByClasses($this->phpDocNode, $classes);
|
|
|
|
return $doctrineAnnotationTagValueNodes[0] ?? null;
|
2021-04-04 09:01:11 +00:00
|
|
|
}
|
2021-06-30 21:52:31 +00:00
|
|
|
/**
|
|
|
|
* @param class-string $class
|
|
|
|
*/
|
|
|
|
public function getByAnnotationClass(string $class) : ?\Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode
|
|
|
|
{
|
2021-07-01 14:46:27 +00:00
|
|
|
$doctrineAnnotationTagValueNodes = $this->phpDocNodeByTypeFinder->findDoctrineAnnotationsByClass($this->phpDocNode, $class);
|
|
|
|
return $doctrineAnnotationTagValueNodes[0] ?? null;
|
2021-06-30 21:52:31 +00:00
|
|
|
}
|
2021-07-01 14:10:40 +00:00
|
|
|
/**
|
|
|
|
* @param class-string $class
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function hasByAnnotationClass(string $class) : bool
|
2021-04-04 09:01:11 +00:00
|
|
|
{
|
2021-06-30 21:52:31 +00:00
|
|
|
return $this->findByAnnotationClass($class) !== [];
|
2021-04-04 09:01:11 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-04-05 09:53:03 +00:00
|
|
|
* @param string[] $annotationsClasses
|
2021-04-04 09:01:11 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function hasByAnnotationClasses(array $annotationsClasses) : bool
|
2021-04-04 09:01:11 +00:00
|
|
|
{
|
|
|
|
return $this->getByAnnotationClasses($annotationsClasses) !== null;
|
|
|
|
}
|
2021-06-30 21:52:31 +00:00
|
|
|
/**
|
|
|
|
* @param class-string $desiredClass
|
|
|
|
*/
|
|
|
|
public function findOneByAnnotationClass(string $desiredClass) : ?\Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode
|
2021-04-04 09:01:11 +00:00
|
|
|
{
|
2021-06-30 21:52:31 +00:00
|
|
|
$foundTagValueNodes = $this->findByAnnotationClass($desiredClass);
|
|
|
|
return $foundTagValueNodes[0] ?? null;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param class-string $desiredClass
|
|
|
|
* @return DoctrineAnnotationTagValueNode[]
|
|
|
|
*/
|
|
|
|
public function findByAnnotationClass(string $desiredClass) : array
|
|
|
|
{
|
2021-07-01 14:46:27 +00:00
|
|
|
return $this->phpDocNodeByTypeFinder->findDoctrineAnnotationsByClass($this->phpDocNode, $desiredClass);
|
2020-04-24 22:57:13 +00:00
|
|
|
}
|
2021-03-19 14:33:58 +00:00
|
|
|
/**
|
|
|
|
* @template T of \PHPStan\PhpDocParser\Ast\Node
|
2021-07-01 23:23:13 +00:00
|
|
|
* @param class-string<T> $typeToRemove
|
2021-03-19 14:33:58 +00:00
|
|
|
*/
|
2021-07-01 23:23:13 +00:00
|
|
|
public function removeByType(string $typeToRemove) : void
|
2019-12-27 00:56:35 +00:00
|
|
|
{
|
2021-08-08 00:26:40 +00:00
|
|
|
$phpDocNodeTraverser = new \RectorPrefix20210808\Symplify\SimplePhpDocParser\PhpDocNodeTraverser();
|
2021-07-02 10:39:22 +00:00
|
|
|
$phpDocNodeTraverser->traverseWithCallable($this->phpDocNode, '', function (\PHPStan\PhpDocParser\Ast\Node $node) use($typeToRemove) : ?int {
|
2021-07-08 15:36:45 +00:00
|
|
|
if ($node instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode && \is_a($node->value, $typeToRemove, \true)) {
|
|
|
|
$this->markAsChanged();
|
2021-08-08 00:26:40 +00:00
|
|
|
return \RectorPrefix20210808\Symplify\SimplePhpDocParser\PhpDocNodeTraverser::NODE_REMOVE;
|
2021-07-08 15:36:45 +00:00
|
|
|
}
|
2021-07-01 23:23:13 +00:00
|
|
|
if (!\is_a($node, $typeToRemove, \true)) {
|
|
|
|
return null;
|
2019-12-27 00:56:35 +00:00
|
|
|
}
|
2021-01-19 23:29:52 +00:00
|
|
|
$this->markAsChanged();
|
2021-08-08 00:26:40 +00:00
|
|
|
return \RectorPrefix20210808\Symplify\SimplePhpDocParser\PhpDocNodeTraverser::NODE_REMOVE;
|
2021-07-01 23:23:13 +00:00
|
|
|
});
|
2019-12-27 00:56:35 +00:00
|
|
|
}
|
2020-01-31 09:46:08 +00:00
|
|
|
/**
|
2020-12-01 01:09:50 +00:00
|
|
|
* @return array<string, Type>
|
2020-01-31 09:46:08 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getParamTypesByName() : array
|
2020-01-31 09:46:08 +00:00
|
|
|
{
|
|
|
|
$paramTypesByName = [];
|
|
|
|
foreach ($this->phpDocNode->getParamTagValues() as $paramTagValueNode) {
|
|
|
|
$parameterName = $paramTagValueNode->parameterName;
|
2021-07-01 23:23:13 +00:00
|
|
|
$parameterType = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($paramTagValueNode, $this->node);
|
|
|
|
$paramTypesByName[$parameterName] = $parameterType;
|
2020-01-31 09:46:08 +00:00
|
|
|
}
|
|
|
|
return $paramTypesByName;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function addTagValueNode(\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode $phpDocTagValueNode) : void
|
2020-02-04 08:39:57 +00:00
|
|
|
{
|
2021-05-10 22:23:08 +00:00
|
|
|
if ($phpDocTagValueNode instanceof \Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode) {
|
2021-06-22 10:43:16 +00:00
|
|
|
$spacelessPhpDocTagNode = new \Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode('@\\' . $phpDocTagValueNode->identifierTypeNode, $phpDocTagValueNode);
|
2021-02-08 23:42:47 +00:00
|
|
|
$this->addPhpDocTagNode($spacelessPhpDocTagNode);
|
|
|
|
return;
|
2021-01-30 15:06:43 +00:00
|
|
|
}
|
2020-03-02 21:16:08 +00:00
|
|
|
$name = $this->resolveNameForPhpDocTagValueNode($phpDocTagValueNode);
|
2021-05-10 22:23:08 +00:00
|
|
|
$phpDocTagNode = new \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode($name, $phpDocTagValueNode);
|
2021-03-19 14:33:58 +00:00
|
|
|
$this->addPhpDocTagNode($phpDocTagNode);
|
2020-02-04 08:39:57 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function isNewNode() : bool
|
2020-02-16 21:29:32 +00:00
|
|
|
{
|
|
|
|
if ($this->phpDocNode->children === []) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-02-16 21:29:32 +00:00
|
|
|
}
|
2021-04-06 18:36:50 +00:00
|
|
|
return $this->betterTokenIterator->count() === 0;
|
2020-02-16 21:29:32 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function makeSingleLined() : void
|
2020-07-22 13:08:40 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$this->isSingleLine = \true;
|
2020-07-22 13:08:40 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function isSingleLine() : bool
|
2020-07-22 13:08:40 +00:00
|
|
|
{
|
|
|
|
return $this->isSingleLine;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function hasInvalidTag(string $name) : bool
|
2021-04-09 17:36:06 +00:00
|
|
|
{
|
|
|
|
// fallback for invalid tag value node
|
|
|
|
foreach ($this->phpDocNode->children as $phpDocChildNode) {
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$phpDocChildNode instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode) {
|
2021-04-09 17:36:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($phpDocChildNode->name !== $name) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$phpDocChildNode->value instanceof \PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode) {
|
2021-04-09 17:36:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-04-09 17:36:06 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-04-09 17:36:06 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getReturnTagValue() : ?\PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode
|
2020-07-27 09:26:41 +00:00
|
|
|
{
|
|
|
|
$returnTagValueNodes = $this->phpDocNode->getReturnTagValues();
|
|
|
|
return $returnTagValueNodes[0] ?? null;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
public function getParamTagValueByName(string $name) : ?\PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode
|
2020-04-23 23:21:05 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$desiredParamNameWithDollar = '$' . \ltrim($name, '$');
|
2021-03-20 15:27:18 +00:00
|
|
|
foreach ($this->getParamTagValueNodes() as $paramTagValueNode) {
|
|
|
|
if ($paramTagValueNode->parameterName !== $desiredParamNameWithDollar) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return $paramTagValueNode;
|
|
|
|
}
|
|
|
|
return null;
|
2020-04-23 23:21:05 +00:00
|
|
|
}
|
2020-12-28 13:01:27 +00:00
|
|
|
/**
|
|
|
|
* @return TemplateTagValueNode[]
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getTemplateTagValueNodes() : array
|
2020-12-28 13:01:27 +00:00
|
|
|
{
|
|
|
|
return $this->phpDocNode->getTemplateTagValues();
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function hasInheritDoc() : bool
|
2020-12-28 16:57:21 +00:00
|
|
|
{
|
2021-01-19 15:03:26 +00:00
|
|
|
return $this->hasByNames(['inheritdoc', 'inheritDoc']);
|
|
|
|
}
|
2021-04-06 17:33:09 +00:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* 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
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function markAsChanged() : void
|
2021-01-19 15:03:26 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$this->hasChanged = \true;
|
2021-01-19 19:45:30 +00:00
|
|
|
$node = $this->currentNodeProvider->getNode();
|
|
|
|
if ($node !== null) {
|
|
|
|
$this->rectorChangeCollector->notifyNodeFileInfo($node);
|
|
|
|
}
|
2021-01-19 15:03:26 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function hasChanged() : bool
|
2021-01-19 15:03:26 +00:00
|
|
|
{
|
2021-01-20 16:17:59 +00:00
|
|
|
if ($this->isNewNode()) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-01-20 16:17:59 +00:00
|
|
|
}
|
2021-04-06 17:33:09 +00:00
|
|
|
if ($this->hasChanged) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-04-06 17:33:09 +00:00
|
|
|
}
|
|
|
|
// has a single node with missing start_end
|
2021-08-08 00:26:40 +00:00
|
|
|
$phpDocNodeTraverser = new \RectorPrefix20210808\Symplify\SimplePhpDocParser\PhpDocNodeTraverser();
|
2021-05-10 22:23:08 +00:00
|
|
|
$changedPhpDocNodeVisitor = new \Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor();
|
2021-04-06 17:33:09 +00:00
|
|
|
$phpDocNodeTraverser->addPhpDocNodeVisitor($changedPhpDocNodeVisitor);
|
|
|
|
$phpDocNodeTraverser->traverse($this->phpDocNode);
|
|
|
|
return $changedPhpDocNodeVisitor->hasChanged();
|
2020-12-28 16:57:21 +00:00
|
|
|
}
|
2021-01-29 20:23:01 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getMethodTagNames() : array
|
2021-01-29 20:23:01 +00:00
|
|
|
{
|
|
|
|
$methodTagNames = [];
|
|
|
|
foreach ($this->phpDocNode->getMethodTagValues() as $methodTagValueNode) {
|
|
|
|
$methodTagNames[] = $methodTagValueNode->methodName;
|
|
|
|
}
|
|
|
|
return $methodTagNames;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
public function makeMultiLined() : void
|
2021-04-04 09:01:11 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$this->isSingleLine = \false;
|
2021-03-30 14:24:16 +00:00
|
|
|
}
|
2021-05-25 16:37:18 +00:00
|
|
|
public function getNode() : \PhpParser\Node
|
|
|
|
{
|
|
|
|
return $this->node;
|
|
|
|
}
|
2021-07-31 11:40:13 +00:00
|
|
|
/**
|
|
|
|
* @return \PHPStan\Type\MixedType|\PHPStan\Type\Type
|
|
|
|
*/
|
|
|
|
private function getTypeOrMixed(?\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode $phpDocTagValueNode)
|
2020-04-08 11:48:33 +00:00
|
|
|
{
|
|
|
|
if ($phpDocTagValueNode === null) {
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \PHPStan\Type\MixedType();
|
2020-04-08 11:48:33 +00:00
|
|
|
}
|
|
|
|
return $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($phpDocTagValueNode, $this->node);
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
private function resolveNameForPhpDocTagValueNode(\PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode $phpDocTagValueNode) : string
|
2020-03-02 21:16:08 +00:00
|
|
|
{
|
2021-01-21 20:58:26 +00:00
|
|
|
foreach (self::TAGS_TYPES_TO_NAMES as $tagValueNodeType => $name) {
|
2021-03-21 23:07:40 +00:00
|
|
|
/** @var class-string<PhpDocTagNode> $tagValueNodeType */
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\is_a($phpDocTagValueNode, $tagValueNodeType, \true)) {
|
2021-01-21 20:58:26 +00:00
|
|
|
return $name;
|
|
|
|
}
|
2020-03-02 21:16:08 +00:00
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
throw new \Rector\Core\Exception\NotImplementedYetException(\get_class($phpDocTagValueNode));
|
2020-03-02 21:16:08 +00:00
|
|
|
}
|
2019-02-28 21:50:53 +00:00
|
|
|
}
|