Skip to content

Commit 6fb48d8

Browse files
committed
add more tests
1 parent 710f127 commit 6fb48d8

4 files changed

Lines changed: 164 additions & 2 deletions

File tree

src/Reflection/Assertions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,10 @@ public function intersect(Assertions $other): self
148148
private static function getAssertKey(AssertTag $assert): string
149149
{
150150
return sprintf(
151-
'%s-%s-%s-%s',
151+
'%s-%s-%s',
152152
$assert->getParameter()->describe(),
153153
$assert->getIf(),
154154
$assert->isNegated() ? '1' : '0',
155-
$assert->isEquality() ? '1' : '0',
156155
);
157156
}
158157

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug14108b;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
public function __construct(private ?string $param)
10+
{
11+
}
12+
13+
public function getParam(): ?string
14+
{
15+
return $this->param;
16+
}
17+
18+
/**
19+
* @phpstan-assert !null $this->getParam()
20+
*/
21+
public function narrowGetParam(): void
22+
{
23+
}
24+
}
25+
26+
class Bar
27+
{
28+
public function __construct(private ?int $param)
29+
{
30+
}
31+
32+
public function getParam(): ?int
33+
{
34+
return $this->param;
35+
}
36+
37+
/**
38+
* @phpstan-assert int $this->getParam()
39+
*/
40+
public function narrowGetParam(): void
41+
{
42+
}
43+
}
44+
45+
function test(Foo|Bar $fooOrBar): void
46+
{
47+
assertType('int|string|null', $fooOrBar->getParam());
48+
49+
$fooOrBar->narrowGetParam();
50+
51+
assertType('int|string|null', $fooOrBar->getParam());
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug14108c;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
public function __construct(private ?string $param)
10+
{
11+
}
12+
13+
public function getParam(): ?string
14+
{
15+
return $this->param;
16+
}
17+
18+
/**
19+
* @phpstan-assert =string $this->getParam()
20+
*/
21+
public function narrowGetParam(): void
22+
{
23+
}
24+
}
25+
26+
class Bar
27+
{
28+
public function __construct(private ?int $param)
29+
{
30+
}
31+
32+
public function getParam(): ?int
33+
{
34+
return $this->param;
35+
}
36+
37+
/**
38+
* @phpstan-assert int $this->getParam()
39+
*/
40+
public function narrowGetParam(): void
41+
{
42+
}
43+
}
44+
45+
function test(Foo|Bar $fooOrBar): void
46+
{
47+
assertType('int|string|null', $fooOrBar->getParam());
48+
49+
$fooOrBar->narrowGetParam();
50+
51+
assertType('int|string', $fooOrBar->getParam());
52+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug14108d;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
public function __construct(private ?string $param)
10+
{
11+
}
12+
13+
public function getParam(): ?string
14+
{
15+
return $this->param;
16+
}
17+
18+
/**
19+
* @phpstan-assert-if-true string $this->getParam()
20+
*/
21+
public function narrowGetParam(): bool
22+
{
23+
}
24+
}
25+
26+
class Bar
27+
{
28+
public function __construct(private ?int $param)
29+
{
30+
}
31+
32+
public function getParam(): ?int
33+
{
34+
return $this->param;
35+
}
36+
37+
/**
38+
* @phpstan-assert int $this->getParam()
39+
*/
40+
public function narrowGetParam(): void
41+
{
42+
}
43+
}
44+
45+
function test(Foo|Bar $fooOrBar): void
46+
{
47+
assertType('int|string|null', $fooOrBar->getParam());
48+
49+
$fooOrBar->narrowGetParam();
50+
51+
assertType('int|string|null', $fooOrBar->getParam());
52+
53+
if ($fooOrBar->narrowGetParam()) {
54+
assertType('int|string|null', $fooOrBar->getParam()); // could be 'int|string'
55+
} else {
56+
assertType('int|string|null', $fooOrBar->getParam());
57+
}
58+
assertType('int|string|null', $fooOrBar->getParam());
59+
}

0 commit comments

Comments
 (0)