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
{
$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;
}
/**

View File

@ -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());
}
/**

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -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]);

View File

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

View File

@ -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'));