From 355ae6e0bf12a20b385a370fcc5fde7179746270 Mon Sep 17 00:00:00 2001 From: "Bruno O. Notario" Date: Mon, 18 Sep 2023 00:30:47 -0300 Subject: [PATCH] Exceptions class, refact to PHP 8.2 fit --- system/database/databases/mysqli.php | 4 +- system/engine/autoload.php | 2 +- system/engine/exception.php | 4 +- system/engine/restful.php | 6 ++- system/exception/BadFunctionCallException.php | 19 +++++++ system/exception/BadMethodCallException.php | 19 +++++++ system/exception/DomainException.php | 19 +++++++ system/exception/InvalidArgumentException.php | 18 +++++++ system/exception/LengthException.php | 19 +++++++ system/exception/LogicException.php | 18 +++++++ system/exception/OutOfBoundsException.php | 19 +++++++ system/exception/OutOfRangeException.php | 19 +++++++ system/exception/OverflowException.php | 19 +++++++ system/exception/RESTException.php | 18 +++++++ system/exception/RangeException.php | 19 +++++++ system/exception/RuntimeException.php | 18 +++++++ system/exception/Throwable.php | 36 +++++++++++++ system/exception/UnderflowException.php | 19 +++++++ system/exception/UnexpectedValueException.php | 19 +++++++ system/exception/WebApiException.php | 53 +++++++++++++++++++ system/json/autoload.php | 4 +- system/login/autoload.php | 2 + system/magiql/Api/Syntax/OrderBy.php | 14 +++++ system/magiql/Builder.php | 4 +- system/magiql/Builder/BuilderException.php | 4 +- .../magiql/Manipulation/AbstractBaseQuery.php | 33 ++++++------ system/magiql/Manipulation/QueryException.php | 5 +- system/magiql/Syntax/OrderBy.php | 12 ++--- system/session/autoload.php | 2 +- 29 files changed, 411 insertions(+), 36 deletions(-) create mode 100644 system/exception/BadFunctionCallException.php create mode 100644 system/exception/BadMethodCallException.php create mode 100644 system/exception/DomainException.php create mode 100644 system/exception/InvalidArgumentException.php create mode 100644 system/exception/LengthException.php create mode 100644 system/exception/LogicException.php create mode 100644 system/exception/OutOfBoundsException.php create mode 100644 system/exception/OutOfRangeException.php create mode 100644 system/exception/OverflowException.php create mode 100644 system/exception/RESTException.php create mode 100644 system/exception/RangeException.php create mode 100644 system/exception/RuntimeException.php create mode 100644 system/exception/Throwable.php create mode 100644 system/exception/UnderflowException.php create mode 100644 system/exception/UnexpectedValueException.php create mode 100644 system/exception/WebApiException.php create mode 100644 system/magiql/Api/Syntax/OrderBy.php diff --git a/system/database/databases/mysqli.php b/system/database/databases/mysqli.php index 831d9a4..f101f97 100644 --- a/system/database/databases/mysqli.php +++ b/system/database/databases/mysqli.php @@ -43,7 +43,9 @@ class MySQLi implements Databases { } if (!$this->connection->connect_errno) { - $this->connection->report_mode = MYSQLI_REPORT_ERROR; + if(isset($this->connection->report_mode)) + $this->connection->report_mode = MYSQLI_REPORT_ERROR; + $this->connection->set_charset($charset); //$this->connection->query("SET SESSION sql_mode = 'NO_ZERO_IN_DATE,NO_ENGINE_SUBSTITUTION'"); $this->connection->query("SET SQL_MODE = ''"); diff --git a/system/engine/autoload.php b/system/engine/autoload.php index d47166c..3c6b3b9 100644 --- a/system/engine/autoload.php +++ b/system/engine/autoload.php @@ -123,7 +123,7 @@ * @throws \TypeError */ static public function run() { - return spl_autoload_register(['self', 'load']); + return spl_autoload_register([self::class, 'load']); } /** diff --git a/system/engine/exception.php b/system/engine/exception.php index ba2730d..219ea0a 100644 --- a/system/engine/exception.php +++ b/system/engine/exception.php @@ -18,7 +18,7 @@ namespace Phacil\Framework; * @package Phacil\Framework * @api */ -class Exception extends \Exception +class Exception extends \Exception implements \Phacil\Framework\Exception\Throwable { public $errorFormat = 'text'; @@ -55,7 +55,7 @@ class Exception extends \Exception 'trace' => ($debugging) ? (($this->errorFormat == 'json') ? ($this->heritageTrace ?: $this->getTrace()) : Debug::trace(($this->heritageTrace ?: $this->getTrace()))) : null ]; $log->write(($this->errorFormat == 'json') ? json_encode($errorStamp) : implode(PHP_EOL, array_map( - ['self','convertArray'], + [self::class,'convertArray'], $errorStamp, array_keys($errorStamp) ))); diff --git a/system/engine/restful.php b/system/engine/restful.php index f1507ac..3955afc 100644 --- a/system/engine/restful.php +++ b/system/engine/restful.php @@ -143,7 +143,11 @@ abstract class RESTful extends Controller { try { //code... call_user_func_array(array($this, $method), $params); - } catch (\Throwable $th) { + } catch (\Phacil\Framework\Exception\WebApiException $re) { + $this->data = is_string($re->getMessage()) ? ['error' => $re->getMessage()] : $re->getMessage(); + $this->response->code($re->getCode()); + $this->out(); + }catch (\Phacil\Framework\Exception\Throwable $th) { if(get_class($th) == 'TypeError'){ new Exception($th->getMessage(), $th->getCode()); return $this->__callInterrupt($th->getMessage()); diff --git a/system/exception/BadFunctionCallException.php b/system/exception/BadFunctionCallException.php new file mode 100644 index 0000000..40dc997 --- /dev/null +++ b/system/exception/BadFunctionCallException.php @@ -0,0 +1,19 @@ +queryWriterInstances[$queryPart]->write($query); } - throw new \RuntimeException('Query builder part not defined.'); + throw new \Phacil\Framework\MagiQL\Builder\BuilderException('Query builder part not defined.'); } /** diff --git a/system/magiql/Builder/BuilderException.php b/system/magiql/Builder/BuilderException.php index 72aec95..f282190 100644 --- a/system/magiql/Builder/BuilderException.php +++ b/system/magiql/Builder/BuilderException.php @@ -8,6 +8,8 @@ namespace Phacil\Framework\MagiQL\Builder; -class BuilderException extends \Exception +use Phacil\Framework\Exception; + +class BuilderException extends Exception { } diff --git a/system/magiql/Manipulation/AbstractBaseQuery.php b/system/magiql/Manipulation/AbstractBaseQuery.php index 661d1b8..ab1651c 100644 --- a/system/magiql/Manipulation/AbstractBaseQuery.php +++ b/system/magiql/Manipulation/AbstractBaseQuery.php @@ -27,7 +27,7 @@ abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface protected $comment = ''; /** - * @var \Phacil\Framework\MagiQL\Builder\BuilderInterface + * @var \Phacil\Framework\MagiQL\Api\BuilderInterface */ protected $builder; @@ -80,10 +80,8 @@ abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface /** * Stores the builder that created this query. - * - * @param BuilderInterface $builder - * - * @return $this + * @param \Phacil\Framework\MagiQL\Api\BuilderInterface $builder + * @return $this */ final public function setBuilder(BuilderInterface $builder) { @@ -93,14 +91,14 @@ abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface } /** - * @return BuilderInterface - * - * @throws \RuntimeException when builder has not been injected + * + * @return \Phacil\Framework\MagiQL\Api\BuilderInterface + * @throws \Phacil\Framework\Exception\RuntimeException */ final public function getBuilder() { if (!$this->builder) { - throw new \RuntimeException('Query builder has not been injected with setBuilder'); + throw new \Phacil\Framework\Exception\RuntimeException('Query builder has not been injected with setBuilder'); } return $this->builder; @@ -163,7 +161,8 @@ abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface } /** - * @return Table + * + * @return \Phacil\Framework\MagiQL\Syntax\Table|null */ public function getTable() { @@ -221,13 +220,13 @@ abstract class AbstractBaseQuery implements QueryInterface, QueryPartInterface return $this->where->getConjunction(); } - /** - * @param string $column - * @param string $direction - * @param null $table - * - * @return $this - */ + /** + * + * @param string $column + * @param string $direction + * @param null|Table $table + * @return $this + */ public function orderBy($column, $direction = OrderBy::ASC, $table = null) { $newColumn = array($column); diff --git a/system/magiql/Manipulation/QueryException.php b/system/magiql/Manipulation/QueryException.php index 99f91d1..d10deb0 100644 --- a/system/magiql/Manipulation/QueryException.php +++ b/system/magiql/Manipulation/QueryException.php @@ -10,8 +10,9 @@ namespace Phacil\Framework\MagiQL\Manipulation; /** - * Class QueryException. + * + * @package Phacil\Framework\MagiQL\Manipulation */ -class QueryException extends \Exception +class QueryException extends \Phacil\Framework\Exception { } diff --git a/system/magiql/Syntax/OrderBy.php b/system/magiql/Syntax/OrderBy.php index 899d6c2..b551328 100644 --- a/system/magiql/Syntax/OrderBy.php +++ b/system/magiql/Syntax/OrderBy.php @@ -9,13 +9,13 @@ namespace Phacil\Framework\MagiQL\Syntax; +use Phacil\Framework\MagiQL\Api\Syntax\OrderBy as OrderByInterface; + /** * Class OrderBy. */ -class OrderBy +class OrderBy implements OrderByInterface { - const ASC = 'ASC'; - const DESC = 'DESC'; /** * @var Column @@ -73,14 +73,14 @@ class OrderBy /** * @param string $direction * - * @throws \InvalidArgumentException + * @throws \Phacil\Framework\Exception\InvalidArgumentException * * @return $this */ public function setDirection($direction) { - if (!in_array($direction, array(self::ASC, self::DESC))) { - throw new \InvalidArgumentException( + if (!in_array($direction, array(OrderByInterface::ASC, OrderByInterface::DESC))) { + throw new \Phacil\Framework\Exception\InvalidArgumentException( "Specified direction '$direction' is not allowed. Only ASC or DESC are allowed." ); } diff --git a/system/session/autoload.php b/system/session/autoload.php index defc677..77c9d87 100644 --- a/system/session/autoload.php +++ b/system/session/autoload.php @@ -247,7 +247,7 @@ final class Session * @return mixed|null */ public function getData($key) { - return $this->data[$key] ?? null; + return isset($this->data[$key]) ? $this->data[$key] : null; } /**