-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Changes from 3 commits
fe7e75f
4597c4f
ed2886f
e66bea8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
*/ | ||
class PhpFormatter implements FormatterInterface | ||
{ | ||
const INDENT = ' '; | ||
|
||
/** | ||
* Format deployment configuration. | ||
* If $comments is present, each item will be added | ||
|
@@ -21,9 +23,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"; | ||
} | ||
|
||
/** | ||
|
@@ -51,18 +53,42 @@ 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, $depth = 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please take advantage of PHP 7.0 syntax (type hints and return types for methods) |
||
{ | ||
if (gettype($var) === 'array') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any specific reason not to use if (!is_array($var)) {
return var_export($var, true);
}
/// Transforming to short array syntax here
... |
||
$indexed = array_keys($var) === range(0, count($var) - 1); | ||
$r = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid variables with unclear names. |
||
foreach ($var as $key => $value) { | ||
$r[] = str_repeat(self::INDENT, $depth) | ||
. ($indexed ? '' : $this->varExportShort($key) . ' => ') | ||
. $this->varExportShort($value, $depth + 1); | ||
} | ||
return sprintf("[\n%s\n%s]", implode(",\n", $r), str_repeat(self::INDENT, $depth - 1)); | ||
} | ||
|
||
return var_export($var, true); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a description for the constant (docBlock)