From c375c225522f66bfca719508bcd780cbd1596786 Mon Sep 17 00:00:00 2001 From: Olivier Paroz Date: Wed, 7 Jan 2015 23:32:02 +0100 Subject: Trying to reduce the complexity of normalize again... --- utility/smarterlogger.php | 52 +++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 22 deletions(-) (limited to 'utility') diff --git a/utility/smarterlogger.php b/utility/smarterlogger.php index 261793ac..4cc2e11c 100644 --- a/utility/smarterlogger.php +++ b/utility/smarterlogger.php @@ -184,15 +184,11 @@ class SmarterLogger implements ILogger { return $data; } - if (is_array($data) || $data instanceof \Traversable) { + if ($this->normalizeTraversable($data)) { return $this->normalizeTraversable($data); } - if (is_object($data)) { - if ($data instanceof \Exception) { - return $this->normalizeException($data); - } - + if ($this->normalizeObject($data)) { return $this->normalizeObject($data); } @@ -211,19 +207,23 @@ class SmarterLogger implements ILogger { * @return string */ private function normalizeTraversable($data) { - $normalized = array(); - $count = 1; - foreach ($data as $key => $value) { - if ($count >= 1000) { - $normalized['...'] = - 'Over 1000 items, aborting normalization'; - break; + if (is_array($data) || $data instanceof \Traversable) { + $normalized = array(); + $count = 1; + foreach ($data as $key => $value) { + if ($count >= 1000) { + $normalized['...'] = + 'Over 1000 items, aborting normalization'; + break; + } + $normalized[$key] = $this->normalize($value); } - $normalized[$key] = $this->normalize($value); + + //return $normalized; + return $this->toJson($normalized); } - //return $normalized; - return $this->toJson($normalized); + return null; } /** @@ -234,13 +234,21 @@ class SmarterLogger implements ILogger { * @return string */ private function normalizeObject($data) { - $arrayObject = new \ArrayObject($data); - $serializedObject = $arrayObject->getArrayCopy(); + if (is_object($data)) { + if ($data instanceof \Exception) { + return $this->normalizeException($data); + } - return sprintf( - "[object] (%s: %s)", get_class($data), - $this->toJson($serializedObject) - ); + $arrayObject = new \ArrayObject($data); + $serializedObject = $arrayObject->getArrayCopy(); + + return sprintf( + "[object] (%s: %s)", get_class($data), + $this->toJson($serializedObject) + ); + } + + return null; } /** -- cgit v1.2.3