From 3fddf35415ab929505304cb94f43afef2d30030f Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Sat, 14 Oct 2023 02:07:18 -0700 Subject: [PATCH] refactor: fix psalm info things Signed-off-by: Varun Patil --- lib/ClustersBackend/Backend.php | 2 ++ lib/Command/Index.php | 3 --- lib/Controller/AdminController.php | 24 +++++++++-------- lib/Controller/DaysController.php | 4 +-- lib/Controller/DownloadController.php | 3 +++ lib/Controller/OtherController.php | 2 +- lib/Controller/PublicController.php | 12 ++++----- lib/Controller/VideoController.php | 5 ++++ lib/Db/AddMissingIndices.php | 2 +- lib/Db/AlbumsQuery.php | 2 +- lib/Db/FsManager.php | 4 +-- lib/Db/LivePhoto.php | 4 +-- lib/Db/TimelineWriteMap.php | 20 +++++++------- lib/Db/TimelineWritePlaces.php | 8 +++--- lib/Exif.php | 5 +++- .../Version505000Date20230821044807.php | 2 +- lib/Service/BinExt.php | 26 ++++++++++++++----- lib/Service/Index.php | 2 +- lib/Service/Places.php | 6 ++--- lib/Util.php | 4 +-- 20 files changed, 81 insertions(+), 59 deletions(-) diff --git a/lib/ClustersBackend/Backend.php b/lib/ClustersBackend/Backend.php index e697bf9af..398bd2e1e 100644 --- a/lib/ClustersBackend/Backend.php +++ b/lib/ClustersBackend/Backend.php @@ -68,6 +68,8 @@ abstract public function getClustersInternal(int $fileid = 0): array; /** * Get a cluster ID for the given cluster. + * + * @return string|int */ abstract public static function getClusterId(array $cluster); diff --git a/lib/Command/Index.php b/lib/Command/Index.php index 301a066d6..ca7276bcd 100644 --- a/lib/Command/Index.php +++ b/lib/Command/Index.php @@ -57,9 +57,6 @@ public function __construct(InputInterface $input) class Index extends Command { - /** @var int[][] */ - protected array $sizes; - protected IUserManager $userManager; protected IGroupManager $groupManager; protected IRootFolder $rootFolder; diff --git a/lib/Controller/AdminController.php b/lib/Controller/AdminController.php index f2794153f..3e0783919 100644 --- a/lib/Controller/AdminController.php +++ b/lib/Controller/AdminController.php @@ -229,18 +229,20 @@ private function getExecutableStatus( bool $testIfFile = true, bool $testIfExecutable = true ): string { - if ($testIfFile) { - if ($path instanceof \Closure) { - try { - $path = $path(); - } catch (\Exception $e) { - return 'test_fail:'.$e->getMessage(); - } + if ($path instanceof \Closure) { + try { + $path = $path(); + } catch (\Exception $e) { + return 'test_fail:'.$e->getMessage(); } + } - if (!\is_string($path) || !is_file($path)) { - return 'not_found'; - } + if (!\is_string($path)) { + return 'not_found'; + } + + if ($testIfFile && !is_file($path)) { + return 'not_found'; } if ($testIfExecutable && !is_executable($path)) { @@ -268,6 +270,6 @@ private function actionToken(bool $set = false): string $token = bin2hex(random_bytes(32)); $session->set('memories_action_token', $token); - return $token ?? ''; + return $token; } } diff --git a/lib/Controller/DaysController.php b/lib/Controller/DaysController.php index ef17ff51f..6ef4ec2ff 100644 --- a/lib/Controller/DaysController.php +++ b/lib/Controller/DaysController.php @@ -99,7 +99,7 @@ public function day(string $id): Http\Response ); // Force month id for dayId for month view - if ($this->isMonthView()) { + if ($this->isMonthView() && $dayIds) { foreach ($list as &$photo) { $photo['dayid'] = (int) $dayIds[0]; } @@ -189,7 +189,7 @@ private function preloadDays(array &$days): void } // Build identical transforms for sub queries - $transforms = $this->getTransformations(false); + $transforms = $this->getTransformations(); $preloaded = 0; $preloadDayIds = []; $preloadDays = []; diff --git a/lib/Controller/DownloadController.php b/lib/Controller/DownloadController.php index 6141a9d31..a35aa46f7 100644 --- a/lib/Controller/DownloadController.php +++ b/lib/Controller/DownloadController.php @@ -186,6 +186,9 @@ public function one(int $fileid, bool $resumable = true): Http\Response // Open file to send $res = $file->fopen('rb'); + if (false === $res) { + throw new \Exception('Failed to open file on disk'); + } // Seek to start if not zero if ($seekStart > 0) { diff --git a/lib/Controller/OtherController.php b/lib/Controller/OtherController.php index db1175807..3f5328f53 100644 --- a/lib/Controller/OtherController.php +++ b/lib/Controller/OtherController.php @@ -77,7 +77,7 @@ public function getUserConfig(): Http\Response } // helper function to get user config values - $getAppConfig = function ($key, $default) use ($uid): string { + $getAppConfig = function ($key, $default) use ($uid) { return $this->config->getUserValue($uid, Application::APPNAME, $key, $default); }; diff --git a/lib/Controller/PublicController.php b/lib/Controller/PublicController.php index 6900d1cbb..b5a8a82f5 100644 --- a/lib/Controller/PublicController.php +++ b/lib/Controller/PublicController.php @@ -162,11 +162,11 @@ protected function isPasswordProtected(): bool return null !== $this->share->getPassword(); } - protected function redirectIfOwned(IShare $share) + protected function redirectIfOwned(IShare $share): void { $user = $this->userSession->getUser(); if (!$user) { - return null; + return; } /** @var \OCP\Files\Node */ @@ -180,16 +180,16 @@ protected function redirectIfOwned(IShare $share) $userFolder = $this->rootFolder->getUserFolder($user->getUID()); $nodes = $userFolder->getById($share->getNodeId()); if (0 === \count($nodes)) { - return null; + return; } $node = $nodes[0]; } catch (NotFoundException $e) { - return null; + return; } // Check if node is a folder if (!$node instanceof \OCP\Files\Folder) { - return null; + return; } // Remove user folder path from start of node path @@ -203,7 +203,7 @@ protected function redirectIfOwned(IShare $share) // Check if relPath starts with foldersPath if (0 !== strpos($relPath, $foldersPath)) { - return null; + return; } // Remove foldersPath from start of relPath diff --git a/lib/Controller/VideoController.php b/lib/Controller/VideoController.php index 13ea0a94a..2e1e88e22 100644 --- a/lib/Controller/VideoController.php +++ b/lib/Controller/VideoController.php @@ -162,6 +162,11 @@ public function livephoto( throw Exceptions::NotFound('Could not read binary EXIF field'); } } elseif (str_starts_with($liveid, 'self__traileroffset=')) { + // Make sure we have a path + if (!$path) { + throw Exceptions::BadRequest('File path missing for self__traileroffset'); + } + // Remove prefix $offset = (int) substr($liveid, \strlen('self__traileroffset=')); if ($offset <= 0) { diff --git a/lib/Db/AddMissingIndices.php b/lib/Db/AddMissingIndices.php index dff0051f1..3daad3b00 100644 --- a/lib/Db/AddMissingIndices.php +++ b/lib/Db/AddMissingIndices.php @@ -59,7 +59,7 @@ public static function run(IOutput $output): SchemaWrapper } // Migrate - if (\count($ops) > 0 && null !== $connection) { + if (\count($ops) > 0) { $output->info('Updating external table schema: '.implode(', ', $ops)); $connection->migrateToSchema($schema->getWrappedSchema()); } elseif (null === $connection) { diff --git a/lib/Db/AlbumsQuery.php b/lib/Db/AlbumsQuery.php index c025243f7..1f5e7cbc9 100644 --- a/lib/Db/AlbumsQuery.php +++ b/lib/Db/AlbumsQuery.php @@ -217,7 +217,7 @@ public function userIsCollaborator(string $uid, int $albumId): bool * Get album object by token. * Returns false if album link does not exist. */ - public function getAlbumByLink(string $token) + public function getAlbumByLink(string $token): ?array { $query = $this->connection->getQueryBuilder(); $query->select('*')->from('photos_albums', 'pa') diff --git a/lib/Db/FsManager.php b/lib/Db/FsManager.php index 6c524dbcb..6c71ceed4 100644 --- a/lib/Db/FsManager.php +++ b/lib/Db/FsManager.php @@ -262,8 +262,8 @@ public function getShareFile(int $id): ?File { try { // Album share - if ($this->hasAlbumToken() && $this->getShareToken()) { - $album = $this->albumsQuery->getAlbumByLink($this->getShareToken()); + if ($this->hasAlbumToken() && ($token = $this->getShareToken())) { + $album = $this->albumsQuery->getAlbumByLink($token); if (null === $album) { return null; } diff --git a/lib/Db/LivePhoto.php b/lib/Db/LivePhoto.php index d101bd41d..970d163eb 100644 --- a/lib/Db/LivePhoto.php +++ b/lib/Db/LivePhoto.php @@ -29,11 +29,11 @@ public function isVideoPart(array $exif): bool } /** Get liveid from photo part */ - public function getLivePhotoId(File $file, array $exif) + public function getLivePhotoId(File $file, array $exif): string { // Apple JPEG (MOV has ContentIdentifier) if (\array_key_exists('MediaGroupUUID', $exif)) { - return $exif['MediaGroupUUID']; + return (string) $exif['MediaGroupUUID']; } // Google MVIMG and Samsung JPEG diff --git a/lib/Db/TimelineWriteMap.php b/lib/Db/TimelineWriteMap.php index b2ee5e891..2f4254772 100644 --- a/lib/Db/TimelineWriteMap.php +++ b/lib/Db/TimelineWriteMap.php @@ -17,15 +17,15 @@ trait TimelineWriteMap * Get the cluster ID for a given point. * If the cluster ID changes, update the old cluster and the new cluster. * - * @param int $prevCluster The current cluster ID of the point - * @param null|float $lat The latitude of the point - * @param null|float $lon The longitude of the point - * @param null|float $oldLat The old latitude of the point - * @param null|float $oldLon The old longitude of the point + * @param int $prevCluster The current cluster ID of the point + * @param ?float $lat The latitude of the point + * @param ?float $lon The longitude of the point + * @param ?float $oldLat The old latitude of the point + * @param ?float $oldLon The old longitude of the point * * @return int The new cluster ID */ - protected function mapGetCluster(int $prevCluster, $lat, $lon, $oldLat, $oldLon): int + protected function mapGetCluster(int $prevCluster, ?float $lat, ?float $lon, ?float $oldLat, ?float $oldLon): int { // Just remove from old cluster if the point is no longer valid if (null === $lat || null === $lon) { @@ -140,11 +140,11 @@ private function mapCreateCluster(float $lat, float $lon): int /** * Remove a point from a cluster. * - * @param int $clusterId The ID of the cluster - * @param float $lat The latitude of the point - * @param float $lon The longitude of the point + * @param int $clusterId The ID of the cluster + * @param ?float $lat The latitude of the point + * @param ?float $lon The longitude of the point */ - private function mapRemoveFromCluster(int $clusterId, $lat, $lon): void + private function mapRemoveFromCluster(int $clusterId, ?float $lat, ?float $lon): void { if ($clusterId <= 0 || null === $lat || null === $lon) { return; diff --git a/lib/Db/TimelineWritePlaces.php b/lib/Db/TimelineWritePlaces.php index 0546bf22e..4a6109cdd 100644 --- a/lib/Db/TimelineWritePlaces.php +++ b/lib/Db/TimelineWritePlaces.php @@ -83,13 +83,13 @@ public function updatePlacesData(int $fileId, $lat, $lon): array * Also update the exif data with the tzid from location (LocationTZID) * Performs an in-place update of the exif data. * - * @param int $fileId The file ID - * @param array $exif The exif data (will change) - * @param array|bool $prevRow The previous row of data + * @param int $fileId The file ID + * @param array $exif The exif data (will change) + * @param ?array $prevRow The previous row of data * * @return array Update values */ - protected function processExifLocation(int $fileId, array &$exif, $prevRow): array + protected function processExifLocation(int $fileId, array &$exif, ?array $prevRow): array { // Store location data [$lat, $lon] = self::readCoord($exif); diff --git a/lib/Exif.php b/lib/Exif.php index d1334df12..7d3492e11 100644 --- a/lib/Exif.php +++ b/lib/Exif.php @@ -17,7 +17,7 @@ class Exif /** Opened instance of exiftool when running in command mode */ private static $staticProc; private static $staticPipes; - private static $noStaticProc = false; + private static bool $noStaticProc = false; public static function closeStaticExiftoolProc(): void { @@ -334,6 +334,9 @@ public static function setFileExif(File $file, array $data): void { // Get path to local file so we can skip reading $path = $file->getStorage()->getLocalFile($file->getInternalPath()); + if (!$path) { + throw new \Exception('Failed to get local file path'); + } // Set exif data self::setExif($path, $data); diff --git a/lib/Migration/Version505000Date20230821044807.php b/lib/Migration/Version505000Date20230821044807.php index b99aeb1f7..62e8f3bfa 100644 --- a/lib/Migration/Version505000Date20230821044807.php +++ b/lib/Migration/Version505000Date20230821044807.php @@ -81,7 +81,7 @@ public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array ->executeQuery() ->fetchOne() ; - $output->startProgress($maxCount); + $output->startProgress((int) $maxCount); // get the required records $result = $this->dbc->getQueryBuilder() diff --git a/lib/Service/BinExt.php b/lib/Service/BinExt.php index bbbd406ef..c48dd47bc 100644 --- a/lib/Service/BinExt.php +++ b/lib/Service/BinExt.php @@ -108,7 +108,11 @@ public static function getExiftoolPBin(): string return self::getTempBin($path, self::getName('exiftool', self::EXIFTOOL_VER)); } - /** Get path to exiftool binary for proc_open */ + /** + * Get path to exiftool binary for proc_open. + * + * @return string[] + */ public static function getExiftool(): array { if (Util::getSystemConfig('memories.exiftool_no_local')) { @@ -118,7 +122,11 @@ public static function getExiftool(): array return [self::getExiftoolPBin()]; } - /** Detect the exiftool binary to use */ + /** + * Detect the exiftool binary to use. + * + * @return false|string + */ public static function detectExiftool() { if (!empty($path = Util::getSystemConfig('memories.exiftool'))) { @@ -216,21 +224,21 @@ public static function getGoVodBin(): string * If local, restart the go-vod instance. * If external, configure the go-vod instance. */ - public static function startGoVod() + public static function startGoVod(): ?string { // Check if disabled if (Util::getSystemConfig('memories.vod.disable')) { // Make sure it's dead, in case the user just disabled it Util::pkill(self::getName('go-vod')); - return; + return null; } // Check if external if (Util::getSystemConfig('memories.vod.external')) { self::configureGoVod(); - return; + return null; } // Get transcoder path @@ -357,7 +365,11 @@ public static function configureGoVod(): bool return true; } - /** Detect the go-vod binary to use */ + /** + * Detect the go-vod binary to use. + * + * @return false|string + */ public static function detectGoVod() { $goVodPath = Util::getSystemConfig('memories.vod.path'); @@ -416,7 +428,7 @@ public static function detectFFmpeg() public static function testFFmpeg(string $path, string $name): string { - $version = shell_exec("{$path} -version"); + $version = shell_exec("{$path} -version") ?: ''; if (!preg_match("/{$name} version \\S*/", $version, $matches)) { throw new \Exception("failed to detect version, found {$version}"); } diff --git a/lib/Service/Index.php b/lib/Service/Index.php index da095fd5d..81e6fc066 100644 --- a/lib/Service/Index.php +++ b/lib/Service/Index.php @@ -299,7 +299,7 @@ private function error(string $message): void */ private function log(string $message, bool $overwrite = false): void { - if ($this->output) { + if ($this->section) { if ($overwrite) { $this->section->clear(1); } diff --git a/lib/Service/Places.php b/lib/Service/Places.php index 41462ffd8..e51813d87 100644 --- a/lib/Service/Places.php +++ b/lib/Service/Places.php @@ -16,8 +16,6 @@ class Places { - protected IDBConnection $db; - protected IConfig $config; protected IDBConnection $connection; protected TimelineWrite $timelineWrite; @@ -322,7 +320,7 @@ public function importPlanet(string $datafile): void } if (GIS_TYPE_MYSQL === $gis) { - $points = implode(',', array_map(static function ($point) { + $points = implode(',', array_map(static function (array $point) { $x = $point[0]; $y = $point[1]; @@ -331,7 +329,7 @@ public function importPlanet(string $datafile): void $geometry = "POLYGON(({$points}))"; } elseif (GIS_TYPE_POSTGRES === $gis) { - $geometry = implode(',', array_map(static function ($point) { + $geometry = implode(',', array_map(static function (array $point) { $x = $point[0]; $y = $point[1]; diff --git a/lib/Util.php b/lib/Util.php index a9934d056..84d743979 100644 --- a/lib/Util.php +++ b/lib/Util.php @@ -18,7 +18,7 @@ class Util { use UtilController; - public static $ARCHIVE_FOLDER = '.archive'; + public static string $ARCHIVE_FOLDER = '.archive'; /** * Get host CPU architecture (amd64 or aarch64). @@ -248,7 +248,7 @@ public static function permissionsToStr(int $permissions): string * @param $url URL of the page * @param $previewArgs Preview arguments (e.g. token) */ - public static function addOgMetadata(Node $node, string $title, string $url, array $previewArgs) + public static function addOgMetadata(Node $node, string $title, string $url, array $previewArgs): void { // Add title \OCP\Util::addHeader('meta', ['property' => 'og:title', 'content' => $title]);