Skip to content

Commit

Permalink
[fix] class phpdoc generic method (#6439)
Browse files Browse the repository at this point in the history
  • Loading branch information
lammafish authored Nov 15, 2024
1 parent 418423d commit 4d3f35f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
5 changes: 4 additions & 1 deletion scoper.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@
// make external rules easier to write without enforing getRuleDefinition()
// as they are not designed for open-sourcing
static function (string $filePath, string $prefix, string $content): string {
if (!\str_ends_with($filePath, 'vendor/symplify/rule-doc-generator-contracts/src/Contract/DocumentedRuleInterface.php')) {
if (! \str_ends_with(
$filePath,
'vendor/symplify/rule-doc-generator-contracts/src/Contract/DocumentedRuleInterface.php'
)) {
return $content;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\PhpDocNodeVisitor;

use PHPStan\PhpDocParser\Ast\Attribute;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
Expand All @@ -12,8 +13,6 @@
use Rector\BetterPhpDocParser\DataProvider\CurrentTokenIteratorProvider;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\SpacingAwareTemplateTagValueNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
use Rector\Exception\ShouldNotHappenException;
use Rector\PhpDocParser\PhpDocParser\PhpDocNodeVisitor\AbstractPhpDocNodeVisitor;

Expand All @@ -37,12 +36,13 @@ public function enterNode(Node $node): ?Node

$betterTokenIterator = $this->currentTokenIteratorProvider->provide();

$startAndEnd = $node->getAttribute(PhpDocAttributeKey::START_AND_END);
if (! $startAndEnd instanceof StartAndEnd) {
$startIndex = $node->getAttribute(Attribute::START_INDEX);
$endIndex = $node->getAttribute(Attribute::END_INDEX);
if ($startIndex === null || $endIndex === null) {
throw new ShouldNotHappenException();
}

$prepositions = $this->resolvePreposition($betterTokenIterator, $startAndEnd);
$prepositions = $this->resolvePreposition($betterTokenIterator, $startIndex, $endIndex);
$spacingAwareTemplateTagValueNode = new SpacingAwareTemplateTagValueNode(
$node->name,
$node->bound,
Expand All @@ -55,9 +55,12 @@ public function enterNode(Node $node): ?Node
return $spacingAwareTemplateTagValueNode;
}

private function resolvePreposition(BetterTokenIterator $betterTokenIterator, StartAndEnd $startAndEnd): string
{
$partialTokens = $betterTokenIterator->partialTokens($startAndEnd->getStart(), $startAndEnd->getEnd());
private function resolvePreposition(
BetterTokenIterator $betterTokenIterator,
int $startIndex,
int $endIndex
): string {
$partialTokens = $betterTokenIterator->partialTokens($startIndex, $endIndex);

foreach ($partialTokens as $partialToken) {
if ($partialToken[1] !== Lexer::TOKEN_IDENTIFIER) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function count(): int
*/
public function partialTokens(int $start, int $end): array
{
return array_slice($this->getTokens(), $start, $end);
return array_slice($this->getTokens(), $start, $end - $start + 1);
}

public function containsTokenType(int $type): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/** @method T foo<T of int>(T $bar) */

0 comments on commit 4d3f35f

Please sign in to comment.