mirror of
https://github.com/standardebooks/web.git
synced 2025-07-07 15:20:32 -04:00
Add type hints and quiet a few other PHPStan complaints
This commit is contained in:
parent
c9ebd71587
commit
9f3104025d
5 changed files with 35 additions and 12 deletions
|
@ -24,5 +24,3 @@ set_exception_handler(function(Throwable $ex): void{
|
||||||
|
|
||||||
throw $ex; // Send the exception back to PHP for its usual logging routine.
|
throw $ex; // Send the exception back to PHP for its usual logging routine.
|
||||||
});
|
});
|
||||||
|
|
||||||
#$GLOBALS['DbConnection'] = new DbConnection(DATABASE_DEFAULT_DATABASE, DATABASE_DEFAULT_HOST);
|
|
||||||
|
|
11
lib/Db.php
11
lib/Db.php
|
@ -1,11 +1,18 @@
|
||||||
<?
|
<?
|
||||||
|
|
||||||
class Db{
|
class Db{
|
||||||
public static function GetLastInsertedId(){
|
public static function GetLastInsertedId(): int{
|
||||||
return $GLOBALS['DbConnection']->GetLastInsertedId();
|
return $GLOBALS['DbConnection']->GetLastInsertedId();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function Query(string $query, $args = []){
|
/**
|
||||||
|
* @return Array<mixed>
|
||||||
|
*/
|
||||||
|
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)){
|
if(!is_array($args)){
|
||||||
$args = [$args];
|
$args = [$args];
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,9 +67,12 @@ class DbConnection{
|
||||||
// Inputs: string $sql = the SQL query to execute
|
// Inputs: string $sql = the SQL query to execute
|
||||||
// array $params = an array of parameters to bind to the SQL statement
|
// array $params = an array of parameters to bind to the SQL statement
|
||||||
// Returns: a resource record or null on error
|
// Returns: a resource record or null on error
|
||||||
public function Query(string $sql, array $params = []){
|
/**
|
||||||
|
* @return Array<mixed>
|
||||||
|
*/
|
||||||
|
public function Query(string $sql, array $params = []): array{
|
||||||
if(!$this->IsConnected){
|
if(!$this->IsConnected){
|
||||||
return;
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->QueryCount++;
|
$this->QueryCount++;
|
||||||
|
@ -112,7 +115,7 @@ class DbConnection{
|
||||||
$done = false;
|
$done = false;
|
||||||
while(!$done){
|
while(!$done){
|
||||||
try{
|
try{
|
||||||
$result = $this->ExecuteQuery($handle, $preparedSql, $params);
|
$result = $this->ExecuteQuery($handle);
|
||||||
$done = true;
|
$done = true;
|
||||||
}
|
}
|
||||||
catch(\PDOException $ex){
|
catch(\PDOException $ex){
|
||||||
|
@ -148,7 +151,10 @@ class DbConnection{
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function ExecuteQuery($handle, string $preparedSql, array $params){
|
/**
|
||||||
|
* @return Array<mixed>
|
||||||
|
*/
|
||||||
|
private function ExecuteQuery(PDOStatement $handle): array{
|
||||||
$handle->execute();
|
$handle->execute();
|
||||||
|
|
||||||
$result = [];
|
$result = [];
|
||||||
|
@ -171,7 +177,7 @@ class DbConnection{
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the last AUTO-INCREMENT id
|
// Gets the last AUTO-INCREMENT id
|
||||||
public function GetLastInsertedId(){
|
public function GetLastInsertedId(): ?int{
|
||||||
$id = $this->_link->lastInsertId();
|
$id = $this->_link->lastInsertId();
|
||||||
|
|
||||||
if($id == 0){
|
if($id == 0){
|
||||||
|
|
|
@ -3,7 +3,11 @@ use Safe\DateTime;
|
||||||
use function Safe\substr;
|
use function Safe\substr;
|
||||||
|
|
||||||
abstract class OrmBase{
|
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){
|
foreach($row as $property => $value){
|
||||||
if(substr($property, strlen($property) - 9) == 'Timestamp'){
|
if(substr($property, strlen($property) - 9) == 'Timestamp'){
|
||||||
if($value !== null){
|
if($value !== null){
|
||||||
|
@ -25,7 +29,7 @@ abstract class OrmBase{
|
||||||
return $object;
|
return $object;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function FromRow($row){
|
public static function FromRow(array $row): Object{
|
||||||
return self::FillObject(new static(), $row);
|
return self::FillObject(new static(), $row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,10 @@
|
||||||
use function Safe\substr;
|
use function Safe\substr;
|
||||||
|
|
||||||
abstract class PropertiesBase extends OrmBase{
|
abstract class PropertiesBase extends OrmBase{
|
||||||
|
/**
|
||||||
|
* @param mixed $var
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
public function __get($var){
|
public function __get($var){
|
||||||
$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;
|
$function = 'Set' . $var;
|
||||||
if(method_exists($this, $function)){
|
if(method_exists($this, $function)){
|
||||||
$this->$function($val);
|
$this->$function($val);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue