Skip to content

Commit c567804

Browse files
author
Oleksii Korshenko
authored
MAGETWO-82237: [Backport 2.1-develop] #11328 : app:config:dump adds extra space every time in multiline array value #11451
2 parents 0b1e9a2 + 878f5af commit c567804

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

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

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,48 @@ class PhpFormatter implements FormatterInterface
2121
public function format($data, array $comments = [])
2222
{
2323
if (!empty($comments) && is_array($data)) {
24-
$elements = array();
24+
return "<?php\nreturn array (\n" . $this->formatData($data, $comments) . "\n);\n";
25+
}
26+
return "<?php\nreturn " . var_export($data, true) . ";\n";
27+
}
28+
29+
/**
30+
* Format supplied data
31+
*
32+
* @param string[] $data
33+
* @param string[] $comments
34+
* @param string $prefix
35+
* @return string
36+
*/
37+
private function formatData($data, $comments = [], $prefix = ' ')
38+
{
39+
$elements = [];
40+
41+
if (is_array($data)) {
2542
foreach ($data as $key => $value) {
26-
$comment = ' ';
2743
if (!empty($comments[$key])) {
28-
$comment = " /**\n * " . str_replace("\n", "\n * ", var_export($comments[$key], true)) . "\n */\n";
44+
$elements[] = $prefix . '/**';
45+
$elements[] = $prefix . ' * For the section: ' . $key;
46+
47+
foreach (explode("\n", $comments[$key]) as $commentLine) {
48+
$elements[] = $prefix . ' * ' . $commentLine;
49+
}
50+
51+
$elements[] = $prefix . " */";
52+
}
53+
54+
$elements[] = $prefix . var_export($key, true) . ' => ' .
55+
(!is_array($value) ? var_export($value, true) . ',' : '');
56+
57+
if (is_array($value)) {
58+
$elements[] = $prefix . 'array (';
59+
$elements[] = $this->formatData($value, [], ' ' . $prefix);
60+
$elements[] = $prefix . '),';
2961
}
30-
$space = is_array($value) ? " \n" : ' ';
31-
$elements[] = $comment . var_export($key, true) . ' =>' . $space . var_export($value, true);
3262
}
33-
return "<?php\nreturn array (\n" . implode(",\n", str_replace("\n", "\n ", $elements)) . "\n);\n";
63+
return implode("\n", $elements);
3464
}
35-
return "<?php\nreturn " . var_export($data, true) . ";\n";
65+
66+
return var_export($data, true);
3667
}
3768
}

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
76
namespace Magento\Framework\App\Test\Unit\DeploymentConfig\Writer;
87

9-
use \Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
8+
use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
109

1110
class PhpFormatterTest extends \PHPUnit_Framework_TestCase
1211
{
1312
/**
1413
* @dataProvider formatWithCommentDataProvider
15-
* @param string|array $data
16-
* @param array $comments
14+
* @param string[] $data
15+
* @param string[] $comments
1716
* @param string $expectedResult
1817
*/
1918
public function testFormat($data, $comments, $expectedResult)
@@ -22,6 +21,9 @@ public function testFormat($data, $comments, $expectedResult)
2221
$this->assertEquals($expectedResult, $formatter->format($data, $comments));
2322
}
2423

24+
/**
25+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
26+
*/
2527
public function formatWithCommentDataProvider()
2628
{
2729
$array = [
@@ -45,9 +47,9 @@ public function formatWithCommentDataProvider()
4547
];
4648
$comments1 = ['ns2' => 'comment for namespace 2'];
4749
$comments2 = [
48-
'ns1' => 'comment for namespace 1',
49-
'ns2' => "comment for namespace 2.\nNext comment for namespace 2",
50-
'ns3' => 'comment for namespace 3',
50+
'ns1' => 'comment for\' namespace 1',
51+
'ns2' => "comment for namespace 2.\nNext comment for' namespace 2",
52+
'ns3' => 'comment for" namespace 3',
5153
'ns4' => 'comment for namespace 4',
5254
'ns5' => 'comment for unexisted namespace 5',
5355
];
@@ -68,7 +70,8 @@ public function formatWithCommentDataProvider()
6870
),
6971
),
7072
/**
71-
* 'comment for namespace 2'
73+
* For the section: ns2
74+
* comment for namespace 2
7275
*/
7376
'ns2' =>
7477
array (
@@ -78,15 +81,16 @@ public function formatWithCommentDataProvider()
7881
),
7982
),
8083
'ns3' => 'just text',
81-
'ns4' => 'just text'
84+
'ns4' => 'just text',
8285
);
8386
8487
TEXT;
8588
$expectedResult2 = <<<TEXT
8689
<?php
8790
return array (
8891
/**
89-
* 'comment for namespace 1'
92+
* For the section: ns1
93+
* comment for' namespace 1
9094
*/
9195
'ns1' =>
9296
array (
@@ -102,8 +106,9 @@ public function formatWithCommentDataProvider()
102106
),
103107
),
104108
/**
105-
* 'comment for namespace 2.
106-
* Next comment for namespace 2'
109+
* For the section: ns2
110+
* comment for namespace 2.
111+
* Next comment for' namespace 2
107112
*/
108113
'ns2' =>
109114
array (
@@ -113,13 +118,15 @@ public function formatWithCommentDataProvider()
113118
),
114119
),
115120
/**
116-
* 'comment for namespace 3'
121+
* For the section: ns3
122+
* comment for" namespace 3
117123
*/
118124
'ns3' => 'just text',
119125
/**
120-
* 'comment for namespace 4'
126+
* For the section: ns4
127+
* comment for namespace 4
121128
*/
122-
'ns4' => 'just text'
129+
'ns4' => 'just text',
123130
);
124131
125132
TEXT;

0 commit comments

Comments
 (0)