From f9c873003e0bcbe3fd376763dfdf14bea830020a Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Tue, 9 Jan 2024 12:06:28 -0600 Subject: [PATCH] Type hinting fixes --- config/phpstan/phpstan.neon | 4 ++-- lib/Artwork.php | 9 ++++++--- lib/CoreFunctions.php | 6 +++--- www/artworks/post.php | 2 +- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/config/phpstan/phpstan.neon b/config/phpstan/phpstan.neon index 2a15c4cd..50227612 100644 --- a/config/phpstan/phpstan.neon +++ b/config/phpstan/phpstan.neon @@ -11,8 +11,8 @@ parameters: # Ignore errors caused by no type hints on class properties, as that's not available till PHP 7.4 - '#Property .+? has no type specified.#' - # Ignore errors caused by type hints that should be union types. Union types are not yet supported in PHP. - - '#Function vd(s|d)?\(\) has parameter \$var with no type specified.#' + # Ignore setters for properties that accept several types + - '#Property Artwork::\$Tags \(array\) does not accept.+#' level: 8 paths: diff --git a/lib/Artwork.php b/lib/Artwork.php index b2f4ad58..30121cbf 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -21,7 +21,7 @@ use function Safe\preg_replace; * @property User $Submitter * @property User $Reviewer * @property ?ImageMimeType $MimeType - * @property array $_Tags + * @property ?array $_Tags */ class Artwork extends PropertiesBase{ public ?string $Name = null; @@ -60,6 +60,9 @@ class Artwork extends PropertiesBase{ // SETTERS // ******* + /** + * @param string|null|array $tags + */ protected function SetTags(string|array|null $tags): void{ if($tags === null || is_array($tags)){ $this->_Tags = $tags; @@ -289,14 +292,14 @@ class Artwork extends PropertiesBase{ $error->Add(new Exceptions\MissingEbookException()); } - if($this->Tags === null || count($this->_Tags) == 0){ + if(count($this->Tags) == 0){ // In-use artwork doesn't have user-provided tags. if($this->Status !== COVER_ARTWORK_STATUS_IN_USE){ $error->Add(new Exceptions\TagsRequiredException()); } } - if($this->Tags !== null && count($this->_Tags) > COVER_ARTWORK_MAX_TAGS){ + if(count($this->Tags) > COVER_ARTWORK_MAX_TAGS){ $error->Add(new Exceptions\TooManyTagsException()); } diff --git a/lib/CoreFunctions.php b/lib/CoreFunctions.php index 2d6dd1df..53c3367c 100644 --- a/lib/CoreFunctions.php +++ b/lib/CoreFunctions.php @@ -6,18 +6,18 @@ use function Safe\ob_end_clean; use function Safe\ob_start; // Convenience alias of var_dump. -function vd($var): void{ +function vd(mixed $var): void{ var_dump($var); } // var_dump($var) then die(). -function vdd($var): void{ +function vdd(mixed $var): void{ var_dump($var); die(); } // var_dump into a string. -function vds($var): string{ +function vds(mixed $var): string{ ob_start(); var_dump($var); $str = ob_get_contents(); diff --git a/www/artworks/post.php b/www/artworks/post.php index 9b7d521b..c0943355 100644 --- a/www/artworks/post.php +++ b/www/artworks/post.php @@ -30,7 +30,7 @@ try{ $artwork->Name = HttpInput::Str(POST, 'artwork-name', false); $artwork->CompletedYear = HttpInput::Int(POST, 'artwork-year'); $artwork->CompletedYearIsCirca = HttpInput::Bool(POST, 'artwork-year-is-circa', false); - $artwork->Tags = HttpInput::Str(POST, 'artwork-tags', false); + $artwork->Tags = HttpInput::Str(POST, 'artwork-tags', false) ?? []; $artwork->Status = HttpInput::Str(POST, 'artwork-status', false, COVER_ARTWORK_STATUS_UNVERIFIED); $artwork->EbookWwwFilesystemPath = HttpInput::Str(POST, 'artwork-ebook-www-filesystem-path', false); $artwork->SubmitterUserId = $GLOBALS['User']->UserId ?? null;