From d48f24ed989ed98ec79f358169843bb0c183e211 Mon Sep 17 00:00:00 2001 From: kj Date: Sat, 22 Apr 2023 05:32:37 -0400 Subject: [PATCH] Improve return types. --- src/Libs/Middleware.php | 2 +- src/Libs/Model.php | 66 ++++++++++++++++++++--------------------- src/Libs/Router.php | 18 +++++------ 3 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/Libs/Middleware.php b/src/Libs/Middleware.php index d824bbf..b98100e 100644 --- a/src/Libs/Middleware.php +++ b/src/Libs/Middleware.php @@ -20,7 +20,7 @@ class Middleware { * * @return mixed */ - public static function next(Neuron $req) { + public static function next(Neuron $req): mixed { $next = array_pop($req->next); return call_user_func_array($next, [$req]); } diff --git a/src/Libs/Model.php b/src/Libs/Model.php index 930da90..c7d1538 100644 --- a/src/Libs/Model.php +++ b/src/Libs/Model.php @@ -214,10 +214,10 @@ class Model { * Puede recibir un arreglo o un objeto que contiene los valores * que tendrán sus atributos. * - * @return Model + * @return static * Retorna un objeto de la clase actual. */ - protected static function getInstance(array $elem = []) : Model { + protected static function getInstance(array $elem = []) : static { $class = get_called_class(); $instance = new $class; @@ -368,9 +368,9 @@ class Model { * @param array $columns * Columnas que se selecionarán en la consulta SQL. * - * @return Model + * @return static */ - public static function select(array $columns) : Model { + public static function select(array $columns) : static { static::$querySelect['select'] = $columns; return new static(); @@ -382,9 +382,9 @@ class Model { * @param array $tables * Tablas que se selecionarán en la consulta SQL. * - * @return Model + * @return static */ - public static function from(array $tables) : Model { + public static function from(array $tables) : static { static::$querySelect['from'] = join(', ', $tables); return new static(); @@ -406,9 +406,9 @@ class Model { * (opcional) Se usa cuando $value es una columna o un valor que no requiere filtros * contra ataques SQLI (por defeco es false). * - * @return Model + * @return static */ - public static function where(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : Model { + public static function where(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : static { return static::and($column, $operatorOrValue, $value, $no_filter); } @@ -428,9 +428,9 @@ class Model { * (opcional) Se usa cuando $value es una columna o un valor que no requiere filtros * contra ataques SQLI (por defecto es false). * - * @return Model + * @return static */ - public static function and(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : Model { + public static function and(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : static { if (is_null($value)) { $value = $operatorOrValue; $operatorOrValue = '='; @@ -463,9 +463,9 @@ class Model { * (opcional) Se usa cuando $value es una columna o un valor que no requiere filtros * contra ataques SQLI (por defecto es false). * - * @return Model + * @return static */ - public static function or(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : Model { + public static function or(string $column, string $operatorOrValue, string $value=null, bool $no_filter = false) : static { if (is_null($value)) { $value = $operatorOrValue; $operatorOrValue = '='; @@ -494,9 +494,9 @@ class Model { * @param bool $in * Define si se tienen que comprobar negativa o positivamente. * - * @return Model + * @return static */ - public static function where_in(string $column, array $arr, bool $in = true) : Model { + public static function where_in(string $column, array $arr, bool $in = true) : static { $arrIn = []; foreach($arr as $value) { $arrIn[] = static::bindValue($value); @@ -525,9 +525,9 @@ class Model { * @param string $columnB * (opcional) Columna a comparar para hacer el join. * - * @return Model + * @return static */ - public static function leftJoin(string $table, string $columnA, string $operatorOrColumnB, string $columnB = null) : Model { + public static function leftJoin(string $table, string $columnA, string $operatorOrColumnB, string $columnB = null) : static { if (is_null($columnB)) { $columnB = $operatorOrColumnB; $operatorOrColumnB = '='; @@ -553,9 +553,9 @@ class Model { * @param string $columnB * (opcional) Columna a comparar para hacer el join. * - * @return Model + * @return static */ - public static function rightJoin(string $table, string $columnA, string $operatorOrColumnB, string $columnB = null) : Model { + public static function rightJoin(string $table, string $columnA, string $operatorOrColumnB, string $columnB = null) : static { if (is_null($columnB)) { $columnB = $operatorOrColumnB; $operatorOrColumnB = '='; @@ -581,9 +581,9 @@ class Model { * @param string $columnB * (opcional) Columna a comparar para hacer el join. * - * @return Model + * @return static */ - public static function innerJoin(string $table, string $columnA, string $operatorOrColumnB, string $columnB = null) : Model { + public static function innerJoin(string $table, string $columnA, string $operatorOrColumnB, string $columnB = null) : static { if (is_null($columnB)) { $columnB = $operatorOrColumnB; $operatorOrColumnB = '='; @@ -600,9 +600,9 @@ class Model { * @param array $arr * Columnas por las que se agrupará. * - * @return Model + * @return static */ - public static function groupBy(array $arr) : Model { + public static function groupBy(array $arr) : static { static::$querySelect['groupBy'] = join(', ', $arr); return new static(); } @@ -616,9 +616,9 @@ class Model { * @param int $quantity * Define la cantidad máxima de filas a tomar. * - * @return Model + * @return static */ - public static function limit(int $offsetOrQuantity, ?int $quantity = null) : Model { + public static function limit(int $offsetOrQuantity, ?int $quantity = null) : static { if (is_null($quantity)) static::$querySelect['limit'] = $offsetOrQuantity; else @@ -637,9 +637,9 @@ class Model { * (opcional) Define si el orden será de manera ascendente (ASC), * descendente (DESC) o aleatorio (RAND). * - * @return Model + * @return static */ - public static function orderBy(string $value, string $order = 'ASC') : Model { + public static function orderBy(string $value, string $order = 'ASC') : static { if ($value == "RAND") { static::$querySelect['orderBy'] = 'RAND()'; return new static(); @@ -705,9 +705,9 @@ class Model { * * @param mixed $id * - * @return Model|null + * @return static|null */ - public static function getById(mixed $id): ?Model { + public static function getById(mixed $id): ?static { return static::where(static::$primaryKey, $id)->getFirst(); } @@ -720,9 +720,9 @@ class Model { * @param array $in * (opcional) Columnas en las que se va a buscar (null para buscar en todas). * - * @return Model + * @return static */ - public static function search(string $search, array $in = null) : Model { + public static function search(string $search, array $in = null) : static { if ($in == null) { $className = get_called_class(); $in = array_keys((new $className())->getVars()); @@ -777,10 +777,10 @@ class Model { * @param bool $resetQuery * (opcional) Indica si el query debe reiniciarse o no (por defecto es true). * - * @return Model|null - * Puede retornar un objeto Model o null. + * @return static|null + * Puede retornar un objeto static o null. */ - public static function getFirst(bool $resetQuery = true): ?Model { // Devuelve null si no encuentra nada. + public static function getFirst(bool $resetQuery = true): ?static { // Devuelve null si no encuentra nada. static::limit(1); $instances = static::get($resetQuery); return empty($instances) ? null : $instances[0]; diff --git a/src/Libs/Router.php b/src/Libs/Router.php index 8bbde5f..85d0ff3 100644 --- a/src/Libs/Router.php +++ b/src/Libs/Router.php @@ -109,11 +109,11 @@ class Router { * @param mixed $callback * @param int $prioriry * - * @return Router + * @return static * Devuelve un enlace estático. */ - public static function middleware($callback, int $priority = null): Router { + public static function middleware($callback, int $priority = null): static { if (!isset(static::$last)) return new static(); @@ -179,10 +179,10 @@ class Router { * @param mixed $callback * Callback que será llamado cuando la ruta configurada en $path coincida. * - * @return Router + * @return static * Devuelve un enlace estático. */ - public static function get(string $path, $callback): Router { + public static function get(string $path, $callback): static { static::$get[] = static::parse($path, $callback); static::$last = ['get', count(static::$get)-1]; return new static(); @@ -197,10 +197,10 @@ class Router { * @param mixed $callback * Callback que será llamado cuando la ruta configurada en $path coincida. * - * @return Router + * @return static * Devuelve un enlace estático. */ - public static function post(string $path, $callback): Router { + public static function post(string $path, $callback): static { static::$post[] = static::parse($path, $callback); static::$last = ['post', count(static::$post)-1]; return new static(); @@ -215,11 +215,11 @@ class Router { * @param mixed $callback * Callback que será llamado cuando la ruta configurada en $path coincida. * - * @return Router + * @return static * Devuelve un enlace estático */ - public static function put(string $path, $callback): Router { + public static function put(string $path, $callback): static { static::$put[] = static::parse($path, $callback); static::$last = ['put', count(static::$put)-1]; return new static(); @@ -237,7 +237,7 @@ class Router { * @return static * Devuelve un enlace estático */ - public static function delete(string $path, $callback): Router { + public static function delete(string $path, $callback): static { static::$delete[] = static::parse($path, $callback); static::$last = ['delete', count(static::$delete)-1]; return new static();