diff --git a/application/Utils.php b/application/Utils.php index 073004cf..48d47415 100644 --- a/application/Utils.php +++ b/application/Utils.php @@ -61,6 +61,7 @@ function smallHash($text) */ function startsWith($haystack, $needle, $case = true) { + $needle = $needle ?? ''; if ($case) { return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0); } @@ -180,7 +181,7 @@ function generateLocation($referer, $host, $loopTerms = []) $host = substr($host, 0, $pos); } - $refererHost = parse_url($referer, PHP_URL_HOST); + $refererHost = parse_url($referer, PHP_URL_HOST) ?? ''; if (!empty($referer) && (strpos($refererHost, $host) !== false || startsWith('?', $refererHost))) { $finalReferer = $referer; } @@ -292,7 +293,7 @@ function generate_api_secret($username, $salt) */ function normalize_spaces($string) { - return preg_replace('/\s{2,}/', ' ', trim($string)); + return preg_replace('/\s{2,}/', ' ', trim($string ?? '')); } /** diff --git a/application/api/controllers/HistoryController.php b/application/api/controllers/HistoryController.php index d83a3a25..e16036f6 100644 --- a/application/api/controllers/HistoryController.php +++ b/application/api/controllers/HistoryController.php @@ -30,7 +30,7 @@ class HistoryController extends ApiController $history = $this->history->getHistory(); // Return history operations from the {offset}th, starting from {since}. - $since = \DateTime::createFromFormat(\DateTime::ATOM, $request->getParam('since')); + $since = \DateTime::createFromFormat(\DateTime::ATOM, $request->getParam('since', '')); $offset = $request->getParam('offset'); if (empty($offset)) { $offset = 0; diff --git a/application/bookmark/BookmarkArray.php b/application/bookmark/BookmarkArray.php index b9328116..0c1a6eca 100644 --- a/application/bookmark/BookmarkArray.php +++ b/application/bookmark/BookmarkArray.php @@ -57,7 +57,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * @return int Number of bookmarks */ - public function count() + public function count(): int { return count($this->bookmarks); } @@ -70,7 +70,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * @throws InvalidBookmarkException */ - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { if ( ! $value instanceof Bookmark @@ -106,7 +106,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * @return bool true if it exists, false otherwise */ - public function offsetExists($offset) + public function offsetExists($offset): bool { return array_key_exists($this->getBookmarkOffset($offset), $this->bookmarks); } @@ -116,7 +116,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * @param int $offset Bookmark ID */ - public function offsetUnset($offset) + public function offsetUnset($offset): void { $realOffset = $this->getBookmarkOffset($offset); $url = $this->bookmarks[$realOffset]->getUrl(); @@ -132,7 +132,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * @return Bookmark|null The Bookmark if found, null otherwise */ - public function offsetGet($offset) + public function offsetGet($offset): ?Bookmark { $realOffset = $this->getBookmarkOffset($offset); return isset($this->bookmarks[$realOffset]) ? $this->bookmarks[$realOffset] : null; @@ -143,7 +143,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * @return Bookmark corresponding to the current position */ - public function current() + public function current(): Bookmark { return $this[$this->keys[$this->position]]; } @@ -153,7 +153,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * @return int Bookmark ID corresponding to the current position */ - public function key() + public function key(): int { return $this->keys[$this->position]; } @@ -161,7 +161,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess /** * Iterator - Moves forward to next element */ - public function next() + public function next(): void { ++$this->position; } @@ -171,7 +171,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * Entries are sorted by date (latest first) */ - public function rewind() + public function rewind(): void { $this->keys = array_keys($this->ids); $this->position = 0; @@ -182,7 +182,7 @@ class BookmarkArray implements \Iterator, \Countable, \ArrayAccess * * @return bool true if the current Bookmark ID exists, false otherwise */ - public function valid() + public function valid(): bool { return isset($this->keys[$this->position]); } diff --git a/application/bookmark/LinkUtils.php b/application/bookmark/LinkUtils.php index 8fa2953a..112b6f00 100644 --- a/application/bookmark/LinkUtils.php +++ b/application/bookmark/LinkUtils.php @@ -219,7 +219,7 @@ function tags_str2array(?string $tags, string $separator): array // For whitespaces, we use the special \s regex character $separator = $separator === ' ' ? '\s' : $separator; - return preg_split('/\s*' . $separator . '+\s*/', trim($tags) ?? '', -1, PREG_SPLIT_NO_EMPTY); + return preg_split('/\s*' . $separator . '+\s*/', trim($tags ?? ''), -1, PREG_SPLIT_NO_EMPTY); } /** diff --git a/application/formatter/BookmarkDefaultFormatter.php b/application/formatter/BookmarkDefaultFormatter.php index 91985ee1..70f65818 100644 --- a/application/formatter/BookmarkDefaultFormatter.php +++ b/application/formatter/BookmarkDefaultFormatter.php @@ -120,7 +120,7 @@ class BookmarkDefaultFormatter extends BookmarkFormatter $prefix = rtrim($this->contextData['base_path'], '/') . '/'; } - return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl(), '/')); + return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl() ?? '', '/')); } return escape($bookmark->getUrl()); diff --git a/application/front/controller/admin/ThumbnailsController.php b/application/front/controller/admin/ThumbnailsController.php index 5dfea096..665dfd03 100644 --- a/application/front/controller/admin/ThumbnailsController.php +++ b/application/front/controller/admin/ThumbnailsController.php @@ -45,7 +45,7 @@ class ThumbnailsController extends ShaarliAdminController */ public function ajaxUpdate(Request $request, Response $response, array $args): Response { - $id = $args['id'] ?? null; + $id = $args['id'] ?? ''; if (false === ctype_digit($id)) { return $response->withStatus(400); diff --git a/application/helper/ApplicationUtils.php b/application/helper/ApplicationUtils.php index f79998b5..063300ca 100644 --- a/application/helper/ApplicationUtils.php +++ b/application/helper/ApplicationUtils.php @@ -238,7 +238,7 @@ class ApplicationUtils $conf->get('resource.update_check'), ] as $path ) { - if (!is_file(realpath($path))) { + if (!is_string($path) || !is_file(realpath($path))) { # the file may not exist yet continue; } diff --git a/application/http/Url.php b/application/http/Url.php index fe87088f..129957bf 100644 --- a/application/http/Url.php +++ b/application/http/Url.php @@ -61,6 +61,7 @@ class Url */ public function __construct($url) { + $url = $url ?? ''; $url = self::cleanupUnparsedUrl(trim($url)); $this->parts = parse_url($url); diff --git a/application/legacy/LegacyLinkDB.php b/application/legacy/LegacyLinkDB.php index d3beafe0..cb19eda5 100644 --- a/application/legacy/LegacyLinkDB.php +++ b/application/legacy/LegacyLinkDB.php @@ -116,7 +116,7 @@ class LegacyLinkDB implements Iterator, Countable, ArrayAccess /** * Countable - Counts elements of an object */ - public function count() + public function count(): int { return count($this->links); } @@ -124,7 +124,7 @@ class LegacyLinkDB implements Iterator, Countable, ArrayAccess /** * ArrayAccess - Assigns a value to the specified offset */ - public function offsetSet($offset, $value) + public function offsetSet($offset, $value): void { // TODO: use exceptions instead of "die" if (!$this->loggedIn) { @@ -155,7 +155,7 @@ class LegacyLinkDB implements Iterator, Countable, ArrayAccess /** * ArrayAccess - Whether or not an offset exists */ - public function offsetExists($offset) + public function offsetExists($offset): bool { return array_key_exists($this->getLinkOffset($offset), $this->links); } @@ -163,7 +163,7 @@ class LegacyLinkDB implements Iterator, Countable, ArrayAccess /** * ArrayAccess - Unsets an offset */ - public function offsetUnset($offset) + public function offsetUnset($offset): void { if (!$this->loggedIn) { // TODO: raise an exception @@ -179,7 +179,7 @@ class LegacyLinkDB implements Iterator, Countable, ArrayAccess /** * ArrayAccess - Returns the value at specified offset */ - public function offsetGet($offset) + public function offsetGet($offset): ?array { $realOffset = $this->getLinkOffset($offset); return isset($this->links[$realOffset]) ? $this->links[$realOffset] : null; @@ -188,14 +188,17 @@ class LegacyLinkDB implements Iterator, Countable, ArrayAccess /** * Iterator - Returns the current element */ - public function current() + public function current(): array { return $this[$this->keys[$this->position]]; } /** * Iterator - Returns the key of the current element + * + * @return int|string */ + #[\ReturnTypeWillChange] public function key() { return $this->keys[$this->position]; @@ -204,7 +207,7 @@ class LegacyLinkDB implements Iterator, Countable, ArrayAccess /** * Iterator - Moves forward to next element */ - public function next() + public function next(): void { ++$this->position; } @@ -214,7 +217,7 @@ class LegacyLinkDB implements Iterator, Countable, ArrayAccess * * Entries are sorted by date (latest first) */ - public function rewind() + public function rewind(): void { $this->keys = array_keys($this->ids); $this->position = 0; @@ -223,7 +226,7 @@ class LegacyLinkDB implements Iterator, Countable, ArrayAccess /** * Iterator - Checks if current position is valid */ - public function valid() + public function valid(): bool { return isset($this->keys[$this->position]); }