Skip to content

Commit 5a008c6

Browse files
Add tests
1 parent a07d0f5 commit 5a008c6

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

src/Type/ValueOfType.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public function isResolvable(): bool
5151
protected function getResult(): Type
5252
{
5353
if ($this->type->isEnum()->yes()) {
54-
if ($this->type instanceof TemplateType) {
55-
$bound = $this->type->getBound();
56-
if ($bound->equals(new ObjectType('BackedEnum'))) {
57-
return new UnionType([new IntegerType(), new StringType()]);
58-
}
54+
if (
55+
$this->type instanceof TemplateType
56+
&& $this->type->getBound()->equals(new ObjectType('BackedEnum'))
57+
) {
58+
return new UnionType([new IntegerType(), new StringType()]);
5959
}
6060

6161
$valueTypes = [];
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug13283;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
enum Test: string
8+
{
9+
case NAME = 'name';
10+
case VALUE = 'value';
11+
}
12+
13+
/**
14+
* @template T of \BackedEnum
15+
* @param class-string<T> $enum
16+
* @phpstan-assert null|value-of<T> $value
17+
*/
18+
function assertValue(mixed $value, string $enum): void
19+
{
20+
if (null === $value) {
21+
return;
22+
}
23+
24+
if (! is_int($value) && ! is_string($value)) {
25+
throw new \Exception();
26+
}
27+
28+
if (null === $enum::tryFrom($value)) {
29+
throw new \Exception();
30+
}
31+
}
32+
33+
function getFromRequest(): mixed
34+
{
35+
return 'name';
36+
}
37+
38+
$v = getFromRequest();
39+
40+
assertType('mixed', $v);
41+
42+
assertValue($v, Test::class);
43+
44+
assertType("'name'|'value'|null", $v);
45+
46+
$a = null !== $v ? Test::from($v) : null;

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,12 @@ public function testBug13208(): void
10341034
$this->analyse([__DIR__ . '/data/bug-13208.php'], []);
10351035
}
10361036

1037+
#[RequiresPhp('>= 8.1')]
1038+
public function testBug13282(): void
1039+
{
1040+
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-13282.php'], []);
1041+
}
1042+
10371043
public function testBug11609(): void
10381044
{
10391045
$this->analyse([__DIR__ . '/data/bug-11609.php'], [

tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,14 @@ public function testBug12274(): void
357357
]);
358358
}
359359

360+
#[RequiresPhp('>= 8.1')]
361+
public function testBug13638(): void
362+
{
363+
$this->checkNullables = true;
364+
$this->checkExplicitMixed = true;
365+
$this->analyse([__DIR__ . '/data/bug-13638.php'], []);
366+
}
367+
360368
public function testBug13484(): void
361369
{
362370
$this->checkExplicitMixed = true;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug13638;
4+
5+
use BackedEnum;
6+
7+
/**
8+
* @template T of BackedEnum
9+
* @param ?value-of<T> $a
10+
* @return ($a is null ? list<?value-of<T>> : list<value-of<T>>)
11+
*/
12+
function test1(int | string | null $a): array
13+
{
14+
return [$a];
15+
}

0 commit comments

Comments
 (0)