Skip to content

Commit 47c9935

Browse files
authored
added minimal IntersectionType support (#228)
1 parent e2939f2 commit 47c9935

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/QueryReflection/QuerySimulation.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace staabm\PHPStanDba\QueryReflection;
66

77
use PHPStan\ShouldNotHappenException;
8+
use PHPStan\Type\Accessory\AccessoryType;
89
use PHPStan\Type\ArrayType;
910
use PHPStan\Type\BooleanType;
1011
use PHPStan\Type\ConstantScalarType;
@@ -79,6 +80,19 @@ public static function simulateParamValueType(Type $paramType, bool $preparedPar
7980
return null;
8081
}
8182

83+
if ($paramType instanceof IntersectionType) {
84+
foreach ($paramType->getTypes() as $type) {
85+
if ($type instanceof AccessoryType) {
86+
continue;
87+
}
88+
89+
$simulated = self::simulateParamValueType($type, $preparedParam);
90+
if (null !== $simulated) {
91+
return $simulated;
92+
}
93+
}
94+
}
95+
8296
// all types which we can't simulate and render a query unresolvable at analysis time
8397
if ($paramType instanceof MixedType || $paramType instanceof IntersectionType) {
8498
if (QueryReflection::getRuntimeConfiguration()->isDebugEnabled()) {

tests/QuerySimulationTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace staabm\PHPStanDba\Tests;
4+
5+
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
6+
use PHPStan\Type\FloatType;
7+
use PHPStan\Type\IntegerType;
8+
use PHPStan\Type\StringType;
9+
use PHPUnit\Framework\TestCase;
10+
use staabm\PHPStanDba\QueryReflection\QuerySimulation;
11+
12+
class QuerySimulationTest extends TestCase
13+
{
14+
public function testIntersectionTypeInt()
15+
{
16+
// non-empty-array<int, int>
17+
$builder = ConstantArrayTypeBuilder::createEmpty();
18+
$builder->setOffsetValueType(new IntegerType(), new IntegerType());
19+
20+
$simulatedValue = QuerySimulation::simulateParamValueType($builder->getArray(), false);
21+
$this->assertNotNull($simulatedValue);
22+
}
23+
24+
public function testIntersectionTypeString()
25+
{
26+
// non-empty-array<string, int>
27+
$builder = ConstantArrayTypeBuilder::createEmpty();
28+
$builder->setOffsetValueType(new StringType(), new IntegerType());
29+
30+
$simulatedValue = QuerySimulation::simulateParamValueType($builder->getArray(), false);
31+
$this->assertNotNull($simulatedValue);
32+
}
33+
34+
public function testIntersectionTypeMix()
35+
{
36+
// non-empty-array<string, int>
37+
$builder = ConstantArrayTypeBuilder::createEmpty();
38+
$builder->setOffsetValueType(new StringType(), new IntegerType());
39+
$builder->setOffsetValueType(new IntegerType(), new FloatType());
40+
41+
$simulatedValue = QuerySimulation::simulateParamValueType($builder->getArray(), false);
42+
$this->assertNotNull($simulatedValue);
43+
}
44+
}

0 commit comments

Comments
 (0)