Skip to content
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

#11328 : app:config:dump adds extra space every time in multiline array value #11452

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
Expand Up @@ -21,21 +21,48 @@ class PhpFormatter implements FormatterInterface
public function format($data, array $comments = [])
{
if (!empty($comments) && is_array($data)) {
$elements = [];
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])) {
$section = " * For the section: " . $key . "\n";
$exportedComment = is_string($comments[$key])
? $comments[$key]
: var_export($comments[$key], true);
$comment = " /**\n" . $section . " * " . str_replace("\n", "\n * ", $exportedComment) . "\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 @@ -5,14 +5,14 @@
*/
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 Down Expand Up @@ -81,7 +81,7 @@ public function formatWithCommentDataProvider()
),
),
'ns3' => 'just text',
'ns4' => 'just text'
'ns4' => 'just text',
);

TEXT;
Expand Down Expand Up @@ -126,7 +126,7 @@ public function formatWithCommentDataProvider()
* For the section: ns4
* comment for namespace 4
*/
'ns4' => 'just text'
'ns4' => 'just text',
);

TEXT;
Expand Down