Skip to content

Commit 72af510

Browse files
author
Oleksii Korshenko
authored
MAGETWO-84815: Format generated config files using the short array syntax #12499
2 parents 762ef17 + aa08c78 commit 72af510

File tree

2 files changed

+85
-31
lines changed

2 files changed

+85
-31
lines changed

lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
*/
1212
class PhpFormatter implements FormatterInterface
1313
{
14+
/**
15+
* 2 space indentation for array formatting
16+
*/
17+
const INDENT = ' ';
18+
1419
/**
1520
* Format deployment configuration.
1621
* If $comments is present, each item will be added
@@ -21,9 +26,9 @@ class PhpFormatter implements FormatterInterface
2126
public function format($data, array $comments = [])
2227
{
2328
if (!empty($comments) && is_array($data)) {
24-
return "<?php\nreturn array (\n" . $this->formatData($data, $comments) . "\n);\n";
29+
return "<?php\nreturn [\n" . $this->formatData($data, $comments) . "\n];\n";
2530
}
26-
return "<?php\nreturn " . var_export($data, true) . ";\n";
31+
return "<?php\nreturn " . $this->varExportShort($data, true) . ";\n";
2732
}
2833

2934
/**
@@ -51,18 +56,43 @@ private function formatData($data, $comments = [], $prefix = ' ')
5156
$elements[] = $prefix . " */";
5257
}
5358

54-
$elements[] = $prefix . var_export($key, true) . ' => ' .
55-
(!is_array($value) ? var_export($value, true) . ',' : '');
59+
$elements[] = $prefix . $this->varExportShort($key) . ' => ' .
60+
(!is_array($value) ? $this->varExportShort($value) . ',' : '');
5661

5762
if (is_array($value)) {
58-
$elements[] = $prefix . 'array (';
63+
$elements[] = $prefix . '[';
5964
$elements[] = $this->formatData($value, [], ' ' . $prefix);
60-
$elements[] = $prefix . '),';
65+
$elements[] = $prefix . '],';
6166
}
6267
}
6368
return implode("\n", $elements);
6469
}
6570

6671
return var_export($data, true);
6772
}
73+
74+
/**
75+
* If variable to export is an array, format with the php >= 5.4 short array syntax. Otherwise use
76+
* default var_export functionality.
77+
*
78+
* @param mixed $var
79+
* @param int $depth
80+
* @return string
81+
*/
82+
private function varExportShort($var, int $depth = 0): string
83+
{
84+
if (!is_array($var)) {
85+
return var_export($var, true);
86+
}
87+
88+
$indexed = array_keys($var) === range(0, count($var) - 1);
89+
$expanded = [];
90+
foreach ($var as $key => $value) {
91+
$expanded[] = str_repeat(self::INDENT, $depth)
92+
. ($indexed ? '' : $this->varExportShort($key) . ' => ')
93+
. $this->varExportShort($value, $depth + 1);
94+
}
95+
96+
return sprintf("[\n%s\n%s]", implode(",\n", $expanded), str_repeat(self::INDENT, $depth - 1));
97+
}
6898
}

lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,68 +55,68 @@ public function formatWithCommentDataProvider()
5555
];
5656
$expectedResult1 = <<<TEXT
5757
<?php
58-
return array (
58+
return [
5959
'ns1' =>
60-
array (
60+
[
6161
's1' =>
62-
array (
62+
[
6363
0 => 's11',
6464
1 => 's12',
65-
),
65+
],
6666
's2' =>
67-
array (
67+
[
6868
0 => 's21',
6969
1 => 's22',
70-
),
71-
),
70+
],
71+
],
7272
/**
7373
* For the section: ns2
7474
* comment for namespace 2
7575
*/
7676
'ns2' =>
77-
array (
77+
[
7878
's1' =>
79-
array (
79+
[
8080
0 => 's11',
81-
),
82-
),
81+
],
82+
],
8383
'ns3' => 'just text',
8484
'ns4' => 'just text',
85-
);
85+
];
8686
8787
TEXT;
8888
$expectedResult2 = <<<TEXT
8989
<?php
90-
return array (
90+
return [
9191
/**
9292
* For the section: ns1
9393
* comment for' namespace 1
9494
*/
9595
'ns1' =>
96-
array (
96+
[
9797
's1' =>
98-
array (
98+
[
9999
0 => 's11',
100100
1 => 's12',
101-
),
101+
],
102102
's2' =>
103-
array (
103+
[
104104
0 => 's21',
105105
1 => 's22',
106-
),
107-
),
106+
],
107+
],
108108
/**
109109
* For the section: ns2
110110
* comment for namespace 2.
111111
* Next comment for' namespace 2
112112
*/
113113
'ns2' =>
114-
array (
114+
[
115115
's1' =>
116-
array (
116+
[
117117
0 => 's11',
118-
),
119-
),
118+
],
119+
],
120120
/**
121121
* For the section: ns3
122122
* comment for" namespace 3
@@ -127,15 +127,39 @@ public function formatWithCommentDataProvider()
127127
* comment for namespace 4
128128
*/
129129
'ns4' => 'just text',
130-
);
130+
];
131+
132+
TEXT;
133+
134+
$expectedResult3 = <<<TEXT
135+
<?php
136+
return [
137+
'ns1' => [
138+
's1' => [
139+
's11',
140+
's12'
141+
],
142+
's2' => [
143+
's21',
144+
's22'
145+
]
146+
],
147+
'ns2' => [
148+
's1' => [
149+
's11'
150+
]
151+
],
152+
'ns3' => 'just text',
153+
'ns4' => 'just text'
154+
];
131155
132156
TEXT;
133157
return [
134158
['string', [], "<?php\nreturn 'string';\n"],
135159
['string', ['comment'], "<?php\nreturn 'string';\n"],
136-
[$array, [], "<?php\nreturn " . var_export($array, true) . ";\n"],
137160
[$array, $comments1, $expectedResult1],
138161
[$array, $comments2, $expectedResult2],
162+
[$array, [], $expectedResult3],
139163
];
140164
}
141165
}

0 commit comments

Comments
 (0)