From a05ac0bfa9f3b1835e19a3375789e43697d8fe06 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Mon, 5 Sep 2016 15:51:52 -0400 Subject: [PATCH] new isset syntax. the future is here --- autoload.php | 2 +- controller/Actions.class.php | 2 +- controller/Controller.class.php | 4 ++-- controller/Request.class.php | 11 +++++------ controller/Session.class.php | 2 +- controller/action/ContentActions.class.php | 2 +- controller/action/MailActions.class.php | 4 ++-- lib/tools/Debug.class.php | 4 ++-- model/Post.class.php | 10 +++++----- view/Response.class.php | 6 +++--- view/template/download/_list.php | 2 +- view/template/mail/_joinList.php | 4 ++-- 12 files changed, 26 insertions(+), 27 deletions(-) diff --git a/autoload.php b/autoload.php index e79cdbe1..7729339e 100644 --- a/autoload.php +++ b/autoload.php @@ -11,7 +11,7 @@ class Autoloader } $class = strtolower($class); - $path = isset(static::$classes[$class]) ? static::$classes[$class] : false; + $path = static::$classes[$class] ?? false; if ($path) { diff --git a/controller/Actions.class.php b/controller/Actions.class.php index 9de926aa..466f5d5c 100644 --- a/controller/Actions.class.php +++ b/controller/Actions.class.php @@ -9,7 +9,7 @@ class Actions { public static function param($key, $default = null) { - return isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : $default); + return $_POST[$key] ?? $_GET[$key] ?? $default; } //this is dumb diff --git a/controller/Controller.class.php b/controller/Controller.class.php index 9bff604a..f070e81a 100644 --- a/controller/Controller.class.php +++ b/controller/Controller.class.php @@ -8,7 +8,7 @@ class Controller { $viewAndParams = static::execute($uri); $viewTemplate = $viewAndParams[0]; - $viewParameters = isset($viewAndParams[1]) ? $viewAndParams[1] : []; + $viewParameters = $viewAndParams[1] ?? []; if (!IS_PRODUCTION && isset($viewAndParams[2])) { throw new Exception('use response::setheader instead of returning headers'); @@ -27,7 +27,7 @@ class Controller $layout = !(isset($viewParameters['_no_layout']) && $viewParameters['_no_layout']); unset($viewParameters['_no_layout']); - $layoutParams = isset($viewParameters[View::LAYOUT_PARAMS]) ? $viewParameters[View::LAYOUT_PARAMS] : []; + $layoutParams = $viewParameters[View::LAYOUT_PARAMS] ?? []; unset($viewParameters[View::LAYOUT_PARAMS]); $content = View::render($viewTemplate, $viewParameters + ['fullPage' => true]); diff --git a/controller/Request.class.php b/controller/Request.class.php index d86f89c1..96de0774 100644 --- a/controller/Request.class.php +++ b/controller/Request.class.php @@ -36,15 +36,14 @@ class Request public static function getOriginalIp(): string { - return isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : - (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? - trim(explode(',',$_SERVER['HTTP_X_FORWARDED_FOR'])[0]) : - (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '')); + return $_SERVER['HTTP_X_REAL_IP'] ?? + (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? trim(explode(',',$_SERVER['HTTP_X_FORWARDED_FOR'])[0]) : + ($_SERVER['REMOTE_ADDR'] ?? '')); } public static function getUserAgent(): string { - return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''; + return $_SERVER['HTTP_USER_AGENT'] ?? ''; } public static function getHost(): string @@ -55,7 +54,7 @@ class Request public static function getRelativeUri(): string { - return isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; + return $_SERVER['REQUEST_URI'] ?? ''; } public static function isGzipAccepted(): bool diff --git a/controller/Session.class.php b/controller/Session.class.php index f6e9bd0b..6b923ca3 100644 --- a/controller/Session.class.php +++ b/controller/Session.class.php @@ -32,7 +32,7 @@ class Session public static function get($key, $default = null) { - return isset($_SESSION[$key]) ? $_SESSION[$key] : $default; + return $_SESSION[$key] ?? $default; } public static function set($key, $value) diff --git a/controller/action/ContentActions.class.php b/controller/action/ContentActions.class.php index 47d4c7a1..8d8601e1 100644 --- a/controller/action/ContentActions.class.php +++ b/controller/action/ContentActions.class.php @@ -193,7 +193,7 @@ class ContentActions extends Actions public static function preparePostListPartial(array $vars): array { - $count = isset($vars['count']) ? $vars['count'] : 3; + $count = $vars['count'] ?? 3; return [ 'posts' => array_slice(Post::find(static::VIEW_FOLDER_NEWS, Post::SORT_DATE_DESC), 0, $count) ]; diff --git a/controller/action/MailActions.class.php b/controller/action/MailActions.class.php index f5a1fc92..c1292588 100644 --- a/controller/action/MailActions.class.php +++ b/controller/action/MailActions.class.php @@ -16,7 +16,7 @@ class MailActions extends Actions return Controller::redirect($nextUrl); } - Session::set(Session::KEY_LIST_SUB_SIGNATURE, isset($_POST['listSig']) ? $_POST['listSig'] : true); + Session::set(Session::KEY_LIST_SUB_SIGNATURE, $_POST['listSig'] ?? true); $email = $_POST['email']; if (!$email|| !filter_var($email, FILTER_VALIDATE_EMAIL)) @@ -37,7 +37,7 @@ class MailActions extends Actions { Session::set(Session::KEY_MAILCHIMP_LIST_IDS, array_merge(Session::get(Session::KEY_MAILCHIMP_LIST_IDS, []), [$mcListId])); Session::set(Session::KEY_LIST_SUB_SUCCESS, true); - Session::set(Session::KEY_LIST_SUB_FB_EVENT, isset($_POST['fbEvent']) ? $_POST['fbEvent'] : null); + Session::set(Session::KEY_LIST_SUB_FB_EVENT, $_POST['fbEvent'] ?? null); } else { diff --git a/lib/tools/Debug.class.php b/lib/tools/Debug.class.php index fb104f59..6db68aac 100644 --- a/lib/tools/Debug.class.php +++ b/lib/tools/Debug.class.php @@ -28,8 +28,8 @@ class Debug $rtn .= sprintf("#%s %s(%s): %s(%s)\n", $count, - isset($frame['file']) ? $frame['file'] : 'unknown file', - isset($frame['line']) ? $frame['line'] : 'unknown line', + $frame['file'] ?? 'unknown file', + $frame['line'] ?? 'unknown line', isset($frame['class']) ? $frame['class'].$frame['type'].$frame['function'] : $frame['function'], $args); } diff --git a/model/Post.class.php b/model/Post.class.php index a8748d11..f612af68 100644 --- a/model/Post.class.php +++ b/model/Post.class.php @@ -48,12 +48,12 @@ class Post $this->slug = $slug; $this->markdown = $markdown; $this->metadata = $frontMatter; - $this->title = isset($frontMatter['title']) ? $frontMatter['title'] : null; - $this->author = isset($frontMatter['author']) ? $frontMatter['author'] : null; + $this->title = $frontMatter['title'] ?? null; + $this->author = $frontMatter['author'] ?? null; $this->date = isset($frontMatter['date']) ? new DateTime($frontMatter['date']) : null; - $this->cover = isset($frontMatter['cover']) ? $frontMatter['cover'] : null; + $this->cover = $frontMatter['cover'] ?? null; $this->isCoverLight = isset($frontMatter['cover-light']) && $frontMatter['cover-light'] == 'true'; - $this->category = isset($frontMatter['category']) ? $frontMatter['category'] : null; + $this->category = $frontMatter['category'] ?? null; } public static function find($folder, $sort = null) @@ -349,7 +349,7 @@ class Post { $values = array_unique(array_map(function(Post $post) use($field) { $metadata = $post->getMetadata(); - return isset($metadata[$field]) ? $metadata[$field] : null; + return $metadata[$field] ?? null; }, $posts)); sort($values); return array_combine($values, $values); diff --git a/view/Response.class.php b/view/Response.class.php index 72c71a2f..dbfb485d 100644 --- a/view/Response.class.php +++ b/view/Response.class.php @@ -204,7 +204,7 @@ class Response foreach (preg_split('/\s*,\s*/', $cacheControl) as $tmp) { $tmp = explode('=', $tmp); - $currentHeaders[$tmp[0]] = isset($tmp[1]) ? $tmp[1] : null; + $currentHeaders[$tmp[0]] = $tmp[1] ?? null; } } $currentHeaders[strtr(strtolower($name), '_', '-')] = $value; @@ -236,7 +236,7 @@ class Response public static function getHeader($name, $default = null) { - return isset(static::$headers[$name]) ? static::$headers[$name] : $default; + return static::$headers[$name] ?? $default; } public static function getHeaders(): array @@ -350,7 +350,7 @@ class Response '505' => 'HTTP Version Not Supported', ]; - return isset($statusTexts[$code]) ? $statusTexts[$code] : null; + return $statusTexts[$code] ?? null; } protected static function normalizeHeaderName($name) diff --git a/view/template/download/_list.php b/view/template/download/_list.php index fdf6b816..8be30acb 100644 --- a/view/template/download/_list.php +++ b/view/template/download/_list.php @@ -1,4 +1,4 @@ - +

diff --git a/view/template/mail/_joinList.php b/view/template/mail/_joinList.php index 97fc16ba..7df0ca75 100644 --- a/view/template/mail/_joinList.php +++ b/view/template/mail/_joinList.php @@ -1,4 +1,4 @@ - +
@@ -18,7 +18,7 @@ - +