Skip to content

Commit

Permalink
[dev] bump phpstan strictness
Browse files Browse the repository at this point in the history
no more null surprises
  • Loading branch information
shish committed Aug 31, 2024
1 parent 845c8b3 commit 399a56a
Show file tree
Hide file tree
Showing 23 changed files with 83 additions and 65 deletions.
4 changes: 2 additions & 2 deletions core/basepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ public function add_block(Block $block): void
* Find a block which contains the given text
* (Useful for unit tests)
*/
public function find_block(string $text): ?Block
public function find_block(string $text): Block
{
foreach ($this->blocks as $block) {
if ($block->header == $text) {
return $block;
}
}
return null;
throw new \Exception("Block not found: $text");
}

// ==============================================
Expand Down
1 change: 0 additions & 1 deletion core/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function __construct(string $header = null, string|\MicroHTML\HTMLElement
$id = (empty($header) ? md5($this->body ?? '') : $header) . $section;
}
$str_id = preg_replace_ex('/[^\w-]/', '', str_replace(' ', '_', $id));
assert(is_string($str_id));
$this->id = $str_id;
}

Expand Down
2 changes: 1 addition & 1 deletion ext/graphql/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testQuery(): void
{
$this->log_in_as_user();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "test");
$image = Image::by_id($image_id);
$image = Image::by_id_ex($image_id);

$result = $this->graphql('{
posts(limit: 3, offset: 0) {
Expand Down
4 changes: 2 additions & 2 deletions ext/image_hash_ban/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public function onPageRequest(PageRequestEvent $event): void

if ($event->page_matches("image_hash_ban/add", method: "POST", permission: Permissions::BAN_IMAGE)) {
$input = validate_input(["c_hash" => "optional,string", "c_reason" => "string", "c_image_id" => "optional,int"]);
$image = isset($input['c_image_id']) ? Image::by_id($input['c_image_id']) : null;
$hash = isset($input["c_hash"]) ? $input["c_hash"] : $image->hash;
$image = isset($input['c_image_id']) ? Image::by_id_ex($input['c_image_id']) : null;
$hash = isset($input["c_hash"]) ? $input["c_hash"] : ($image ? $image->hash : null);
$reason = isset($input['c_reason']) ? $input['c_reason'] : "DNP";

if ($hash) {
Expand Down
1 change: 1 addition & 0 deletions ext/index/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ public function onSearchTermParse(SearchTermParseEvent $event): void

// If we've reached this far, and nobody else has done anything with this term, then treat it as a tag
if ($event->order === null && $event->img_conditions == [] && $event->tag_conditions == []) {
assert(is_string($event->term));
$event->add_tag_condition(new TagCondition($event->term, $event->positive));
}
}
Expand Down
4 changes: 2 additions & 2 deletions ext/ipban/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testIPBan(): void
$page = $this->get_page('ip_ban/list');
$this->assertStringContainsString(
"42.42.42.42",
$page->find_block("Edit IP Bans")->body
$page->find_block("Edit IP Bans")->body ?? ""
);

// Delete ban
Expand All @@ -48,7 +48,7 @@ public function testIPBan(): void
$page = $this->get_page('ip_ban/list');
$this->assertStringNotContainsString(
"42.42.42.42",
$page->find_block("Edit IP Bans")->body
$page->find_block("Edit IP Bans")->body ?? ""
);
}

Expand Down
2 changes: 1 addition & 1 deletion ext/link_image/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function links_block(Page $page, array $data): void
));
}

protected function url(string $url, string $content, string $type): string
protected function url(string $url, ?string $content, string $type): string
{
if (empty($content)) {
$content = $url;
Expand Down
3 changes: 3 additions & 0 deletions ext/log_db/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,11 @@ public function display(array $row): HTMLElement
protected function scan_entities(string $line): string
{
$line = preg_replace_callback("/Image #(\d+)/s", [$this, "link_image"], $line);
assert(is_string($line));
$line = preg_replace_callback("/Post #(\d+)/s", [$this, "link_image"], $line);
assert(is_string($line));
$line = preg_replace_callback("/>>(\d+)/s", [$this, "link_image"], $line);
assert(is_string($line));
return $line;
}

Expand Down
40 changes: 17 additions & 23 deletions ext/ouroboros_api/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ public function __construct(array $tag)

class OuroborosAPI extends Extension
{
private ?PageRequestEvent $event;
private ?string $type;

public const HEADER_HTTP_200 = 'OK';
Expand Down Expand Up @@ -246,7 +245,6 @@ public function onPageRequest(PageRequestEvent $event): void
global $page, $user;

if (preg_match("%\.(xml|json)$%", implode('/', $event->args), $matches) === 1) {
$this->event = $event;
$this->type = $matches[1];
if ($this->type == 'json') {
$page->set_mime('application/json; charset=utf-8');
Expand All @@ -257,21 +255,21 @@ public function onPageRequest(PageRequestEvent $event): void
$this->tryAuth();

if ($event->page_matches('post')) {
if ($this->match('create')) {
if ($this->match($event, 'create')) {
// Create
if ($user->can(Permissions::CREATE_IMAGE)) {
$md5 = !empty($_REQUEST['md5']) ? filter_var_ex($_REQUEST['md5'], FILTER_SANITIZE_STRING) : null;
$this->postCreate(new OuroborosPost($_REQUEST['post']), $md5);
} else {
$this->sendResponse(403, 'You cannot create new posts');
}
} elseif ($this->match('update')) {
} elseif ($this->match($event, 'update')) {
throw new ServerError("update not implemented");
} elseif ($this->match('show')) {
} elseif ($this->match($event, 'show')) {
// Show
$id = !empty($_REQUEST['id']) ? (int)filter_var_ex($_REQUEST['id'], FILTER_SANITIZE_NUMBER_INT) : null;
$this->postShow($id);
} elseif ($this->match('index') || $this->match('list')) {
} elseif ($this->match($event, 'index') || $this->match($event, 'list')) {
// List
$limit = !empty($_REQUEST['limit']) ? intval(
filter_var_ex($_REQUEST['limit'], FILTER_SANITIZE_NUMBER_INT)
Expand All @@ -286,7 +284,7 @@ public function onPageRequest(PageRequestEvent $event): void
$this->postIndex($limit, $p, $tags);
}
} elseif ($event->page_matches('tag')) {
if ($this->match('index') || $this->match('list')) {
if ($this->match($event, 'index') || $this->match($event, 'list')) {
$limit = !empty($_REQUEST['limit']) ? intval(
filter_var_ex($_REQUEST['limit'], FILTER_SANITIZE_NUMBER_INT)
) : 50;
Expand All @@ -297,18 +295,12 @@ public function onPageRequest(PageRequestEvent $event): void
$_REQUEST['order'],
FILTER_SANITIZE_STRING
) : 'date';
$id = !empty($_REQUEST['id']) ? intval(
filter_var_ex($_REQUEST['id'], FILTER_SANITIZE_NUMBER_INT)
) : null;
$after_id = !empty($_REQUEST['after_id']) ? intval(
filter_var_ex($_REQUEST['after_id'], FILTER_SANITIZE_NUMBER_INT)
) : null;
$name = !empty($_REQUEST['name']) ? filter_var_ex($_REQUEST['name'], FILTER_SANITIZE_STRING) : '';
$name_pattern = !empty($_REQUEST['name_pattern']) ? filter_var_ex(
$_REQUEST['name_pattern'],
FILTER_SANITIZE_STRING
) : '';
$this->tagIndex($limit, $p, $order, $id, $after_id, $name, $name_pattern);
$this->tagIndex($limit, $p, $order, $name, $name_pattern);
}
}
} elseif ($event->page_matches('post/show')) {
Expand Down Expand Up @@ -337,17 +329,18 @@ protected function postCreate(OuroborosPost $post, ?string $md5 = ''): void
return;
}
}
/** @var array<string, string> $meta */
$meta = [];
$meta['tags'] = $post->tags;
$meta['source'] = $post->source;
$meta['source'] = $post->source ?? '';
if (Extension::is_enabled(RatingsInfo::KEY) !== false) {
$meta['rating'] = $post->rating;
}
// Check where we should try for the file
if (empty($post->file) && !empty($post->file_url) && filter_var_ex(
$post->file_url,
FILTER_VALIDATE_URL
) !== false
if (
empty($post->file) &&
!empty($post->file_url) &&
filter_var_ex($post->file_url, FILTER_VALIDATE_URL) !== false
) {
// Transload from source
$meta['file'] = shm_tempnam('transload_' . $config->get_string(UploadConfig::TRANSLOAD_ENGINE));
Expand All @@ -361,6 +354,7 @@ protected function postCreate(OuroborosPost $post, ?string $md5 = ''): void
$meta['hash'] = \Safe\md5_file($meta['file']);
} else {
// Use file
assert(!is_null($post->file));
$meta['file'] = $post->file['tmp_name'];
$meta['filename'] = $post->file['name'];
$meta['hash'] = \Safe\md5_file($meta['file']);
Expand All @@ -379,7 +373,7 @@ protected function postCreate(OuroborosPost $post, ?string $md5 = ''): void
send_event(new TagSetEvent($img, $merged));

// This is really the only thing besides tags we should care
if (isset($meta['source'])) {
if (!empty($meta['source'])) {
send_event(new SourceSetEvent($img, $meta['source']));
}
$this->sendResponse(200, self::OK_POST_CREATE_UPDATE . ' ID: ' . $img->id);
Expand Down Expand Up @@ -437,7 +431,7 @@ protected function postIndex(int $limit, int $page, array $tags): void
* Tag
*/

protected function tagIndex(int $limit, int $page, string $order, int $id, int $after_id, string $name, string $name_pattern): void
protected function tagIndex(int $limit, int $page, string $order, string $name, string $name_pattern): void
{
global $database, $config;
$start = ($page - 1) * $limit;
Expand Down Expand Up @@ -624,8 +618,8 @@ private function tryAuth(): void
/**
* Helper for matching API methods from event
*/
private function match(string $page): bool
private function match(PageRequestEvent $event, string $page): bool
{
return (preg_match("%{$page}\.(xml|json)$%", implode('/', $this->event->args), $matches) === 1);
return (preg_match("%{$page}\.(xml|json)$%", implode('/', $event->args), $matches) === 1);
}
}
4 changes: 3 additions & 1 deletion ext/pools/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ public function onPageRequest(PageRequestEvent $event): void
WHERE pool_id=:pid AND i.id=:iid",
["pid" => $pool_id, "iid" => (int) $row['image_id']]
);
$images[] = ($image ? new Image($image) : null);
if ($image) {
$images[] = new Image($image);
}
}

$this->theme->edit_order($page, $pool, $images);
Expand Down
4 changes: 2 additions & 2 deletions ext/pools/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function list_pools(Page $page, array $pools, string $search, int $pageNu
$pool_rows = [];
foreach ($pools as $pool) {
$pool_link = SHM_A("pool/view/" . $pool->id, $pool->title);
$user_link = SHM_A("user/" . url_escape($pool->user_name), $pool->user_name);
$user_link = SHM_A("user/" . url_escape($pool->user_name), $pool->user_name ?? "No Name");

$pool_rows[] = TR(
TD(["class" => "left"], $pool_link),
Expand All @@ -75,7 +75,7 @@ public function list_pools(Page $page, array $pools, string $search, int $pageNu
);

$order_arr = ['created' => 'Recently created', 'updated' => 'Last updated', 'name' => 'Name', 'count' => 'Post Count'];
$order_selected = $page->get_cookie('ui-order-pool');
$order_selected = $page->get_cookie('ui-order-pool') ?? "";
$order_sel = SHM_SELECT("order_pool", $order_arr, selected_options: [$order_selected], attrs: ["id" => "order_pool"]);

$this->display_top(null, "Pools");
Expand Down
6 changes: 3 additions & 3 deletions ext/post_source/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
class SourceSetEvent extends Event
{
public Image $image;
public ?string $source;
public string $source;

public function __construct(Image $image, string $source = null)
public function __construct(Image $image, string $source)
{
parent::__construct();
$this->image = $image;
Expand Down Expand Up @@ -93,7 +93,7 @@ public function onTagTermCheck(TagTermCheckEvent $event): void
public function onTagTermParse(TagTermParseEvent $event): void
{
if (preg_match("/^source[=|:](.*)$/i", $event->term, $matches)) {
$source = ($matches[1] !== "none" ? $matches[1] : null);
$source = ($matches[1] !== "none" ? $matches[1] : "");
send_event(new SourceSetEvent(Image::by_id_ex($event->image_id), $source));
}
}
Expand Down
6 changes: 3 additions & 3 deletions ext/random_image/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testPostListBlock(): void
# enabled, no image = no text
$config->set_bool("show_random_block", true);
$page = $this->get_page("post/list");
$this->assertNull($page->find_block("Random Post"));
$this->assertException(\Exception::class, function () use ($page) {$page->find_block("Random Post");});

# enabled, image = text
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "test");
Expand All @@ -42,11 +42,11 @@ public function testPostListBlock(): void
# disabled, image = no text
$config->set_bool("show_random_block", false);
$page = $this->get_page("post/list");
$this->assertNull($page->find_block("Random Post"));
$this->assertException(\Exception::class, function () use ($page) {$page->find_block("Random Post");});

# disabled, no image = no image
$this->delete_image($image_id);
$page = $this->get_page("post/list");
$this->assertNull($page->find_block("Random Post"));
$this->assertException(\Exception::class, function () use ($page) {$page->find_block("Random Post");});
}
}
4 changes: 3 additions & 1 deletion ext/rating/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public function testUserConfig(): void
// If user prefers to see all images, going to the safe image
// and clicking next should show the explicit image
$user_config->set_array(RatingsConfig::USER_DEFAULTS, ["s", "q", "e"]);
$this->assertEquals($image_s->get_next()->id, $image_id_e);
$next = $image_s->get_next();
$this->assertNotNull($next);
$this->assertEquals($next->id, $image_id_e);

// If the user prefers to see only safe images by default, then
// going to the safe image and clicking next should not show
Expand Down
2 changes: 1 addition & 1 deletion ext/ratings_blur/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testRatingBlurUserConfig(): void
private function create_test_user(string $username): void
{
$uce = send_event(new UserCreationEvent($username, $username, $username, "$username@test.com", false));
send_event(new UserLoginEvent($uce->user));
send_event(new UserLoginEvent($uce->get_user()));
}

private function delete_test_user(string $username): void
Expand Down
10 changes: 7 additions & 3 deletions ext/s3/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public function onAdminAction(AdminActionEvent $event): void
) as $row) {
if ($row['action'] == "S") {
$image = Image::by_hash($row['hash']);
$this->sync_post($image);
if ($image) {
$this->sync_post($image);
}
} elseif ($row['action'] == "D") {
$this->remove_file($row['hash']);
}
Expand All @@ -87,8 +89,10 @@ public function onCliGen(CliGenEvent $event): void
) as $row) {
if ($row['action'] == "S") {
$image = Image::by_hash($row['hash']);
$output->writeln("SYN {$row['hash']} ($image->id)");
$this->sync_post($image);
if ($image) {
$output->writeln("SYN {$row['hash']} ($image->id)");
$this->sync_post($image);
}
} elseif ($row['action'] == "D") {
$output->writeln("DEL {$row['hash']}");
$this->remove_file($row['hash']);
Expand Down
15 changes: 14 additions & 1 deletion ext/user/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(

class UserCreationEvent extends Event
{
public ?User $user;
private ?User $user;

public function __construct(
public string $username,
Expand All @@ -55,6 +55,19 @@ public function __construct(
) {
parent::__construct();
}

public function set_user(User $user): void
{
$this->user = $user;
}

public function get_user(): User
{
if (is_null($this->user)) {
throw new \Exception("User not created");
}
return $this->user;
}
}

class UserLoginEvent extends Event
Expand Down
Loading

0 comments on commit 399a56a

Please sign in to comment.