Skip to content

Commit 2227f74

Browse files
staabmclxmstaab
andauthored
added Stringable object support (#229)
Co-authored-by: Markus Staab <[email protected]>
1 parent 7fdea9c commit 2227f74

File tree

5 files changed

+320
-1
lines changed

5 files changed

+320
-1
lines changed

.phpstan-dba.cache

Lines changed: 149 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.phpunit-phpstan-dba.cache

Lines changed: 149 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/QueryReflection/QuerySimulation.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
use PHPStan\Type\IntegerType;
1414
use PHPStan\Type\IntersectionType;
1515
use PHPStan\Type\MixedType;
16+
use PHPStan\Type\ObjectType;
1617
use PHPStan\Type\StringType;
1718
use PHPStan\Type\Type;
1819
use PHPStan\Type\UnionType;
1920
use PHPStan\Type\VerbosityLevel;
2021
use staabm\PHPStanDba\DbaException;
2122
use staabm\PHPStanDba\UnresolvableQueryException;
23+
use Stringable;
2224

2325
/**
2426
* @internal
@@ -70,7 +72,7 @@ public static function simulateParamValueType(Type $paramType, bool $preparedPar
7072
}
7173

7274
$stringType = new StringType();
73-
if ($stringType->isSuperTypeOf($paramType)->yes()) {
75+
if ($stringType->isSuperTypeOf($paramType)->yes() || $paramType instanceof ObjectType && $paramType->isInstanceOf(Stringable::class)->yes()) {
7476
// in a prepared context, regular strings are fine
7577
if (true === $preparedParam) {
7678
return '1';

tests/Fixture/StringableObject.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace staabm\PHPStanDba\Tests\Fixture;
4+
5+
class StringableObject
6+
{
7+
public function __toString()
8+
{
9+
return 'foo';
10+
}
11+
}

tests/data/doctrine-dbal.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\DBAL\Cache\QueryCacheProfile;
66
use Doctrine\DBAL\Connection;
77
use function PHPStan\Testing\assertType;
8+
use staabm\PHPStanDba\Tests\Fixture\StringableObject;
89

910
class Foo
1011
{
@@ -149,4 +150,11 @@ public function fetchAllKeyValue(Connection $conn)
149150
$fetchResult = $conn->fetchAllKeyValue($query, [1]);
150151
assertType('array<string, int<0, 4294967295>>', $fetchResult);
151152
}
153+
154+
public function fetchStringable(Connection $conn)
155+
{
156+
$query = 'SELECT email, adaid, gesperrt, freigabe1u1 FROM ada WHERE email = ?';
157+
$fetchResult = $conn->fetchAllKeyValue($query, [new StringableObject()]);
158+
assertType('array<string, int<0, 4294967295>>', $fetchResult);
159+
}
152160
}

0 commit comments

Comments
 (0)