From 5455adf88a2a537d6e7b6ad376b2d934f8d9e7cf Mon Sep 17 00:00:00 2001 From: Shish Date: Fri, 19 Jan 2024 18:24:52 +0000 Subject: [PATCH] more typing --- core/imageboard/search.php | 30 +++++++++++++++++------------- core/polyfills.php | 6 ++++++ ext/danbooru_api/main.php | 15 +++++++++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/core/imageboard/search.php b/core/imageboard/search.php index b71803fc..363f9a54 100644 --- a/core/imageboard/search.php +++ b/core/imageboard/search.php @@ -8,6 +8,10 @@ use GQLA\Query; class Querylet { + /** + * @param string $sql + * @param array $variables + */ public function __construct( public string $sql, public array $variables = [], @@ -19,16 +23,6 @@ class Querylet $this->sql .= $querylet->sql; $this->variables = array_merge($this->variables, $querylet->variables); } - - public function append_sql(string $sql): void - { - $this->sql .= $sql; - } - - public function add_variable($var): void - { - $this->variables[] = $var; - } } class TagCondition @@ -51,9 +45,13 @@ class ImgCondition class Search { + /** @var list */ public static array $_search_path = []; - private static function find_images_internal(int $start = 0, ?int $limit = null, array $tags = []): iterable + /** + * @param list $tags + */ + private static function find_images_internal(int $start = 0, ?int $limit = null, array $tags = []): \FFSPHP\PDOStatement { global $database, $user; @@ -78,7 +76,7 @@ class Search /** * Search for an array of images * - * @param string[] $tags + * @param list $tags * @return Image[] */ #[Query(name: "posts", type: "[Post!]!", args: ["tags" => "[string!]"])] @@ -95,6 +93,9 @@ class Search /** * Search for an array of images, returning a iterable object of Image + * + * @param list $tags + * @return \Generator */ public static function find_images_iterable(int $start = 0, ?int $limit = null, array $tags = []): \Generator { @@ -186,6 +187,9 @@ class Search } + /** + * @return list + */ private static function tag_or_wildcard_to_ids(string $tag): array { global $database; @@ -406,7 +410,7 @@ class Search $img_sql .= " (" . $iq->qlet->sql . ")"; $img_vars = array_merge($img_vars, $iq->qlet->variables); } - $query->append_sql(" AND "); + $query->append(new Querylet(" AND ")); $query->append(new Querylet($img_sql, $img_vars)); } diff --git a/core/polyfills.php b/core/polyfills.php index adcc8391..34420c23 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -804,6 +804,12 @@ function stringer(mixed $s): string /** * If a value is in the cache, return it; otherwise, call the callback * to generate it and store it in the cache. + * + * @template T + * @param string $key + * @param callable():T $callback + * @param int|null $ttl + * @return T */ function cache_get_or_set(string $key, callable $callback, ?int $ttl = null): mixed { diff --git a/ext/danbooru_api/main.php b/ext/danbooru_api/main.php index d29a3eee..fb82d6f7 100644 --- a/ext/danbooru_api/main.php +++ b/ext/danbooru_api/main.php @@ -6,18 +6,30 @@ namespace Shimmie2; use MicroHTML\HTMLElement; +/** + * @param mixed[] ...$args + */ function TAGS(...$args): HTMLElement { return new HTMLElement("tags", $args); } +/** + * @param mixed[] ...$args + */ function TAG(...$args): HTMLElement { return new HTMLElement("tag", $args); } +/** + * @param mixed[] ...$args + */ function POSTS(...$args): HTMLElement { return new HTMLElement("posts", $args); } +/** + * @param mixed[] ...$args + */ function POST(...$args): HTMLElement { return new HTMLElement("post", $args); @@ -75,6 +87,7 @@ class DanbooruApi extends Extension $user = $duser; } else { $user = User::by_id($config->get_int("anon_id", 0)); + assert(!is_null($user)); } send_event(new UserLoginEvent($user)); } @@ -299,6 +312,7 @@ class DanbooruApi extends Extension } elseif (isset($_REQUEST['source']) || isset($_REQUEST['post']['source'])) { // A url was provided $source = isset($_REQUEST['source']) ? $_REQUEST['source'] : $_REQUEST['post']['source']; $file = tempnam(sys_get_temp_dir(), "shimmie_transload"); + assert($file !== false); $ok = fetch_url($source, $file); if (!$ok) { $page->set_code(409); @@ -317,6 +331,7 @@ class DanbooruApi extends Extension // Was an md5 supplied? Does it match the file hash? $hash = md5_file($file); + assert($hash !== false); if (isset($_REQUEST['md5']) && strtolower($_REQUEST['md5']) != $hash) { $page->set_code(409); $page->add_http_header("X-Danbooru-Errors: md5 mismatch");