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 // Post process
foreach ($faces as &$row) { 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']; $row['count'] = (int) $row['count'];
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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