Skip to content

[Backport 2.1-develop] #11328 : app:config:dump adds extra space every time in multiline array value #11451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

Expand All @@ -21,17 +21,48 @@ class PhpFormatter implements FormatterInterface
public function format($data, array $comments = [])
{
if (!empty($comments) && is_array($data)) {
$elements = array();
return "<?php\nreturn array (\n" . $this->formatData($data, $comments) . "\n);\n";
}
return "<?php\nreturn " . var_export($data, true) . ";\n";
}

/**
* Format supplied data
*
* @param string[] $data
* @param string[] $comments
* @param string $prefix
* @return string
*/
private function formatData($data, $comments = [], $prefix = ' ')
{
$elements = [];

if (is_array($data)) {
foreach ($data as $key => $value) {
$comment = ' ';
if (!empty($comments[$key])) {
$comment = " /**\n * " . str_replace("\n", "\n * ", var_export($comments[$key], true)) . "\n */\n";
$elements[] = $prefix . '/**';
$elements[] = $prefix . ' * For the section: ' . $key;

foreach (explode("\n", $comments[$key]) as $commentLine) {
$elements[] = $prefix . ' * ' . $commentLine;
}

$elements[] = $prefix . " */";
}

$elements[] = $prefix . var_export($key, true) . ' => ' .
(!is_array($value) ? var_export($value, true) . ',' : '');

if (is_array($value)) {
$elements[] = $prefix . 'array (';
$elements[] = $this->formatData($value, [], ' ' . $prefix);
$elements[] = $prefix . '),';
}
$space = is_array($value) ? " \n" : ' ';
$elements[] = $comment . var_export($key, true) . ' =>' . $space . var_export($value, true);
}
return "<?php\nreturn array (\n" . implode(",\n", str_replace("\n", "\n ", $elements)) . "\n);\n";
return implode("\n", $elements);
}
return "<?php\nreturn " . var_export($data, true) . ";\n";

return var_export($data, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework\App\Test\Unit\DeploymentConfig\Writer;

use \Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;
use Magento\Framework\App\DeploymentConfig\Writer\PhpFormatter;

class PhpFormatterTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider formatWithCommentDataProvider
* @param string|array $data
* @param array $comments
* @param string[] $data
* @param string[] $comments
* @param string $expectedResult
*/
public function testFormat($data, $comments, $expectedResult)
Expand All @@ -22,6 +21,9 @@ public function testFormat($data, $comments, $expectedResult)
$this->assertEquals($expectedResult, $formatter->format($data, $comments));
}

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function formatWithCommentDataProvider()
{
$array = [
Expand All @@ -45,9 +47,9 @@ public function formatWithCommentDataProvider()
];
$comments1 = ['ns2' => 'comment for namespace 2'];
$comments2 = [
'ns1' => 'comment for namespace 1',
'ns2' => "comment for namespace 2.\nNext comment for namespace 2",
'ns3' => 'comment for namespace 3',
'ns1' => 'comment for\' namespace 1',
'ns2' => "comment for namespace 2.\nNext comment for' namespace 2",
'ns3' => 'comment for" namespace 3',
'ns4' => 'comment for namespace 4',
'ns5' => 'comment for unexisted namespace 5',
];
Expand All @@ -68,7 +70,8 @@ public function formatWithCommentDataProvider()
),
),
/**
* 'comment for namespace 2'
* For the section: ns2
* comment for namespace 2
*/
'ns2' =>
array (
Expand All @@ -78,15 +81,16 @@ public function formatWithCommentDataProvider()
),
),
'ns3' => 'just text',
'ns4' => 'just text'
'ns4' => 'just text',
);

TEXT;
$expectedResult2 = <<<TEXT
<?php
return array (
/**
* 'comment for namespace 1'
* For the section: ns1
* comment for' namespace 1
*/
'ns1' =>
array (
Expand All @@ -102,8 +106,9 @@ public function formatWithCommentDataProvider()
),
),
/**
* 'comment for namespace 2.
* Next comment for namespace 2'
* For the section: ns2
* comment for namespace 2.
* Next comment for' namespace 2
*/
'ns2' =>
array (
Expand All @@ -113,13 +118,15 @@ public function formatWithCommentDataProvider()
),
),
/**
* 'comment for namespace 3'
* For the section: ns3
* comment for" namespace 3
*/
'ns3' => 'just text',
/**
* 'comment for namespace 4'
* For the section: ns4
* comment for namespace 4
*/
'ns4' => 'just text'
'ns4' => 'just text',
);

TEXT;
Expand Down