refactor: reduce array_key_exists

Signed-off-by: Varun Patil <radialapps@gmail.com>
pull/888/head
Varun Patil 2023-10-22 11:56:29 -07:00
parent e04bb0a7b6
commit 4be31f19d5
9 changed files with 44 additions and 51 deletions

View File

@ -125,7 +125,7 @@ class FaceRecognitionBackend extends Backend
// Post process
foreach ($faces as &$row) {
$row['id'] = \array_key_exists('name', $row) ? $row['name'] : (int) $row['id'];
$row['id'] = $row['name'] ?? (int) $row['id'];
$row['count'] = (int) $row['count'];
}

View File

@ -27,7 +27,11 @@ use OCP\IRequest;
class Manager
{
/** Mapping of backend name to className */
/**
* Mapping of backend name to className.
*
* @var array<string, class-string>
*/
public static array $backends = [];
/**
@ -39,20 +43,20 @@ class Manager
*/
public static function get(string $name): Backend
{
if (!\array_key_exists($name, self::$backends)) {
throw new \Exception("Invalid clusters backend '{$name}'");
if ($className = self::$backends[$name] ?? null) {
/** @var Backend */
return \OC::$server->get($className);
}
return \OC::$server->get(self::$backends[$name]);
throw new \Exception("Invalid clusters backend '{$name}'");
}
/**
* Register a new backend.
*
* @param mixed $name
* @param mixed $className
* @param class-string $className
*/
public static function register($name, $className): void
public static function register(string $name, string $className): void
{
self::$backends[$name] = $className;
}

View File

@ -211,8 +211,8 @@ class PlacesBackend extends Backend
$json = json_decode($otherNames, true);
// Check if the language is available
if (\array_key_exists($lang, $json) && \is_string($json[$lang])) {
return $json[$lang];
if ($translated = ($json[$lang] ?? null)) {
return (string) $translated;
}
} catch (\Error) {
// Ignore errors, just use original name

View File

@ -187,11 +187,11 @@ class DaysController extends GenericApiController
// Load details into map byref
foreach ($details as $photo) {
$dayId = (int) $photo['dayid'];
if (!\array_key_exists($dayId, $drefMap)) {
if (!($drefMap[$dayId] ?? null)) {
continue;
}
if (!\array_key_exists('detail', $drefMap[$dayId])) {
if (!($drefMap[$dayId]['detail'] ?? null)) {
$drefMap[$dayId]['detail'] = [];
}

View File

@ -18,21 +18,20 @@ class LivePhoto
*/
public function isVideoPart(array $exif): bool
{
return \array_key_exists('MIMEType', $exif)
&& 'video/quicktime' === $exif['MIMEType']
&& \array_key_exists('ContentIdentifier', $exif);
return 'video/quicktime' === ($exif['MIMEType'] ?? null)
&& !empty($exif['ContentIdentifier'] ?? null);
}
/** Get liveid from photo part */
public function getLivePhotoId(File $file, array $exif): string
{
// Apple JPEG (MOV has ContentIdentifier)
if (\array_key_exists('MediaGroupUUID', $exif)) {
return (string) $exif['MediaGroupUUID'];
if ($uuid = ($exif['MediaGroupUUID'] ?? null)) {
return (string) $uuid;
}
// Google MVIMG and Samsung JPEG
if (\array_key_exists('MicroVideoOffset', $exif) && ($videoLength = $exif['MicroVideoOffset']) > 0) {
if (($offset = ($exif['MicroVideoOffset'] ?? null)) && ($offset > 0)) {
// As explained in the following issue,
// https://github.com/pulsejet/memories/issues/468
//
@ -48,14 +47,14 @@ class LivePhoto
// and subsequently extract the video file using the
// EmbeddedVideoFile binary prop, but setting the offset
// is faster for the same reason mentioned above.
$videoOffset = $file->getSize() - $videoLength;
$videoOffset = $file->getSize() - $offset;
return "self__traileroffset={$videoOffset}";
}
// Google JPEG and Samsung HEIC / JPEG (Apple?)
if (\array_key_exists('MotionPhoto', $exif)) {
if ('image/jpeg' === $exif['MIMEType']) {
if ($exif['MotionPhoto'] ?? null) {
if ('image/jpeg' === ($exif['MIMEType'] ?? null)) {
// Google Motion Photo JPEG
// We need to read the DirectoryItemLength key to get the length of the video
@ -90,7 +89,7 @@ class LivePhoto
return 'self__trailer';
}
if ('image/heic' === $exif['MIMEType']) {
if ('image/heic' === ($exif['MIMEType'] ?? null)) {
// Samsung HEIC -- no way to get this out yet (DirectoryItemLength is senseless)
// The reason this is above the MotionPhotoVideo check is because extracting binary
// EXIF fields on the fly is extremely expensive compared to trailer extraction.
@ -98,7 +97,7 @@ class LivePhoto
}
// Samsung HEIC (at least S21)
if (\array_key_exists('MotionPhotoVideo', $exif) && !empty($exif['MotionPhotoVideo'])) {
if (!empty($exif['MotionPhotoVideo'] ?? null)) {
// It's a binary exif field, decode when the user requests it
return 'self__exifbin=MotionPhotoVideo';
}

View File

@ -276,13 +276,13 @@ trait TimelineQueryDays
}
// Favorite field, may not be present
if (\array_key_exists('categoryid', $row) && $row['categoryid']) {
if ($row['categoryid'] ?? null) {
$row['isfavorite'] = 1;
}
unset($row['categoryid']);
// Get hidden field if present
if (\array_key_exists('hidden', $row) && $row['hidden']) {
if ($row['hidden'] ?? null) {
$row['ishidden'] = 1;
}
unset($row['hidden']);
@ -294,11 +294,10 @@ trait TimelineQueryDays
unset($row['datetaken']);
// Calculate the AUID if we can
if (\array_key_exists('epoch', $row) && \array_key_exists('size', $row)
&& ($epoch = (int) $row['epoch']) && ($size = (int) $row['size'])) {
if (($epoch = $row['epoch'] ?? null) && ($size = $row['size'] ?? null)) {
// compute AUID and discard size
// epoch is used for ordering, so we keep it
$row['auid'] = Exif::getAUID($epoch, $size);
$row['auid'] = Exif::getAUID((int) $epoch, (int) $size);
unset($row['size']);
}
@ -337,8 +336,8 @@ trait TimelineQueryDays
private function dayIdToMonthId(int $dayId): int
{
static $memoize = [];
if (\array_key_exists($dayId, $memoize)) {
return $memoize[$dayId];
if ($cache = $memoize[$dayId] ?? null) {
return $cache;
}
return $memoize[$dayId] = strtotime(date('Ym', $dayId * 86400).'01') / 86400;

View File

@ -64,22 +64,14 @@ trait TimelineQueryMap
$res = $this->executeQueryWithCTEs($query)->fetchAll();
// Post-process results
$clusters = [];
foreach ($res as &$cluster) {
$c = [
'center' => [
(float) $cluster['lat'],
(float) $cluster['lon'],
],
'count' => (float) $cluster['count'],
];
if (\array_key_exists('id', $cluster)) {
$c['id'] = (int) $cluster['id'];
}
$clusters[] = $c;
}
return $clusters;
return array_map(static fn ($row) => [
'id' => (int) $row['id'],
'center' => [
(float) $row['lat'],
(float) $row['lon'],
],
'count' => (float) $row['count'],
], $res);
}
/**

View File

@ -137,8 +137,7 @@ class TimelineWrite
// Get BUID from ImageUniqueId if not present
$buid = $prevRow ? $prevRow['buid'] : '';
if (empty($buid)) {
$imageUniqueId = \array_key_exists('ImageUniqueID', $exif) ? $exif['ImageUniqueID'] : null;
$buid = Exif::getBUID($file->getName(), $imageUniqueId, (int) $file->getSize());
$buid = Exif::getBUID($file->getName(), $exif['ImageUniqueID'] ?? null, (int) $file->getSize());
}
// Get exif json

View File

@ -161,9 +161,9 @@ class Exif
// Get timezone from exif
try {
$tzStr = $exif['OffsetTimeOriginal']
?: $exif['OffsetTime']
?: $exif['LocationTZID']
?: throw new \Exception();
?? $exif['OffsetTime']
?? $exif['LocationTZID']
?? throw new \Exception();
$exifTz = new \DateTimeZone((string) $tzStr);
} catch (\Exception) {
$exifTz = null;