Skip to content

Commit 22c78a9

Browse files
committed
tests
1 parent 3854cbc commit 22c78a9

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php declare(strict_types = 0);
2+
3+
namespace Bug12393b;
4+
5+
use function PHPStan\Testing\assertNativeType;
6+
use function PHPStan\Testing\assertType;
7+
8+
class HelloWorld
9+
{
10+
private string $name;
11+
12+
/** @var string */
13+
private $untypedName;
14+
15+
private float $float;
16+
17+
/** @var float */
18+
private $untypedFloat;
19+
20+
private array $a;
21+
22+
/**
23+
* @param mixed[] $plugin
24+
*/
25+
public function __construct(array $plugin){
26+
$this->name = $plugin["name"];
27+
assertType('string', $this->name);
28+
}
29+
30+
/**
31+
* @param mixed[] $plugin
32+
*/
33+
public function doFoo(array $plugin){
34+
$this->untypedName = $plugin["name"];
35+
assertType('mixed', $this->untypedName);
36+
}
37+
38+
public function doBar(int $i){
39+
$this->float = $i;
40+
assertType('float', $this->float);
41+
}
42+
43+
public function doBaz(int $i){
44+
$this->untypedFloat = $i;
45+
assertType('int', $this->untypedFloat);
46+
}
47+
48+
public function doLorem(): void
49+
{
50+
$this->a = ['a' => 1];
51+
assertType('array{a: 1}', $this->a);
52+
}
53+
54+
public function doFloatTricky(){
55+
$this->float = 1;
56+
assertType('1.0', $this->float);
57+
}
58+
}
59+
60+
class HelloWorldStatic
61+
{
62+
private static string $name;
63+
64+
/** @var string */
65+
private static $untypedName;
66+
67+
private static float $float;
68+
69+
/** @var float */
70+
private static $untypedFloat;
71+
72+
private static array $a;
73+
74+
/**
75+
* @param mixed[] $plugin
76+
*/
77+
public function __construct(array $plugin){
78+
self::$name = $plugin["name"];
79+
assertType('string', self::$name);
80+
}
81+
82+
/**
83+
* @param mixed[] $plugin
84+
*/
85+
public function doFoo(array $plugin){
86+
self::$untypedName = $plugin["name"];
87+
assertType('mixed', self::$untypedName);
88+
}
89+
90+
public function doBar(int $i){
91+
self::$float = $i;
92+
assertType('float', self::$float);
93+
}
94+
95+
public function doBaz(int $i){
96+
self::$untypedFloat = $i;
97+
assertType('int', self::$untypedFloat);
98+
}
99+
100+
public function doLorem(): void
101+
{
102+
self::$a = ['a' => 1];
103+
assertType('array{a: 1}', self::$a);
104+
}
105+
}
106+
107+
class EntryPointLookup
108+
{
109+
110+
/** @var array<string, mixed>|null */
111+
private ?array $entriesData = null;
112+
113+
/**
114+
* @return array<string, mixed>
115+
*/
116+
public function doFoo(): void
117+
{
118+
if ($this->entriesData !== null) {
119+
return;
120+
}
121+
122+
assertType('null', $this->entriesData);
123+
assertNativeType('null', $this->entriesData);
124+
125+
$data = $this->getMixed();
126+
if ($data !== null) {
127+
$this->entriesData = $data;
128+
assertType('array', $this->entriesData);
129+
assertNativeType('array', $this->entriesData);
130+
return;
131+
}
132+
133+
assertType('null', $this->entriesData);
134+
assertNativeType('null', $this->entriesData);
135+
}
136+
137+
/**
138+
* @return mixed
139+
*/
140+
public function getMixed()
141+
{
142+
143+
}
144+
145+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php // lint >= 8.1
2+
3+
declare(strict_types = 0);
4+
5+
namespace RememberNullablePropertyWhenStrictTypesDisabled;
6+
7+
use function PHPStan\Testing\assertNativeType;
8+
use function PHPStan\Testing\assertType;
9+
10+
interface ObjectDataMapper
11+
{
12+
/**
13+
* @template OutType of object
14+
*
15+
* @param literal-string&class-string<OutType> $class
16+
* @param mixed $data
17+
*
18+
* @return OutType
19+
*
20+
* @throws \Exception
21+
*/
22+
public function map(string $class, $data): object;
23+
}
24+
25+
final class ApiProductController
26+
{
27+
28+
protected ?SearchProductsVM $searchProductsVM = null;
29+
30+
protected static ?SearchProductsVM $searchProductsVMStatic = null;
31+
32+
public function search(ObjectDataMapper $dataMapper): void
33+
{
34+
$this->searchProductsVM = $dataMapper->map(SearchProductsVM::class, $_REQUEST);
35+
assertType('RememberNullablePropertyWhenStrictTypesDisabled\SearchProductsVM', $this->searchProductsVM);
36+
}
37+
38+
public function searchStatic(ObjectDataMapper $dataMapper): void
39+
{
40+
self::$searchProductsVMStatic = $dataMapper->map(SearchProductsVM::class, $_REQUEST);
41+
assertType('RememberNullablePropertyWhenStrictTypesDisabled\SearchProductsVM', self::$searchProductsVMStatic);
42+
}
43+
}
44+
45+
class SearchProductsVM {}

tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,4 +1016,9 @@ public function testBug11019(): void
10161016
$this->analyse([__DIR__ . '/data/bug-11019.php'], []);
10171017
}
10181018

1019+
public function testBug12946(): void
1020+
{
1021+
$this->analyse([__DIR__ . '/data/bug-12946.php'], []);
1022+
}
1023+
10191024
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php // lint >= 8.1
2+
3+
namespace Bug12946;
4+
5+
interface UserInterface {}
6+
class User implements UserInterface{}
7+
8+
class UserMapper {
9+
function getFromId(int $id) : ?UserInterface {
10+
return $id === 10 ? new User : null;
11+
}
12+
}
13+
14+
class GetUserCommand {
15+
16+
private ?UserInterface $currentUser = null;
17+
18+
public function __construct(
19+
private readonly UserMapper $userMapper,
20+
private readonly int $id,
21+
) {
22+
}
23+
24+
public function __invoke() : UserInterface {
25+
if( $this->currentUser ) {
26+
return $this->currentUser;
27+
}
28+
29+
$this->currentUser = $this->userMapper->getFromId($this->id);
30+
if( $this->currentUser === null ) {
31+
throw new \Exception;
32+
}
33+
34+
return $this->currentUser;
35+
}
36+
37+
}

0 commit comments

Comments
 (0)