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) function startsWith($haystack, $needle, $case = true)
{ {
$needle = $needle ?? '';
if ($case) { if ($case) {
return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0); return (strcmp(substr($haystack, 0, strlen($needle)), $needle) === 0);
} }
@ -180,7 +181,7 @@ function generateLocation($referer, $host, $loopTerms = [])
$host = substr($host, 0, $pos); $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))) { if (!empty($referer) && (strpos($refererHost, $host) !== false || startsWith('?', $refererHost))) {
$finalReferer = $referer; $finalReferer = $referer;
} }
@ -292,7 +293,7 @@ function generate_api_secret($username, $salt)
*/ */
function normalize_spaces($string) function normalize_spaces($string)
{ {
return preg_replace('/\s{2,}/', ' ', trim($string)); return preg_replace('/\s{2,}/', ' ', trim($string ?? ''));
} }
/** /**

View file

@ -30,7 +30,7 @@ public function getHistory($request, $response)
$history = $this->history->getHistory(); $history = $this->history->getHistory();
// Return history operations from the {offset}th, starting from {since}. // 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'); $offset = $request->getParam('offset');
if (empty($offset)) { if (empty($offset)) {
$offset = 0; $offset = 0;

View file

@ -57,7 +57,7 @@ public function __construct()
* *
* @return int Number of bookmarks * @return int Number of bookmarks
*/ */
public function count() public function count(): int
{ {
return count($this->bookmarks); return count($this->bookmarks);
} }
@ -70,7 +70,7 @@ public function count()
* *
* @throws InvalidBookmarkException * @throws InvalidBookmarkException
*/ */
public function offsetSet($offset, $value) public function offsetSet($offset, $value): void
{ {
if ( if (
! $value instanceof Bookmark ! $value instanceof Bookmark
@ -106,7 +106,7 @@ public function offsetSet($offset, $value)
* *
* @return bool true if it exists, false otherwise * @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); return array_key_exists($this->getBookmarkOffset($offset), $this->bookmarks);
} }
@ -116,7 +116,7 @@ public function offsetExists($offset)
* *
* @param int $offset Bookmark ID * @param int $offset Bookmark ID
*/ */
public function offsetUnset($offset) public function offsetUnset($offset): void
{ {
$realOffset = $this->getBookmarkOffset($offset); $realOffset = $this->getBookmarkOffset($offset);
$url = $this->bookmarks[$realOffset]->getUrl(); $url = $this->bookmarks[$realOffset]->getUrl();
@ -132,7 +132,7 @@ public function offsetUnset($offset)
* *
* @return Bookmark|null The Bookmark if found, null otherwise * @return Bookmark|null The Bookmark if found, null otherwise
*/ */
public function offsetGet($offset) public function offsetGet($offset): ?Bookmark
{ {
$realOffset = $this->getBookmarkOffset($offset); $realOffset = $this->getBookmarkOffset($offset);
return isset($this->bookmarks[$realOffset]) ? $this->bookmarks[$realOffset] : null; return isset($this->bookmarks[$realOffset]) ? $this->bookmarks[$realOffset] : null;
@ -143,7 +143,7 @@ public function offsetGet($offset)
* *
* @return Bookmark corresponding to the current position * @return Bookmark corresponding to the current position
*/ */
public function current() public function current(): Bookmark
{ {
return $this[$this->keys[$this->position]]; return $this[$this->keys[$this->position]];
} }
@ -153,7 +153,7 @@ public function current()
* *
* @return int Bookmark ID corresponding to the current position * @return int Bookmark ID corresponding to the current position
*/ */
public function key() public function key(): int
{ {
return $this->keys[$this->position]; return $this->keys[$this->position];
} }
@ -161,7 +161,7 @@ public function key()
/** /**
* Iterator - Moves forward to next element * Iterator - Moves forward to next element
*/ */
public function next() public function next(): void
{ {
++$this->position; ++$this->position;
} }
@ -171,7 +171,7 @@ public function next()
* *
* Entries are sorted by date (latest first) * Entries are sorted by date (latest first)
*/ */
public function rewind() public function rewind(): void
{ {
$this->keys = array_keys($this->ids); $this->keys = array_keys($this->ids);
$this->position = 0; $this->position = 0;
@ -182,7 +182,7 @@ public function rewind()
* *
* @return bool true if the current Bookmark ID exists, false otherwise * @return bool true if the current Bookmark ID exists, false otherwise
*/ */
public function valid() public function valid(): bool
{ {
return isset($this->keys[$this->position]); 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 // For whitespaces, we use the special \s regex character
$separator = $separator === ' ' ? '\s' : $separator; $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 @@ protected function formatRealUrl($bookmark)
$prefix = rtrim($this->contextData['base_path'], '/') . '/'; $prefix = rtrim($this->contextData['base_path'], '/') . '/';
} }
return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl(), '/')); return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl() ?? '', '/'));
} }
return escape($bookmark->getUrl()); return escape($bookmark->getUrl());

View file

@ -45,7 +45,7 @@ public function index(Request $request, Response $response): Response
*/ */
public function ajaxUpdate(Request $request, Response $response, array $args): Response public function ajaxUpdate(Request $request, Response $response, array $args): Response
{ {
$id = $args['id'] ?? null; $id = $args['id'] ?? '';
if (false === ctype_digit($id)) { if (false === ctype_digit($id)) {
return $response->withStatus(400); return $response->withStatus(400);

View file

@ -238,7 +238,7 @@ public static function checkResourcePermissions(ConfigManager $conf, bool $minim
$conf->get('resource.update_check'), $conf->get('resource.update_check'),
] as $path ] as $path
) { ) {
if (!is_file(realpath($path))) { if (!is_string($path) || !is_file(realpath($path))) {
# the file may not exist yet # the file may not exist yet
continue; continue;
} }

View file

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

View file

@ -116,7 +116,7 @@ public function __construct(
/** /**
* Countable - Counts elements of an object * Countable - Counts elements of an object
*/ */
public function count() public function count(): int
{ {
return count($this->links); return count($this->links);
} }
@ -124,7 +124,7 @@ public function count()
/** /**
* ArrayAccess - Assigns a value to the specified offset * 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" // TODO: use exceptions instead of "die"
if (!$this->loggedIn) { if (!$this->loggedIn) {
@ -155,7 +155,7 @@ public function offsetSet($offset, $value)
/** /**
* ArrayAccess - Whether or not an offset exists * 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); return array_key_exists($this->getLinkOffset($offset), $this->links);
} }
@ -163,7 +163,7 @@ public function offsetExists($offset)
/** /**
* ArrayAccess - Unsets an offset * ArrayAccess - Unsets an offset
*/ */
public function offsetUnset($offset) public function offsetUnset($offset): void
{ {
if (!$this->loggedIn) { if (!$this->loggedIn) {
// TODO: raise an exception // TODO: raise an exception
@ -179,7 +179,7 @@ public function offsetUnset($offset)
/** /**
* ArrayAccess - Returns the value at specified offset * ArrayAccess - Returns the value at specified offset
*/ */
public function offsetGet($offset) public function offsetGet($offset): ?array
{ {
$realOffset = $this->getLinkOffset($offset); $realOffset = $this->getLinkOffset($offset);
return isset($this->links[$realOffset]) ? $this->links[$realOffset] : null; return isset($this->links[$realOffset]) ? $this->links[$realOffset] : null;
@ -188,14 +188,17 @@ public function offsetGet($offset)
/** /**
* Iterator - Returns the current element * Iterator - Returns the current element
*/ */
public function current() public function current(): array
{ {
return $this[$this->keys[$this->position]]; return $this[$this->keys[$this->position]];
} }
/** /**
* Iterator - Returns the key of the current element * Iterator - Returns the key of the current element
*
* @return int|string
*/ */
#[\ReturnTypeWillChange]
public function key() public function key()
{ {
return $this->keys[$this->position]; return $this->keys[$this->position];
@ -204,7 +207,7 @@ public function key()
/** /**
* Iterator - Moves forward to next element * Iterator - Moves forward to next element
*/ */
public function next() public function next(): void
{ {
++$this->position; ++$this->position;
} }
@ -214,7 +217,7 @@ public function next()
* *
* Entries are sorted by date (latest first) * Entries are sorted by date (latest first)
*/ */
public function rewind() public function rewind(): void
{ {
$this->keys = array_keys($this->ids); $this->keys = array_keys($this->ids);
$this->position = 0; $this->position = 0;
@ -223,7 +226,7 @@ public function rewind()
/** /**
* Iterator - Checks if current position is valid * Iterator - Checks if current position is valid
*/ */
public function valid() public function valid(): bool
{ {
return isset($this->keys[$this->position]); return isset($this->keys[$this->position]);
} }