diff --git a/core/basepage.php b/core/basepage.php index 37218343..1d7ae666 100644 --- a/core/basepage.php +++ b/core/basepage.php @@ -174,7 +174,7 @@ class BasePage $this->flash[] = $message; } - public function disable_left() + public function disable_left(): void { $this->left_enabled = false; } @@ -480,7 +480,7 @@ class BasePage } /** - * @return array A list of stylesheets relative to the theme root. + * @return string[] A list of stylesheets relative to the theme root. */ protected function get_theme_stylesheets(): array { @@ -489,7 +489,7 @@ class BasePage /** - * @return array A list of script files relative to the theme root. + * @return string[] A list of script files relative to the theme root. */ protected function get_theme_scripts(): array { @@ -546,7 +546,7 @@ class BasePage /** * turns the Page into HTML */ - public function render() + public function render(): void { global $config, $user; @@ -643,7 +643,7 @@ class PageNavBuildingEvent extends Event { public array $links = []; - public function add_nav_link(string $name, Link $link, string $desc, ?bool $active = null, int $order = 50) + public function add_nav_link(string $name, Link $link, string $desc, ?bool $active = null, int $order = 50): void { $this->links[] = new NavLink($name, $link, $desc, $active, $order); } @@ -661,7 +661,7 @@ class PageSubNavBuildingEvent extends Event $this->parent = $parent; } - public function add_nav_link(string $name, Link $link, string|HTMLElement $desc, ?bool $active = null, int $order = 50) + public function add_nav_link(string $name, Link $link, string|HTMLElement $desc, ?bool $active = null, int $order = 50): void { $this->links[] = new NavLink($name, $link, $desc, $active, $order); } diff --git a/core/config.php b/core/config.php index 35f06869..e403435a 100644 --- a/core/config.php +++ b/core/config.php @@ -234,7 +234,7 @@ abstract class BaseConfig implements Config return explode(",", $this->get($name, "")); } - private function get(string $name, $default = null) + private function get(string $name, $default = null): mixed { if (isset($this->values[$name])) { return $this->values[$name]; diff --git a/core/database.php b/core/database.php index 72348dee..4c7c87dc 100644 --- a/core/database.php +++ b/core/database.php @@ -297,7 +297,7 @@ class Database /** * Execute an SQL query and return a single value, or null. */ - public function get_one(string $query, array $args = []) + public function get_one(string $query, array $args = []): mixed { $_start = ftime(); $row = $this->_execute($query, $args)->fetch(); diff --git a/core/dbengine.php b/core/dbengine.php index 9c05c1a4..4467a296 100644 --- a/core/dbengine.php +++ b/core/dbengine.php @@ -16,7 +16,7 @@ abstract class DBEngine { public DatabaseDriverID $id; - public function init(PDO $db) + public function init(PDO $db): void { } @@ -30,7 +30,7 @@ abstract class DBEngine return 'CREATE TABLE '.$name.' ('.$data.')'; } - abstract public function set_timeout(PDO $db, ?int $time); + abstract public function set_timeout(PDO $db, ?int $time): void; abstract public function get_version(PDO $db): string; @@ -41,7 +41,7 @@ class MySQL extends DBEngine { public DatabaseDriverID $id = DatabaseDriverID::MYSQL; - public function init(PDO $db) + public function init(PDO $db): void { $db->exec("SET NAMES utf8;"); } @@ -80,7 +80,7 @@ class PostgreSQL extends DBEngine { public DatabaseDriverID $id = DatabaseDriverID::PGSQL; - public function init(PDO $db) + public function init(PDO $db): void { if (array_key_exists('REMOTE_ADDR', $_SERVER)) { $db->exec("SET application_name TO 'shimmie [{$_SERVER['REMOTE_ADDR']}]';"); @@ -178,7 +178,7 @@ class SQLite extends DBEngine { public DatabaseDriverID $id = DatabaseDriverID::SQLITE; - public function init(PDO $db) + public function init(PDO $db): void { ini_set('sqlite.assoc_case', '0'); $db->exec("PRAGMA foreign_keys = ON;"); diff --git a/core/extension.php b/core/extension.php index ce82f4dc..c7364856 100644 --- a/core/extension.php +++ b/core/extension.php @@ -102,7 +102,7 @@ abstract class Extension return $config->get_int($name, 0); } - protected function set_version(string $name, int $ver) + protected function set_version(string $name, int $ver): void { global $config; $config->set_int($name, $ver); @@ -188,7 +188,7 @@ abstract class ExtensionInfo return Extension::is_enabled($this->key); } - private function check_support() + private function check_support(): void { global $database; $this->support_info = ""; @@ -243,7 +243,7 @@ abstract class ExtensionInfo } } - public static function load_all_extension_info() + public static function load_all_extension_info(): void { foreach (get_subclasses_of("Shimmie2\ExtensionInfo") as $class) { $extension_info = new $class(); diff --git a/core/imageboard/image.php b/core/imageboard/image.php index 706e0e3a..abf3f515 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -208,7 +208,7 @@ class Image } } - public function save_to_db() + public function save_to_db(): void { global $database, $user; $cut_name = substr($this->filename, 0, 255); diff --git a/core/imageboard/misc.php b/core/imageboard/misc.php index 84d92ec9..402c9093 100644 --- a/core/imageboard/misc.php +++ b/core/imageboard/misc.php @@ -133,7 +133,7 @@ function get_thumbnail_max_size_scaled(): array } -function create_image_thumb(Image $image, string $engine = null) +function create_image_thumb(Image $image, string $engine = null): void { global $config; create_scaled_image( @@ -148,8 +148,14 @@ function create_image_thumb(Image $image, string $engine = null) -function create_scaled_image(string $inname, string $outname, array $tsize, string $mime, ?string $engine = null, ?string $resize_type = null) -{ +function create_scaled_image( + string $inname, + string $outname, + array $tsize, + string $mime, + ?string $engine = null, + ?string $resize_type = null +): void { global $config; if (empty($engine)) { $engine = $config->get_string(ImageConfig::THUMB_ENGINE); diff --git a/core/install.php b/core/install.php index 8014bed6..6cee3900 100644 --- a/core/install.php +++ b/core/install.php @@ -20,7 +20,7 @@ namespace Shimmie2; * and other such things that aren't ready yet */ -function install() +function install(): void { date_default_timezone_set('UTC'); @@ -50,7 +50,7 @@ function install() } } -function get_dsn() +function get_dsn(): string { if (getenv("INSTALL_DSN")) { $dsn = getenv("INSTALL_DSN"); @@ -66,7 +66,7 @@ function get_dsn() return $dsn; } -function do_install($dsn) +function do_install(string $dsn): void { try { create_dirs(); @@ -77,7 +77,7 @@ function do_install($dsn) } } -function ask_questions() +function ask_questions(): void { $warnings = []; $errors = []; @@ -187,7 +187,7 @@ EOD } -function create_dirs() +function create_dirs(): void { $data_exists = file_exists("data") || mkdir("data"); $data_writable = $data_exists && (is_writable("data") || chmod("data", 0755)); @@ -204,7 +204,7 @@ function create_dirs() } } -function create_tables(Database $db) +function create_tables(Database $db): void { try { if ($db->count_tables() > 0) { @@ -301,7 +301,7 @@ function create_tables(Database $db) } } -function write_config($dsn) +function write_config(string $dsn): void { $file_content = "<" . "?php\ndefine('DATABASE_DSN', '$dsn');\n"; diff --git a/core/logging.php b/core/logging.php index a558e3c5..0d113724 100644 --- a/core/logging.php +++ b/core/logging.php @@ -31,7 +31,7 @@ const LOGGING_LEVEL_NAMES = [ * When taking action, a log event should be stored by the server * Quite often, both of these happen at once, hence log_*() having $flash */ -function log_msg(string $section, int $priority, string $message, ?string $flash = null) +function log_msg(string $section, int $priority, string $message, ?string $flash = null): void { global $page; send_event(new LogEvent($section, $priority, $message)); @@ -47,23 +47,23 @@ function log_msg(string $section, int $priority, string $message, ?string $flash } // More shorthand ways of logging -function log_debug(string $section, string $message, ?string $flash = null) +function log_debug(string $section, string $message, ?string $flash = null): void { log_msg($section, SCORE_LOG_DEBUG, $message, $flash); } -function log_info(string $section, string $message, ?string $flash = null) +function log_info(string $section, string $message, ?string $flash = null): void { log_msg($section, SCORE_LOG_INFO, $message, $flash); } -function log_warning(string $section, string $message, ?string $flash = null) +function log_warning(string $section, string $message, ?string $flash = null): void { log_msg($section, SCORE_LOG_WARNING, $message, $flash); } -function log_error(string $section, string $message, ?string $flash = null) +function log_error(string $section, string $message, ?string $flash = null): void { log_msg($section, SCORE_LOG_ERROR, $message, $flash); } -function log_critical(string $section, string $message, ?string $flash = null) +function log_critical(string $section, string $message, ?string $flash = null): void { log_msg($section, SCORE_LOG_CRITICAL, $message, $flash); } diff --git a/core/polyfills.php b/core/polyfills.php index c82c53a2..9ad1432b 100644 --- a/core/polyfills.php +++ b/core/polyfills.php @@ -253,6 +253,8 @@ function get_subclasses_of(string $parent): array /** * Like glob, with support for matching very long patterns with braces. + * + * @return string[] */ function zglob(string $pattern): array { @@ -763,7 +765,7 @@ function iterator_map_to_array(callable $callback, \iterator $iter): array return iterator_to_array(iterator_map($callback, $iter)); } -function stringer($s): string +function stringer(mixed $s): string { if (is_array($s)) { if (isset($s[0])) { @@ -798,7 +800,7 @@ function stringer($s): string * If a value is in the cache, return it; otherwise, call the callback * to generate it and store it in the cache. */ -function cache_get_or_set(string $key, callable $callback, ?int $ttl = null) +function cache_get_or_set(string $key, callable $callback, ?int $ttl = null): mixed { global $cache; $value = $cache->get($key); diff --git a/core/sanitize_php.php b/core/sanitize_php.php index fca03915..8fbacdea 100644 --- a/core/sanitize_php.php +++ b/core/sanitize_php.php @@ -9,7 +9,7 @@ namespace Shimmie2; * be included right at the very start of index.php and tests/bootstrap.php */ -function die_nicely($title, $body, $code = 0) +function die_nicely(string $title, string $body, int $code = 0): void { print(" diff --git a/core/sys_config.php b/core/sys_config.php index 1fea1fde..d6b7825b 100644 --- a/core/sys_config.php +++ b/core/sys_config.php @@ -17,7 +17,7 @@ namespace Shimmie2; * define("SPEED_HAX", true); */ -function _d(string $name, $value): void +function _d(string $name, mixed $value): void { if (!defined($name)) { define($name, $value); diff --git a/core/testcase.php b/core/testcase.php index 8995f92d..3bfd47bc 100644 --- a/core/testcase.php +++ b/core/testcase.php @@ -87,8 +87,12 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) { return $args; } - protected static function request($method, $page_name, $get_args = null, $post_args = null): Page - { + protected static function request( + string $method, + string $page_name, + ?array $get_args = null, + ?array $post_args = null + ): Page { // use a fresh page global $page; $get_args = self::check_args($get_args); @@ -108,12 +112,12 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) { return $page; } - protected static function get_page($page_name, $args = null): Page + protected static function get_page(string $page_name, array $args = null): Page { return self::request("GET", $page_name, $args, null); } - protected static function post_page($page_name, $args = null): Page + protected static function post_page(string $page_name, array $args = null): Page { return self::request("POST", $page_name, null, $args); } @@ -125,7 +129,7 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) { $this->assertStringContainsString($title, $page->title); } - protected function assert_title_matches($title): void + protected function assert_title_matches(string $title): void { global $page; $this->assertStringMatchesFormat($title, $page->title); @@ -190,7 +194,11 @@ if(class_exists("\\PHPUnit\\Framework\\TestCase")) { $this->assertStringNotContainsString($content, $page->data); } - protected function assert_search_results($tags, $results): void + /** + * @param string[] $tags + * @param int[] $results + */ + protected function assert_search_results(array $tags, array $results): void { $images = Search::find_images(0, null, $tags); $ids = []; diff --git a/core/tests/SearchTest.php b/core/tests/SearchTest.php index 633610c0..a9b1be28 100644 --- a/core/tests/SearchTest.php +++ b/core/tests/SearchTest.php @@ -44,8 +44,12 @@ class SearchTest extends ShimmiePHPUnitTestCase /******************************************************** * Test turning a string into an abstract query */ - private function assert_TTC(string $tags, array $expected_tag_conditions, array $expected_img_conditions, string $expected_order) - { + private function assert_TTC( + string $tags, + array $expected_tag_conditions, + array $expected_img_conditions, + string $expected_order, + ): void { $class = new \ReflectionClass('\Shimmie2\Search'); $terms_to_conditions = $class->getMethod("terms_to_conditions"); $terms_to_conditions->setAccessible(true); // Use this if you are running PHP older than 8.1.0 @@ -149,7 +153,7 @@ class SearchTest extends ShimmiePHPUnitTestCase int $start = 0, array $res = [], array $path = null, - ) { + ): void { global $database; $tcs = array_map( @@ -192,7 +196,7 @@ class SearchTest extends ShimmiePHPUnitTestCase * No-tag search * * * * * * * * * * * */ #[Depends('testUpload')] - public function testBSQ_NoTags($image_ids): void + public function testBSQ_NoTags(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -206,7 +210,7 @@ class SearchTest extends ShimmiePHPUnitTestCase * Fast-path search * * * * * * * * * * * */ #[Depends('testUpload')] - public function testBSQ_FastPath_NoResults($image_ids): void + public function testBSQ_FastPath_NoResults(array $image_ids): void { $this->testUpload(); $this->assert_BSQ( @@ -217,7 +221,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_FastPath_OneResult($image_ids): void + public function testBSQ_FastPath_OneResult(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -228,7 +232,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_FastPath_ManyResults($image_ids): void + public function testBSQ_FastPath_ManyResults(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -239,7 +243,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_FastPath_WildNoResults($image_ids): void + public function testBSQ_FastPath_WildNoResults(array $image_ids): void { $this->testUpload(); $this->assert_BSQ( @@ -258,7 +262,7 @@ class SearchTest extends ShimmiePHPUnitTestCase * https://github.com/shish/shimmie2/issues/547 */ #[Depends('testUpload')] - public function testBSQ_FastPath_WildOneResult($image_ids): void + public function testBSQ_FastPath_WildOneResult(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -273,7 +277,7 @@ class SearchTest extends ShimmiePHPUnitTestCase * when a wildcard matches one image multiple times. */ #[Depends('testUpload')] - public function testBSQ_FastPath_WildManyResults($image_ids): void + public function testBSQ_FastPath_WildManyResults(array $image_ids): void { $image_ids = $this->testUpload(); // two images match comp* - one matches it once, one matches it twice @@ -288,7 +292,7 @@ class SearchTest extends ShimmiePHPUnitTestCase * General search * * * * * * * * * * * */ #[Depends('testUpload')] - public function testBSQ_GeneralPath_NoResults($image_ids): void + public function testBSQ_GeneralPath_NoResults(array $image_ids): void { $this->testUpload(); # multiple tags, one of which doesn't exist @@ -301,7 +305,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_GeneralPath_OneResult($image_ids): void + public function testBSQ_GeneralPath_OneResult(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -320,7 +324,7 @@ class SearchTest extends ShimmiePHPUnitTestCase * https://github.com/shish/shimmie2/issues/547 */ #[Depends('testUpload')] - public function testBSQ_GeneralPath_WildOneResult($image_ids): void + public function testBSQ_GeneralPath_WildOneResult(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -331,7 +335,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_GeneralPath_ManyResults($image_ids): void + public function testBSQ_GeneralPath_ManyResults(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -342,7 +346,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_GeneralPath_WildManyResults($image_ids): void + public function testBSQ_GeneralPath_WildManyResults(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -353,7 +357,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_GeneralPath_SubtractValidFromResults($image_ids): void + public function testBSQ_GeneralPath_SubtractValidFromResults(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -364,7 +368,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_GeneralPath_SubtractNotValidFromResults($image_ids): void + public function testBSQ_GeneralPath_SubtractNotValidFromResults(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -375,7 +379,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_GeneralPath_SubtractValidFromDefault($image_ids): void + public function testBSQ_GeneralPath_SubtractValidFromDefault(array $image_ids): void { $image_ids = $this->testUpload(); // negative tag alone, should remove the image with that tag @@ -387,7 +391,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_GeneralPath_SubtractNotValidFromDefault($image_ids): void + public function testBSQ_GeneralPath_SubtractNotValidFromDefault(array $image_ids): void { $image_ids = $this->testUpload(); // negative that doesn't exist, should return all results @@ -399,7 +403,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_GeneralPath_SubtractMultipleNotValidFromDefault($image_ids): void + public function testBSQ_GeneralPath_SubtractMultipleNotValidFromDefault(array $image_ids): void { $image_ids = $this->testUpload(); // multiple negative tags that don't exist, should return all results @@ -414,7 +418,7 @@ class SearchTest extends ShimmiePHPUnitTestCase * Meta Search * * * * * * * * * * * */ #[Depends('testUpload')] - public function testBSQ_ImgCond_NoResults($image_ids): void + public function testBSQ_ImgCond_NoResults(array $image_ids): void { $this->testUpload(); $this->assert_BSQ( @@ -430,7 +434,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_ImgCond_OneResult($image_ids): void + public function testBSQ_ImgCond_OneResult(array $image_ids): void { $image_ids = $this->testUpload(); $this->assert_BSQ( @@ -451,7 +455,7 @@ class SearchTest extends ShimmiePHPUnitTestCase } #[Depends('testUpload')] - public function testBSQ_ImgCond_ManyResults($image_ids): void + public function testBSQ_ImgCond_ManyResults(array $image_ids): void { $image_ids = $this->testUpload(); @@ -476,7 +480,7 @@ class SearchTest extends ShimmiePHPUnitTestCase * Mixed * * * * * * * * * * * */ #[Depends('testUpload')] - public function testBSQ_TagCondWithImgCond($image_ids): void + public function testBSQ_TagCondWithImgCond(array $image_ids): void { $image_ids = $this->testUpload(); // multiple tags, many results diff --git a/core/urls.php b/core/urls.php index 845873e9..b715e1d5 100644 --- a/core/urls.php +++ b/core/urls.php @@ -24,6 +24,8 @@ class Link /** * Build a link to a search page for given terms, * with all the appropriate escaping + * + * @param string[] $terms */ function search_link(array $terms = [], int $page = 1): string { @@ -67,12 +69,19 @@ function make_link(?string $page = null, ?string $query = null, ?string $fragmen /** * Take the current URL and modify some parameters + * + * @param array $changes */ function modify_current_url(array $changes): string { return modify_url($_SERVER['REQUEST_URI'], $changes); } +/** + * Take a URL and modify some parameters + * + * @param array $changes + */ function modify_url(string $url, array $changes): string { $parts = parse_url($url); @@ -115,6 +124,8 @@ function make_http(string $link): string /** * If HTTP_REFERER is set, and not blacklisted, then return it * Else return a default $dest + * + * @param string[]|null $blacklist */ function referer_or(string $dest, ?array $blacklist = null): string { diff --git a/core/util.php b/core/util.php index 0e6c81c4..dea379f9 100644 --- a/core/util.php +++ b/core/util.php @@ -536,6 +536,11 @@ function get_debug_info(): string return $debug; } +/** + * Collects some debug information (execution time, memory usage, queries, etc) + * + * @return array + */ function get_debug_info_arr(): array { global $cache, $config, $_shm_event_count, $database, $_shm_load_start; @@ -565,6 +570,9 @@ function get_debug_info_arr(): array * Request initialisation stuff * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +/** + * @param string[] $files + */ function require_all(array $files): void { foreach ($files as $filename) {