Skip to content

Commit eb654fd

Browse files
staabmclxmstaab
andauthored
use proper default return value (#48)
Co-authored-by: Markus Staab <m.staab@complex-it.de>
1 parent 9185081 commit eb654fd

File tree

5 files changed

+20
-28
lines changed

5 files changed

+20
-28
lines changed

src/Extensions/PdoPrepareDynamicReturnTypeExtension.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\Php\PhpVersion;
1212
use PHPStan\Reflection\MethodReflection;
13-
use PHPStan\Type\ArrayType;
13+
use PHPStan\Reflection\ParametersAcceptorSelector;
1414
use PHPStan\Type\Constant\ConstantBooleanType;
1515
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1616
use PHPStan\Type\Generic\GenericObjectType;
17-
use PHPStan\Type\MixedType;
1817
use PHPStan\Type\Type;
1918
use PHPStan\Type\TypeCombinator;
2019
use staabm\PHPStanDba\QueryReflection\QueryReflection;
@@ -45,12 +44,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
4544
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
4645
{
4746
$args = $methodCall->getArgs();
48-
$mixed = new MixedType(true);
49-
50-
$defaultReturn = TypeCombinator::union(
51-
new GenericObjectType(PDOStatement::class, [new ArrayType($mixed, $mixed)]),
52-
new ConstantBooleanType(false)
53-
);
47+
$defaultReturn = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
5448

5549
if (QueryReflection::getRuntimeConfiguration()->throwsPdoExceptions($this->phpVersion)) {
5650
$defaultReturn = TypeCombinator::remove($defaultReturn, new ConstantBooleanType(false));

src/Extensions/PdoQueryDynamicReturnTypeExtension.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\Php\PhpVersion;
1212
use PHPStan\Reflection\MethodReflection;
13-
use PHPStan\Type\ArrayType;
13+
use PHPStan\Reflection\ParametersAcceptorSelector;
1414
use PHPStan\Type\Constant\ConstantBooleanType;
1515
use PHPStan\Type\Constant\ConstantIntegerType;
1616
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1717
use PHPStan\Type\Generic\GenericObjectType;
18-
use PHPStan\Type\MixedType;
1918
use PHPStan\Type\Type;
2019
use PHPStan\Type\TypeCombinator;
2120
use staabm\PHPStanDba\QueryReflection\QueryReflection;
@@ -46,12 +45,11 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
4645
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
4746
{
4847
$args = $methodCall->getArgs();
49-
$mixed = new MixedType(true);
50-
51-
$defaultReturn = TypeCombinator::union(
52-
new GenericObjectType(PDOStatement::class, [new ArrayType($mixed, $mixed)]),
53-
new ConstantBooleanType(false)
54-
);
48+
$defaultReturn = ParametersAcceptorSelector::selectFromArgs(
49+
$scope,
50+
$methodCall->getArgs(),
51+
$methodReflection->getVariants(),
52+
)->getReturnType();
5553

5654
if (QueryReflection::getRuntimeConfiguration()->throwsPdoExceptions($this->phpVersion)) {
5755
$defaultReturn = TypeCombinator::remove($defaultReturn, new ConstantBooleanType(false));

tests/data/pdo-fetch-types.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function supportedFetchTypes(PDO $pdo)
2626
public function unsupportedFetchTypes(PDO $pdo)
2727
{
2828
$stmt = $pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada', PDO::FETCH_COLUMN);
29-
assertType('PDOStatement<array>', $stmt);
29+
assertType('PDOStatement', $stmt);
3030

3131
$stmt = $pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada', PDO::FETCH_OBJ);
32-
assertType('PDOStatement<array>', $stmt);
32+
assertType('PDOStatement', $stmt);
3333
}
3434
}

tests/data/pdo-quote.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ public function quotedArguments(PDO $pdo, int $i, float $f, $n, string $s, $nonE
7272

7373
// when quote() cannot return a numeric-string, we can't infer the precise result-type
7474
$stmt = $pdo->query('SELECT email, adaid FROM ada WHERE adaid='.$pdo->quote($s), PDO::FETCH_ASSOC);
75-
assertType('PDOStatement<array>', $stmt);
75+
assertType('PDOStatement', $stmt);
7676

7777
$stmt = $pdo->query('SELECT email, adaid FROM ada WHERE adaid='.$pdo->quote($nonE), PDO::FETCH_ASSOC);
78-
assertType('PDOStatement<array>', $stmt);
78+
assertType('PDOStatement', $stmt);
7979
}
8080
}

tests/data/pdo.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function queryWithNullColumn(PDO $pdo)
4242
public function syntaxError(PDO $pdo)
4343
{
4444
$stmt = $pdo->query('SELECT email adaid WHERE gesperrt freigabe1u1 FROM ada', PDO::FETCH_ASSOC);
45-
assertType('PDOStatement<array>', $stmt);
45+
assertType('PDOStatement', $stmt);
4646
}
4747

4848
/**
@@ -76,39 +76,39 @@ public function concatedQuerySelected(PDO $pdo, int $int, string $string, float
7676
// ---- queries, for which we cannot infer the return type
7777

7878
$stmt = $pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE '.$string, PDO::FETCH_ASSOC);
79-
assertType('PDOStatement<array>', $stmt);
79+
assertType('PDOStatement', $stmt);
8080

8181
$stmt = $pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE '.$nonEmptyString, PDO::FETCH_ASSOC);
82-
assertType('PDOStatement<array>', $stmt);
82+
assertType('PDOStatement', $stmt);
8383

8484
$stmt = $pdo->query('SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE '.$mixed, PDO::FETCH_ASSOC);
85-
assertType('PDOStatement<array>', $stmt);
85+
assertType('PDOStatement', $stmt);
8686
}
8787

8888
public function dynamicQuery(PDO $pdo, string $query)
8989
{
9090
$stmt = $pdo->query($query, PDO::FETCH_ASSOC);
91-
assertType('PDOStatement<array>', $stmt);
91+
assertType('PDOStatement', $stmt);
9292
}
9393

9494
public function insertQuery(PDO $pdo)
9595
{
9696
$query = "INSERT INTO ada SET email='test@complex-it.de'";
9797
$stmt = $pdo->query($query, PDO::FETCH_ASSOC);
98-
assertType('PDOStatement<array>', $stmt);
98+
assertType('PDOStatement', $stmt);
9999
}
100100

101101
public function replaceQuery(PDO $pdo)
102102
{
103103
$query = "REPLACE INTO ada SET email='test@complex-it.de'";
104104
$stmt = $pdo->query($query, PDO::FETCH_ASSOC);
105-
assertType('PDOStatement<array>', $stmt);
105+
assertType('PDOStatement', $stmt);
106106
}
107107

108108
public function updateQuery(PDO $pdo)
109109
{
110110
$query = "UPDATE ada SET email='test@complex-it.de' where adaid=-5";
111111
$stmt = $pdo->query($query, PDO::FETCH_ASSOC);
112-
assertType('PDOStatement<array>', $stmt);
112+
assertType('PDOStatement', $stmt);
113113
}
114114
}

0 commit comments

Comments
 (0)