From 26f846b745af5f6987e9dbd55303cdb65abd305f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 11 Mar 2019 21:15:55 +0100 Subject: [PATCH] simplified code --- src/Http/RequestFactory.php | 5 ++-- src/Http/Response.php | 3 +-- src/Http/Session.php | 46 +++++++++++-------------------------- src/Http/SessionSection.php | 6 +---- src/Http/Url.php | 8 +++---- 5 files changed, 21 insertions(+), 47 deletions(-) diff --git a/src/Http/RequestFactory.php b/src/Http/RequestFactory.php index af49891d..6c6e73ac 100644 --- a/src/Http/RequestFactory.php +++ b/src/Http/RequestFactory.php @@ -120,7 +120,7 @@ private function getScriptPath(Url $url): string { $path = $url->getPath(); $lpath = strtolower($path); - $script = isset($_SERVER['SCRIPT_NAME']) ? strtolower($_SERVER['SCRIPT_NAME']) : ''; + $script = strtolower($_SERVER['SCRIPT_NAME'] ?? ''); if ($lpath !== $script) { $max = min(strlen($lpath), strlen($script)); for ($i = 0; $i < $max && $lpath[$i] === $script[$i]; $i++); @@ -238,8 +238,7 @@ private function getMethod(): ?string $method = $_SERVER['REQUEST_METHOD'] ?? null; if ( $method === 'POST' - && isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']) - && preg_match('#^[A-Z]+\z#', $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']) + && preg_match('#^[A-Z]+\z#', $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] ?? '') ) { $method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']; } diff --git a/src/Http/Response.php b/src/Http/Response.php index 0b3eec5b..65c875da 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -230,8 +230,7 @@ public function __destruct() { if ( self::$fixIE - && isset($_SERVER['HTTP_USER_AGENT']) - && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== false + && strpos($_SERVER['HTTP_USER_AGENT'] ?? '', 'MSIE ') !== false && in_array($this->code, [400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505], true) && preg_match('#^text/html(?:;|$)#', (string) $this->getHeader('Content-Type')) ) { diff --git a/src/Http/Session.php b/src/Http/Session.php index f6ebeb85..44334878 100644 --- a/src/Http/Session.php +++ b/src/Http/Session.php @@ -128,21 +128,16 @@ private function initialize(): void } } - // process meta metadata - if (isset($nf['META'])) { - $now = time(); - // expire section variables - foreach ($nf['META'] as $section => $metadata) { - if (is_array($metadata)) { - foreach ($metadata as $variable => $value) { - if (!empty($value['T']) && $now > $value['T']) { - if ($variable === '') { // expire whole section - unset($nf['META'][$section], $nf['DATA'][$section]); - continue 2; - } - unset($nf['META'][$section][$variable], $nf['DATA'][$section][$variable]); - } + // expire section variables + $now = time(); + foreach ($nf['META'] ?? [] as $section => $metadata) { + foreach ($metadata ?? [] as $variable => $value) { + if (!empty($value['T']) && $now > $value['T']) { + if ($variable === '') { // expire whole section + unset($nf['META'][$section], $nf['DATA'][$section]); + continue 2; } + unset($nf['META'][$section][$variable], $nf['DATA'][$section][$variable]); } } } @@ -292,12 +287,7 @@ public function getIterator(): \Iterator $this->start(); } - if (isset($_SESSION['__NF']['DATA'])) { - return new \ArrayIterator(array_keys($_SESSION['__NF']['DATA'])); - - } else { - return new \ArrayIterator; - } + return new \ArrayIterator(array_keys($_SESSION['__NF']['DATA'] ?? [])); } @@ -312,21 +302,11 @@ public function clean(): void } $nf = &$_SESSION['__NF']; - if (isset($nf['META']) && is_array($nf['META'])) { - foreach ($nf['META'] as $name => $foo) { - if (empty($nf['META'][$name])) { - unset($nf['META'][$name]); - } + foreach ($nf['META'] ?? [] as $name => $foo) { + if (empty($nf['META'][$name])) { + unset($nf['META'][$name]); } } - - if (empty($nf['META'])) { - unset($nf['META']); - } - - if (empty($nf['DATA'])) { - unset($nf['DATA']); - } } diff --git a/src/Http/SessionSection.php b/src/Http/SessionSection.php index eb03b198..31160687 100644 --- a/src/Http/SessionSection.php +++ b/src/Http/SessionSection.php @@ -61,11 +61,7 @@ private function start(): void public function getIterator(): \Iterator { $this->start(); - if (isset($this->data)) { - return new \ArrayIterator($this->data); - } else { - return new \ArrayIterator; - } + return new \ArrayIterator($this->data ?? []); } diff --git a/src/Http/Url.php b/src/Http/Url.php index 9bc48f85..716e356a 100644 --- a/src/Http/Url.php +++ b/src/Http/Url.php @@ -91,12 +91,12 @@ public function __construct($url = null) $this->scheme = $p['scheme'] ?? ''; $this->port = $p['port'] ?? null; - $this->host = isset($p['host']) ? rawurldecode($p['host']) : ''; - $this->user = isset($p['user']) ? rawurldecode($p['user']) : ''; - $this->password = isset($p['pass']) ? rawurldecode($p['pass']) : ''; + $this->host = rawurldecode($p['host'] ?? ''); + $this->user = rawurldecode($p['user'] ?? ''); + $this->password = rawurldecode($p['pass'] ?? ''); $this->setPath($p['path'] ?? ''); $this->setQuery($p['query'] ?? []); - $this->fragment = isset($p['fragment']) ? rawurldecode($p['fragment']) : ''; + $this->fragment = rawurldecode($p['fragment'] ?? ''); } elseif ($url instanceof UrlImmutable || $url instanceof self) { [$this->scheme, $this->user, $this->password, $this->host, $this->port, $this->path, $this->query, $this->fragment] = $url->export();