2
2
3
3
namespace PHPStan \Type \Constant ;
4
4
5
+ use PHPStan \Type \BooleanType ;
6
+ use PHPStan \Type \NullType ;
5
7
use PHPStan \Type \VerbosityLevel ;
6
8
use PHPUnit \Framework \TestCase ;
7
9
@@ -27,7 +29,7 @@ public function testOptionalKeysNextAutoIndex(): void
27
29
$ builder ->setOffsetValueType (null , new ConstantIntegerType (3 ));
28
30
$ array3 = $ builder ->getArray ();
29
31
$ this ->assertInstanceOf (ConstantArrayType::class, $ array3 );
30
- $ this ->assertSame ('array{0: 1, 1? : 2|3, 2?: 3} ' , $ array3 ->describe (VerbosityLevel::precise ()));
32
+ $ this ->assertSame ('array{0: 1, 1: 2|3, 2?: 3} ' , $ array3 ->describe (VerbosityLevel::precise ()));
31
33
$ this ->assertSame ([2 , 3 ], $ array3 ->getNextAutoIndexes ());
32
34
33
35
$ this ->assertTrue ($ array3 ->isKeysSupersetOf ($ array2 ));
@@ -38,19 +40,19 @@ public function testOptionalKeysNextAutoIndex(): void
38
40
$ builder ->setOffsetValueType (null , new ConstantIntegerType (4 ));
39
41
$ array4 = $ builder ->getArray ();
40
42
$ this ->assertInstanceOf (ConstantArrayType::class, $ array4 );
41
- $ this ->assertSame ('array{0: 1, 1? : 2|3, 2? : 3|4, 3?: 4} ' , $ array4 ->describe (VerbosityLevel::precise ()));
43
+ $ this ->assertSame ('array{0: 1, 1: 2|3, 2: 3|4, 3?: 4} ' , $ array4 ->describe (VerbosityLevel::precise ()));
42
44
$ this ->assertSame ([3 , 4 ], $ array4 ->getNextAutoIndexes ());
43
45
44
46
$ builder ->setOffsetValueType (new ConstantIntegerType (3 ), new ConstantIntegerType (5 ), true );
45
47
$ array5 = $ builder ->getArray ();
46
48
$ this ->assertInstanceOf (ConstantArrayType::class, $ array5 );
47
- $ this ->assertSame ('array{0: 1, 1? : 2|3, 2? : 3|4, 3?: 4|5} ' , $ array5 ->describe (VerbosityLevel::precise ()));
49
+ $ this ->assertSame ('array{0: 1, 1: 2|3, 2: 3|4, 3?: 4|5} ' , $ array5 ->describe (VerbosityLevel::precise ()));
48
50
$ this ->assertSame ([3 , 4 ], $ array5 ->getNextAutoIndexes ());
49
51
50
52
$ builder ->setOffsetValueType (new ConstantIntegerType (3 ), new ConstantIntegerType (6 ));
51
53
$ array6 = $ builder ->getArray ();
52
54
$ this ->assertInstanceOf (ConstantArrayType::class, $ array6 );
53
- $ this ->assertSame ('array{0: 1, 1?: 2|3, 2?: 3|4, 3: 6} ' , $ array6 ->describe (VerbosityLevel::precise ()));
55
+ $ this ->assertSame ('array{1, 2|3, 3|4, 6} ' , $ array6 ->describe (VerbosityLevel::precise ()));
54
56
$ this ->assertSame ([4 ], $ array6 ->getNextAutoIndexes ());
55
57
}
56
58
@@ -82,4 +84,18 @@ public function testNextAutoIndexAnother(): void
82
84
$ this ->assertSame ([2 ], $ array ->getNextAutoIndexes ());
83
85
}
84
86
87
+ public function testAppendingOptionalKeys (): void
88
+ {
89
+ $ builder = ConstantArrayTypeBuilder::createEmpty ();
90
+
91
+ $ builder ->setOffsetValueType (null , new BooleanType (), true );
92
+ $ this ->assertSame ('array{0?: bool} ' , $ builder ->getArray ()->describe (VerbosityLevel::precise ()));
93
+
94
+ $ builder ->setOffsetValueType (null , new NullType (), true );
95
+ $ this ->assertSame ('array{0?: bool|null, 1?: null} ' , $ builder ->getArray ()->describe (VerbosityLevel::precise ()));
96
+
97
+ $ builder ->setOffsetValueType (null , new ConstantIntegerType (17 ));
98
+ $ this ->assertSame ('array{0: 17|bool|null, 1?: 17|null, 2?: 17} ' , $ builder ->getArray ()->describe (VerbosityLevel::precise ()));
99
+ }
100
+
85
101
}
0 commit comments