Skip to content

Commit

Permalink
MAGETWO-84815: Format generated config files using the short array sy…
Browse files Browse the repository at this point in the history
…ntax #12499
  • Loading branch information
Oleksii Korshenko authored Dec 1, 2017
2 parents 762ef17 + aa08c78 commit 72af510
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
*/
class PhpFormatter implements FormatterInterface
{
/**
* 2 space indentation for array formatting
*/
const INDENT = ' ';

/**
* Format deployment configuration.
* If $comments is present, each item will be added
Expand All @@ -21,9 +26,9 @@ class PhpFormatter implements FormatterInterface
public function format($data, array $comments = [])
{
if (!empty($comments) && is_array($data)) {
return "<?php\nreturn array (\n" . $this->formatData($data, $comments) . "\n);\n";
return "<?php\nreturn [\n" . $this->formatData($data, $comments) . "\n];\n";
}
return "<?php\nreturn " . var_export($data, true) . ";\n";
return "<?php\nreturn " . $this->varExportShort($data, true) . ";\n";
}

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

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

if (is_array($value)) {
$elements[] = $prefix . 'array (';
$elements[] = $prefix . '[';
$elements[] = $this->formatData($value, [], ' ' . $prefix);
$elements[] = $prefix . '),';
$elements[] = $prefix . '],';
}
}
return implode("\n", $elements);
}

return var_export($data, true);
}

/**
* If variable to export is an array, format with the php >= 5.4 short array syntax. Otherwise use
* default var_export functionality.
*
* @param mixed $var
* @param int $depth
* @return string
*/
private function varExportShort($var, int $depth = 0): string
{
if (!is_array($var)) {
return var_export($var, true);
}

$indexed = array_keys($var) === range(0, count($var) - 1);
$expanded = [];
foreach ($var as $key => $value) {
$expanded[] = str_repeat(self::INDENT, $depth)
. ($indexed ? '' : $this->varExportShort($key) . ' => ')
. $this->varExportShort($value, $depth + 1);
}

return sprintf("[\n%s\n%s]", implode(",\n", $expanded), str_repeat(self::INDENT, $depth - 1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,68 +55,68 @@ public function formatWithCommentDataProvider()
];
$expectedResult1 = <<<TEXT
<?php
return array (
return [
'ns1' =>
array (
[
's1' =>
array (
[
0 => 's11',
1 => 's12',
),
],
's2' =>
array (
[
0 => 's21',
1 => 's22',
),
),
],
],
/**
* For the section: ns2
* comment for namespace 2
*/
'ns2' =>
array (
[
's1' =>
array (
[
0 => 's11',
),
),
],
],
'ns3' => 'just text',
'ns4' => 'just text',
);
];
TEXT;
$expectedResult2 = <<<TEXT
<?php
return array (
return [
/**
* For the section: ns1
* comment for' namespace 1
*/
'ns1' =>
array (
[
's1' =>
array (
[
0 => 's11',
1 => 's12',
),
],
's2' =>
array (
[
0 => 's21',
1 => 's22',
),
),
],
],
/**
* For the section: ns2
* comment for namespace 2.
* Next comment for' namespace 2
*/
'ns2' =>
array (
[
's1' =>
array (
[
0 => 's11',
),
),
],
],
/**
* For the section: ns3
* comment for" namespace 3
Expand All @@ -127,15 +127,39 @@ public function formatWithCommentDataProvider()
* comment for namespace 4
*/
'ns4' => 'just text',
);
];
TEXT;

$expectedResult3 = <<<TEXT
<?php
return [
'ns1' => [
's1' => [
's11',
's12'
],
's2' => [
's21',
's22'
]
],
'ns2' => [
's1' => [
's11'
]
],
'ns3' => 'just text',
'ns4' => 'just text'
];
TEXT;
return [
['string', [], "<?php\nreturn 'string';\n"],
['string', ['comment'], "<?php\nreturn 'string';\n"],
[$array, [], "<?php\nreturn " . var_export($array, true) . ";\n"],
[$array, $comments1, $expectedResult1],
[$array, $comments2, $expectedResult2],
[$array, [], $expectedResult3],
];
}
}

0 comments on commit 72af510

Please sign in to comment.