[core] fix more warnings

This commit is contained in:
Shish 2024-01-15 20:34:53 +00:00
parent c1bc63e86b
commit d75b410075
5 changed files with 26 additions and 57 deletions

View file

@ -70,11 +70,11 @@ class BaseThemelet
} }
$custom_classes = ""; $custom_classes = "";
if (class_exists("Shimmie2\Relationships")) { if (Extension::is_enabled(RelationshipsInfo::KEY)) {
if (property_exists($image, 'parent_id') && $image->parent_id !== null) { if ($image['parent_id'] !== null) {
$custom_classes .= "shm-thumb-has_parent "; $custom_classes .= "shm-thumb-has_parent ";
} }
if (property_exists($image, 'has_children') && bool_escape($image->has_children)) { if ($image['has_children']) {
$custom_classes .= "shm-thumb-has_child "; $custom_classes .= "shm-thumb-has_child ";
} }
} }

View file

@ -365,7 +365,8 @@ class Database
$this->get_all("SELECT name FROM sqlite_master WHERE type = 'table'") $this->get_all("SELECT name FROM sqlite_master WHERE type = 'table'")
); );
} else { } else {
throw new SCoreException("Can't count tables for database type {$this->get_engine()->id}"); $did = (string)$this->get_engine()->id;
throw new SCoreException("Can't count tables for database type {$did}");
} }
} }

View file

@ -121,7 +121,7 @@ class PageRequestEvent extends Event
{ {
if ($this->count_args() > $n) { if ($this->count_args() > $n) {
$i = $this->get_arg($n); $i = $this->get_arg($n);
if (is_numeric($i) && int_escape($i) > 0) { if (is_numberish($i) && int_escape($i) > 0) {
return page_number($i, $max); return page_number($i, $max);
} else { } else {
return 0; return 0;

View file

@ -15,23 +15,6 @@ enum ImagePropType
case STRING; case STRING;
} }
function pop_val(array &$row, string $name): mixed
{
$value = $row[$name];
unset($row[$name]);
return $value;
}
function pop_int(array &$row, string $name): ?int
{
$val = pop_val($row, $name);
return (is_null($val)) ? null : (int)$val;
}
function pop_bool(array &$row, string $name): ?bool
{
$val = pop_val($row, $name);
return (is_null($val)) ? null : bool_escape($val);
}
/** /**
* Class Image * Class Image
* *
@ -92,42 +75,22 @@ class Image implements \ArrayAccess
public function __construct(?array $row = null) public function __construct(?array $row = null)
{ {
if (!is_null($row)) { if (!is_null($row)) {
// some databases return both key=>value and numeric indices,
// we only want the key=>value ones
foreach ($row as $name => $value) { foreach ($row as $name => $value) {
// some databases return both key=>value and numeric indices,
// we only want the key=>value ones
if (is_numeric($name)) { if (is_numeric($name)) {
unset($row[$name]); continue;
} } elseif(property_exists($this, $name)) {
} $t = (new \ReflectionProperty($this, $name))->getType();
if(is_a($t, \ReflectionNamedType::class)) {
// pull out known fields $this->$name = match($t->getName()) {
$this->id = pop_int($row, 'id'); "int" => is_null($value) ? $value : int_escape((string)$value),
$this->hash = pop_val($row, 'hash'); "bool" => is_null($value) ? $value : bool_escape((string)$value),
$this->filename = pop_val($row, 'filename'); "string" => (string)$value,
$this->filesize = pop_int($row, 'filesize'); default => $value,
$this->mime = pop_val($row, 'mime'); };
$this->ext = pop_val($row, 'ext'); }
$this->posted = pop_val($row, 'posted'); } elseif(array_key_exists($name, static::$prop_types)) {
$this->source = pop_val($row, 'source');
$this->owner_id = pop_int($row, 'owner_id');
$this->owner_ip = pop_val($row, 'owner_ip');
$this->locked = pop_bool($row, 'locked');
$this->lossless = pop_bool($row, 'lossless');
$this->video = pop_bool($row, 'video');
$this->video_codec = pop_val($row, 'video_codec');
$this->image = pop_bool($row, 'image');
$this->audio = pop_bool($row, 'audio');
$this->height = pop_int($row, 'height');
$this->width = pop_int($row, 'width');
$this->length = pop_int($row, 'length');
// Any remaining fields are dynamic properties
foreach ($row as $name => $value) {
if(property_exists($this, $name)) {
throw new \Exception("Property $name already exists on Image");
}
if(array_key_exists($name, static::$prop_types)) {
if (is_null($value)) { if (is_null($value)) {
$value = null; $value = null;
} else { } else {
@ -204,7 +167,7 @@ class Image implements \ArrayAccess
public static function by_id_or_hash(string $id): ?Image public static function by_id_or_hash(string $id): ?Image
{ {
return (is_numeric($id) && strlen($id) != 32) ? Image::by_id((int)$id) : Image::by_hash($id); return (is_numberish($id) && strlen($id) != 32) ? Image::by_id((int)$id) : Image::by_hash($id);
} }
public static function by_random(array $tags = [], int $limit_range = 0): ?Image public static function by_random(array $tags = [], int $limit_range = 0): ?Image

View file

@ -433,6 +433,11 @@ function page_number(string $input, ?int $max = null): int
return $pageNumber; return $pageNumber;
} }
function is_numberish(string $s): bool
{
return is_numeric($s);
}
function clamp(int $val, ?int $min = null, ?int $max = null): int function clamp(int $val, ?int $min = null, ?int $max = null): int
{ {
if (!is_null($min) && $val < $min) { if (!is_null($min) && $val < $min) {