Skip to content

Commit e8e7704

Browse files
committed
skip connection calls in AddReturnDocblockFromMethodCallDocblockRector
1 parent 598e5d3 commit e8e7704

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
namespace Rector\TypeDeclarationDocblocks\Rector\ClassMethod;
66

7+
use PHPStan\TrinaryLogic;
78
use PhpParser\Node;
89
use PhpParser\Node\Expr\MethodCall;
910
use PhpParser\Node\Identifier;
1011
use PhpParser\Node\Stmt\ClassMethod;
1112
use PhpParser\Node\Stmt\Return_;
1213
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
14+
use PHPStan\Type\ObjectType;
1315
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1416
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
17+
use Rector\Doctrine\Enum\DoctrineClass;
1518
use Rector\PhpParser\AstResolver;
1619
use Rector\Rector\AbstractRector;
1720
use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder;
@@ -120,6 +123,12 @@ public function refactor(Node $node): ?Node
120123

121124
$returnedMethodCall = $onlyReturnWithExpr->expr;
122125

126+
// skip doctrine connection calls, as to generic and not helpful
127+
$callerType = $this->getType($returnedMethodCall->var);
128+
if ($callerType instanceof ObjectType && $callerType->isInstanceOf(DoctrineClass::CONNECTION)->yes()) {
129+
return null;
130+
}
131+
123132
$calledClassMethod = $this->astResolver->resolveClassMethodFromCall($returnedMethodCall);
124133
if (! $calledClassMethod instanceof ClassMethod) {
125134
return null;
@@ -140,6 +149,10 @@ public function refactor(Node $node): ?Node
140149
return null;
141150
}
142151

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

145158
return $node;

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)