Code and docblock optimizations

This commit is contained in:
Dennis Eichhorn 2018-06-03 16:04:13 +02:00
parent ffda1a2724
commit 6f452f25e8
8 changed files with 94 additions and 31 deletions

View File

@ -162,19 +162,45 @@ final class EventManager
*/ */
public function detach(string $group) : bool 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])) { if (isset($this->callbacks[$group])) {
unset($this->callbacks[$group]); unset($this->callbacks[$group]);
$found = true; $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])) { if (isset($this->groups[$group])) {
unset($this->groups[$group]); unset($this->groups[$group]);
$found = true; $found = true;
} }
return $found; return false;
} }
/** /**

View File

@ -113,8 +113,6 @@ final class Request extends RequestAbstract
* *
* @return void * @return void
* *
* @throws \Exception
*
* @since 1.0.0 * @since 1.0.0
*/ */
private function initCurrentRequest() : void private function initCurrentRequest() : void
@ -124,20 +122,34 @@ final class Request extends RequestAbstract
$this->files = $_FILES ?? []; $this->files = $_FILES ?? [];
$this->header->getL11n()->setLanguage($this->loadRequestLanguage()); $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 (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) { if (($json = \json_decode(($input = \file_get_contents('php://input')), true)) === false || $json === null) {
throw new \Exception('Is not valid json ' . $input); throw new \Exception('Is not valid json ' . $input);
} }
$this->data += $json; $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); parse_str(file_get_contents('php://input'), $temp);
$this->data += $temp; $this->data += $temp;
} }
} }
$this->uri = $this->uri ?? new Http(Http::getCurrent());
} }
/** /**

View File

@ -105,7 +105,7 @@ final class Response extends ResponseAbstract implements RenderableInterface
$types = $this->header->get('Content-Type'); $types = $this->header->get('Content-Type');
foreach ($types as $type) { foreach ($types as $type) {
if (stripos($type, MimeType::M_JSON) !== false) { if (\stripos($type, MimeType::M_JSON) !== false) {
return \json_encode($this->jsonSerialize()); return \json_encode($this->jsonSerialize());
} }
} }
@ -129,20 +129,30 @@ final class Response extends ResponseAbstract implements RenderableInterface
foreach ($this->response as $key => $response) { foreach ($this->response as $key => $response) {
if ($response instanceOf \Serializable) { if ($response instanceOf \Serializable) {
$render .= $response->serialize(); $render .= $response->serialize();
} elseif (is_string($response) || is_numeric($response)) { } elseif (\is_string($response) || \is_numeric($response)) {
$render .= $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 { } else {
throw new \Exception('Wrong response type'); 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; return $render;
@ -159,9 +169,9 @@ final class Response extends ResponseAbstract implements RenderableInterface
foreach ($this->response as $key => $response) { foreach ($this->response as $key => $response) {
if ($response instanceof View) { if ($response instanceof View) {
$result += $response->toArray(); $result += $response->toArray();
} elseif (is_array($response)) { } elseif (\is_array($response)) {
$result += $response; $result += $response;
} elseif (is_scalar($response)) { } elseif (\is_scalar($response)) {
$result[] = $response; $result[] = $response;
} elseif ($response instanceof \JsonSerializable) { } elseif ($response instanceof \JsonSerializable) {
$result[] = $response->jsonSerialize(); $result[] = $response->jsonSerialize();

View File

@ -338,14 +338,7 @@ final class ModuleManager
$this->deactivateModule($info); $this->deactivateModule($info);
return true; return true;
} catch (PathException $e) {
// todo: handle module doesn't exist or files are missing
//echo $e->getMessage();
return false;
} catch (\Exception $e) { } catch (\Exception $e) {
//echo $e->getMessage();
return false; return false;
} }
} }

View File

@ -41,7 +41,7 @@ class StatusAbstract
*/ */
public static function activate(DatabasePool $dbPool, InfoManager $info) : void 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); self::activateInDatabase($dbPool, $info);
} }
@ -104,7 +104,7 @@ class StatusAbstract
*/ */
public static function deactivate(DatabasePool $dbPool, InfoManager $info) : void 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); self::deactivateInDatabase($dbPool, $info);
} }

View File

@ -121,14 +121,35 @@ class MultiMap implements \Countable
*/ */
private function garbageCollect() : void 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) { foreach ($this->keys as $key => $keyValue) {
if (!isset($this->values[$keyValue])) { if (!isset($this->values[$keyValue])) {
unset($this->keys[$key]); 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) { foreach ($this->values as $valueKey => $value) {
if (!\in_array($valueKey, $this->keys)) { if (!\in_array($valueKey, $this->keys)) {
unset($this->values[$valueKey]); unset($this->values[$valueKey]);

View File

@ -117,6 +117,7 @@ final class StringCompare
} }
} }
// todo: is this correct?
$total += $total + $best; $total += $total + $best;
} }

View File

@ -43,8 +43,8 @@ class StringCompareTest extends \PHPUnit\Framework\TestCase
self::assertEquals('Cartoon', $dict->matchDictionary('Cartoon')); self::assertEquals('Cartoon', $dict->matchDictionary('Cartoon'));
self::assertEquals('Bathtub Sidewalk Table', $dict->matchDictionary('Sidewalk Table')); self::assertEquals('Bathtub Sidewalk Table', $dict->matchDictionary('Sidewalk Table'));
// todo: this doesn't match since the length is too far apart // too far apart
//self::assertEquals('Snowflake Bathtub Snowflake Toothbrush Sidewalk', $dict->matchDictionary('Toothbrush')); self::assertNotEquals('Snowflake Bathtub Snowflake Toothbrush Sidewalk', $dict->matchDictionary('Toothbrush'));
$dict->add('Carton'); $dict->add('Carton');
self::assertEquals('Carton', $dict->matchDictionary('carton')); self::assertEquals('Carton', $dict->matchDictionary('carton'));