From 9f3104025d86ff52a0ddb2661c07b9037901b8c7 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Wed, 9 Feb 2022 12:56:09 -0600 Subject: [PATCH] Add type hints and quiet a few other PHPStan complaints --- lib/Core.php | 2 -- lib/Db.php | 11 +++++++++-- lib/DbConnection.php | 16 +++++++++++----- lib/OrmBase.php | 8 ++++++-- lib/PropertiesBase.php | 10 +++++++++- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/lib/Core.php b/lib/Core.php index f98755c2..f83331d1 100644 --- a/lib/Core.php +++ b/lib/Core.php @@ -24,5 +24,3 @@ set_exception_handler(function(Throwable $ex): void{ throw $ex; // Send the exception back to PHP for its usual logging routine. }); - -#$GLOBALS['DbConnection'] = new DbConnection(DATABASE_DEFAULT_DATABASE, DATABASE_DEFAULT_HOST); diff --git a/lib/Db.php b/lib/Db.php index 6be4db21..0a9f93a1 100644 --- a/lib/Db.php +++ b/lib/Db.php @@ -1,11 +1,18 @@ GetLastInsertedId(); } - public static function Query(string $query, $args = []){ + /** + * @return Array + */ + public static function Query(string $query, array $args = []): array{ + if(!isset($GLOBALS['DbConnection'])){ + $GLOBALS['DbConnection'] = new DbConnection(DATABASE_DEFAULT_DATABASE, DATABASE_DEFAULT_HOST); + } + if(!is_array($args)){ $args = [$args]; } diff --git a/lib/DbConnection.php b/lib/DbConnection.php index ee040bbc..f4edb8a0 100644 --- a/lib/DbConnection.php +++ b/lib/DbConnection.php @@ -67,9 +67,12 @@ class DbConnection{ // Inputs: string $sql = the SQL query to execute // array $params = an array of parameters to bind to the SQL statement // Returns: a resource record or null on error - public function Query(string $sql, array $params = []){ + /** + * @return Array + */ + public function Query(string $sql, array $params = []): array{ if(!$this->IsConnected){ - return; + return []; } $this->QueryCount++; @@ -112,7 +115,7 @@ class DbConnection{ $done = false; while(!$done){ try{ - $result = $this->ExecuteQuery($handle, $preparedSql, $params); + $result = $this->ExecuteQuery($handle); $done = true; } catch(\PDOException $ex){ @@ -148,7 +151,10 @@ class DbConnection{ return $result; } - private function ExecuteQuery($handle, string $preparedSql, array $params){ + /** + * @return Array + */ + private function ExecuteQuery(PDOStatement $handle): array{ $handle->execute(); $result = []; @@ -171,7 +177,7 @@ class DbConnection{ } // Gets the last AUTO-INCREMENT id - public function GetLastInsertedId(){ + public function GetLastInsertedId(): ?int{ $id = $this->_link->lastInsertId(); if($id == 0){ diff --git a/lib/OrmBase.php b/lib/OrmBase.php index 4518bfd5..1aaac458 100644 --- a/lib/OrmBase.php +++ b/lib/OrmBase.php @@ -3,7 +3,11 @@ use Safe\DateTime; use function Safe\substr; abstract class OrmBase{ - public static function FillObject($object, $row){ + final public function __construct(){ + // Satisfy PHPStan and prevent child classes from having their own constructor + } + + public static function FillObject(Object $object, array $row): Object{ foreach($row as $property => $value){ if(substr($property, strlen($property) - 9) == 'Timestamp'){ if($value !== null){ @@ -25,7 +29,7 @@ abstract class OrmBase{ return $object; } - public static function FromRow($row){ + public static function FromRow(array $row): Object{ return self::FillObject(new static(), $row); } } diff --git a/lib/PropertiesBase.php b/lib/PropertiesBase.php index b407734f..90101971 100644 --- a/lib/PropertiesBase.php +++ b/lib/PropertiesBase.php @@ -2,6 +2,10 @@ use function Safe\substr; abstract class PropertiesBase extends OrmBase{ + /** + * @param mixed $var + * @return mixed + */ public function __get($var){ $function = 'Get' . $var; @@ -22,7 +26,11 @@ abstract class PropertiesBase extends OrmBase{ } } - public function __set($var, $val){ + /** + * @param mixed $val + * @return mixed + */ + public function __set(string $var, $val){ $function = 'Set' . $var; if(method_exists($this, $function)){ $this->$function($val);