Skip to content

Commit fc052c4

Browse files
committed
ConstantArrayType::generalize() should generalize item type too
1 parent cc91080 commit fc052c4

File tree

5 files changed

+83
-5
lines changed

5 files changed

+83
-5
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ public function generalize(): Type
613613

614614
$arrayType = new ArrayType(
615615
TypeUtils::generalizeType($this->getKeyType()),
616-
$this->getItemType()
616+
TypeUtils::generalizeType($this->getItemType())
617617
);
618618

619619
if (count($this->keyTypes) > count($this->optionalKeys)) {

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3191,11 +3191,11 @@ public function dataBinaryOperations(): array
31913191
'$coalesceArray',
31923192
],
31933193
[
3194-
'array<int, 1|2|3>',
3194+
'array<int, int>',
31953195
'$arrayToBeUnset',
31963196
],
31973197
[
3198-
'array<int, 1|2|3>',
3198+
'array<int, int>',
31993199
'$arrayToBeUnset2',
32003200
],
32013201
[
@@ -9140,7 +9140,7 @@ public function dataIsset(): array
91409140
'$anotherArrayCopy',
91419141
],
91429142
[
9143-
'array<string, 1|2|3|4|null>',
9143+
'array<string, int|null>',
91449144
'$yetAnotherArrayCopy',
91459145
],
91469146
[

tests/PHPStan/Analyser/data/generics.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function testD($int, $float, $intFloat)
9797
assertType('DateTime|int', d($int, new \DateTime()));
9898
assertType('DateTime|float|int', d($intFloat, new \DateTime()));
9999
assertType('array()|DateTime', d([], new \DateTime()));
100-
assertType('(array<string, \'barrrr\'>&nonEmpty)|DateTime', d(['blabla' => 'barrrr'], new \DateTime()));
100+
assertType('(array<string, string>&nonEmpty)|DateTime', d(['blabla' => 'barrrr'], new \DateTime()));
101101
}
102102

103103
/**

tests/PHPStan/Rules/Classes/InstantiationRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,9 @@ public function testPromotedProperties(): void
287287
]);
288288
}
289289

290+
public function testBug4056(): void
291+
{
292+
$this->analyse([__DIR__ . '/data/bug-4056.php'], []);
293+
}
294+
290295
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Bug4056;
4+
5+
$a = new \ArrayObject([
6+
'Dummy' => new \ArrayObject([
7+
'properties' => [
8+
'id' => [
9+
'readOnly' => true,
10+
'type' => 'integer',
11+
],
12+
'description' => [
13+
'type' => 'string',
14+
],
15+
],
16+
]),
17+
'Dummy-list' => new \ArrayObject([
18+
'properties' => [
19+
'id' => [
20+
'readOnly' => true,
21+
'type' => 'integer',
22+
],
23+
'description' => [
24+
'type' => 'string',
25+
],
26+
],
27+
]),
28+
'Dummy-list_details' => new \ArrayObject([
29+
'properties' => [
30+
'id' => [
31+
'readOnly' => true,
32+
'type' => 'integer',
33+
],
34+
'description' => [
35+
'type' => 'string',
36+
],
37+
'relatedDummy' => new \ArrayObject([
38+
'$ref' => '#/definitions/RelatedDummy-list_details',
39+
]),
40+
],
41+
]),
42+
'Dummy:OutputDto' => new \ArrayObject([
43+
'type' => 'object',
44+
'properties' => [
45+
'baz' => new \ArrayObject([
46+
'readOnly' => true,
47+
'type' => 'string',
48+
]),
49+
'bat' => new \ArrayObject([
50+
'type' => 'integer',
51+
]),
52+
],
53+
]),
54+
'Dummy:InputDto' => new \ArrayObject([
55+
'type' => 'object',
56+
'properties' => [
57+
'foo' => new \ArrayObject([
58+
'type' => 'string',
59+
]),
60+
'bar' => new \ArrayObject([
61+
'type' => 'integer',
62+
]),
63+
],
64+
]),
65+
'RelatedDummy-list_details' => new \ArrayObject([
66+
'type' => 'object',
67+
'properties' => [
68+
'name' => new \ArrayObject([
69+
'type' => 'string',
70+
]),
71+
],
72+
]),
73+
]);

0 commit comments

Comments
 (0)