Skip to content

Commit c66bb4a

Browse files
committed
test union and intersection
1 parent f996a20 commit c66bb4a

File tree

7 files changed

+74
-0
lines changed

7 files changed

+74
-0
lines changed

src/Type/Accessory/AccessoryLiteralStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PHPStan\Type\Traits\NonRemoveableTypeTrait;
3030
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
3131
use PHPStan\Type\Type;
32+
use PHPStan\Type\TypeCombinator;
3233
use PHPStan\Type\UnionType;
3334
use PHPStan\Type\VerbosityLevel;
3435

@@ -215,6 +216,10 @@ public function toArrayKey(): Type
215216

216217
public function toCoercedArgumentType(bool $strictTypes): Type
217218
{
219+
if (!$strictTypes) {
220+
return TypeCombinator::union(new IntegerType(), new FloatType(), new StringType(), new BooleanType());
221+
}
222+
218223
return $this;
219224
}
220225

src/Type/Accessory/AccessoryLowercaseStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PHPStan\Type\Traits\NonRemoveableTypeTrait;
3030
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
3131
use PHPStan\Type\Type;
32+
use PHPStan\Type\TypeCombinator;
3233
use PHPStan\Type\UnionType;
3334
use PHPStan\Type\VerbosityLevel;
3435

@@ -212,6 +213,10 @@ public function toArrayKey(): Type
212213

213214
public function toCoercedArgumentType(bool $strictTypes): Type
214215
{
216+
if (!$strictTypes) {
217+
return TypeCombinator::union(new IntegerType(), new FloatType(), new StringType(), new BooleanType());
218+
}
219+
215220
return $this;
216221
}
217222

src/Type/Accessory/AccessoryNonEmptyStringType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ public function toArrayKey(): Type
213213

214214
public function toCoercedArgumentType(bool $strictTypes): Type
215215
{
216+
if (!$strictTypes) {
217+
return TypeCombinator::union(new IntegerType(), new FloatType(), new StringType(), new BooleanType());
218+
}
219+
216220
return $this;
217221
}
218222

src/Type/Accessory/AccessoryNonFalsyStringType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ public function toArrayKey(): Type
215215

216216
public function toCoercedArgumentType(bool $strictTypes): Type
217217
{
218+
if (!$strictTypes) {
219+
return TypeCombinator::union(new IntegerType(), new FloatType(), new StringType(), new BooleanType());
220+
}
221+
218222
return $this;
219223
}
220224

src/Type/Accessory/AccessoryNumericStringType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ public function toArrayKey(): Type
215215

216216
public function toCoercedArgumentType(bool $strictTypes): Type
217217
{
218+
if (!$strictTypes) {
219+
return TypeCombinator::union(new IntegerType(), new FloatType(), new StringType(), new BooleanType());
220+
}
221+
218222
return $this;
219223
}
220224

src/Type/Accessory/AccessoryUppercaseStringType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PHPStan\Type\Traits\NonRemoveableTypeTrait;
3030
use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
3131
use PHPStan\Type\Type;
32+
use PHPStan\Type\TypeCombinator;
3233
use PHPStan\Type\UnionType;
3334
use PHPStan\Type\VerbosityLevel;
3435

@@ -212,6 +213,10 @@ public function toArrayKey(): Type
212213

213214
public function toCoercedArgumentType(bool $strictTypes): Type
214215
{
216+
if (!$strictTypes) {
217+
return TypeCombinator::union(new IntegerType(), new FloatType(), new StringType(), new BooleanType());
218+
}
219+
215220
return $this;
216221
}
217222

tests/PHPStan/Analyser/nsrt/bug-12393b.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,50 @@ public function doBar(): void
273273
assertType('string', $this->foo); // could be numeric-string
274274
}
275275
}
276+
277+
class FooStringToUnion
278+
{
279+
280+
public int|float $foo;
281+
282+
public function doFoo(string $b): void
283+
{
284+
$this->foo = $b;
285+
assertType('float|int', $this->foo);
286+
}
287+
288+
public function doBar(): void
289+
{
290+
$this->foo = "1.0";
291+
assertType('float|int', $this->foo);
292+
}
293+
}
294+
295+
class FooNumericToString
296+
{
297+
298+
public string $foo;
299+
300+
public function doFoo(float|int $b): void
301+
{
302+
$this->foo = $b;
303+
assertType('string', $this->foo); // could be numeric-string
304+
}
305+
306+
}
307+
308+
class FooIntersectionToInt
309+
{
310+
311+
public int $foo;
312+
313+
/**
314+
* @param numeric-string $b
315+
*/
316+
public function doFoo(string $b): void
317+
{
318+
$this->foo = $b;
319+
assertType('int', $this->foo);
320+
}
321+
322+
}

0 commit comments

Comments
 (0)