From 6f452f25e86bd6ba76344a0ba1578952bd8faa2f Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 3 Jun 2018 16:04:13 +0200 Subject: [PATCH] Code and docblock optimizations --- Event/EventManager.php | 30 ++++++++++++++++++++++++++++-- Message/Http/Request.php | 24 ++++++++++++++++++------ Message/Http/Response.php | 30 ++++++++++++++++++++---------- Module/ModuleManager.php | 7 ------- Module/StatusAbstract.php | 4 ++-- Stdlib/Map/MultiMap.php | 25 +++++++++++++++++++++++-- Utils/StringCompare.php | 1 + tests/Utils/StringCompareTest.php | 4 ++-- 8 files changed, 94 insertions(+), 31 deletions(-) diff --git a/Event/EventManager.php b/Event/EventManager.php index a3a75b201..cd38a3275 100644 --- a/Event/EventManager.php +++ b/Event/EventManager.php @@ -162,19 +162,45 @@ final class EventManager */ public function detach(string $group) : bool { - $found = false; + return $this->detachCallback($group) || $this->detachGroup($group); + } + /** + * Detach an event + * + * @param string $group Name of the event + * + * @return bool + * + * @since 1.0.0 + */ + private function detachCallback(string $group) : bool + { if (isset($this->callbacks[$group])) { unset($this->callbacks[$group]); $found = true; } + return false; + } + + /** + * Detach an event + * + * @param string $group Name of the event + * + * @return bool + * + * @since 1.0.0 + */ + private function detachGroup(string $group) : bool + { if (isset($this->groups[$group])) { unset($this->groups[$group]); $found = true; } - return $found; + return false; } /** diff --git a/Message/Http/Request.php b/Message/Http/Request.php index f623bfff6..834db20b8 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -113,8 +113,6 @@ final class Request extends RequestAbstract * * @return void * - * @throws \Exception - * * @since 1.0.0 */ private function initCurrentRequest() : void @@ -124,20 +122,34 @@ final class Request extends RequestAbstract $this->files = $_FILES ?? []; $this->header->getL11n()->setLanguage($this->loadRequestLanguage()); + $this->initNonGetData(); + + $this->uri = $this->uri ?? new Http(Http::getCurrent()); + } + + /** + * Init non get data + * + * @return void + * + * @throws \Exception + * + * @since 1.0.0 + */ + private function initNonGetData() : void + { if (isset($_SERVER['CONTENT_TYPE'])) { - if (strpos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) { + if (\stripos($_SERVER['CONTENT_TYPE'], 'application/json') !== false) { if (($json = \json_decode(($input = \file_get_contents('php://input')), true)) === false || $json === null) { throw new \Exception('Is not valid json ' . $input); } $this->data += $json; - } elseif (strpos($_SERVER['CONTENT_TYPE'], 'application/x-www-form-urlencoded') !== false) { + } elseif (\stripos($_SERVER['CONTENT_TYPE'], 'application/x-www-form-urlencoded') !== false) { parse_str(file_get_contents('php://input'), $temp); $this->data += $temp; } } - - $this->uri = $this->uri ?? new Http(Http::getCurrent()); } /** diff --git a/Message/Http/Response.php b/Message/Http/Response.php index 55458efb7..1a8b30f3a 100644 --- a/Message/Http/Response.php +++ b/Message/Http/Response.php @@ -105,7 +105,7 @@ final class Response extends ResponseAbstract implements RenderableInterface $types = $this->header->get('Content-Type'); foreach ($types as $type) { - if (stripos($type, MimeType::M_JSON) !== false) { + if (\stripos($type, MimeType::M_JSON) !== false) { return \json_encode($this->jsonSerialize()); } } @@ -129,20 +129,30 @@ final class Response extends ResponseAbstract implements RenderableInterface foreach ($this->response as $key => $response) { if ($response instanceOf \Serializable) { $render .= $response->serialize(); - } elseif (is_string($response) || is_numeric($response)) { + } elseif (\is_string($response) || \is_numeric($response)) { $render .= $response; - } elseif (is_array($response)) { - $render .= \json_encode($response); - // TODO: remove this. This should never happen since then someone forgot to set the correct header. it should be json header! } else { throw new \Exception('Wrong response type'); } } - $types = $this->header->get('Content-Type'); + return $this->removeWhitespaceAndLineBreak($render); + } - if (stripos($types[0], MimeType::M_HTML) !== false) { - return trim(preg_replace('/(\s{2,}|\n|\t)(?![^<>]*<\/pre>)/', ' ', $render)); + /** + * Remove whitespace and line break from render + * + * @param string $render Rendered string + * + * @return string + * + * @since 1.0.0 + */ + private function removeWhitespaceAndLineBreak(string $render) : string + { + $types = $this->header->get('Content-Type'); + if (\stripos($types[0], MimeType::M_HTML) !== false) { + return \trim(\preg_replace('/(\s{2,}|\n|\t)(?![^<>]*<\/pre>)/', ' ', $render)); } return $render; @@ -159,9 +169,9 @@ final class Response extends ResponseAbstract implements RenderableInterface foreach ($this->response as $key => $response) { if ($response instanceof View) { $result += $response->toArray(); - } elseif (is_array($response)) { + } elseif (\is_array($response)) { $result += $response; - } elseif (is_scalar($response)) { + } elseif (\is_scalar($response)) { $result[] = $response; } elseif ($response instanceof \JsonSerializable) { $result[] = $response->jsonSerialize(); diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index 98644c43a..9cfc949e2 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -338,14 +338,7 @@ final class ModuleManager $this->deactivateModule($info); return true; - } catch (PathException $e) { - // todo: handle module doesn't exist or files are missing - //echo $e->getMessage(); - - return false; } catch (\Exception $e) { - //echo $e->getMessage(); - return false; } } diff --git a/Module/StatusAbstract.php b/Module/StatusAbstract.php index 7f043fc2a..8fcd227d3 100644 --- a/Module/StatusAbstract.php +++ b/Module/StatusAbstract.php @@ -41,7 +41,7 @@ class StatusAbstract */ public static function activate(DatabasePool $dbPool, InfoManager $info) : void { - self::activateRoutes(__DIR__ . '/../../Web/Routes.php', __DIR__ . '/../../Modules/' . $info->getDirectory() . '/Admin/Routes/http.php'); + self::activateRoutes(__DIR__ . '/../../Web/Routes.php', __DIR__ . '/../../Modules/' . $info->getDirectory() . '/Admin/Routes/'); self::activateInDatabase($dbPool, $info); } @@ -104,7 +104,7 @@ class StatusAbstract */ public static function deactivate(DatabasePool $dbPool, InfoManager $info) : void { - self::deactivateRoutes(__DIR__ . '/../../Web/Routes.php', __DIR__ . '/../../Modules/' . $info->getDirectory() . '/Admin/Routes/http.php'); + self::deactivateRoutes(__DIR__ . '/../../Web/Routes.php', __DIR__ . '/../../Modules/' . $info->getDirectory() . '/Admin/Routes/'); self::deactivateInDatabase($dbPool, $info); } diff --git a/Stdlib/Map/MultiMap.php b/Stdlib/Map/MultiMap.php index 470604544..013ced169 100644 --- a/Stdlib/Map/MultiMap.php +++ b/Stdlib/Map/MultiMap.php @@ -121,14 +121,35 @@ class MultiMap implements \Countable */ private function garbageCollect() : void { - /* garbage collect keys */ + $this->garbageCollectKeys(); + $this->garbageCollectValues(); + } + + /** + * Garbage collect unreferenced keys + * + * @return void + * + * @since 1.0.0 + */ + private function garbageCollectKeys() : void + { foreach ($this->keys as $key => $keyValue) { if (!isset($this->values[$keyValue])) { unset($this->keys[$key]); } } + } - /* garbage collect values */ + /** + * Garbage collect unreferenced values + * + * @return void + * + * @since 1.0.0 + */ + private function garbageCollectValues() : void + { foreach ($this->values as $valueKey => $value) { if (!\in_array($valueKey, $this->keys)) { unset($this->values[$valueKey]); diff --git a/Utils/StringCompare.php b/Utils/StringCompare.php index 6446f3694..abbc4345f 100644 --- a/Utils/StringCompare.php +++ b/Utils/StringCompare.php @@ -117,6 +117,7 @@ final class StringCompare } } + // todo: is this correct? $total += $total + $best; } diff --git a/tests/Utils/StringCompareTest.php b/tests/Utils/StringCompareTest.php index 94aeffc57..9ace59504 100644 --- a/tests/Utils/StringCompareTest.php +++ b/tests/Utils/StringCompareTest.php @@ -43,8 +43,8 @@ class StringCompareTest extends \PHPUnit\Framework\TestCase self::assertEquals('Cartoon', $dict->matchDictionary('Cartoon')); self::assertEquals('Bathtub Sidewalk Table', $dict->matchDictionary('Sidewalk Table')); - // todo: this doesn't match since the length is too far apart - //self::assertEquals('Snowflake Bathtub Snowflake Toothbrush Sidewalk', $dict->matchDictionary('Toothbrush')); + // too far apart + self::assertNotEquals('Snowflake Bathtub Snowflake Toothbrush Sidewalk', $dict->matchDictionary('Toothbrush')); $dict->add('Carton'); self::assertEquals('Carton', $dict->matchDictionary('carton'));