From 802ff336a25c1415cc3486572e6b931ffc49a888 Mon Sep 17 00:00:00 2001 From: Shish Date: Mon, 15 Jan 2024 13:40:18 +0000 Subject: [PATCH] [core,tests] even more types --- core/extension.php | 2 +- core/imageboard/search.php | 3 --- core/imageboard/tag.php | 4 ---- core/user.php | 10 +++------- core/util.php | 2 +- ext/artists/main.php | 2 +- ext/autocomplete/main.php | 4 ---- ext/danbooru_api/main.php | 9 ++------- ext/graphql/main.php | 4 +--- ext/ouroboros_api/main.php | 4 ++-- ext/pools/main.php | 8 ++++---- ext/rotate/main.php | 3 --- ext/statsd/main.php | 14 +++++++------- ext/tag_list/main.php | 2 +- ext/upload/main.php | 3 --- tests/phpstan.neon | 8 ++++++++ 16 files changed, 31 insertions(+), 51 deletions(-) diff --git a/core/extension.php b/core/extension.php index c6ebdb7b..ce82f4dc 100644 --- a/core/extension.php +++ b/core/extension.php @@ -82,7 +82,7 @@ abstract class Extension } } - public static function is_enabled(string $key): ?bool + public static function is_enabled(string $key): bool { return in_array($key, self::$enabled_extensions); } diff --git a/core/imageboard/search.php b/core/imageboard/search.php index 330f0317..7e153fe8 100644 --- a/core/imageboard/search.php +++ b/core/imageboard/search.php @@ -182,9 +182,6 @@ class Search } } } - if (is_null($total)) { - return 0; - } return $total; } diff --git a/core/imageboard/tag.php b/core/imageboard/tag.php index 096a3adc..9d7de0c4 100644 --- a/core/imageboard/tag.php +++ b/core/imageboard/tag.php @@ -30,10 +30,6 @@ class TagUsage { global $cache, $database; - if (!$search) { - return []; - } - $search = strtolower($search); if ( $search == '' || diff --git a/core/user.php b/core/user.php index 8dc8c130..127ca881 100644 --- a/core/user.php +++ b/core/user.php @@ -202,13 +202,9 @@ class User { global $database; $hash = password_hash($password, PASSWORD_BCRYPT); - if (is_string($hash)) { - $this->passhash = $hash; - $database->execute("UPDATE users SET pass=:hash WHERE id=:id", ["hash" => $this->passhash, "id" => $this->id]); - log_info("core-user", 'Set password for '.$this->name); - } else { - throw new SCoreException("Failed to hash password"); - } + $this->passhash = $hash; + $database->execute("UPDATE users SET pass=:hash WHERE id=:id", ["hash" => $this->passhash, "id" => $this->id]); + log_info("core-user", 'Set password for '.$this->name); } public function set_email(string $address): void diff --git a/core/util.php b/core/util.php index 84e7555f..6507a6ba 100644 --- a/core/util.php +++ b/core/util.php @@ -615,7 +615,7 @@ function _set_up_shimmie_environment(): void // The trace system has a certain amount of memory consumption every time it is used, // so to prevent running out of memory during complex operations code that uses it should // check if tracer output is enabled before making use of it. - $tracer_enabled = constant('TRACE_FILE') !== null; + $tracer_enabled = !is_null('TRACE_FILE'); } diff --git a/ext/artists/main.php b/ext/artists/main.php index f286c2bd..00311720 100644 --- a/ext/artists/main.php +++ b/ext/artists/main.php @@ -605,7 +605,7 @@ class Artists extends Extension $i = 0; $urlsAsString = str_replace("\r\n", "\n", $urlsAsString); $urlsAsString = str_replace("\n\r", "\n", $urlsAsString); - $urlsAsArray = is_null($urlsAsString) ? [] : explode("\n", $urlsAsString); + $urlsAsArray = empty($urlsAsString) ? [] : explode("\n", $urlsAsString); $urlsIDsAsArray = is_null($urlsIDsAsString) ? [] : explode(" ", $urlsIDsAsString); while ($i < count($urlsAsArray)) { // if an URL was updated diff --git a/ext/autocomplete/main.php b/ext/autocomplete/main.php index 521e0b8e..b6ddbbec 100644 --- a/ext/autocomplete/main.php +++ b/ext/autocomplete/main.php @@ -31,10 +31,6 @@ class AutoComplete extends Extension { global $cache, $database; - if (!$search) { - return []; - } - $search = strtolower($search); if ( $search == '' || diff --git a/ext/danbooru_api/main.php b/ext/danbooru_api/main.php index 8e500878..e950ee2a 100644 --- a/ext/danbooru_api/main.php +++ b/ext/danbooru_api/main.php @@ -265,7 +265,6 @@ class DanbooruApi extends Extension private function api_add_post(): void { global $database, $user, $page; - $danboorup_kludge = 1; // danboorup for firefox makes broken links out of location: /path // Check first if a login was supplied, if it wasn't check if the user is logged in via cookie // If all that fails, it's an anonymous upload @@ -331,9 +330,7 @@ class DanbooruApi extends Extension $page->set_code(409); $page->add_http_header("X-Danbooru-Errors: duplicate"); $existinglink = make_link("post/view/" . $existing->id); - if ($danboorup_kludge) { - $existinglink = make_http($existinglink); - } + $existinglink = make_http($existinglink); $page->add_http_header("X-Danbooru-Location: $existinglink"); return; } @@ -356,9 +353,7 @@ class DanbooruApi extends Extension }); $newid = make_link("post/view/" . $newimg->id); - if ($danboorup_kludge) { - $newid = make_http($newid); - } + $newid = make_http($newid); // Did we POST or GET this call? if ($_SERVER['REQUEST_METHOD'] == 'POST') { diff --git a/ext/graphql/main.php b/ext/graphql/main.php index 4dc88f61..66212258 100644 --- a/ext/graphql/main.php +++ b/ext/graphql/main.php @@ -146,9 +146,7 @@ class GraphQL extends Extension private static function handle_upload(int $n, string $common_tags, string $common_source): array { if (!empty($_POST["url$n"])) { - return ["error" => "URLs not handled yet"]; - $tmpname = "..."; - $filename = "..."; + throw new \Exception("URLs not handled yet"); } else { $ec = $_FILES["data$n"]["error"]; switch($ec) { diff --git a/ext/ouroboros_api/main.php b/ext/ouroboros_api/main.php index 1b751d60..7827e658 100644 --- a/ext/ouroboros_api/main.php +++ b/ext/ouroboros_api/main.php @@ -331,7 +331,7 @@ class OuroborosAPI extends Extension } } $meta = []; - $meta['tags'] = is_array($post->tags) ? $post->tags : Tag::explode($post->tags); + $meta['tags'] = Tag::explode($post->tags); $meta['source'] = $post->source; if (Extension::is_enabled(RatingsInfo::KEY) !== false) { $meta['rating'] = $post->rating; @@ -365,7 +365,7 @@ class OuroborosAPI extends Extension if (!is_null($img)) { $handler = $config->get_string(ImageConfig::UPLOAD_COLLISION_HANDLER); if ($handler == ImageConfig::COLLISION_MERGE) { - $postTags = is_array($post->tags) ? $post->tags : Tag::explode($post->tags); + $postTags = Tag::explode($post->tags); $merged = array_merge($postTags, $img->get_tag_array()); send_event(new TagSetEvent($img, $merged)); diff --git a/ext/pools/main.php b/ext/pools/main.php index 662f0136..66744611 100644 --- a/ext/pools/main.php +++ b/ext/pools/main.php @@ -737,19 +737,19 @@ class Pools extends Extension return; } - $images = " "; + $images = []; foreach ($event->posts as $post_id) { if ($this->add_post($event->pool_id, $post_id, false)) { - $images .= " " . $post_id; + $images[] = $post_id; } } - if (!strlen($images) == 0) { + if (count($images) > 0) { $count = (int)$database->get_one( "SELECT COUNT(*) FROM pool_images WHERE pool_id=:pid", ["pid" => $event->pool_id] ); - $this->add_history($event->pool_id, 1, $images, $count); + $this->add_history($event->pool_id, 1, implode(" ", $images), $count); } } diff --git a/ext/rotate/main.php b/ext/rotate/main.php index 4073a4f2..1a0c1e01 100644 --- a/ext/rotate/main.php +++ b/ext/rotate/main.php @@ -104,9 +104,6 @@ class RotateImage extends Extension $image_obj = Image::by_id($image_id); $hash = $image_obj->hash; - if (is_null($hash)) { - throw new ImageRotateException("Post does not have a hash associated with it."); - } $image_filename = warehouse_path(Image::IMAGE_DIR, $hash); if (file_exists($image_filename) === false) { diff --git a/ext/statsd/main.php b/ext/statsd/main.php index 876284fa..d0848e6f 100644 --- a/ext/statsd/main.php +++ b/ext/statsd/main.php @@ -45,7 +45,11 @@ class StatsDInterface extends Extension $this->_stats("other"); } - $this->send(StatsDInterface::$stats, 1.0); + // @phpstan-ignore-next-line + if (STATSD_HOST) { + $this->send(STATSD_HOST, StatsDInterface::$stats, 1.0); + } + StatsDInterface::$stats = []; } @@ -74,12 +78,8 @@ class StatsDInterface extends Extension return 99; } - private function send(array $data, float $sampleRate = 1) + private function send(string $host, array $data, float $sampleRate = 1) { - if (!STATSD_HOST) { - return; - } - // sampling $sampledData = []; @@ -99,7 +99,7 @@ class StatsDInterface extends Extension // Wrap this in a try/catch - failures in any of this should be silently ignored try { - $parts = explode(":", STATSD_HOST); + $parts = explode(":", $host); $host = $parts[0]; $port = (int)$parts[1]; $fp = fsockopen("udp://$host", $port, $errno, $errstr); diff --git a/ext/tag_list/main.php b/ext/tag_list/main.php index 8e887d9a..f31e4e9c 100644 --- a/ext/tag_list/main.php +++ b/ext/tag_list/main.php @@ -160,7 +160,7 @@ class TagList extends Extension $results = $cache->get("tag_list_omitted_tags:".$tags_config); if (is_null($results)) { - $tags = explode(" ", $tags_config); + $tags = Tag::explode($tags_config, false); if (count($tags) == 0) { return []; diff --git a/ext/upload/main.php b/ext/upload/main.php index b42d0988..5fb59287 100644 --- a/ext/upload/main.php +++ b/ext/upload/main.php @@ -407,9 +407,6 @@ class Upload extends Extension if (count($event->images) == 0) { throw new UploadException("File type not supported: " . $event->mime); } - if (count($event->images) == 0) { - throw new UploadException("File type not supported: " . $event->mime); - } return $event->images; }); foreach($new_images as $image) { diff --git a/tests/phpstan.neon b/tests/phpstan.neon index 3106f728..100af3fd 100644 --- a/tests/phpstan.neon +++ b/tests/phpstan.neon @@ -8,3 +8,11 @@ parameters: - ../themes ignoreErrors: - '#Access to an undefined property Shimmie2\\Image::\$#' + dynamicConstantNames: + - DEBUG + - SPEED_HAX + - TRUSTED_PROXIES + - TIMEZONE + - BASE_HREF + - STATSD_HOST + - TRACE_FILE