Skip to content

Commit 6cce378

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

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-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: 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;

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)