From 04e471bf462018a5f3d65c08209f08c5b55478e7 Mon Sep 17 00:00:00 2001 From: Masroor Ehsan Date: Sat, 8 May 2021 14:26:42 +0200 Subject: [PATCH] PHP ^7.4|^8.0 compatibility --- composer.json | 3 +- flight/Engine.php | 336 ++++++++++++++----------- flight/Flight.php | 63 +++-- flight/autoload.php | 8 +- flight/core/Dispatcher.php | 135 +++++----- flight/core/Loader.php | 138 +++++----- flight/net/Request.php | 133 +++++----- flight/net/Response.php | 153 +++++------ flight/net/Route.php | 84 ++++--- flight/net/Router.php | 75 +++--- flight/template/View.php | 80 +++--- flight/util/Collection.php | 119 +++++---- flight/util/LegacyJsonSerializable.php | 8 +- 13 files changed, 761 insertions(+), 574 deletions(-) diff --git a/composer.json b/composer.json index 89456e7..6999b21 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ } ], "require": { - "php": ">=5.3.0" + "php": "^7.4|^8.0", + "ext-json": "*" }, "autoload": { "files": [ "flight/autoload.php", "flight/Flight.php" ] diff --git a/flight/Engine.php b/flight/Engine.php index 784d431..5ad9238 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -1,4 +1,6 @@ vars = array(); + public function __construct() + { + $this->vars = []; $this->loader = new Loader(); $this->dispatcher = new Dispatcher(); @@ -80,38 +83,42 @@ class Engine { /** * Handles calls to class methods. * - * @param string $name Method name - * @param array $params Method parameters + * @param string $name Method name + * @param array $params Method parameters + * + *@throws Exception + * * @return mixed Callback results - * @throws \Exception */ - public function __call($name, $params) { + public function __call(string $name, array $params) + { $callback = $this->dispatcher->get($name); - if (is_callable($callback)) { + if (\is_callable($callback)) { return $this->dispatcher->run($name, $params); } if (!$this->loader->get($name)) { - throw new \Exception("{$name} must be a mapped method."); + throw new Exception("{$name} must be a mapped method."); } - $shared = (!empty($params)) ? (bool)$params[0] : true; + $shared = (!empty($params)) ? (bool) $params[0] : true; return $this->loader->load($name, $shared); } - /*** Core Methods ***/ + // Core Methods /** * Initializes the framework. */ - public function init() { + public function init(): void + { static $initialized = false; $self = $this; if ($initialized) { - $this->vars = array(); + $this->vars = []; $this->loader->reset(); $this->dispatcher->reset(); } @@ -120,18 +127,18 @@ class Engine { $this->loader->register('request', '\flight\net\Request'); $this->loader->register('response', '\flight\net\Response'); $this->loader->register('router', '\flight\net\Router'); - $this->loader->register('view', '\flight\template\View', array(), function($view) use ($self) { + $this->loader->register('view', '\flight\template\View', [], function ($view) use ($self) { $view->path = $self->get('flight.views.path'); $view->extension = $self->get('flight.views.extension'); }); // Register framework methods - $methods = array( - 'start','stop','route','halt','error','notFound', - 'render','redirect','etag','lastModified','json','jsonp' - ); + $methods = [ + 'start', 'stop', 'route', 'halt', 'error', 'notFound', + 'render', 'redirect', 'etag', 'lastModified', 'json', 'jsonp', + ]; foreach ($methods as $name) { - $this->dispatcher->set($name, array($this, '_'.$name)); + $this->dispatcher->set($name, [$this, '_' . $name]); } // Default configuration settings @@ -144,11 +151,11 @@ class Engine { $this->set('flight.content_length', true); // Startup configuration - $this->before('start', function() use ($self) { + $this->before('start', function () use ($self) { // Enable error handling if ($self->get('flight.handle_errors')) { - set_error_handler(array($self, 'handleError')); - set_exception_handler(array($self, 'handleException')); + set_error_handler([$self, 'handleError']); + set_exception_handler([$self, 'handleException']); } // Set case-sensitivity @@ -163,24 +170,27 @@ class Engine { /** * Custom error handler. Converts errors into exceptions. * - * @param int $errno Error number - * @param int $errstr Error string + * @param int $errno Error number + * @param int $errstr Error string * @param int $errfile Error file name * @param int $errline Error file line number - * @throws \ErrorException + * + * @throws ErrorException */ - public function handleError($errno, $errstr, $errfile, $errline) { + public function handleError(int $errno, int $errstr, int $errfile, int $errline) + { if ($errno & error_reporting()) { - throw new \ErrorException($errstr, $errno, 0, $errfile, $errline); + throw new ErrorException($errstr, $errno, 0, $errfile, $errline); } } /** * Custom exception handler. Logs exceptions. * - * @param \Exception $e Thrown exception + * @param Exception $e Thrown exception */ - public function handleException($e) { + public function handleException($e): void + { if ($this->get('flight.log_errors')) { error_log($e->getMessage()); } @@ -191,13 +201,15 @@ class Engine { /** * Maps a callback to a framework method. * - * @param string $name Method name + * @param string $name Method name * @param callback $callback Callback function - * @throws \Exception If trying to map over a framework method + * + * @throws Exception If trying to map over a framework method */ - public function map($name, $callback) { + public function map(string $name, callable $callback): void + { if (method_exists($this, $name)) { - throw new \Exception('Cannot override an existing framework method.'); + throw new Exception('Cannot override an existing framework method.'); } $this->dispatcher->set($name, $callback); @@ -206,15 +218,17 @@ class Engine { /** * Registers a class to a framework method. * - * @param string $name Method name - * @param string $class Class name - * @param array $params Class initialization parameters - * @param callback $callback Function to call after object instantiation - * @throws \Exception If trying to map over a framework method + * @param string $name Method name + * @param string $class Class name + * @param array $params Class initialization parameters + * @param callable|null $callback $callback Function to call after object instantiation + * + * @throws Exception If trying to map over a framework method */ - public function register($name, $class, array $params = array(), $callback = null) { + public function register(string $name, string $class, array $params = [], ?callable $callback = null): void + { if (method_exists($this, $name)) { - throw new \Exception('Cannot override an existing framework method.'); + throw new Exception('Cannot override an existing framework method.'); } $this->loader->register($name, $class, $params, $callback); @@ -223,48 +237,54 @@ class Engine { /** * Adds a pre-filter to a method. * - * @param string $name Method name + * @param string $name Method name * @param callback $callback Callback function */ - public function before($name, $callback) { + public function before(string $name, callable $callback): void + { $this->dispatcher->hook($name, 'before', $callback); } /** * Adds a post-filter to a method. * - * @param string $name Method name + * @param string $name Method name * @param callback $callback Callback function */ - public function after($name, $callback) { + public function after(string $name, callable $callback): void + { $this->dispatcher->hook($name, 'after', $callback); } /** * Gets a variable. * - * @param string $key Key - * @return mixed + * @param string|null $key Key + * + * @return array|mixed|null */ - public function get($key = null) { - if ($key === null) return $this->vars; + public function get(?string $key = null) + { + if (null === $key) { + return $this->vars; + } - return isset($this->vars[$key]) ? $this->vars[$key] : null; + return $this->vars[$key] ?? null; } /** * Sets a variable. * - * @param mixed $key Key - * @param string $value Value + * @param mixed $key Key + * @param mixed|null $value Value */ - public function set($key, $value = null) { - if (is_array($key) || is_object($key)) { + public function set($key, $value = null): void + { + if (\is_array($key) || \is_object($key)) { foreach ($key as $k => $v) { $this->vars[$k] = $v; } - } - else { + } else { $this->vars[$key] = $value; } } @@ -273,22 +293,24 @@ class Engine { * Checks if a variable has been set. * * @param string $key Key + * * @return bool Variable status */ - public function has($key) { + public function has(string $key): bool + { return isset($this->vars[$key]); } /** * Unsets a variable. If no key is passed in, clear all variables. * - * @param string $key Key + * @param string|null $key Key */ - public function clear($key = null) { - if (is_null($key)) { - $this->vars = array(); - } - else { + public function clear(?string $key = null): void + { + if (null === $key) { + $this->vars = []; + } else { unset($this->vars[$key]); } } @@ -298,17 +320,20 @@ class Engine { * * @param string $dir Directory path */ - public function path($dir) { + public function path(string $dir): void + { $this->loader->addDirectory($dir); } - /*** Extensible Methods ***/ + // Extensible Methods /** * Starts the framework. - * @throws \Exception + * + * @throws Exception */ - public function _start() { + public function _start(): void + { $dispatched = false; $self = $this; $request = $this->request(); @@ -316,7 +341,7 @@ class Engine { $router = $this->router(); // Allow filters to run - $this->after('start', function() use ($self) { + $this->after('start', function () use ($self) { $self->stop(); }); @@ -345,7 +370,9 @@ class Engine { $dispatched = true; - if (!$continue) break; + if (!$continue) { + break; + } $router->next(); @@ -360,14 +387,16 @@ class Engine { /** * Stops the framework and outputs the current response. * - * @param int $code HTTP status code - * @throws \Exception + * @param int|null $code HTTP status code + * + * @throws Exception */ - public function _stop($code = null) { + public function _stop(?int $code = null): void + { $response = $this->response(); if (!$response->sent()) { - if ($code !== null) { + if (null !== $code) { $response->status($code); } @@ -380,21 +409,23 @@ class Engine { /** * Routes a URL to a callback function. * - * @param string $pattern URL pattern to match - * @param callback $callback Callback function - * @param boolean $pass_route Pass the matching route object to the callback + * @param string $pattern URL pattern to match + * @param callback $callback Callback function + * @param bool $pass_route Pass the matching route object to the callback */ - public function _route($pattern, $callback, $pass_route = false) { + public function _route(string $pattern, callable $callback, bool $pass_route = false): void + { $this->router()->map($pattern, $callback, $pass_route); } /** * Stops processing and returns a given response. * - * @param int $code HTTP status code + * @param int $code HTTP status code * @param string $message Response message */ - public function _halt($code = 200, $message = '') { + public function _halt(int $code = 200, string $message = ''): void + { $this->response() ->clear() ->status($code) @@ -406,11 +437,12 @@ class Engine { /** * Sends an HTTP 500 response for any errors. * - * @param \Exception|\Throwable $e Thrown exception + * @param Exception|Throwable $e Thrown exception */ - public function _error($e) { - $msg = sprintf('

500 Internal Server Error

'. - '

%s (%s)

'. + public function _error($e): void + { + $msg = sprintf('

500 Internal Server Error

' . + '

%s (%s)

' . '
%s
', $e->getMessage(), $e->getCode(), @@ -423,10 +455,9 @@ class Engine { ->status(500) ->write($msg) ->send(); - } - catch (\Throwable $t) { // PHP 7.0+ + } catch (Throwable $t) { // PHP 7.0+ exit($msg); - } catch(\Exception $e) { // PHP < 7 + } catch (Exception $e) { // PHP < 7 exit($msg); } } @@ -434,13 +465,14 @@ class Engine { /** * Sends an HTTP 404 response when a URL is not found. */ - public function _notFound() { + public function _notFound(): void + { $this->response() ->clear() ->status(404) ->write( - '

404 Not Found

'. - '

The page you have requested could not be found.

'. + '

404 Not Found

' . + '

The page you have requested could not be found.

' . str_repeat(' ', 512) ) ->send(); @@ -449,18 +481,19 @@ class Engine { /** * Redirects the current request to another URL. * - * @param string $url URL - * @param int $code HTTP status code + * @param string $url URL + * @param int $code HTTP status code */ - public function _redirect($url, $code = 303) { + public function _redirect(string $url, int $code = 303): void + { $base = $this->get('flight.base_url'); - if ($base === null) { + if (null === $base) { $base = $this->request()->base; } // Append base url to redirect url - if ($base != '/' && strpos($url, '://') === false) { + if ('/' != $base && false === strpos($url, '://')) { $url = $base . preg_replace('#/+#', '/', '/' . $url); } @@ -474,16 +507,17 @@ class Engine { /** * Renders a template. * - * @param string $file Template file - * @param array $data Template data - * @param string $key View variable name - * @throws \Exception + * @param string $file Template file + * @param array|null $data Template data + * @param string|null $key View variable name + * + * @throws Exception */ - public function _render($file, $data = null, $key = null) { - if ($key !== null) { + public function _render(string $file, ?array $data = null, ?string $key = null): void + { + if (null !== $key) { $this->view()->set($key, $this->view()->fetch($file, $data)); - } - else { + } else { $this->view()->render($file, $data); } } @@ -491,67 +525,70 @@ class Engine { /** * Sends a JSON response. * - * @param mixed $data JSON data - * @param int $code HTTP status code - * @param bool $encode Whether to perform JSON encoding + * @param mixed $data JSON data + * @param int $code HTTP status code + * @param bool $encode Whether to perform JSON encoding * @param string $charset Charset - * @param int $option Bitmask Json constant such as JSON_HEX_QUOT - * @throws \Exception + * @param int $option Bitmask Json constant such as JSON_HEX_QUOT + * + * @throws Exception */ public function _json( $data, - $code = 200, - $encode = true, - $charset = 'utf-8', - $option = 0 - ) { + int $code = 200, + bool $encode = true, + string $charset = 'utf-8', + int $option = 0 + ): void { $json = ($encode) ? json_encode($data, $option) : $data; $this->response() ->status($code) - ->header('Content-Type', 'application/json; charset='.$charset) + ->header('Content-Type', 'application/json; charset=' . $charset) ->write($json) ->send(); } - + /** * Sends a JSONP response. * - * @param mixed $data JSON data - * @param string $param Query parameter that specifies the callback name. - * @param int $code HTTP status code - * @param bool $encode Whether to perform JSON encoding + * @param mixed $data JSON data + * @param string $param Query parameter that specifies the callback name. + * @param int $code HTTP status code + * @param bool $encode Whether to perform JSON encoding * @param string $charset Charset - * @param int $option Bitmask Json constant such as JSON_HEX_QUOT - * @throws \Exception + * @param int $option Bitmask Json constant such as JSON_HEX_QUOT + * + * @throws Exception */ public function _jsonp( $data, - $param = 'jsonp', - $code = 200, - $encode = true, - $charset = 'utf-8', - $option = 0 - ) { + string $param = 'jsonp', + int $code = 200, + bool $encode = true, + string $charset = 'utf-8', + int $option = 0 + ): void { $json = ($encode) ? json_encode($data, $option) : $data; $callback = $this->request()->query[$param]; $this->response() ->status($code) - ->header('Content-Type', 'application/javascript; charset='.$charset) - ->write($callback.'('.$json.');') + ->header('Content-Type', 'application/javascript; charset=' . $charset) + ->write($callback . '(' . $json . ');') ->send(); } /** * Handles ETag HTTP caching. * - * @param string $id ETag identifier + * @param string $id ETag identifier * @param string $type ETag type */ - public function _etag($id, $type = 'strong') { - $id = (($type === 'weak') ? 'W/' : '').$id; + public function _etag(string $id, string $type = 'strong'): void + { + $id = (('weak' === $type) ? 'W/' : '') . $id; $this->response()->header('ETag', $id); @@ -566,7 +603,8 @@ class Engine { * * @param int $time Unix timestamp */ - public function _lastModified($time) { + public function _lastModified(int $time): void + { $this->response()->header('Last-Modified', gmdate('D, d M Y H:i:s \G\M\T', $time)); if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && diff --git a/flight/Flight.php b/flight/Flight.php index 1f75cde..101c455 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -1,4 +1,6 @@ events[$name] = $callback; } @@ -70,19 +75,23 @@ class Dispatcher { * Gets an assigned callback. * * @param string $name Event name + * * @return callback $callback Callback function */ - public function get($name) { - return isset($this->events[$name]) ? $this->events[$name] : null; + public function get(string $name): ?callable + { + return $this->events[$name] ?? null; } /** * Checks if an event has been set. * * @param string $name Event name + * * @return bool Event status */ - public function has($name) { + public function has(string $name): bool + { return isset($this->events[$name]); } @@ -90,27 +99,28 @@ class Dispatcher { * Clears an event. If no name is given, * all events are removed. * - * @param string $name Event name + * @param string|null $name Event name */ - public function clear($name = null) { - if ($name !== null) { + public function clear(?string $name = null): void + { + if (null !== $name) { unset($this->events[$name]); unset($this->filters[$name]); - } - else { - $this->events = array(); - $this->filters = array(); + } else { + $this->events = []; + $this->filters = []; } } /** * Hooks a callback to an event. * - * @param string $name Event name - * @param string $type Filter type + * @param string $name Event name + * @param string $type Filter type * @param callback $callback Callback function */ - public function hook($name, $type, $callback) { + public function hook(string $name, string $type, callable $callback) + { $this->filters[$name][$type][] = $callback; } @@ -118,15 +128,19 @@ class Dispatcher { * Executes a chain of method filters. * * @param array $filters Chain of filters - * @param array $params Method parameters - * @param mixed $output Method output - * @throws \Exception + * @param array $params Method parameters + * @param mixed $output Method output + * + * @throws Exception */ - public function filter($filters, &$params, &$output) { - $args = array(&$params, &$output); + public function filter(array $filters, array &$params, &$output): void + { + $args = [&$params, &$output]; foreach ($filters as $callback) { $continue = $this->execute($callback, $args); - if ($continue === false) break; + if (false === $continue) { + break; + } } } @@ -134,35 +148,39 @@ class Dispatcher { * Executes a callback function. * * @param callback $callback Callback function - * @param array $params Function parameters + * @param array $params Function parameters + * + *@throws Exception + * * @return mixed Function results - * @throws \Exception */ - public static function execute($callback, array &$params = array()) { - if (is_callable($callback)) { - return is_array($callback) ? + public static function execute(callable $callback, array &$params = []) + { + if (\is_callable($callback)) { + return \is_array($callback) ? self::invokeMethod($callback, $params) : self::callFunction($callback, $params); } - else { - throw new \Exception('Invalid callback specified.'); - } + + throw new Exception('Invalid callback specified.'); } /** * Calls a function. * - * @param string $func Name of function to call - * @param array $params Function parameters + * @param callable|string $func Name of function to call + * @param array $params Function parameters + * * @return mixed Function results */ - public static function callFunction($func, array &$params = array()) { + public static function callFunction($func, array &$params = []) + { // Call static method - if (is_string($func) && strpos($func, '::') !== false) { - return call_user_func_array($func, $params); + if (\is_string($func) && false !== strpos($func, '::')) { + return \call_user_func_array($func, $params); } - switch (count($params)) { + switch (\count($params)) { case 0: return $func(); case 1: @@ -176,23 +194,25 @@ class Dispatcher { case 5: return $func($params[0], $params[1], $params[2], $params[3], $params[4]); default: - return call_user_func_array($func, $params); + return \call_user_func_array($func, $params); } } /** * Invokes a method. * - * @param mixed $func Class method + * @param mixed $func Class method * @param array $params Class method parameters + * * @return mixed Function results */ - public static function invokeMethod($func, array &$params = array()) { - list($class, $method) = $func; + public static function invokeMethod($func, array &$params = []) + { + [$class, $method] = $func; + + $instance = \is_object($class); - $instance = is_object($class); - - switch (count($params)) { + switch (\count($params)) { case 0: return ($instance) ? $class->$method() : @@ -218,15 +238,16 @@ class Dispatcher { $class->$method($params[0], $params[1], $params[2], $params[3], $params[4]) : $class::$method($params[0], $params[1], $params[2], $params[3], $params[4]); default: - return call_user_func_array($func, $params); + return \call_user_func_array($func, $params); } } /** * Resets the object to the initial state. */ - public function reset() { - $this->events = array(); - $this->filters = array(); + public function reset(): void + { + $this->events = []; + $this->filters = []; } } diff --git a/flight/core/Loader.php b/flight/core/Loader.php index 76e3ea4..4520b32 100644 --- a/flight/core/Loader.php +++ b/flight/core/Loader.php @@ -1,4 +1,6 @@ instances[$name]); - $this->classes[$name] = array($class, $params, $callback); + $this->classes[$name] = [$class, $params, $callback]; } /** @@ -55,23 +57,27 @@ class Loader { * * @param string $name Registry name */ - public function unregister($name) { + public function unregister(string $name): void + { unset($this->classes[$name]); } /** * Loads a registered class. * - * @param string $name Method name - * @param bool $shared Shared instance + * @param string $name Method name + * @param bool $shared Shared instance + * + * @throws Exception + * * @return object Class instance - * @throws \Exception */ - public function load($name, $shared = true) { + public function load(string $name, bool $shared = true): ?object + { $obj = null; if (isset($this->classes[$name])) { - list($class, $params, $callback) = $this->classes[$name]; + [$class, $params, $callback] = $this->classes[$name]; $exists = isset($this->instances[$name]); @@ -79,18 +85,17 @@ class Loader { $obj = ($exists) ? $this->getInstance($name) : $this->newInstance($class, $params); - + if (!$exists) { $this->instances[$name] = $obj; } - } - else { + } else { $obj = $this->newInstance($class, $params); } if ($callback && (!$shared || !$exists)) { - $ref = array(&$obj); - call_user_func_array($callback, $ref); + $ref = [&$obj]; + \call_user_func_array($callback, $ref); } } @@ -101,26 +106,31 @@ class Loader { * Gets a single instance of a class. * * @param string $name Instance name + * * @return object Class instance */ - public function getInstance($name) { - return isset($this->instances[$name]) ? $this->instances[$name] : null; + public function getInstance(string $name): ?object + { + return $this->instances[$name] ?? null; } /** * Gets a new instance of a class. * - * @param string|callable $class Class name or callback function to instantiate class - * @param array $params Class initialization parameters + * @param callable|string $class Class name or callback function to instantiate class + * @param array $params Class initialization parameters + * + * @throws Exception + * * @return object Class instance - * @throws \Exception */ - public function newInstance($class, array $params = array()) { - if (is_callable($class)) { - return call_user_func_array($class, $params); + public function newInstance($class, array $params = []): object + { + if (\is_callable($class)) { + return \call_user_func_array($class, $params); } - switch (count($params)) { + switch (\count($params)) { case 0: return new $class(); case 1: @@ -135,44 +145,48 @@ class Loader { return new $class($params[0], $params[1], $params[2], $params[3], $params[4]); default: try { - $refClass = new \ReflectionClass($class); + $refClass = new ReflectionClass($class); + return $refClass->newInstanceArgs($params); - } catch (\ReflectionException $e) { - throw new \Exception("Cannot instantiate {$class}", 0, $e); + } catch (ReflectionException $e) { + throw new Exception("Cannot instantiate {$class}", 0, $e); } } } /** * @param string $name Registry name + * * @return mixed Class information or null if not registered */ - public function get($name) { - return isset($this->classes[$name]) ? $this->classes[$name] : null; + public function get(string $name) + { + return $this->classes[$name] ?? null; } /** * Resets the object to the initial state. */ - public function reset() { - $this->classes = array(); - $this->instances = array(); + public function reset(): void + { + $this->classes = []; + $this->instances = []; } - /*** Autoloading Functions ***/ + // Autoloading Functions /** * Starts/stops autoloader. * - * @param bool $enabled Enable/disable autoloading - * @param array $dirs Autoload directories + * @param bool $enabled Enable/disable autoloading + * @param array $dirs Autoload directories */ - public static function autoload($enabled = true, $dirs = array()) { + public static function autoload(bool $enabled = true, array $dirs = []): void + { if ($enabled) { - spl_autoload_register(array(__CLASS__, 'loadClass')); - } - else { - spl_autoload_unregister(array(__CLASS__, 'loadClass')); + spl_autoload_register([__CLASS__, 'loadClass']); + } else { + spl_autoload_unregister([__CLASS__, 'loadClass']); } if (!empty($dirs)) { @@ -185,13 +199,15 @@ class Loader { * * @param string $class Class name */ - public static function loadClass($class) { - $class_file = str_replace(array('\\', '_'), '/', $class).'.php'; + public static function loadClass(string $class): void + { + $class_file = str_replace(['\\', '_'], '/', $class) . '.php'; foreach (self::$dirs as $dir) { - $file = $dir.'/'.$class_file; + $file = $dir . '/' . $class_file; if (file_exists($file)) { require $file; + return; } } @@ -202,14 +218,16 @@ class Loader { * * @param mixed $dir Directory path */ - public static function addDirectory($dir) { - if (is_array($dir) || is_object($dir)) { + public static function addDirectory($dir): void + { + if (\is_array($dir) || \is_object($dir)) { foreach ($dir as $value) { self::addDirectory($value); } - } - else if (is_string($dir)) { - if (!in_array($dir, self::$dirs)) self::$dirs[] = $dir; + } elseif (\is_string($dir)) { + if (!\in_array($dir, self::$dirs, true)) { + self::$dirs[] = $dir; + } } } } diff --git a/flight/net/Request.php b/flight/net/Request.php index 19a8331..670ce79 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -1,4 +1,6 @@ str_replace('@', '%40', self::getVar('REQUEST_URI', '/')), - 'base' => str_replace(array('\\',' '), array('/','%20'), dirname(self::getVar('SCRIPT_NAME'))), + 'base' => str_replace(['\\', ' '], ['/', '%20'], \dirname(self::getVar('SCRIPT_NAME'))), 'method' => self::getMethod(), 'referrer' => self::getVar('HTTP_REFERER'), 'ip' => self::getVar('REMOTE_ADDR'), - 'ajax' => self::getVar('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest', + 'ajax' => 'XMLHttpRequest' == self::getVar('HTTP_X_REQUESTED_WITH'), 'scheme' => self::getScheme(), 'user_agent' => self::getVar('HTTP_USER_AGENT'), 'type' => self::getVar('CONTENT_TYPE'), @@ -148,11 +152,11 @@ class Request { 'data' => new Collection($_POST), 'cookies' => new Collection($_COOKIE), 'files' => new Collection($_FILES), - 'secure' => self::getScheme() == 'https', + 'secure' => 'https' == self::getScheme(), 'accept' => self::getVar('HTTP_ACCEPT'), 'proxy_ip' => self::getProxyIpAddress(), 'host' => self::getVar('HTTP_HOST'), - ); + ]; } $this->init($config); @@ -163,15 +167,16 @@ class Request { * * @param array $properties Array of request properties */ - public function init($properties = array()) { + public function init(array $properties = []) + { // Set all the defined properties foreach ($properties as $name => $value) { $this->$name = $value; } // Get the requested URL without the base directory - if ($this->base != '/' && strlen($this->base) > 0 && strpos($this->url, $this->base) === 0) { - $this->url = substr($this->url, strlen($this->base)); + if ('/' != $this->base && \strlen($this->base) > 0 && 0 === strpos($this->url, $this->base)) { + $this->url = substr($this->url, \strlen($this->base)); } // Default url @@ -186,11 +191,11 @@ class Request { } // Check for JSON input - if (strpos($this->type, 'application/json') === 0) { + if (0 === strpos($this->type, 'application/json')) { $body = $this->getBody(); - if ($body != '') { + if ('' != $body) { $data = json_decode($body, true); - if ($data != null) { + if (null != $data) { $this->data->setData($data); } } @@ -202,16 +207,17 @@ class Request { * * @return string Raw HTTP request body */ - public static function getBody() { + public static function getBody(): ?string + { static $body; - if (!is_null($body)) { + if (null !== $body) { return $body; } $method = self::getMethod(); - if ($method == 'POST' || $method == 'PUT' || $method == 'DELETE' || $method == 'PATCH') { + if ('POST' === $method || 'PUT' === $method || 'DELETE' === $method || 'PATCH' === $method) { $body = file_get_contents('php://input'); } @@ -220,16 +226,14 @@ class Request { /** * Gets the request method. - * - * @return string */ - public static function getMethod() { + public static function getMethod(): string + { $method = self::getVar('REQUEST_METHOD', 'GET'); if (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) { $method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']; - } - elseif (isset($_REQUEST['_method'])) { + } elseif (isset($_REQUEST['_method'])) { $method = $_REQUEST['_method']; } @@ -241,22 +245,23 @@ class Request { * * @return string IP address */ - public static function getProxyIpAddress() { - static $forwarded = array( + public static function getProxyIpAddress(): string + { + static $forwarded = [ 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', - 'HTTP_FORWARDED' - ); + 'HTTP_FORWARDED', + ]; $flags = \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE; foreach ($forwarded as $key) { - if (array_key_exists($key, $_SERVER)) { + if (\array_key_exists($key, $_SERVER)) { sscanf($_SERVER[$key], '%[^,]', $ip); - if (filter_var($ip, \FILTER_VALIDATE_IP, $flags) !== false) { + if (false !== filter_var($ip, \FILTER_VALIDATE_IP, $flags)) { return $ip; } } @@ -268,22 +273,26 @@ class Request { /** * Gets a variable from $_SERVER using $default if not provided. * - * @param string $var Variable name - * @param string $default Default value to substitute - * @return string Server variable value + * @param string $var Variable name + * @param mixed $default Default value to substitute + * + * @return mixed Server variable value */ - public static function getVar($var, $default = '') { - return isset($_SERVER[$var]) ? $_SERVER[$var] : $default; + public static function getVar(string $var, $default = '') + { + return $_SERVER[$var] ?? $default; } /** * Parse query parameters from a URL. * * @param string $url URL string + * * @return array Query parameters */ - public static function parseQuery($url) { - $params = array(); + public static function parseQuery(string $url): array + { + $params = []; $args = parse_url($url); if (isset($args['query'])) { @@ -293,18 +302,20 @@ class Request { return $params; } - public static function getScheme() { + public static function getScheme(): string + { if ( - (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on') + (isset($_SERVER['HTTPS']) && 'on' === strtolower($_SERVER['HTTPS'])) || - (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') + (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO']) || - (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && $_SERVER['HTTP_FRONT_END_HTTPS'] === 'on') + (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && 'on' === $_SERVER['HTTP_FRONT_END_HTTPS']) || - (isset($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] === 'https') + (isset($_SERVER['REQUEST_SCHEME']) && 'https' === $_SERVER['REQUEST_SCHEME']) ) { return 'https'; } + return 'http'; } } diff --git a/flight/net/Response.php b/flight/net/Response.php index 9f4b925..38c4dcc 100644 --- a/flight/net/Response.php +++ b/flight/net/Response.php @@ -1,4 +1,6 @@ 'Continue', 101 => 'Switching Protocols', 102 => 'Processing', @@ -112,25 +95,46 @@ class Response { 508 => 'Loop Detected', 510 => 'Not Extended', - 511 => 'Network Authentication Required' - ); + 511 => 'Network Authentication Required', + ]; + /** + * @var int HTTP status + */ + protected $status = 200; + + /** + * @var array HTTP headers + */ + protected $headers = []; + + /** + * @var string HTTP response body + */ + protected $body; + + /** + * @var bool HTTP response sent + */ + protected $sent = false; /** * Sets the HTTP status of the response. * * @param int $code HTTP status code. - * @return object|int Self reference + * * @throws \Exception If invalid status code + * + * @return int|object Self reference */ - public function status($code = null) { - if ($code === null) { + public function status($code = null) + { + if (null === $code) { return $this->status; } - if (array_key_exists($code, self::$codes)) { + if (\array_key_exists($code, self::$codes)) { $this->status = $code; - } - else { + } else { throw new \Exception('Invalid status code.'); } @@ -140,17 +144,18 @@ class Response { /** * Adds a header to the response. * - * @param string|array $name Header name or array of names and values - * @param string $value Header value + * @param array|string $name Header name or array of names and values + * @param string $value Header value + * * @return object Self reference */ - public function header($name, $value = null) { - if (is_array($name)) { + public function header($name, $value = null) + { + if (\is_array($name)) { foreach ($name as $k => $v) { $this->headers[$k] = $v; } - } - else { + } else { $this->headers[$name] = $value; } @@ -158,10 +163,12 @@ class Response { } /** - * Returns the headers from the response + * Returns the headers from the response. + * * @return array */ - public function headers() { + public function headers() + { return $this->headers; } @@ -169,9 +176,11 @@ class Response { * Writes content to the response body. * * @param string $str Response content + * * @return object Self reference */ - public function write($str) { + public function write($str) + { $this->body .= $str; return $this; @@ -182,9 +191,10 @@ class Response { * * @return object Self reference */ - public function clear() { + public function clear() + { $this->status = 200; - $this->headers = array(); + $this->headers = []; $this->body = ''; return $this; @@ -194,26 +204,28 @@ class Response { * Sets caching headers for the response. * * @param int|string $expires Expiration time + * * @return object Self reference */ - public function cache($expires) { - if ($expires === false) { + public function cache($expires) + { + if (false === $expires) { $this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; - $this->headers['Cache-Control'] = array( + $this->headers['Cache-Control'] = [ 'no-store, no-cache, must-revalidate', 'post-check=0, pre-check=0', - 'max-age=0' - ); + 'max-age=0', + ]; $this->headers['Pragma'] = 'no-cache'; - } - else { - $expires = is_int($expires) ? $expires : strtotime($expires); + } else { + $expires = \is_int($expires) ? $expires : strtotime($expires); $this->headers['Expires'] = gmdate('D, d M Y H:i:s', $expires) . ' GMT'; - $this->headers['Cache-Control'] = 'max-age='.($expires - time()); - if (isset($this->headers['Pragma']) && $this->headers['Pragma'] == 'no-cache'){ + $this->headers['Cache-Control'] = 'max-age=' . ($expires - time()); + if (isset($this->headers['Pragma']) && 'no-cache' == $this->headers['Pragma']) { unset($this->headers['Pragma']); } } + return $this; } @@ -222,9 +234,10 @@ class Response { * * @return object Self reference */ - public function sendHeaders() { + public function sendHeaders() + { // Send status code header - if (strpos(php_sapi_name(), 'cgi') !== false) { + if (false !== strpos(\PHP_SAPI, 'cgi')) { header( sprintf( 'Status: %d %s', @@ -233,12 +246,11 @@ class Response { ), true ); - } - else { + } else { header( sprintf( '%s %d %s', - (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'), + ($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1'), $this->status, self::$codes[$this->status]), true, @@ -248,13 +260,12 @@ class Response { // Send other headers foreach ($this->headers as $field => $value) { - if (is_array($value)) { + if (\is_array($value)) { foreach ($value as $v) { - header($field.': '.$v, false); + header($field . ': ' . $v, false); } - } - else { - header($field.': '.$value); + } else { + header($field . ': ' . $value); } } @@ -263,7 +274,7 @@ class Response { $length = $this->getContentLength(); if ($length > 0) { - header('Content-Length: '.$length); + header('Content-Length: ' . $length); } } @@ -275,23 +286,26 @@ class Response { * * @return string Content length */ - public function getContentLength() { - return extension_loaded('mbstring') ? + public function getContentLength() + { + return \extension_loaded('mbstring') ? mb_strlen($this->body, 'latin1') : - strlen($this->body); + \strlen($this->body); } /** * Gets whether response was sent. */ - public function sent() { + public function sent() + { return $this->sent; } /** * Sends a HTTP response. */ - public function send() { + public function send() + { if (ob_get_length() > 0) { ob_end_clean(); } @@ -305,4 +319,3 @@ class Response { $this->sent = true; } } - diff --git a/flight/net/Route.php b/flight/net/Route.php index a3c4457..f82536a 100644 --- a/flight/net/Route.php +++ b/flight/net/Route.php @@ -1,4 +1,6 @@ pattern = $pattern; $this->callback = $callback; $this->methods = $methods; @@ -67,61 +71,67 @@ class Route { /** * Checks if a URL matches the route pattern. Also parses named parameters in the URL. * - * @param string $url Requested URL - * @param boolean $case_sensitive Case sensitive matching - * @return boolean Match status + * @param string $url Requested URL + * @param bool $case_sensitive Case sensitive matching + * + * @return bool Match status */ - public function matchUrl($url, $case_sensitive = false) { + public function matchUrl(string $url, bool $case_sensitive = false): bool + { // Wildcard or exact match - if ($this->pattern === '*' || $this->pattern === $url) { + if ('*' === $this->pattern || $this->pattern === $url) { return true; } - $ids = array(); + $ids = []; $last_char = substr($this->pattern, -1); // Get splat - if ($last_char === '*') { + if ('*' === $last_char) { $n = 0; - $len = strlen($url); + $len = \strlen($url); $count = substr_count($this->pattern, '/'); for ($i = 0; $i < $len; $i++) { - if ($url[$i] == '/') $n++; - if ($n == $count) break; + if ('/' === $url[$i]) { + $n++; + } + if ($n === $count) { + break; + } } - $this->splat = (string)substr($url, $i+1); + $this->splat = (string) substr($url, $i + 1); } // Build the regex for matching - $regex = str_replace(array(')','/*'), array(')?','(/?|/.*?)'), $this->pattern); + $regex = str_replace([')', '/*'], [')?', '(/?|/.*?)'], $this->pattern); $regex = preg_replace_callback( '#@([\w]+)(:([^/\(\)]*))?#', - function($matches) use (&$ids) { + static function ($matches) use (&$ids) { $ids[$matches[1]] = null; if (isset($matches[3])) { - return '(?P<'.$matches[1].'>'.$matches[3].')'; + return '(?P<' . $matches[1] . '>' . $matches[3] . ')'; } - return '(?P<'.$matches[1].'>[^/\?]+)'; + + return '(?P<' . $matches[1] . '>[^/\?]+)'; }, $regex ); // Fix trailing slash - if ($last_char === '/') { + if ('/' === $last_char) { $regex .= '?'; - } - // Allow trailing slash + } // Allow trailing slash else { $regex .= '/?'; } // Attempt to match route and named parameters - if (preg_match('#^'.$regex.'(?:\?.*)?$#'.(($case_sensitive) ? '' : 'i'), $url, $matches)) { + if (preg_match('#^' . $regex . '(?:\?.*)?$#' . (($case_sensitive) ? '' : 'i'), $url, $matches)) { foreach ($ids as $k => $v) { - $this->params[$k] = (array_key_exists($k, $matches)) ? urldecode($matches[$k]) : null; + $this->params[$k] = (\array_key_exists($k, $matches)) ? urldecode($matches[$k]) : null; } $this->regex = $regex; @@ -136,9 +146,11 @@ class Route { * Checks if an HTTP method matches the route methods. * * @param string $method HTTP method + * * @return bool Match status */ - public function matchMethod($method) { - return count(array_intersect(array($method, '*'), $this->methods)) > 0; + public function matchMethod(string $method): bool + { + return \count(array_intersect([$method, '*'], $this->methods)) > 0; } } diff --git a/flight/net/Router.php b/flight/net/Router.php index 173ba11..c85dba4 100644 --- a/flight/net/Router.php +++ b/flight/net/Router.php @@ -1,4 +1,6 @@ routes; } /** * Clears all routes in the router. */ - public function clear() { - $this->routes = array(); + public function clear(): void + { + $this->routes = []; } /** * Maps a URL pattern to a callback function. * - * @param string $pattern URL pattern to match - * @param callback $callback Callback function - * @param boolean $pass_route Pass the matching route object to the callback + * @param string $pattern URL pattern to match + * @param callback $callback Callback function + * @param bool $pass_route Pass the matching route object to the callback */ - public function map($pattern, $callback, $pass_route = false) { + public function map(string $pattern, callable $callback, bool $pass_route = false): void + { $url = $pattern; - $methods = array('*'); + $methods = ['*']; - if (strpos($pattern, ' ') !== false) { - list($method, $url) = explode(' ', trim($pattern), 2); + if (false !== strpos($pattern, ' ')) { + [$method, $url] = explode(' ', trim($pattern), 2); $url = trim($url); $methods = explode('|', $method); } @@ -75,12 +74,14 @@ class Router { * Routes the current request. * * @param Request $request Request object - * @return Route|bool Matching route or false if no match + * + * @return bool|Route Matching route or false if no match */ - public function route(Request $request) { - $url_decoded = urldecode( $request->url ); + public function route(Request $request) + { + $url_decoded = urldecode($request->url); while ($route = $this->current()) { - if ($route !== false && $route->matchMethod($request->method) && $route->matchUrl($url_decoded, $this->case_sensitive)) { + if (false !== $route && $route->matchMethod($request->method) && $route->matchUrl($url_decoded, $this->case_sensitive)) { return $route; } $this->next(); @@ -92,26 +93,26 @@ class Router { /** * Gets the current route. * - * @return Route + * @return bool|Route */ - public function current() { - return isset($this->routes[$this->index]) ? $this->routes[$this->index] : false; + public function current() + { + return $this->routes[$this->index] ?? false; } /** * Gets the next route. - * - * @return Route */ - public function next() { + public function next(): void + { $this->index++; } /** * Reset to the first route. */ - public function reset() { + public function reset(): void + { $this->index = 0; } } - diff --git a/flight/template/View.php b/flight/template/View.php index a0bc3c3..90dbb81 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -1,4 +1,6 @@ path = $path; } @@ -55,25 +59,27 @@ class View { * Gets a template variable. * * @param string $key Key + * * @return mixed Value */ - public function get($key) { - return isset($this->vars[$key]) ? $this->vars[$key] : null; + public function get($key) + { + return $this->vars[$key] ?? null; } /** * Sets a template variable. * - * @param mixed $key Key + * @param mixed $key Key * @param string $value Value */ - public function set($key, $value = null) { - if (is_array($key) || is_object($key)) { + public function set($key, $value = null) + { + if (\is_array($key) || \is_object($key)) { foreach ($key as $k => $v) { $this->vars[$k] = $v; } - } - else { + } else { $this->vars[$key] = $value; } } @@ -82,9 +88,11 @@ class View { * Checks if a template variable is set. * * @param string $key Key - * @return boolean If key exists + * + * @return bool If key exists */ - public function has($key) { + public function has($key) + { return isset($this->vars[$key]); } @@ -93,11 +101,11 @@ class View { * * @param string $key Key */ - public function clear($key = null) { - if (is_null($key)) { - $this->vars = array(); - } - else { + public function clear($key = null) + { + if (null === $key) { + $this->vars = []; + } else { unset($this->vars[$key]); } } @@ -106,17 +114,19 @@ class View { * Renders a template. * * @param string $file Template file - * @param array $data Template data + * @param array $data Template data + * * @throws \Exception If template not found */ - public function render($file, $data = null) { + public function render($file, $data = null) + { $this->template = $this->getTemplate($file); if (!file_exists($this->template)) { throw new \Exception("Template file not found: {$this->template}."); } - if (is_array($data)) { + if (\is_array($data)) { $this->vars = array_merge($this->vars, $data); } @@ -129,25 +139,28 @@ class View { * Gets the output of a template. * * @param string $file Template file - * @param array $data Template data + * @param array $data Template data + * * @return string Output of template */ - public function fetch($file, $data = null) { + public function fetch($file, $data = null) + { ob_start(); $this->render($file, $data); - $output = ob_get_clean(); - return $output; + return ob_get_clean(); } /** * Checks if a template file exists. * * @param string $file Template file + * * @return bool Template file exists */ - public function exists($file) { + public function exists($file) + { return file_exists($this->getTemplate($file)); } @@ -155,30 +168,33 @@ class View { * Gets the full path to a template file. * * @param string $file Template file + * * @return string Template file location */ - public function getTemplate($file) { + public function getTemplate($file) + { $ext = $this->extension; - if (!empty($ext) && (substr($file, -1 * strlen($ext)) != $ext)) { + if (!empty($ext) && (substr($file, -1 * \strlen($ext)) != $ext)) { $file .= $ext; } - if ((substr($file, 0, 1) == '/')) { + if (('/' == substr($file, 0, 1))) { return $file; } - - return $this->path.'/'.$file; + + return $this->path . '/' . $file; } /** * Displays escaped output. * * @param string $str String to escape + * * @return string Escaped string */ - public function e($str) { + public function e($str) + { echo htmlentities($str); } } - diff --git a/flight/util/Collection.php b/flight/util/Collection.php index 1062b11..883dccf 100644 --- a/flight/util/Collection.php +++ b/flight/util/Collection.php @@ -1,4 +1,6 @@ data = $data; } @@ -37,19 +45,22 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab * Gets an item. * * @param string $key Key + * * @return mixed Value */ - public function __get($key) { - return isset($this->data[$key]) ? $this->data[$key] : null; + public function __get(string $key) + { + return $this->data[$key] ?? null; } /** * Set an item. * - * @param string $key Key - * @param mixed $value Value + * @param string $key Key + * @param mixed $value Value */ - public function __set($key, $value) { + public function __set(string $key, $value): void + { $this->data[$key] = $value; } @@ -57,9 +68,11 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab * Checks if an item exists. * * @param string $key Key + * * @return bool Item status */ - public function __isset($key) { + public function __isset(string $key): bool + { return isset($this->data[$key]); } @@ -68,7 +81,8 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab * * @param string $key Key */ - public function __unset($key) { + public function __unset(string $key): void + { unset($this->data[$key]); } @@ -76,23 +90,25 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab * Gets an item at the offset. * * @param string $offset Offset + * * @return mixed Value */ - public function offsetGet($offset) { - return isset($this->data[$offset]) ? $this->data[$offset] : null; + public function offsetGet($offset) + { + return $this->data[$offset] ?? null; } /** * Sets an item at the offset. * * @param string $offset Offset - * @param mixed $value Value + * @param mixed $value Value */ - public function offsetSet($offset, $value) { - if (is_null($offset)) { + public function offsetSet($offset, $value) + { + if (null === $offset) { $this->data[] = $value; - } - else { + } else { $this->data[$offset] = $value; } } @@ -101,9 +117,11 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab * Checks if an item exists at the offset. * * @param string $offset Offset + * * @return bool Item status */ - public function offsetExists($offset) { + public function offsetExists($offset): bool + { return isset($this->data[$offset]); } @@ -112,54 +130,59 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab * * @param string $offset Offset */ - public function offsetUnset($offset) { + public function offsetUnset($offset): void + { unset($this->data[$offset]); } /** * Resets the collection. */ - public function rewind() { + public function rewind(): void + { reset($this->data); } - + /** * Gets current collection item. * * @return mixed Value - */ - public function current() { + */ + public function current() + { return current($this->data); } - + /** * Gets current collection key. * * @return mixed Value - */ - public function key() { + */ + public function key() + { return key($this->data); } - + /** * Gets the next collection value. * * @return mixed Value - */ - public function next() + */ + public function next() { return next($this->data); } - + /** * Checks if the current collection key is valid. * * @return bool Key status - */ - public function valid() + */ + public function valid(): bool { $key = key($this->data); - return ($key !== NULL && $key !== FALSE); + + return null !== $key && false !== $key; } /** @@ -167,8 +190,9 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab * * @return int Collection size */ - public function count() { - return sizeof($this->data); + public function count(): int + { + return \count($this->data); } /** @@ -176,7 +200,8 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab * * @return array Collection keys */ - public function keys() { + public function keys(): array + { return array_keys($this->data); } @@ -185,7 +210,8 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab * * @return array Collection data */ - public function getData() { + public function getData(): array + { return $this->data; } @@ -194,23 +220,26 @@ class Collection implements \ArrayAccess, \Iterator, \Countable, \JsonSerializab * * @param array $data New collection data */ - public function setData(array $data) { + public function setData(array $data): void + { $this->data = $data; } /** - * Gets the collection data which can be serialized to JSON + * Gets the collection data which can be serialized to JSON. * * @return array Collection data which can be serialized by json_encode */ - public function jsonSerialize() { + public function jsonSerialize(): array + { return $this->data; } /** * Removes all items from the collection. */ - public function clear() { - $this->data = array(); + public function clear(): void + { + $this->data = []; } } diff --git a/flight/util/LegacyJsonSerializable.php b/flight/util/LegacyJsonSerializable.php index 4c907c7..0c973aa 100644 --- a/flight/util/LegacyJsonSerializable.php +++ b/flight/util/LegacyJsonSerializable.php @@ -1,11 +1,13 @@ * @license MIT, http://flightphp.com/license */ - -interface JsonSerializable { +interface LegacyJsonSerializable +{ public function jsonSerialize(); -} \ No newline at end of file +}