From fe7e75f59b9950866956d4e7299aef4b252cc88e Mon Sep 17 00:00:00 2001 From: Cy Kirsch Date: Wed, 29 Nov 2017 22:27:49 -0600 Subject: [PATCH 1/5] Format generated config files using the short array syntax --- .../DeploymentConfig/Writer/PhpFormatter.php | 27 ++++++++++++++++++- .../Writer/PhpFormatterTest.php | 26 +++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php b/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php index b419b3827e5ff..c1e7d557479f8 100644 --- a/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php +++ b/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php @@ -11,6 +11,8 @@ */ class PhpFormatter implements FormatterInterface { + const INDENT = ' '; + /** * Format deployment configuration. * If $comments is present, each item will be added @@ -23,7 +25,7 @@ public function format($data, array $comments = []) if (!empty($comments) && is_array($data)) { return "formatData($data, $comments) . "\n);\n"; } - return "varExportShort($data, true) . ";\n"; } /** @@ -65,4 +67,27 @@ private function formatData($data, $comments = [], $prefix = ' ') 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) { + if (gettype($var) === 'array') { + $indexed = array_keys($var) === range(0, count($var) - 1); + $r = []; + 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); + } } diff --git a/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php b/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php index a1fdedc701e8c..7e6ad7b02fcd9 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php @@ -129,13 +129,37 @@ public function formatWithCommentDataProvider() 'ns4' => 'just text', ); +TEXT; + + $expectedResult3 = << [ + 's1' => [ + 's11', + 's12' + ], + 's2' => [ + 's21', + 's22' + ] + ], + 'ns2' => [ + 's1' => [ + 's11' + ] + ], + 'ns3' => 'just text', + 'ns4' => 'just text' +]; + TEXT; return [ ['string', [], " Date: Thu, 30 Nov 2017 09:30:25 -0600 Subject: [PATCH 2/5] Fix the config formatter scenarios with $comments to also use short array syntax --- .../DeploymentConfig/Writer/PhpFormatter.php | 10 ++-- .../Writer/PhpFormatterTest.php | 48 +++++++++---------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php b/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php index c1e7d557479f8..ef0b3a5d29092 100644 --- a/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php +++ b/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php @@ -23,7 +23,7 @@ class PhpFormatter implements FormatterInterface public function format($data, array $comments = []) { if (!empty($comments) && is_array($data)) { - return "formatData($data, $comments) . "\n);\n"; + return "formatData($data, $comments) . "\n];\n"; } return "varExportShort($data, true) . ";\n"; } @@ -53,13 +53,13 @@ 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); diff --git a/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php b/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php index 7e6ad7b02fcd9..cc673e084c3b2 100644 --- a/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php +++ b/lib/internal/Magento/Framework/App/Test/Unit/DeploymentConfig/Writer/PhpFormatterTest.php @@ -55,68 +55,68 @@ public function formatWithCommentDataProvider() ]; $expectedResult1 = << - 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 = << - 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 @@ -127,7 +127,7 @@ public function formatWithCommentDataProvider() * comment for namespace 4 */ 'ns4' => 'just text', -); +]; TEXT; From ed2886fe1bf7b93761266f718322e940e27eaa77 Mon Sep 17 00:00:00 2001 From: Cy Kirsch Date: Thu, 30 Nov 2017 11:48:13 -0600 Subject: [PATCH 3/5] Fix code style issues --- .../Framework/App/DeploymentConfig/Writer/PhpFormatter.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php b/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php index ef0b3a5d29092..6556a0472f9d8 100644 --- a/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php +++ b/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php @@ -76,7 +76,8 @@ private function formatData($data, $comments = [], $prefix = ' ') * @param integer $depth * @return string */ - private function varExportShort($var, $depth=0) { + private function varExportShort($var, $depth = 0) + { if (gettype($var) === 'array') { $indexed = array_keys($var) === range(0, count($var) - 1); $r = []; @@ -88,6 +89,6 @@ private function varExportShort($var, $depth=0) { return sprintf("[\n%s\n%s]", implode(",\n", $r), str_repeat(self::INDENT, $depth - 1)); } - return var_export($var, TRUE); + return var_export($var, true); } } From e66bea87c061a16ffa70cdbb2f26d06e3b66cd04 Mon Sep 17 00:00:00 2001 From: Cy Kirsch Date: Fri, 1 Dec 2017 08:28:32 -0600 Subject: [PATCH 4/5] Fixes per ishakhsuvarov code review: - const documenation - reverse condition for readability - better variable name - type and return hints --- .../DeploymentConfig/Writer/PhpFormatter.php | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php b/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php index 6556a0472f9d8..09f2bc888e347 100644 --- a/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php +++ b/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php @@ -11,6 +11,9 @@ */ class PhpFormatter implements FormatterInterface { + /** + * 2 space indentation for array formatting + */ const INDENT = ' '; /** @@ -76,19 +79,20 @@ private function formatData($data, $comments = [], $prefix = ' ') * @param integer $depth * @return string */ - private function varExportShort($var, $depth = 0) + private function varExportShort($var, int $depth = 0): string { - if (gettype($var) === 'array') { - $indexed = array_keys($var) === range(0, count($var) - 1); - $r = []; - 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)); + 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 var_export($var, true); + return sprintf("[\n%s\n%s]", implode(",\n", $expanded), str_repeat(self::INDENT, $depth - 1)); } } From aa08c78543b6bbe5376be2728ea7f41a465d6922 Mon Sep 17 00:00:00 2001 From: Ievgen Shakhsuvarov Date: Fri, 1 Dec 2017 17:59:29 +0200 Subject: [PATCH 5/5] magento/magento2#12499: Format generated config files using the short array syntax - minor coding style improvement --- .../Framework/App/DeploymentConfig/Writer/PhpFormatter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php b/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php index 09f2bc888e347..ef7b654b66a1d 100644 --- a/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php +++ b/lib/internal/Magento/Framework/App/DeploymentConfig/Writer/PhpFormatter.php @@ -75,8 +75,8 @@ private function formatData($data, $comments = [], $prefix = ' ') * 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 + * @param mixed $var + * @param int $depth * @return string */ private function varExportShort($var, int $depth = 0): string