Skip to content

Commit 275970c

Browse files
authored
Skip Doctrine Connection calls in AddReturnDocblockFromMethodCallDocblockRector (#7797)
* applyps * skip connection calls in AddReturnDocblockFromMethodCallDocblockRector
1 parent ec0c5d6 commit 275970c

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockFromMethodCallDocblockRector\Fixture;
6+
7+
use Doctrine\DBAL\Connection;
8+
9+
final class SkipFetchFirstColumn
10+
{
11+
private Connection $connection;
12+
13+
public function __construct(Connection $connection)
14+
{
15+
$this->connection = $connection;
16+
}
17+
18+
public function getAll(): array
19+
{
20+
return $this->connection->fetchFirstColumn();
21+
}
22+
}

rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockFromMethodCallDocblockRector.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use PhpParser\Node\Stmt\ClassMethod;
1111
use PhpParser\Node\Stmt\Return_;
1212
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
13+
use PHPStan\Type\ObjectType;
1314
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1415
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
16+
use Rector\Doctrine\Enum\DoctrineClass;
1517
use Rector\PhpParser\AstResolver;
1618
use Rector\Rector\AbstractRector;
1719
use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder;
@@ -120,6 +122,12 @@ public function refactor(Node $node): ?Node
120122

121123
$returnedMethodCall = $onlyReturnWithExpr->expr;
122124

125+
// skip doctrine connection calls, as to generic and not helpful
126+
$callerType = $this->getType($returnedMethodCall->var);
127+
if ($callerType instanceof ObjectType && $callerType->isInstanceOf(DoctrineClass::CONNECTION)->yes()) {
128+
return null;
129+
}
130+
123131
$calledClassMethod = $this->astResolver->resolveClassMethodFromCall($returnedMethodCall);
124132
if (! $calledClassMethod instanceof ClassMethod) {
125133
return null;
@@ -140,6 +148,10 @@ public function refactor(Node $node): ?Node
140148
return null;
141149
}
142150

151+
if (! $this->usefulArrayTagNodeAnalyzer->isUsefulArrayTag($calledReturnTagValue)) {
152+
return null;
153+
}
154+
143155
$this->phpDocTypeChanger->changeReturnTypeNode($node, $phpDocInfo, $calledReturnTagValue->type);
144156

145157
return $node;

src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function getOriginalPhpDocNode(): PhpDocNode
9494
}
9595

9696
/**
97-
* @return mixed[]
97+
* @return list<array{string, int, int}>
9898
*/
9999
public function getTokens(): array
100100
{

stubs/Doctrine/DBAL/Connection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL;
6+
7+
final class Connection
8+
{
9+
/**
10+
* @return list<mixed>
11+
*/
12+
public function fetchFirstColumn(string $query, array $params = [], array $types = []): array
13+
{
14+
return [];
15+
}
16+
}

0 commit comments

Comments
 (0)