File tree 3 files changed +69
-2
lines changed
3 files changed +69
-2
lines changed Original file line number Diff line number Diff line change @@ -735,12 +735,12 @@ public function isConstantScalarValue(): TrinaryLogic
735
735
736
736
public function getConstantScalarTypes (): array
737
737
{
738
- return $ this ->pickFromTypes (static fn (Type $ type ) => $ type ->getConstantScalarTypes ());
738
+ return $ this ->notBenevolentPickFromTypes (static fn (Type $ type ) => $ type ->getConstantScalarTypes ());
739
739
}
740
740
741
741
public function getConstantScalarValues (): array
742
742
{
743
- return $ this ->pickFromTypes (static fn (Type $ type ) => $ type ->getConstantScalarValues ());
743
+ return $ this ->notBenevolentPickFromTypes (static fn (Type $ type ) => $ type ->getConstantScalarValues ());
744
744
}
745
745
746
746
public function isTrue (): TrinaryLogic
@@ -1008,4 +1008,26 @@ protected function pickFromTypes(callable $getValues): array
1008
1008
return $ values ;
1009
1009
}
1010
1010
1011
+ /**
1012
+ * @template T
1013
+ * @param callable(Type $type): list<T> $getValues
1014
+ * @return list<T>
1015
+ */
1016
+ private function notBenevolentPickFromTypes (callable $ getValues ): array
1017
+ {
1018
+ $ values = [];
1019
+ foreach ($ this ->types as $ type ) {
1020
+ $ innerValues = $ getValues ($ type );
1021
+ if ($ innerValues === []) {
1022
+ return [];
1023
+ }
1024
+
1025
+ foreach ($ innerValues as $ innerType ) {
1026
+ $ values [] = $ innerType ;
1027
+ }
1028
+ }
1029
+
1030
+ return $ values ;
1031
+ }
1032
+
1011
1033
}
Original file line number Diff line number Diff line change @@ -1201,6 +1201,7 @@ public function dataFileAsserts(): iterable
1201
1201
yield from $ this ->gatherAssertTypes (__DIR__ . '/data/list-shapes.php ' );
1202
1202
yield from $ this ->gatherAssertTypes (__DIR__ . '/data/bug-7607.php ' );
1203
1203
yield from $ this ->gatherAssertTypes (__DIR__ . '/data/ibm_db2.php ' );
1204
+ yield from $ this ->gatherAssertTypes (__DIR__ . '/data/benevolent-union-math.php ' );
1204
1205
}
1205
1206
1206
1207
/**
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace BenevolentUnionMath ;
4
+
5
+ use function PHPStan \Testing \assertType ;
6
+ use function strtotime ;
7
+
8
+ class Foo
9
+ {
10
+
11
+ public function doFoo ($ s , $ r )
12
+ {
13
+ $ timeS = strtotime ($ s );
14
+ assertType ('(int|false) ' , $ timeS );
15
+
16
+ $ timeR = strtotime ($ r );
17
+ assertType ('(int|false) ' , $ timeR );
18
+
19
+ assertType ('int ' , $ timeS - $ timeR );
20
+ }
21
+
22
+ }
23
+
24
+ class HelloWorld
25
+ {
26
+ public function sayHello (int $ x ): void
27
+ {
28
+ $ dbresponse = $ this ->getBenevolent ();
29
+ assertType ('array<string, (float|int|string|null)>|null ' , $ dbresponse );
30
+
31
+ if ($ dbresponse === null ) {return ;}
32
+
33
+ assertType ('array<string, (float|int|string|null)> ' , $ dbresponse );
34
+ assertType ('(float|int|string|null) ' , $ dbresponse ['Value ' ]);
35
+ assertType ('int<0, max> ' , strlen ($ dbresponse ['Value ' ]));
36
+ }
37
+
38
+ /**
39
+ * @return array<string, __benevolent<float|int|string|null>>|null
40
+ */
41
+ private function getBenevolent (): ?array {
42
+ return rand (10 ) > 1 ? null : [];
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments