Skip to content

Format generated config files using the short array syntax #12499

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
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 @@ -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 integer $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],
];
}
}