Skip to content

Commit 6be3066

Browse files
committed
Merge degraded members into one oversized array only when each is an array
OversizedArrayWidening merged its results into one general array when each member held a shape it degraded. A member such as bool|int|list{...}|string holds shapes without being an array, and its iterable key and value types are *ERROR*, so the merged array was non-empty-array<*ERROR*, *ERROR*>&oversized-array. Such a member now gives its arrays to the merged array and keeps the rest beside it. bug-6948.php (`$row[] = $row` fifteen times) took the same path: $row was non-empty-list<non-empty-array<*ERROR*>&oversized-array>, and is now the nested non-empty-list<int|non-empty-list<int|...>> its values are. Computing with those nested lists is the price of the new numbers. Amp-Thread-ID: https://ampcode.com/threads/T-01a21c4a-f4aa-73fb-ab5d-2ac321ba792a
1 parent 024ac54 commit 6be3066

4 files changed

Lines changed: 185 additions & 8 deletions

File tree

‎src/Type/Combinator/OversizedArrayWidening.php‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Type\Constant\ConstantIntegerType;
1212
use PHPStan\Type\ConstantScalarType;
1313
use PHPStan\Type\GeneralizePrecision;
14+
use PHPStan\Type\Generic\TemplateType;
1415
use PHPStan\Type\IntersectionType;
1516
use PHPStan\Type\Type;
1617
use PHPStan\Type\TypeCombinator;
@@ -68,13 +69,34 @@ public function apply(array $types): array
6869
return [...$emptyArrays, ...$results];
6970
}
7071

72+
// a value type holding shapes, such as `int|array{a: int}`, merges
73+
// its arrays only, and the rest of it stays beside them
74+
$arrays = [];
75+
$otherTypes = [];
76+
$emptyMembers = [];
77+
foreach ($results as $result) {
78+
$members = $result instanceof UnionType && !$result instanceof TemplateType ? $result->getTypes() : [$result];
79+
foreach ($members as $member) {
80+
if (!$member->isArray()->yes()) {
81+
$otherTypes[] = $member;
82+
} elseif ($member->isConstantArray()->yes() && $member->isIterableAtLeastOnce()->no()) {
83+
$emptyMembers[] = $member;
84+
} else {
85+
$arrays[] = $member;
86+
}
87+
}
88+
}
89+
if ($arrays === []) {
90+
return [...$emptyArrays, ...$results];
91+
}
92+
7193
$eachIsList = true;
7294
$keyTypes = [];
7395
$valueTypes = [];
74-
foreach ($results as $result) {
75-
$keyTypes[] = $result->getIterableKeyType();
76-
$valueTypes[] = $result->getIterableValueType();
77-
if ($result->isList()->yes()) {
96+
foreach ($arrays as $array) {
97+
$keyTypes[] = $array->getIterableKeyType();
98+
$valueTypes[] = $array->getIterableValueType();
99+
if ($array->isList()->yes()) {
78100
continue;
79101
}
80102
$eachIsList = false;
@@ -87,7 +109,7 @@ public function apply(array $types): array
87109
$valueType = $valueType->generalize(GeneralizePrecision::lessSpecific());
88110
}
89111

90-
return [...$emptyArrays, self::oversizedArray($keyType, $valueType, $eachIsList)];
112+
return [...$emptyArrays, ...$emptyMembers, ...$otherTypes, self::oversizedArray($keyType, $valueType, $eachIsList)];
91113
}
92114

93115
private static function degrade(ConstantArrayType $type): Type

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3714,6 +3714,11 @@ public function testBug13164(): void
37143714
$this->analyse([__DIR__ . '/data/bug-13164.php'], []);
37153715
}
37163716

3717+
public function testBug15861(): void
3718+
{
3719+
$this->analyse([__DIR__ . '/data/bug-15861.php'], []);
3720+
}
3721+
37173722
public function testFloatConstantLoopGeneralize(): void
37183723
{
37193724
$this->analyse([__DIR__ . '/../../Analyser/nsrt/float-constant-loop-generalize.php'], []);
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug15861;
4+
5+
function xl(string $s): string { return $s; }
6+
function text(string $s): string { return $s; }
7+
function f(): void {
8+
$search_options = [
9+
"demos" => [
10+
"title" => xl("Demographics"),
11+
"cols" => [
12+
"patient_name" => ["heading" => xl("Patient Name"), "width" => "10%"],
13+
"patient_id" => ["heading" => xl("PID"), "width" => "nowrap"],
14+
"patient_age" => ["heading" => xl("Age"), "width" => "nowrap"],
15+
"patient_sex" => ["heading" => xl("Gender"), "width" => "nowrap"],
16+
"patient_ethnic" => ["heading" => xl("Ethnicity"), "width" => "10%"],
17+
"patient_race" => ["heading" => xl("Race"), "width" => "10%"],
18+
"users_provider" => ["heading" => xl("Provider"), "width" => "10%"]
19+
],
20+
],
21+
"diagnosis_check" => [
22+
"hidden" => true,
23+
"cols" => [
24+
"other_date" => ["heading" => xl("Diagnosis Date"), "width" => "nowrap"],
25+
"patient_name" => ["heading" => xl("Patient Name"), "width" => "10%"],
26+
"patient_id" => ["heading" => xl("PID"), "width" => "nowrap"],
27+
"patient_age" => ["heading" => xl("Age"), "width" => "nowrap"],
28+
"patient_sex" => ["heading" => xl("Gender"), "width" => "nowrap"],
29+
"patient_ethnic" => ["heading" => xl("Ethnicity"), "width" => "10%"],
30+
"users_provider" => ["heading" => xl("Provider"), "width" => "10%"],
31+
"lists_title" => [ "width" => "15%"], // Heading assigned later
32+
"pr_diagnosis" => ["heading" => xl("Diagnosis Codes"), "width" => "20%"]
33+
],
34+
"sort_cols" => -1,
35+
],
36+
"prescripts" => [
37+
"title" => xl("Prescriptions"),
38+
"cols" => [
39+
"other_date" => ["heading" => xl("Filled"), "width" => "10%"],
40+
"patient_name" => ["heading" => xl("Patient Name"), "width" => "10%"],
41+
"patient_id" => ["heading" => xl("PID"), "width" => "nowrap"],
42+
"patient_age" => ["heading" => xl("Age"), "width" => "nowrap"],
43+
"patient_sex" => ["heading" => xl("Gender"), "width" => "nowrap"],
44+
"rx_drug" => ["heading" => xl("Drug"), "width" => "20%"],
45+
"rx_medicine_units" => ["heading" => xl("Units"), "width" => "nowrap"],
46+
"rx_directions" => ["heading" => xl("Directions"), "width" => "10%"],
47+
"rx_quantity" => ["heading" => xl("Quantity"), "width" => "nowrap"],
48+
"rx_refills" => ["heading" => xl("Refills"), "width" => "nowrap"]
49+
],
50+
],
51+
"comms" => [
52+
"title" => xl("Communication"),
53+
"cols" => [
54+
"patient_date" => ["heading" => xl("Date Created"), "width" => "nowrap"],
55+
"patient_name" => ["heading" => xl("Patient Name"), "width" => "10%"],
56+
"patient_id" => ["heading" => xl("PID"), "width" => "nowrap"],
57+
"patient_age" => ["heading" => xl("Age"), "width" => "nowrap"],
58+
"patient_sex" => ["heading" => xl("Gender"), "width" => "nowrap"],
59+
"patient_ethnic" => ["heading" => xl("Ethnicity"), "width" => "10%"],
60+
"users_provider" => ["heading" => xl("Provider"), "width" => "10%"],
61+
"communications" => ["heading" => xl("Communication"), "width" => "15%"]
62+
],
63+
],
64+
"insurers" => [
65+
"title" => xl("Insurance Companies"),
66+
"cols" => [
67+
"patient_date" => ["heading" => xl("Date Created"), "width" => "nowrap"],
68+
"patient_name" => ["heading" => xl("Patient Name"), "width" => "10%"],
69+
"patient_id" => ["heading" => xl("PID"), "width" => "nowrap"],
70+
"patient_age" => ["heading" => xl("Age"), "width" => "nowrap"],
71+
"patient_sex" => ["heading" => xl("Gender"), "width" => "nowrap"],
72+
"patient_ethnic" => ["heading" => xl("Ethnicity"), "width" => "10%"],
73+
"users_provider" => ["heading" => xl("Insurance Provider"), "width" => "10%"],
74+
"ins_name" => ["heading" => xl("Primary Insurance"), "width" => "10%"]
75+
],
76+
],
77+
"encounts" => [
78+
"title" => xl("Encounters"),
79+
"cols" => [
80+
"other_date" => ["heading" => xl("Encounter Date"), "width" => "nowrap"],
81+
"patient_name" => ["heading" => xl("Patient Name"), "width" => "10%"],
82+
"patient_id" => ["heading" => xl("PID"), "width" => "nowrap"],
83+
"patient_age" => ["heading" => xl("Age"), "width" => "nowrap"],
84+
"patient_sex" => ["heading" => xl("Gender"), "width" => "nowrap"],
85+
"users_provider" => ["heading" => xl("Provider"), "width" => "10%"],
86+
"enc_type" => ["heading" => xl("Encounter type"), "width" => "20%"],
87+
"enc_reason" => ["heading" => xl("Reason"), "width" => "15%"],
88+
"enc_facility" => ["heading" => xl("Facility"), "width" => "10%"],
89+
"enc_discharge" => ["heading" => xl("Discharge Disposition"), "width" => "10%"]
90+
],
91+
],
92+
"observs" => [
93+
"title" => xl("Observations"),
94+
"cols" => [
95+
"other_date" => ["heading" => xl("Date"), "width" => "nowrap"],
96+
"patient_name" => ["heading" => xl("Patient Name"), "width" => "10%"],
97+
"patient_id" => ["heading" => xl("PID"), "width" => "nowrap"],
98+
"patient_age" => ["heading" => xl("Age"), "width" => "nowrap"],
99+
"patient_sex" => ["heading" => xl("Gender"), "width" => "nowrap"],
100+
"users_provider" => ["heading" => xl("Provider"), "width" => "10%"],
101+
"obs_code" => ["heading" => xl("Code"), "width" => "nowrap"],
102+
"obs_description" => ["heading" => xl("Description"), "width" => "15%"],
103+
"obs_type" => ["heading" => xl("Type"), "width" => "10%"],
104+
"obs_value" => ["heading" => xl("Value"), "width" => "nowrap"],
105+
"obs_units" => ["heading" => xl("Units"), "width" => "nowrap"],
106+
"obs_comments" => ["heading" => xl("Comments"), "width" => "20%"]
107+
],
108+
"sort_cols" => -1,
109+
],
110+
"procs" => [
111+
"title" => xl("Procedures"),
112+
"cols" => [
113+
"other_date" => ["heading" => xl("Order Date"), "width" => "nowrap"],
114+
"patient_name" => ["heading" => xl("Patient Name"), "width" => "10%"],
115+
"patient_id" => ["heading" => xl("PID"), "width" => "nowrap"],
116+
"users_provider" => ["heading" => xl("Procedure Provider"), "width" => "10%"],
117+
"pr_lab" => ["heading" => xl("Lab"), "width" => "10%"],
118+
"pr_status" => ["heading" => xl("Status"), "width" => "nowrap"],
119+
"prc_procedure" => ["heading" => xl("Procedure Test"), "width" => "10%"],
120+
"pr_diagnosis" => ["heading" => xl("Primary Diagnosis"), "width" => "20%"],
121+
"prc_diagnoses" => ["heading" => xl("Diagnosis Codes"), "width" => "20%"]
122+
],
123+
"sort_cols" => -2,
124+
"acl" => ["encounters", "coding_a"]
125+
],
126+
"results" => [
127+
"title" => xl("Lab Results"),
128+
"cols" => [
129+
"other_date" => ["heading" => xl("Date"), "width" => "nowrap"],
130+
"result_facility" => ["heading" => xl("Facility"), "width" => "10%"],
131+
"patient_id" => ["heading" => xl("PID"), "width" => "nowrap"],
132+
"result_description" => ["heading" => xl("Procedure Test"), "width" => "10%"],
133+
"result_result" => ["heading" => xl("Result"), "width" => "5%"],
134+
"result_units" => ["heading" => xl("Unit"), "width" => "nowrap"],
135+
"result_range" => ["heading" => xl("Range"), "width" => "5%"],
136+
"result_abnormal" => ["heading" => xl("Abnormal"), "width" => "nowrap"],
137+
"result_comments" => ["heading" => xl("Comments"), "width" => "20%"],
138+
"result_document_id" => ["heading" => xl("Document ID"), "width" => "nowrap"],
139+
],
140+
"sort_cols" => -2,
141+
"acl" => ["patients", "lab"]
142+
]
143+
144+
];
145+
foreach ($search_options as $key => $items) {
146+
if (!isset($items["hidden"]) || !$items["hidden"]) {
147+
echo text($items["title"]);
148+
}
149+
}
150+
}

‎tests/bench/data/bug-6948.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php declare(strict_types = 1);
22

33
/*
4-
* instructions: 211.9M
5-
* cycles: 271.1M
6-
* memory: 1.290MiB
4+
* instructions: 692.1M
5+
* cycles: 842.0M
6+
* memory: 2.177MiB
77
*/
88

99
namespace Bug6948;

0 commit comments

Comments
 (0)