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.
|
||||
});
|
||||
|
||||
#$GLOBALS['DbConnection'] = new DbConnection(DATABASE_DEFAULT_DATABASE, DATABASE_DEFAULT_HOST);
|
||||
|
|
11
lib/Db.php
11
lib/Db.php
|
@ -1,11 +1,18 @@
|
|||
<?
|
||||
|
||||
class Db{
|
||||
public static function GetLastInsertedId(){
|
||||
public static function GetLastInsertedId(): int{
|
||||
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)){
|
||||
$args = [$args];
|
||||
}
|
||||
|
|
|
@ -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<mixed>
|
||||
*/
|
||||
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<mixed>
|
||||
*/
|
||||
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){
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue