Skip to content

Commit

Permalink
Type hinting + [] arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
eldadfux committed Jun 19, 2020
1 parent 8eff79a commit e4f6c7d
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 123 deletions.
26 changes: 13 additions & 13 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class App
*
* @var callback[]
*/
protected $init = array();
protected $init = [];

/**
* Shutdown
Expand All @@ -76,7 +76,7 @@ class App
*
* @var callback[]
*/
protected $shutdown = array();
protected $shutdown = [];

/**
* Options
Expand All @@ -85,7 +85,7 @@ class App
*
* @var callback[]
*/
protected $options = array();
protected $options = [];

/**
* Route
Expand All @@ -103,7 +103,7 @@ class App
*
* @var null
*/
protected $matches = array();
protected $matches = [];

/**
* App
Expand Down Expand Up @@ -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] = [];
}

/*
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand All @@ -452,27 +452,27 @@ 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;
}
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;
Expand Down
79 changes: 20 additions & 59 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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;
}
Expand All @@ -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
*
Expand All @@ -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;
}
Expand All @@ -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'));
}

Expand All @@ -194,7 +156,8 @@ public function getIP() {
*
* @return string
*/
public function getMethod() {
public function getMethod(): string
{
return $this->getServer('REQUEST_METHOD', 'UNKNOWN');
}

Expand All @@ -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] : [];
}

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

Expand All @@ -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');
}
Expand All @@ -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');
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit e4f6c7d

Please sign in to comment.