Merge pull request #528 from flightphp/add-typehint

Replaced some docblocks to native PHP 7.4 typehint
pull/529/head
n0nag0n 1 year ago committed by GitHub
commit 0d7b79bab9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -33,7 +33,7 @@
]
},
"require-dev": {
"ext-pdo_sqlite": "*",
"ext-pdo_sqlite": "*",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.10",
"phpstan/extension-installer": "^1.3",
@ -46,14 +46,15 @@
},
"scripts": {
"test": "phpunit",
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
"test-coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
"lint": "phpstan --no-progress -cphpstan.neon"
},
"suggest": {
"latte/latte": "Latte template engine",
"tracy/tracy": "Tracy debugger"
"latte/latte": "Latte template engine",
"tracy/tracy": "Tracy debugger",
"phpstan": "PHP Static Analyzer"
},
"replace": {
"mikecao/flight": "2.0.2"
}
"replace": {
"mikecao/flight": "2.0.2"
}
}

@ -2,6 +2,7 @@
"folders": [
{
"path": ".",
"name": "FlightPHP/Core",
}
],
"settings": {
@ -12,10 +13,29 @@
"intelephense.format.braces": "psr12",
},
},
"formatters":
{
"embedding.php": "LSP-intelephense"
},
"formatters": {
"embedding.php": "LSP-intelephense",
"source.json.composer": "LSP-json",
"source.json.sublime": "LSP-json",
},
"LSP-html": {
"enabled": false
},
"LSP-tailwindcss": {
"enabled": false
},
},
},
"build_systems": [
{
"name": "Linter - HARD",
"quiet": true,
"shell_cmd": "composer lint -- --no-ansi -lmax"
},
{
"name": "Linter - Default",
"quiet": true,
"shell_cmd": "composer lint -- --no-ansi"
}
],
}

@ -41,7 +41,7 @@ class Dispatcher
*
* @throws Exception
*
* @return mixed|null Output of callback
* @return mixed Output of callback
*/
public function run(string $name, array $params = [])
{
@ -80,7 +80,7 @@ class Dispatcher
*
* @param string $name Event name
*
* @return callable $callback Callback function
* @return ?callable $callback Callback function
*/
public function get(string $name): ?callable
{
@ -100,10 +100,9 @@ class Dispatcher
}
/**
* Clears an event. If no name is given,
* all events are removed.
* Clears an event. If no name is given, all events are removed.
*
* @param string|null $name Event name
* @param ?string $name Event name
*/
public function clear(?string $name = null): void
{

@ -12,8 +12,6 @@ namespace flight\core;
use Closure;
use Exception;
use ReflectionClass;
use ReflectionException;
/**
* The Loader class is responsible for loading objects. It maintains
@ -25,7 +23,7 @@ class Loader
{
/**
* Registered classes.
* @var array<string, array{class-string, array<int, mixed>, ?callable}> $classes
* @var array<string, array{class-string|Closure(): object, array<int, mixed>, ?callable}> $classes
*/
protected array $classes = [];
@ -46,7 +44,7 @@ class Loader
* @template T of object
*
* @param string $name Registry name
* @param class-string<T> $class Class name or function to instantiate class
* @param class-string<T>|Closure(): T $class Class name or function to instantiate class
* @param array<int, mixed> $params Class initialization parameters
* @param ?callable(T $instance): void $callback $callback Function to call after object instantiation
*/
@ -75,7 +73,7 @@ class Loader
*
* @throws Exception
*
* @return object Class instance
* @return ?object Class instance
*/
public function load(string $name, bool $shared = true): ?object
{
@ -112,7 +110,7 @@ class Loader
*
* @param string $name Instance name
*
* @return object Class instance
* @return ?object Class instance
*/
public function getInstance(string $name): ?object
{

@ -40,104 +40,102 @@ use flight\util\Collection;
class Request
{
/**
* @var string URL being requested
* URL being requested
*/
public string $url;
/**
* @var string Parent subdirectory of the URL
* Parent subdirectory of the URL
*/
public string $base;
/**
* @var string Request method (GET, POST, PUT, DELETE)
* Request method (GET, POST, PUT, DELETE)
*/
public string $method;
/**
* @var string Referrer URL
* Referrer URL
*/
public string $referrer;
/**
* @var string IP address of the client
* IP address of the client
*/
public string $ip;
/**
* @var bool Whether the request is an AJAX request
* Whether the request is an AJAX request
*/
public bool $ajax;
/**
* @var string Server protocol (http, https)
* Server protocol (http, https)
*/
public string $scheme;
/**
* @var string Browser information
* Browser information
*/
public string $user_agent;
/**
* @var string Content type
* Content type
*/
public string $type;
/**
* @var int Content length
* Content length
*/
public int $length;
/**
* @var Collection Query string parameters
* Query string parameters
*/
public Collection $query;
/**
* @var Collection Post parameters
* Post parameters
*/
public Collection $data;
/**
* @var Collection Cookie parameters
* Cookie parameters
*/
public Collection $cookies;
/**
* @var Collection Uploaded files
* Uploaded files
*/
public Collection $files;
/**
* @var bool Whether the connection is secure
* Whether the connection is secure
*/
public bool $secure;
/**
* @var string HTTP accept parameters
* HTTP accept parameters
*/
public string $accept;
/**
* @var string Proxy IP address of the client
* Proxy IP address of the client
*/
public string $proxy_ip;
/**
* @var string HTTP host name
* HTTP host name
*/
public string $host;
/**
* Stream path for where to pull the request body from
*
* @var string
*/
private string $stream_path = 'php://input';
/**
* @var string Raw HTTP request body
* Raw HTTP request body
*/
public string $body = '';
@ -146,7 +144,7 @@ class Request
*
* @param array<string, mixed> $config Request configuration
*/
public function __construct($config = [])
public function __construct(array $config = [])
{
// Default properties
if (empty($config)) {
@ -179,9 +177,9 @@ class Request
* Initialize request properties.
*
* @param array<string, mixed> $properties Array of request properties
* @return self
* @return $this
*/
public function init(array $properties = [])
public function init(array $properties = []): self
{
// Set all the defined properties
foreach ($properties as $name => $value) {
@ -322,6 +320,7 @@ class Request
return $params;
}
/** @return 'http'|'https' */
public static function getScheme(): string
{
if (

@ -98,7 +98,7 @@ class Response
511 => 'Network Authentication Required',
];
/**
* @var int HTTP status
* HTTP status
*/
protected int $status = 200;
@ -108,23 +108,23 @@ class Response
protected array $headers = [];
/**
* @var string HTTP response body
* HTTP response body
*/
protected string $body = '';
/**
* @var bool HTTP response sent
* HTTP response sent
*/
protected bool $sent = false;
/**
* Sets the HTTP status of the response.
*
* @param int|null $code HTTP status code.
* @param ?int $code HTTP status code.
*
* @throws Exception If invalid status code
*
* @return int|static Self reference
* @return int|$this Self reference
*/
public function status(?int $code = null)
{
@ -145,11 +145,11 @@ class Response
* Adds a header to the response.
*
* @param array<string, int|string>|string $name Header name or array of names and values
* @param string|null $value Header value
* @param ?string $value Header value
*
* @return self
* @return $this
*/
public function header($name, ?string $value = null)
public function header($name, ?string $value = null): self
{
if (\is_array($name)) {
foreach ($name as $k => $v) {
@ -166,7 +166,7 @@ class Response
* Returns the headers from the response.
* @return array<string, int|string|array<int, string>>
*/
public function headers()
public function headers(): array
{
return $this->headers;
}
@ -176,7 +176,7 @@ class Response
*
* @param string $str Response content
*
* @return Response Self reference
* @return $this Self reference
*/
public function write(string $str): self
{
@ -188,7 +188,7 @@ class Response
/**
* Clears the response.
*
* @return Response Self reference
* @return $this Self reference
*/
public function clear(): self
{
@ -204,7 +204,7 @@ class Response
*
* @param int|string|false $expires Expiration time as time() or as strtotime() string value
*
* @return Response Self reference
* @return $this Self reference
*/
public function cache($expires): self
{
@ -231,7 +231,7 @@ class Response
/**
* Sends HTTP headers.
*
* @return Response Self reference
* @return $this Self reference
*/
public function sendHeaders(): self
{
@ -289,7 +289,7 @@ class Response
* @param string $header_string The header string you would pass to header()
* @param bool $replace The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type. By default it will replace, but if you pass in false as the second argument you can force multiple headers of the same type.
* @param int $response_code The response code to send
* @return self
* @return $this
* @codeCoverageIgnore
*/
public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): self {
@ -299,8 +299,6 @@ class Response
/**
* Gets the content length.
*
* @return int Content length
*/
public function getContentLength(): int
{

@ -18,7 +18,7 @@ namespace flight\net;
class Route
{
/**
* @var string URL pattern
* URL pattern
*/
public string $pattern;
@ -38,22 +38,22 @@ class Route
public array $params = [];
/**
* @var string|null Matching regular expression
* Matching regular expression
*/
public ?string $regex = null;
/**
* @var string URL splat content
* URL splat content
*/
public string $splat = '';
/**
* @var bool Pass self in callback parameters
* Pass self in callback parameters
*/
public bool $pass = false;
/**
* @var string The alias is a way to identify the route using a simple name ex: 'login' instead of /admin/login
* The alias is a way to identify the route using a simple name ex: 'login' instead of /admin/login
*/
public string $alias = '';
@ -70,7 +70,7 @@ class Route
* @param array<int, string> $methods HTTP methods
* @param bool $pass Pass self in callback parameters
*/
public function __construct(string $pattern, $callback, array $methods, bool $pass, string $alias = '')
public function __construct(string $pattern, callable $callback, array $methods, bool $pass, string $alias = '')
{
$this->pattern = $pattern;
$this->callback = $callback;
@ -167,9 +167,6 @@ class Route
/**
* Checks if an alias matches the route alias.
*
* @param string $alias [description]
* @return boolean
*/
public function matchAlias(string $alias): bool
{
@ -180,7 +177,6 @@ class Route
* Hydrates the route url with the given parameters
*
* @param array<string,mixed> $params the parameters to pass to the route
* @return string
*/
public function hydrateUrl(array $params = []): string {
$url = preg_replace_callback("/(?:@([a-zA-Z0-9]+)(?:\:([^\/]+))?\)*)/i", function($match) use ($params) {
@ -199,7 +195,7 @@ class Route
/**
* Sets the route alias
*
* @return self
* @return $this
*/
public function setAlias(string $alias): self {
$this->alias = $alias;
@ -209,8 +205,8 @@ class Route
/**
* Sets the route middleware
*
* @param array<callable>|callable $middleware
* @return self
* @param array<int, callable>|callable $middleware
* @return $this
*/
public function addMiddleware($middleware): self {
if(is_array($middleware) === true) {

@ -25,8 +25,7 @@ class Router
*/
public bool $case_sensitive = false;
/**
* Mapped routes.
* @var array<int,Route>
* @var array<int,Route> Mapped routes.
*/
protected array $routes = [];
@ -37,8 +36,6 @@ class Router
/**
* When groups are used, this is mapped against all the routes
*
* @var string
*/
protected string $group_prefix = '';
@ -74,7 +71,6 @@ class Router
* @param callable $callback Callback function
* @param bool $pass_route Pass the matching route object to the callback
* @param string $route_alias Alias for the route
* @return Route
*/
public function map(string $pattern, callable $callback, bool $pass_route = false, string $route_alias = ''): Route
{
@ -106,7 +102,6 @@ class Router
* @param callable $callback Callback function
* @param bool $pass_route Pass the matching route object to the callback
* @param string $alias Alias for the route
* @return Route
*/
public function get(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route {
return $this->map('GET ' . $pattern, $callback, $pass_route, $alias);
@ -119,7 +114,6 @@ class Router
* @param callable $callback Callback function
* @param bool $pass_route Pass the matching route object to the callback
* @param string $alias Alias for the route
* @return Route
*/
public function post(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route {
return $this->map('POST ' . $pattern, $callback, $pass_route, $alias);
@ -132,7 +126,6 @@ class Router
* @param callable $callback Callback function
* @param bool $pass_route Pass the matching route object to the callback
* @param string $alias Alias for the route
* @return Route
*/
public function put(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route {
return $this->map('PUT ' . $pattern, $callback, $pass_route, $alias);
@ -145,7 +138,6 @@ class Router
* @param callable $callback Callback function
* @param bool $pass_route Pass the matching route object to the callback
* @param string $alias Alias for the route
* @return Route
*/
public function patch(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route {
return $this->map('PATCH ' . $pattern, $callback, $pass_route, $alias);
@ -158,7 +150,6 @@ class Router
* @param callable $callback Callback function
* @param bool $pass_route Pass the matching route object to the callback
* @param string $alias Alias for the route
* @return Route
*/
public function delete(string $pattern, callable $callback, bool $pass_route = false, string $alias = ''): Route {
return $this->map('DELETE ' . $pattern, $callback, $pass_route, $alias);
@ -170,7 +161,6 @@ class Router
* @param string $group_prefix group URL prefix (such as /api/v1)
* @param callable $callback The necessary calling that holds the Router class
* @param array<int,callable|object> $group_middlewares The middlewares to be applied to the group Ex: [ $middleware1, $middleware2 ]
* @return void
*/
public function group(string $group_prefix, callable $callback, array $group_middlewares = []): void {
$old_group_prefix = $this->group_prefix;
@ -185,9 +175,7 @@ class Router
/**
* Routes the current request.
*
* @param Request $request Request object
*
* @return bool|Route Matching route or false if no match
* @return false|Route Matching route or false if no match
*/
public function route(Request $request)
{
@ -207,7 +195,6 @@ class Router
*
* @param string $alias the alias to match
* @param array<string,mixed> $params the parameters to pass to the route
* @return string
*/
public function getUrlByAlias(string $alias, array $params = []): string {
$potential_aliases = [];
@ -259,7 +246,7 @@ class Router
/**
* Gets the current route.
*
* @return bool|Route
* @return false|Route
*/
public function current()
{

@ -17,24 +17,24 @@ namespace flight\template;
*/
class View
{
/** @var string Location of view templates. */
public $path;
/** Location of view templates. */
public string $path;
/** @var string File extension. */
public $extension = '.php';
/** File extension. */
public string $extension = '.php';
/** @var array<string, mixed> View variables. */
protected $vars = [];
protected array $vars = [];
/** @var string Template file. */
private $template;
/** Template file. */
private string $template;
/**
* Constructor.
*
* @param string $path Path to templates directory
*/
public function __construct($path = '.')
public function __construct(string $path = '.')
{
$this->path = $path;
}
@ -42,11 +42,9 @@ class View
/**
* Gets a template variable.
*
* @param string $key
*
* @return mixed Variable value or `null` if doesn't exists
*/
public function get($key)
public function get(string $key)
{
return $this->vars[$key] ?? null;
}
@ -58,7 +56,7 @@ class View
* @param mixed $value Value
* @return $this
*/
public function set($key, $value = null)
public function set($key, $value = null): self
{
if (\is_iterable($key)) {
foreach ($key as $k => $v) {
@ -74,11 +72,9 @@ class View
/**
* Checks if a template variable is set.
*
* @param string $key
*
* @return bool If key exists
*/
public function has($key)
public function has(string $key): bool
{
return isset($this->vars[$key]);
}
@ -86,11 +82,9 @@ class View
/**
* Unsets a template variable. If no key is passed in, clear all variables.
*
* @param ?string $key
*
* @return $this
*/
public function clear($key = null)
public function clear(?string $key = null): self
{
if (null === $key) {
$this->vars = [];
@ -107,10 +101,9 @@ class View
* @param string $file Template file
* @param ?array<string, mixed> $data Template data
*
* @return void
* @throws \Exception If template not found
*/
public function render($file, $data = null)
public function render(string $file, ?array $data = null): void
{
$this->template = $this->getTemplate($file);
@ -136,7 +129,7 @@ class View
*
* @return string Output of template
*/
public function fetch($file, $data = null)
public function fetch(string $file, ?array $data = null): string
{
\ob_start();
@ -152,7 +145,7 @@ class View
*
* @return bool Template file exists
*/
public function exists($file)
public function exists(string $file): bool
{
return \file_exists($this->getTemplate($file));
}
@ -164,7 +157,7 @@ class View
*
* @return string Template file location
*/
public function getTemplate($file)
public function getTemplate(string $file): string
{
$ext = $this->extension;
@ -188,20 +181,14 @@ class View
*
* @return string Escaped string
*/
public function e($str)
public function e(string $str): string
{
$value = \htmlentities($str);
echo $value;
return $value;
}
/**
* @param string $path An unnormalized path.
* @param string $separator Path separator.
*
* @return string Normalized path.
*/
protected static function normalizePath($path, $separator = DIRECTORY_SEPARATOR)
protected static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string
{
return \str_replace(['\\', '/'], $separator, $path);
}

@ -42,9 +42,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
/**
* Gets an item.
*
* @param string $key Key
*
* @return mixed Value
* @return mixed Value if `$key` exists in collection data, otherwise returns `NULL`
*/
public function __get(string $key)
{
@ -54,7 +52,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
/**
* Set an item.
*
* @param string $key Key
* @param mixed $value Value
*/
public function __set(string $key, $value): void
@ -64,10 +61,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
/**
* Checks if an item exists.
*
* @param string $key Key
*
* @return bool Item status
*/
public function __isset(string $key): bool
{
@ -76,8 +69,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
/**
* Removes an item.
*
* @param string $key Key
*/
public function __unset(string $key): void
{
@ -116,9 +107,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
/**
* Checks if an item exists at the offset.
*
* @param string $offset Offset
*
* @return bool Item status
* @param string $offset
*/
public function offsetExists($offset): bool
{
@ -128,7 +117,7 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
/**
* Removes an item at the offset.
*
* @param string $offset Offset
* @param string $offset
*/
public function offsetUnset($offset): void
{
@ -176,8 +165,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
/**
* Checks if the current collection key is valid.
*
* @return bool Key status
*/
public function valid(): bool
{
@ -188,8 +175,6 @@ class Collection implements ArrayAccess, Iterator, Countable, JsonSerializable
/**
* Gets the size of the collection.
*
* @return int Collection size
*/
public function count(): int
{

@ -115,9 +115,8 @@ class EngineTest extends PHPUnit\Framework\TestCase
};
// doing this so we can overwrite some parts of the response
$engine->getLoader()->register('response', function() {
return new class extends \flight\net\Response {
public function __construct() {}
public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): Response
return new class extends Response {
public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): self
{
return $this;
}

@ -124,7 +124,7 @@ class ViewTest extends PHPUnit\Framework\TestCase
public function testNormalizePath(): void
{
$viewMock = new class extends View {
public static function normalizePath($path, $separator = DIRECTORY_SEPARATOR)
public static function normalizePath(string $path, string $separator = DIRECTORY_SEPARATOR): string
{
return parent::normalizePath($path, $separator);
}

Loading…
Cancel
Save