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
20 changes: 20 additions & 0 deletions packages/BetterPhpDocParser/src/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,26 @@ public function removeByName(string $nameToRemove): void
}
}

/**
* With "name" as key
* @return Type[]
*/
public function getParamTypesByName(): array
{
$paramTypesByName = [];

foreach ($this->phpDocNode->getParamTagValues() as $paramTagValueNode) {
$parameterName = $paramTagValueNode->parameterName;

$paramTypesByName[$parameterName] = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType(
$paramTagValueNode,
$this->node
);
}

return $paramTypesByName;
}

private function getParamTagValueByName(string $name): ?AttributeAwareParamTagValueNode
{
$phpDocNode = $this->getPhpDocNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
use Nette\Utils\Strings;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\PhpDocParser\Ast\Node as PhpDocParserNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
Expand Down Expand Up @@ -63,11 +59,6 @@ final class DocBlockManipulator
*/
private $staticTypeMapper;

/**
* @var bool
*/
private $hasPhpDocChanged = false;

/**
* @var DocBlockClassRenamer
*/
Expand Down Expand Up @@ -173,48 +164,6 @@ public function replaceAnnotationInNode(Node $node, string $oldAnnotation, strin
$this->replaceTagByAnother($phpDocInfo->getPhpDocNode(), $oldAnnotation, $newAnnotation);
}

/**
* With "name" as key
*
* @param Function_|ClassMethod|Closure $functionLike
* @return Type[]
*/
public function getParamTypesByName(FunctionLike $functionLike): array
{
/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo === null) {
return [];
}

$paramTypesByName = [];

foreach ($phpDocInfo->getParamTagValues() as $paramTagValueNode) {
$parameterName = $paramTagValueNode->parameterName;

$paramTypesByName[$parameterName] = $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType(
$paramTagValueNode,
$functionLike
);
}

return $paramTypesByName;
}

/**
* @return PhpDocTagNode[]
*/
public function getTagsByName(Node $node, string $name): array
{
/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo === null) {
return [];
}

return $phpDocInfo->getTagsByName($name);
}

public function changeVarTag(Node $node, Type $newType): void
{
/** @var PhpDocInfo|null $phpDocInfo */
Expand Down Expand Up @@ -348,14 +297,8 @@ public function changeUnderscoreType(Node $node, string $namespacePrefix, array
$nameParts = explode('_', $staticType->getClassName());
$node->name = '\\' . implode('\\', $nameParts);

$this->hasPhpDocChanged = true;

return $node;
});

if (! $this->hasPhpDocChanged) {
return;
}
}

/**
Expand Down
17 changes: 10 additions & 7 deletions packages/PHPUnit/src/Rector/ExceptionAnnotationRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/**
* @see https://thephp.cc/news/2016/02/questioning-phpunit-best-practices
* @see https://github.com/sebastianbergmann/phpunit/commit/17c09b33ac5d9cad1459ace0ae7b1f942d1e9afd
*
* @see \Rector\PHPUnit\Tests\Rector\ExceptionAnnotationRector\ExceptionAnnotationRectorTest
*/
final class ExceptionAnnotationRector extends AbstractPHPUnitRector
Expand Down Expand Up @@ -82,13 +83,19 @@ public function refactor(Node $node): ?Node
return null;
}

/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo === null) {
return null;
}

foreach ($this->annotationToMethod as $annotation => $method) {
if (! $this->docBlockManipulator->hasTag($node, $annotation)) {
if (! $phpDocInfo->hasByName($annotation)) {
continue;
}

/** @var GenericTagValueNode[] $tags */
$tags = $this->docBlockManipulator->getTagsByName($node, $annotation);
$tags = $phpDocInfo->getTagsByName($annotation);

$methodCallExpressions = array_map(function (PhpDocTagNode $phpDocTagNode) use ($method): Expression {
$methodCall = $this->createMethodCallExpressionFromTag($phpDocTagNode, $method);
Expand All @@ -97,11 +104,7 @@ public function refactor(Node $node): ?Node

$node->stmts = array_merge($methodCallExpressions, (array) $node->stmts);

/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo !== null) {
$phpDocInfo->removeByName($annotation);
}
$phpDocInfo->removeByName($annotation);
}

return $node;
Expand Down
6 changes: 4 additions & 2 deletions packages/Renaming/src/Rector/Class_/RenameClassRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -149,8 +150,9 @@ public function refactor(Node $node): ?Node
*/
private function refactorPhpDoc(Node $node): void
{
$nodePhpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($nodePhpDocInfo === null) {
/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo === null) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand Down Expand Up @@ -104,8 +105,13 @@ public function refactor(Node $node): ?Node
return null;
}

$paramWithTypes = $this->docBlockManipulator->getParamTypesByName($node);
/** @var PhpDocInfo|null $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($phpDocInfo === null) {
return null;
}

$paramWithTypes = $phpDocInfo->getParamTypesByName();
// no tags, nothing to complete here
if ($paramWithTypes === []) {
return null;
Expand Down