Bump PHPStan check level to max and add final round of type hints

This commit is contained in:
Alex Cabal 2024-05-13 10:48:05 -05:00
parent 110c091a7b
commit 70ae877dd8
15 changed files with 86 additions and 52 deletions

View file

@ -9,21 +9,22 @@ class Db{
}
/**
* @param string $query
* @param array<mixed> $args
* @param string $class
* @return Array<mixed>
*/
* @template T
* @param string $query
* @param array<mixed> $args
* @param class-string<T> $class
* @return Array<T>
*/
public static function Query(string $query, array $args = [], string $class = 'stdClass'): array{
return $GLOBALS['DbConnection']->Query($query, $args, $class);
}
/**
* Returns a single integer value for the first column database query result.
* This is useful for queries that return a single integer as a result, like count(*) or sum(*).
* @param string $query
* @param array<mixed> $args
*/
* Returns a single integer value for the first column database query result.
* This is useful for queries that return a single integer as a result, like count(*) or sum(*).
* @param string $query
* @param array<mixed> $args
*/
public static function QueryInt(string $query, array $args = []): int{
$result = $GLOBALS['DbConnection']->Query($query, $args);

View file

@ -41,24 +41,26 @@ class DbConnection{
$connectionString .= ';dbname=' . $defaultDatabase;
}
// We can't use persistent connections (connection pooling) because we would have race condition problems with last_insert_id()
$params = [\PDO::ATTR_EMULATE_PREPARES => false, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_PERSISTENT => false];
if($forceUtf8){
$params[\PDO::MYSQL_ATTR_INIT_COMMAND] = 'set names utf8mb4 collate utf8mb4_unicode_ci;';
}
// We can't use persistent connections (connection pooling) because we would have race condition problems with last_insert_id()
$this->_link = new \PDO($connectionString, $user, $password, $params);
}
/**
* @param string $sql The SQL query to execute.
* @param array<mixed> $params An array of parameters to bind to the SQL statement.
* @param string $class The type of object to return in the return array.
* @return Array<mixed>
* @throws Exceptions\DuplicateDatabaseKeyException When a unique key constraint has been violated.
* @throws Exceptions\DatabaseQueryException When an error occurs during execution of the query.
*/
* Execute a generic query in the database.
* @template T
* @param string $sql The SQL query to execute.
* @param array<mixed> $params An array of parameters to bind to the SQL statement.
* @param class-string<T> $class The type of object to return in the return array.
* @return Array<T>
* @throws Exceptions\DuplicateDatabaseKeyException When a unique key constraint has been violated.
* @throws Exceptions\DatabaseQueryException When an error occurs during execution of the query.
*/
public function Query(string $sql, array $params = [], string $class = 'stdClass'): array{
if($this->_link === null){
return [];
@ -131,19 +133,11 @@ class DbConnection{
}
/**
* @param \PDOException $ex The exception to create details from.
* @param string $sql The prepared SQL that caused the exception.
* @param array<mixed> $params The parameters passed to the prepared SQL.
*/
private function CreateDetailedException(\PDOException $ex, string $sql, array $params): Exceptions\DatabaseQueryException{
// Throw a custom exception that includes more information on the query and paramaters
return new Exceptions\DatabaseQueryException('Error when executing query: ' . $ex->getMessage() . '. Query: ' . $sql . '. Parameters: ' . vds($params));
}
/**
* @return Array<mixed>
* @throws \PDOException When an error occurs during execution of the query.
*/
* @template T
* @param class-string<T> $class
* @return Array<T>
* @throws \PDOException When an error occurs during execution of the query.
*/
private function ExecuteQuery(\PDOStatement $handle, string $class = 'stdClass'): array{
$handle->execute();
@ -287,4 +281,14 @@ class DbConnection{
return $id;
}
/**
* @param \PDOException $ex The exception to create details from.
* @param string $sql The prepared SQL that caused the exception.
* @param array<mixed> $params The parameters passed to the prepared SQL.
*/
private function CreateDetailedException(\PDOException $ex, string $sql, array $params): Exceptions\DatabaseQueryException{
// Throw a custom exception that includes more information on the query and paramaters
return new Exceptions\DatabaseQueryException('Error when executing query: ' . $ex->getMessage() . '. Query: ' . $sql . '. Parameters: ' . vds($params));
}
}

View file

@ -15,7 +15,7 @@ class Ebook{
public string $WwwFilesystemPath;
public string $RepoFilesystemPath;
public string $Url;
public string $KindleCoverUrl;
public ?string $KindleCoverUrl;
public string $EpubUrl;
public string $AdvancedEpubUrl;
public string $KepubUrl;
@ -68,8 +68,8 @@ class Ebook{
public string $TitleWithCreditsHtml = '';
public DateTimeImmutable $Created;
public DateTimeImmutable $Updated;
public string $TextUrl;
public string $TextSinglePageUrl;
public ?string $TextUrl;
public ?string $TextSinglePageUrl;
public ?string $TextSinglePageSizeNumber = null;
public ?string $TextSinglePageSizeUnit = null;
/** @var ?array<string> $TocEntries */

View file

@ -84,18 +84,22 @@ class HttpInput{
return null;
}
/** @var ?string $var */
return $var;
}
public static function Int(HttpVariableSource $set, string $variable): ?int{
/** @var ?int */
return self::GetHttpVar($variable, HttpVariableType::Integer, $set);
}
public static function Bool(HttpVariableSource $set, string $variable): ?bool{
/** @var ?bool */
return self::GetHttpVar($variable, HttpVariableType::Boolean, $set);
}
public static function Dec(HttpVariableSource $set, string $variable): ?float{
/** @var ?float */
return self::GetHttpVar($variable, HttpVariableType::Decimal, $set);
}
@ -104,9 +108,13 @@ class HttpInput{
* @return array<string>
*/
public static function Array(HttpVariableSource $set, string $variable): ?array{
/** @var array<string> */
return self::GetHttpVar($variable, HttpVariableType::Array, $set);
}
/**
* @return array<string>|array<int>|array<float>|array<bool>|string|int|float|bool|null
*/
private static function GetHttpVar(string $variable, HttpVariableType $type, HttpVariableSource $set): mixed{
$vars = [];

View file

@ -113,6 +113,7 @@ class Library{
*/
public static function GetEbooks(): array{
// Get all ebooks, unsorted.
/** @var array<Ebook> */
return self::GetFromApcu('ebooks');
}
@ -121,6 +122,7 @@ class Library{
* @throws Exceptions\AppException
*/
public static function GetEbooksByAuthor(string $wwwFilesystemPath): array{
/** @var array<Ebook> */
return self::GetFromApcu('author-' . $wwwFilesystemPath);
}
@ -129,6 +131,7 @@ class Library{
*/
public static function GetEbooksByTag(string $tag): array{
try{
/** @var array<Ebook> */
return apcu_fetch('tag-' . $tag) ?? [];
}
catch(Safe\Exceptions\ApcuException){
@ -141,6 +144,7 @@ class Library{
* @throws Exceptions\AppException
*/
public static function GetEbookCollections(): array{
/** @var array<string, Collection> */
return self::GetFromApcu('collections');
}
@ -150,6 +154,7 @@ class Library{
*/
public static function GetEbooksByCollection(string $collection): array{
// Do we have the tag's ebooks cached?
/** @var array<Ebook> */
return self::GetFromApcu('collection-' . $collection);
}
@ -158,6 +163,7 @@ class Library{
* @throws Exceptions\AppException
*/
public static function GetTags(): array{
/** @var array<Tag> */
return self::GetFromApcu('tags');
}
@ -538,7 +544,7 @@ class Library{
}
/**
* @return array<string, array<int|string, array<int|string, mixed>>>
* @return array<string, array<int|string, array<int|string, stdClass>>>
* @throws Exceptions\AppException
*/
public static function RebuildBulkDownloadsCache(): array{
@ -662,6 +668,7 @@ class Library{
return null;
}
/** @var array<Ebook> $result */
$result = self::GetFromApcu('ebook-' . $ebookWwwFilesystemPath);
if(sizeof($result) > 0){

View file

@ -25,7 +25,7 @@ class Template{
* @param array<mixed> $arguments
*/
public static function __callStatic(string $function, array $arguments): string{
if(isset($arguments[0])){
if(isset($arguments[0]) && is_array($arguments[0])){
return self::Get($function, $arguments[0]);
}
else{