Resolve PHP 8.1 deprecations (#1866)

Co-authored-by: Adrien Crivelli <adrien.crivelli@gmail.com>
Co-authored-by: ArthurHoaro <arthur@hoa.ro>
This commit is contained in:
Hazhar Galeh 2022-09-14 08:17:07 +02:00 committed by GitHub
parent 221a2534b2
commit dbd99f310f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 31 additions and 26 deletions

View file

@ -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 ?? ''));
}
/**

View file

@ -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;

View file

@ -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]);
}

View file

@ -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);
}
/**

View file

@ -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());

View file

@ -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);

View file

@ -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;
}

View file

@ -61,6 +61,7 @@ class Url
*/
public function __construct($url)
{
$url = $url ?? '';
$url = self::cleanupUnparsedUrl(trim($url));
$this->parts = parse_url($url);

View file

@ -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]);
}