22
33namespace PHPStan \Analyser ;
44
5+ use PhpParser \Node \Expr \BinaryOp \Equal ;
6+ use PhpParser \Node \Expr \FuncCall ;
7+ use PhpParser \Node \Scalar \String_ ;
58use PHPStan \ShouldNotHappenException ;
69use PHPStan \Testing \PHPStanTestCase ;
10+ use PHPStan \Type \NullType ;
711
812class TypeSpecifierContextTest extends PHPStanTestCase
913{
@@ -13,23 +17,27 @@ public function dataContext(): array
1317 return [
1418 [
1519 TypeSpecifierContext::createTrue (),
16- [true , true , false , false , false ],
20+ [true , true , false , false , false , false ],
1721 ],
1822 [
1923 TypeSpecifierContext::createTruthy (),
20- [true , true , false , false , false ],
24+ [true , true , false , false , false , false ],
2125 ],
2226 [
2327 TypeSpecifierContext::createFalse (),
24- [false , false , true , true , false ],
28+ [false , false , true , true , false , false ],
2529 ],
2630 [
2731 TypeSpecifierContext::createFalsey (),
28- [false , false , true , true , false ],
32+ [false , false , true , true , false , false ],
2933 ],
3034 [
3135 TypeSpecifierContext::createNull (),
32- [false , false , false , false , true ],
36+ [false , false , false , false , true , false ],
37+ ],
38+ [
39+ $ this ->createComparisonContext (),
40+ [false , false , false , false , false , true ],
3341 ],
3442 ];
3543 }
@@ -45,27 +53,40 @@ public function testContext(TypeSpecifierContext $context, array $results): void
4553 $ this ->assertSame ($ results [2 ], $ context ->false ());
4654 $ this ->assertSame ($ results [3 ], $ context ->falsey ());
4755 $ this ->assertSame ($ results [4 ], $ context ->null ());
56+
57+ if ($ results [5 ]) {
58+ $ this ->assertNotNull ($ context ->comparison ());
59+ } else {
60+ $ this ->assertNull ($ context ->comparison ());
61+ }
4862 }
4963
5064 public function dataNegate (): array
5165 {
5266 return [
5367 [
5468 TypeSpecifierContext::createTrue ()->negate (),
55- [false , true , true , true , false ],
69+ [false , true , true , true , false , false ],
5670 ],
5771 [
5872 TypeSpecifierContext::createTruthy ()->negate (),
59- [false , false , true , true , false ],
73+ [false , false , true , true , false , false ],
6074 ],
6175 [
6276 TypeSpecifierContext::createFalse ()->negate (),
63- [true , true , false , true , false ],
77+ [true , true , false , true , false , false ],
6478 ],
6579 [
6680 TypeSpecifierContext::createFalsey ()->negate (),
67- [true , true , false , false , false ],
81+ [true , true , false , false , false , false ],
82+ ],
83+ /*
84+ // XXX should a comparison context be negatable?
85+ [
86+ $this->createComparisonContext()->negate(),
87+ [false, false, false, false, false, true],
6888 ],
89+ */
6990 ];
7091 }
7192
@@ -80,6 +101,12 @@ public function testNegate(TypeSpecifierContext $context, array $results): void
80101 $ this ->assertSame ($ results [2 ], $ context ->false ());
81102 $ this ->assertSame ($ results [3 ], $ context ->falsey ());
82103 $ this ->assertSame ($ results [4 ], $ context ->null ());
104+
105+ if ($ results [5 ]) {
106+ $ this ->assertNotNull ($ context ->comparison ());
107+ } else {
108+ $ this ->assertNull ($ context ->comparison ());
109+ }
83110 }
84111
85112 public function testNegateNull (): void
@@ -88,4 +115,17 @@ public function testNegateNull(): void
88115 TypeSpecifierContext::createNull ()->negate ();
89116 }
90117
118+ private function createComparisonContext (): TypeSpecifierContext
119+ {
120+ return TypeSpecifierContext::createComparison (
121+ new TypeSpecifierComparisonContext (
122+ new Equal (new String_ ('dummy ' ), new String_ ('dummy2 ' )),
123+ new FuncCall ('dummyFunc ' ),
124+ new NullType (),
125+ TypeSpecifierContext::createNull (),
126+ null ,
127+ ),
128+ );
129+ }
130+
91131}
0 commit comments