diff --git a/src/App.php b/src/App.php index 166c036f..68cb65a3 100755 --- a/src/App.php +++ b/src/App.php @@ -67,7 +67,7 @@ class App * * @var callback[] */ - protected $init = array(); + protected $init = []; /** * Shutdown @@ -76,7 +76,7 @@ class App * * @var callback[] */ - protected $shutdown = array(); + protected $shutdown = []; /** * Options @@ -85,7 +85,7 @@ class App * * @var callback[] */ - protected $options = array(); + protected $options = []; /** * Route @@ -103,7 +103,7 @@ class App * * @var null */ - protected $matches = array(); + protected $matches = []; /** * App @@ -361,7 +361,7 @@ public function match(Request $request) { $method = (self::REQUEST_METHOD_HEAD == $method) ? self::REQUEST_METHOD_GET : $method; if(!isset($this->routes[$method])) { - $this->routes[$method] = array(); + $this->routes[$method] = []; } /* @@ -413,8 +413,8 @@ public function match(Request $request) { */ public function run(Request $request, Response $response) { - $keys = array(); - $params = array(); + $keys = []; + $params = []; $method = $request->getServer('REQUEST_METHOD', ''); $route = $this->match($request); @@ -435,7 +435,7 @@ public function run(Request $request, Response $response) try { foreach($this->init as $init) { - call_user_func_array($init, array()); + call_user_func_array($init, []); } foreach($route->getParams() as $key => $param) { @@ -452,11 +452,11 @@ public function run(Request $request, Response $response) call_user_func_array($route->getAction(), $params); foreach($this->shutdown as $shutdown) { - call_user_func_array($shutdown, array()); + call_user_func_array($shutdown, []); } } catch (\Exception $e) { - call_user_func_array($this->error, array($e)); + call_user_func_array($this->error, [$e]); } return $this; @@ -464,15 +464,15 @@ public function run(Request $request, Response $response) elseif(self::REQUEST_METHOD_OPTIONS == $method) { try { foreach($this->options as $option) { - call_user_func_array($option, array()); + call_user_func_array($option, []); } } catch (\Exception $e) { - call_user_func_array($this->error, array($e)); + call_user_func_array($this->error, [$e]); } } else { - call_user_func_array($this->error, array(new Exception('Not Found', 404))); + call_user_func_array($this->error, [new Exception('Not Found', 404)]); } return $this; diff --git a/src/Request.php b/src/Request.php index f082028c..1255bd6b 100755 --- a/src/Request.php +++ b/src/Request.php @@ -51,7 +51,7 @@ class Request * @param mixed $default * @return mixed */ - public function getParam($key, $default = null) + public function getParam(string $key, $default = null) { switch($this->getServer('REQUEST_METHOD', '')) { case self::METHOD_GET: @@ -75,7 +75,7 @@ public function getParam($key, $default = null) * * @return array */ - public function getParams() + public function getParams(): array { switch($this->getServer('REQUEST_METHOD', '')) { case self::METHOD_GET: @@ -100,7 +100,7 @@ public function getParams() * @param mixed $default * @return mixed */ - public function getQuery($key, $default = null) + public function getQuery(string $key, $default = null) { return (isset($_GET[$key])) ? $_GET[$key] : $default; } @@ -114,54 +114,13 @@ public function getQuery($key, $default = null) * @param mixed $default * @return mixed */ - public function getPayload($key, $default = null) + public function getPayload(string $key, $default = null) { $payload = $this->generateInput(); return (isset($payload[$key])) ? $payload[$key] : $default; } - /** - * Get request - * - * Method for querying HTTP request parameters whether carried with GET, POST or cookie. If $key is not found $default value will be returned. - * - * @param string $key - * @param mixed $default - * @param array $priority The later will overwrite $key value if set - * @throws Exception - * @return mixed - */ - public function getRequest($key, $default = null, $priority = ['get', 'post', 'cookie', 'header']) - { - $value = $default; - - foreach ($priority as $method) { - switch ($method) { - case 'get': - $temp = $this->getQuery($key, null); - $value = (null !== $temp) ? $temp : $value; - break; - case 'post': - $temp = $this->getPayload($key, null); - $value = (null !== $temp) ? $temp : $value; - break; - case 'cookie': - $temp = $this->getCookie($key, null); - $value = (null !== $temp) ? $temp : $value; - break; - case 'header': - $temp = $this->getHeader($key, null); - $value = (null !== $temp) ? $temp : $value; - break; - default: - throw new Exception($method . ' method not supported'); - } - } - - return $value; - } - /** * Get server * @@ -171,7 +130,7 @@ public function getRequest($key, $default = null, $priority = ['get', 'post', 'c * @param mixed $default * @return mixed */ - public function getServer($key, $default = null) + public function getServer(string $key, $default = null) { return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default; } @@ -182,8 +141,11 @@ public function getServer($key, $default = null) * Returns users IP address. * Support HTTP_X_FORWARDED_FOR header usually return * from different proxy servers or PHP default REMOTE_ADDR + * + * @return string */ - public function getIP() { + public function getIP(): string + { return $this->getServer('HTTP_X_FORWARDED_FOR', $this->getServer('REMOTE_ADDR', '0.0.0.0')); } @@ -194,7 +156,8 @@ public function getIP() { * * @return string */ - public function getMethod() { + public function getMethod(): string + { return $this->getServer('REQUEST_METHOD', 'UNKNOWN'); } @@ -206,9 +169,9 @@ public function getMethod() { * @param string $key * @return array */ - public function getFiles($key) + public function getFiles(string $key): array { - return (isset($_FILES[$key])) ? $_FILES[$key] : array(); + return (isset($_FILES[$key])) ? $_FILES[$key] : []; } /** @@ -220,7 +183,7 @@ public function getFiles($key) * @param mixed $default * @return mixed */ - public function getCookie($key, $default = null) + public function getCookie(string $key, string $default = '') { return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default; } @@ -231,10 +194,10 @@ public function getCookie($key, $default = null) * Method for querying HTTP header parameters. If $key is not found $default value will be returned. * * @param string $key - * @param mixed $default - * @return mixed + * @param string $default + * @return string */ - public function getHeader($key, $default = null) + public function getHeader(string $key, string $default = ''): string { $headers = $this->generateHeaders(); @@ -248,7 +211,7 @@ public function getHeader($key, $default = null) * * @return int */ - public function getSize() + public function getSize(): int { return mb_strlen(implode("\n", $this->generateHeaders()), '8bit') + mb_strlen(file_get_contents('php://input'), '8bit'); } @@ -260,7 +223,7 @@ public function getSize() * * @return array */ - protected function generateInput() + protected function generateInput(): array { if (null === $this->payload) { $contentType = $this->getHeader('Content-Type'); @@ -295,11 +258,9 @@ protected function generateInput() * * @return array */ - protected function generateHeaders() + protected function generateHeaders(): array { if (null === $this->headers) { - $fallback = null; - /** * Fallback for older PHP versions * that do not support generateHeaders diff --git a/src/Response.php b/src/Response.php index c2a683ac..7b70dd06 100755 --- a/src/Response.php +++ b/src/Response.php @@ -75,7 +75,7 @@ class Response /** * @var array */ - protected $statusCodes = Array( + protected $statusCodes = [ self::STATUS_CODE_CONTINUE => 'Continue', self::STATUS_CODE_SWITCHING_PROTOCOLS => 'Switching Protocols', self::STATUS_CODE_OK => 'OK', @@ -118,7 +118,7 @@ class Response self::STATUS_CODE_SERVICE_UNAVAILABLE => 'Service Unavailable', self::STATUS_CODE_GATEWAY_TIMEOUT => 'Gateway Timeout', self::STATUS_CODE_HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported', - ); + ]; const COOKIE_SAMESITE_NONE = 'None'; const COOKIE_SAMESITE_STRICT = 'Strict'; @@ -142,12 +142,12 @@ class Response /** * @var array */ - protected $headers = array(); + protected $headers = []; /** * @var array */ - protected $cookies = array(); + protected $cookies = []; /** * @var int @@ -175,7 +175,7 @@ public function __construct() * @param string $type * @return self */ - public function setContentType($type) + public function setContentType(string $type): self { $this->contentType = $type; @@ -191,7 +191,7 @@ public function setContentType($type) * @return self * @throws Exception */ - public function setStatusCode($code = 200) + public function setStatusCode(int $code = 200): self { if (!array_key_exists($code, $this->statusCodes)) { throw new Exception('Unknown HTTP status code'); @@ -209,7 +209,7 @@ public function setStatusCode($code = 200) * * @return int */ - public function getSize() + public function getSize(): int { return $this->size; } @@ -217,7 +217,8 @@ public function getSize() /** * Don't allow payload on response output */ - public function disablePayload() { + public function disablePayload(): self + { $this->disablePayload = true; return $this; } @@ -225,7 +226,8 @@ public function disablePayload() { /** * Allow payload on response output */ - public function enablePayload() { + public function enablePayload(): self + { $this->disablePayload = false; return $this; } @@ -239,7 +241,7 @@ public function enablePayload() { * @param string $value * @return self */ - public function addHeader($key, $value) + public function addHeader(string $key, string $value): self { $this->headers[$key] = $value; @@ -254,7 +256,7 @@ public function addHeader($key, $value) * @param string $key * @return self */ - public function removeHeader($key) + public function removeHeader(string $key): self { if(isset($this->headers[$key])) { unset($this->headers[$key]); @@ -270,7 +272,7 @@ public function removeHeader($key) * * @return array */ - public function getHeaders() + public function getHeaders(): array { return $this->headers; } @@ -290,9 +292,9 @@ public function getHeaders() * @param string $sameSite [optional] * @return self */ - public function addCookie($name, $value = null, $expire = null, $path = null, $domain = null, $secure = null, $httponly = null, $sameSite = null) + public function addCookie(string $name, string $value = null, int $expire = null, string $path = null, string $domain = null, bool $secure = null, bool $httponly = null, string $sameSite = null): self { - $this->cookies[$name] = array( + $this->cookies[$name] = [ 'name' => $name, 'value' => $value, 'expire' => $expire, @@ -301,7 +303,7 @@ public function addCookie($name, $value = null, $expire = null, $path = null, $d 'secure' => $secure, 'httponly' => $httponly, 'samesite' => $sameSite, - ); + ]; return $this; } @@ -314,7 +316,7 @@ public function addCookie($name, $value = null, $expire = null, $path = null, $d * @param string $name * @return self */ - public function removeCookie($name) + public function removeCookie(string $name): self { if(isset($this->headers[$name])) { unset($this->cookies[$name]); @@ -330,7 +332,7 @@ public function removeCookie($name) * * @return array */ - public function getCookies() + public function getCookies(): array { return $this->cookies; } @@ -345,7 +347,7 @@ public function getCookies() * * @return self */ - public function send($body = '', $exit = null) + public function send(string $body = '', int $exit = null): void { if(!$this->disablePayload) { $this->addHeader('X-Debug-Speed', microtime(true) - $this->startTime); @@ -375,7 +377,7 @@ public function send($body = '', $exit = null) * * @return self */ - protected function appendHeaders() + protected function appendHeaders(): self { // Send status code header http_response_code($this->statusCode); @@ -400,7 +402,7 @@ protected function appendHeaders() * * @return self */ - protected function appendCookies() + protected function appendCookies(): self { foreach ($this->cookies as $cookie) { @@ -434,13 +436,13 @@ protected function appendCookies() * @see https://bugs.webkit.org/show_bug.cgi?id=47425 * * @param string $url complete absolute URI for redirection as required by the internet standard RFC 2616 (HTTP 1.1) - * @param int $statusCode valid HTTP/1.1 status code - * + * @param int $statusCode valid HTTP status code * @param null $exit + * * @throws Exception * @see http://tools.ietf.org/html/rfc2616 */ - public function redirect($url, $statusCode = 301, $exit = null) + public function redirect(string $url, int $statusCode = 301, int $exit = null): void { if (300 == $statusCode) { trigger_error('It seems webkit based browsers have problems redirecting link with 300 status codes!', E_USER_NOTICE); @@ -462,7 +464,7 @@ public function redirect($url, $statusCode = 301, $exit = null) * * @param string $data */ - public function text($data) + public function text(string $data): void { $this ->setContentType(Response::CONTENT_TYPE_TEXT) @@ -480,7 +482,7 @@ public function text($data) * * @param array $data */ - public function json(array $data) + public function json(array $data): void { $this ->setContentType(Response::CONTENT_TYPE_JSON) @@ -499,7 +501,7 @@ public function json(array $data) * @param string $callback * @param array $data */ - public function jsonp($callback, array $data) + public function jsonp(string $callback, array $data): void { $this ->setContentType(Response::CONTENT_TYPE_JAVASCRIPT) @@ -516,7 +518,7 @@ public function jsonp($callback, array $data) * @param string $callback * @param array $data */ - public function iframe($callback, array $data) + public function iframe(string $callback, array $data): void { $this ->send(''); @@ -530,7 +532,7 @@ public function iframe($callback, array $data) * The server has successfully fulfilled the request * and that there is no additional content to send in the response payload body. */ - public function noContent() + public function noContent(): void { $this ->setStatusCode(self::STATUS_CODE_NOCONTENT) diff --git a/src/Route.php b/src/Route.php index 67308231..40b1dc8e 100755 --- a/src/Route.php +++ b/src/Route.php @@ -55,7 +55,7 @@ class Route * * @var array */ - protected $params = array(); + protected $params = []; /** * Labels @@ -64,7 +64,7 @@ class Route * * @var array */ - protected $labels = array(); + protected $labels = []; /** * @var int @@ -133,13 +133,13 @@ public function action($action) */ public function param($key, $default, $validator, $description = '', $optional = false) { - $this->params[$key] = array( + $this->params[$key] = [ 'default' => $default, 'validator' => $validator, 'description' => $description, 'optional' => $optional, 'value' => null, - ); + ]; return $this; } diff --git a/src/Validator/Multiple.php b/src/Validator/Multiple.php index 94933adb..420e7cd5 100755 --- a/src/Validator/Multiple.php +++ b/src/Validator/Multiple.php @@ -27,7 +27,7 @@ class Multiple extends Validator /** * @var Validator[] */ - protected $rules = array(); + protected $rules = []; /** * Constructor diff --git a/src/View.php b/src/View.php index ccf4447d..20ca1dfd 100755 --- a/src/View.php +++ b/src/View.php @@ -280,17 +280,17 @@ public function render($minify = true) $html = str_replace($foundPre[0], array_map(function($el){ return '
'.$el.'
'; }, array_keys($foundPre[0])), $html); // your stuff - $search = array( + $search = [ '/\>[^\S ]+/s', // strip whitespaces after tags, except space '/[^\S ]+\', '<', '\\1' - ); + ]; $html = preg_replace($search, $replace, $html); diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 101bd09b..d1671b5b 100755 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -49,16 +49,6 @@ public function testGetPayload() $this->assertEquals($this->request->getPayload('unknown', 'test'), 'test'); } - public function testGetRequest() - { - // Mock - $_REQUEST['key'] = 'value'; - - // Assertions - $this->assertEquals($this->request->getRequest('key'), 'value'); - $this->assertEquals($this->request->getRequest('unknown', 'test'), 'test'); - } - public function testGetServer() { // Mock diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index f9afbbd2..601ca3df 100755 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -111,7 +111,7 @@ public function testJson() { ob_start(); //Start of build - @$this->response->json(array('key' => 'value')); + @$this->response->json(['key' => 'value']); $html = ob_get_contents(); ob_end_clean(); //End of build @@ -123,7 +123,7 @@ public function testJsonp() { ob_start(); //Start of build - @$this->response->jsonp('test', array('key' => 'value')); + @$this->response->jsonp('test', ['key' => 'value']); $html = ob_get_contents(); ob_end_clean(); //End of build @@ -135,7 +135,7 @@ public function testIframe() { ob_start(); //Start of build - @$this->response->iframe('test', array('key' => 'value')); + @$this->response->iframe('test', ['key' => 'value']); $html = ob_get_contents(); ob_end_clean(); //End of build diff --git a/tests/Validator/NumericTest.php b/tests/Validator/NumericTest.php index 4f374182..6df6c0d6 100755 --- a/tests/Validator/NumericTest.php +++ b/tests/Validator/NumericTest.php @@ -42,6 +42,6 @@ public function testIsValid() $this->assertEquals($this->numeric->isValid(1337e0), true); $this->assertEquals($this->numeric->isValid(9.1), true); $this->assertEquals($this->numeric->isValid('not numeric'), false); - $this->assertEquals($this->numeric->isValid(array()), false); + $this->assertEquals($this->numeric->isValid([]), false); } }