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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\PhpParser\NodeFinder\LocalMethodCallFinder;
Expand Down Expand Up @@ -94,7 +95,11 @@ public function refactor(Node $node): ?Node
$classMethodParameterTypes = $this->callTypesResolver->resolveStrictTypesFromCalls($methodCalls);

foreach ($classMethod->getParams() as $parameterPosition => $param) {
if ($param->type === null || ! $this->isName($param->type, 'array')) {
if ($param->type === null) {
continue;
}

if (! $this->isName($param->type, 'array')) {
continue;
}

Expand All @@ -107,7 +112,7 @@ public function refactor(Node $node): ?Node
}

$resolvedParameterType = $classMethodParameterTypes[$parameterPosition] ?? null;
if (! $resolvedParameterType instanceof \PHPStan\Type\Type) {
if (! $resolvedParameterType instanceof Type) {
continue;
}

Expand Down
15 changes: 14 additions & 1 deletion src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,22 @@ public function getGenericTagClassNames(): array
$resolvedClasses = [];

foreach ($genericTagValueNodes as $genericTagValueNode) {
if ($genericTagValueNode->value !== '') {
if ($genericTagValueNode->value === '') {
continue;
}

if (! str_contains($genericTagValueNode->value, '::')) {
$resolvedClasses[] = $genericTagValueNode->value;
continue;
}

$resolvedClass = $genericTagValueNode->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS);
if ($resolvedClass === null) {
$resolvedClasses[] = $genericTagValueNode->value;
continue;
}

$resolvedClasses[] = $resolvedClass;
}

return $resolvedClasses;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\PhpDocParser;

use PhpParser\Node as PhpNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeTraverser;
use Rector\StaticTypeMapper\Naming\NameScopeFactory;

/**
* Decorate node with fully qualified class name for generic annotations for @uses
* e.g. @uses Direction::*
*
* @see https://docs.phpdoc.org/guide/references/phpdoc/tags/uses.html
*/
final readonly class PhpDocTagGenericUsesDecorator implements PhpDocNodeDecoratorInterface
{
public function __construct(
private NameScopeFactory $nameScopeFactory,
private PhpDocNodeTraverser $phpDocNodeTraverser
) {
}

public function decorate(PhpDocNode $phpDocNode, PhpNode $phpNode): void
{
// iterating all phpdocs has big overhead. peek into the phpdoc to exit early
if (! str_contains($phpDocNode->__toString(), '::')) {
return;
}

$this->phpDocNodeTraverser->traverseWithCallable($phpDocNode, '', function (Node $node) use (
$phpNode
): Node|null {
if (! $node instanceof PhpDocTagNode) {
return null;
}

if (! $node->value instanceof GenericTagValueNode) {
return null;
}

if (! in_array($node->name, ['@uses', '@used-by'], true)) {
return null;
}

$reference = $node->value->value;
if (! str_contains($reference, '::')) {
return null;
}

if ($node->value->hasAttribute(PhpDocAttributeKey::RESOLVED_CLASS)) {
return null;
}

$classValue = explode('::', $reference)[0];
$className = $this->resolveFullyQualifiedClass($classValue, $phpNode);
$node->value->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $className);

return $node;
});
}

private function resolveFullyQualifiedClass(string $classValue, PhpNode $phpNode): string
{
$nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($phpNode);
return $nameScope->resolveStringName($classValue);
}
}
2 changes: 2 additions & 0 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Rector\BetterPhpDocParser\PhpDocParser\BetterPhpDocParser;
use Rector\BetterPhpDocParser\PhpDocParser\ConstExprClassNameDecorator;
use Rector\BetterPhpDocParser\PhpDocParser\DoctrineAnnotationDecorator;
use Rector\BetterPhpDocParser\PhpDocParser\PhpDocTagGenericUsesDecorator;
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser;
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\ArrayParser;
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\PlainValueParser;
Expand Down Expand Up @@ -316,6 +317,7 @@ final class LazyContainerFactory
ConstExprClassNameDecorator::class,
DoctrineAnnotationDecorator::class,
ArrayItemClassNameDecorator::class,
PhpDocTagGenericUsesDecorator::class,
];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use bridge\workshop\viewmodel\AppointmentFormVM;

/** @used-by AppointmentFormVM::addItem() */
function foo($locations)
{
}
6 changes: 6 additions & 0 deletions tests/Issues/NoNamespaced/Fixture/skip_used_in_uses.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

use bridge\workshop\viewmodel\AppointmentFormVM;

/** @uses AppointmentFormVM::$locations */
$r->setLocations('locations');
Loading