From 1e3af2d9205893ed7945a0a145b30a1a6d3205d7 Mon Sep 17 00:00:00 2001 From: Matthew Butterworth Date: Wed, 20 Feb 2019 17:10:05 +0000 Subject: [PATCH 1/7] Make Yaml parser compliant with ParserInterface Also do some clean up (unused params, docblocks, typos) --- src/Parser/Yaml.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Parser/Yaml.php b/src/Parser/Yaml.php index e6a41e8..5552edf 100644 --- a/src/Parser/Yaml.php +++ b/src/Parser/Yaml.php @@ -22,7 +22,7 @@ class Yaml implements ParserInterface * {@inheritDoc} * Loads a YAML/YML file as an array * - * @throws ParseException If If there is an error parsing the YAML file + * @throws ParseException If there is an error parsing the YAML file */ public function parseFile($filename) { @@ -37,7 +37,9 @@ public function parseFile($filename) ); } - return $this->parse($data, $filename); + $parsed = $this->parse($data); + + return $parsed === null ? [] : $parsed; } /** @@ -59,18 +61,19 @@ public function parseString($config) ); } - return $this->parse($data); + $parsed = $this->parse($data); + + return $parsed === null ? [] : $parsed; } /** * Completes parsing of YAML/YML data * - * @param array $data - * @param strring $filename + * @param array $data * - * @throws ParseException If there is an error parsing the YAML data + * @return array|null */ - protected function parse($data = null, $filename = null) + protected function parse($data = null) { return $data; } From 290c06d117b57ee8171dd4adf64547d7443ab138 Mon Sep 17 00:00:00 2001 From: Matthew Butterworth Date: Wed, 20 Feb 2019 17:18:14 +0000 Subject: [PATCH 2/7] Make XML parser compliant with ParserInterface --- src/Parser/Xml.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Parser/Xml.php b/src/Parser/Xml.php index 3b3a063..6b67d88 100644 --- a/src/Parser/Xml.php +++ b/src/Parser/Xml.php @@ -26,7 +26,9 @@ public function parseFile($filename) { libxml_use_internal_errors(true); $data = simplexml_load_file($filename, null, LIBXML_NOERROR); - return $this->parse($data, $filename); + $parsed = $this->parse($data, $filename); + + return $parsed === null ? [] : $parsed; } /** @@ -39,15 +41,18 @@ public function parseString($config) { libxml_use_internal_errors(true); $data = simplexml_load_string($config, null, LIBXML_NOERROR); - return $this->parse($data); + $parsed = $this->parse($data); + + return $parsed === null ? [] : $parsed; } /** * Completes parsing of XML data * - * @param array $data - * @param strring $filename + * @param array $data + * @param string $filename * + * @return array|null * @throws ParseException If there is an error parsing the XML data */ protected function parse($data = null, $filename = null) From bfc98b1e675bdcc1045a900cbb4d2a5607be51d8 Mon Sep 17 00:00:00 2001 From: Matthew Butterworth Date: Wed, 20 Feb 2019 17:24:14 +0000 Subject: [PATCH 3/7] Make PHP parser ParserInterface compliant --- src/Parser/Php.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Parser/Php.php b/src/Parser/Php.php index 806d834..36a211f 100644 --- a/src/Parser/Php.php +++ b/src/Parser/Php.php @@ -40,7 +40,8 @@ public function parseFile($filename) } // Complete parsing - return $this->parse($data, $filename); + $parsed = $this->parse($data, $filename); + return $parsed === null ? [] : $parsed; } /** @@ -71,16 +72,18 @@ public function parseString($config) } // Complete parsing - return $this->parse($data); + $parsed = $this->parse($data); + return $parsed === null ? [] : $parsed; } /** * Completes parsing of PHP data * - * @param array $data - * @param strring $filename + * @param array $data + * @param string $filename * - * @throws ParseException If there is an error parsing the PHP data + * @return array|null + * @throws UnsupportedFormatException */ protected function parse($data = null, $filename = null) { From f97f1bb866759305a091902601dbfadb6779c800 Mon Sep 17 00:00:00 2001 From: Matthew Butterworth Date: Wed, 20 Feb 2019 17:35:45 +0000 Subject: [PATCH 4/7] Make json parser compliant with Parser Interface --- src/Parser/Json.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Parser/Json.php b/src/Parser/Json.php index 16bba18..5d66234 100644 --- a/src/Parser/Json.php +++ b/src/Parser/Json.php @@ -25,7 +25,8 @@ class Json implements ParserInterface public function parseFile($filename) { $data = json_decode(file_get_contents($filename), true); - return $this->parse($data, $filename); + $parsed = $this->parse($data, $filename); + return $parsed === null ? [] : $parsed; } /** @@ -37,7 +38,9 @@ public function parseFile($filename) public function parseString($config) { $data = json_decode($config, true); - return $this->parse($data); + $parsed = $this->parse($data); + + return $parsed === null ? [] : $parsed; } /** From 3895a037bcfbd240403bf71fe343e52cd7a0b2f3 Mon Sep 17 00:00:00 2001 From: Matthew Butterworth Date: Wed, 20 Feb 2019 21:35:45 +0000 Subject: [PATCH 5/7] Remove array casts from parseString and parseFile calls --- src/Config.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Config.php b/src/Config.php index 71e6d9a..f4a08f8 100644 --- a/src/Config.php +++ b/src/Config.php @@ -94,13 +94,13 @@ protected function loadFromFile($path, ParserInterface $parser = null) $parser = $this->getParser($extension); // Try to load file - $this->data = array_replace_recursive($this->data, (array) $parser->parseFile($path)); + $this->data = array_replace_recursive($this->data, $parser->parseFile($path)); // Clean parser $parser = null; } else { // Try to load file using specified parser - $this->data = array_replace_recursive($this->data, (array) $parser->parseFile($path)); + $this->data = array_replace_recursive($this->data, $parser->parseFile($path)); } } } @@ -116,7 +116,7 @@ protected function loadFromString($configuration, ParserInterface $parser) $this->data = []; // Try to parse string - $this->data = array_replace_recursive($this->data, (array) $parser->parseString($configuration)); + $this->data = array_replace_recursive($this->data, $parser->parseString($configuration)); } /** From e410d0d2c5ffe8da720868f23c8653bdf482d4a0 Mon Sep 17 00:00:00 2001 From: Matthew Butterworth Date: Fri, 22 Feb 2019 13:35:48 +0000 Subject: [PATCH 6/7] Fix incorrect docblock --- src/Parser/Json.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Parser/Json.php b/src/Parser/Json.php index 5d66234..ecdf1b5 100644 --- a/src/Parser/Json.php +++ b/src/Parser/Json.php @@ -47,7 +47,7 @@ public function parseString($config) * Completes parsing of JSON data * * @param array $data - * @param strring $filename + * @param string $filename * * @throws ParseException If there is an error parsing the JSON data */ @@ -64,6 +64,7 @@ protected function parse($data = null, $filename = null) 'type' => json_last_error(), 'file' => $filename, ]; + throw new ParseException($error); } From 00462ce8fc7727e961f3841d7ed7eaca3fffa2c9 Mon Sep 17 00:00:00 2001 From: Personal Date: Wed, 27 Mar 2019 23:56:58 +0000 Subject: [PATCH 7/7] Cast to array --- src/Parser/Json.php | 10 +++++----- src/Parser/Php.php | 6 ++---- src/Parser/Xml.php | 8 +++----- src/Parser/Yaml.php | 8 ++------ 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/Parser/Json.php b/src/Parser/Json.php index ecdf1b5..bfb22c7 100644 --- a/src/Parser/Json.php +++ b/src/Parser/Json.php @@ -25,8 +25,8 @@ class Json implements ParserInterface public function parseFile($filename) { $data = json_decode(file_get_contents($filename), true); - $parsed = $this->parse($data, $filename); - return $parsed === null ? [] : $parsed; + + return (array)$this->parse($data, $filename); } /** @@ -38,16 +38,16 @@ public function parseFile($filename) public function parseString($config) { $data = json_decode($config, true); - $parsed = $this->parse($data); - return $parsed === null ? [] : $parsed; + return (array)$this->parse($data); } /** * Completes parsing of JSON data * - * @param array $data + * @param array $data * @param string $filename + * @return array|null * * @throws ParseException If there is an error parsing the JSON data */ diff --git a/src/Parser/Php.php b/src/Parser/Php.php index 36a211f..fb4fba6 100644 --- a/src/Parser/Php.php +++ b/src/Parser/Php.php @@ -40,8 +40,7 @@ public function parseFile($filename) } // Complete parsing - $parsed = $this->parse($data, $filename); - return $parsed === null ? [] : $parsed; + return (array)$this->parse($data, $filename); } /** @@ -72,8 +71,7 @@ public function parseString($config) } // Complete parsing - $parsed = $this->parse($data); - return $parsed === null ? [] : $parsed; + return (array)$this->parse($data); } /** diff --git a/src/Parser/Xml.php b/src/Parser/Xml.php index 6b67d88..66bce5e 100644 --- a/src/Parser/Xml.php +++ b/src/Parser/Xml.php @@ -26,9 +26,8 @@ public function parseFile($filename) { libxml_use_internal_errors(true); $data = simplexml_load_file($filename, null, LIBXML_NOERROR); - $parsed = $this->parse($data, $filename); - return $parsed === null ? [] : $parsed; + return (array)$this->parse($data, $filename); } /** @@ -41,9 +40,7 @@ public function parseString($config) { libxml_use_internal_errors(true); $data = simplexml_load_string($config, null, LIBXML_NOERROR); - $parsed = $this->parse($data); - - return $parsed === null ? [] : $parsed; + return (array)$this->parse($data); } /** @@ -53,6 +50,7 @@ public function parseString($config) * @param string $filename * * @return array|null + * * @throws ParseException If there is an error parsing the XML data */ protected function parse($data = null, $filename = null) diff --git a/src/Parser/Yaml.php b/src/Parser/Yaml.php index 5552edf..46f3cb5 100644 --- a/src/Parser/Yaml.php +++ b/src/Parser/Yaml.php @@ -37,9 +37,7 @@ public function parseFile($filename) ); } - $parsed = $this->parse($data); - - return $parsed === null ? [] : $parsed; + return (array)$this->parse($data); } /** @@ -61,9 +59,7 @@ public function parseString($config) ); } - $parsed = $this->parse($data); - - return $parsed === null ? [] : $parsed; + return (array)$this->parse($data); } /**