From 437631a33435db6902d09cf0fd58c35ac9e4f1f9 Mon Sep 17 00:00:00 2001 From: Faraz Samapoor Date: Fri, 2 Jun 2023 15:43:47 +0330 Subject: [PATCH] Refactors "strpos" calls in /apps/theming to improve code readability. Signed-off-by: Faraz Samapoor --- apps/theming/lib/Command/UpdateConfig.php | 2 +- apps/theming/lib/Controller/ThemingController.php | 4 ++-- apps/theming/lib/ImageManager.php | 8 ++++---- apps/theming/lib/ThemingDefaults.php | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/theming/lib/Command/UpdateConfig.php b/apps/theming/lib/Command/UpdateConfig.php index 58dfcff8a8e1d..aebb6b9be15bb 100644 --- a/apps/theming/lib/Command/UpdateConfig.php +++ b/apps/theming/lib/Command/UpdateConfig.php @@ -112,7 +112,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } if (in_array($key, ImageManager::SUPPORTED_IMAGE_KEYS, true)) { - if (strpos($value, '/') !== 0) { + if (!str_starts_with($value, '/')) { $output->writeln('The image file needs to be provided as an absolute path: ' . $value . '.'); return 1; } diff --git a/apps/theming/lib/Controller/ThemingController.php b/apps/theming/lib/Controller/ThemingController.php index a323bac180ba9..c247f2c9d566b 100644 --- a/apps/theming/lib/Controller/ThemingController.php +++ b/apps/theming/lib/Controller/ThemingController.php @@ -182,7 +182,7 @@ public function updateStylesheet($setting, $value) { * Check that a string is a valid http/https url */ private function isValidUrl(string $url): bool { - return ((strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) && + return ((str_starts_with($url, 'http://') || str_starts_with($url, 'https://')) && filter_var($url, FILTER_VALIDATE_URL) !== false); } @@ -400,7 +400,7 @@ public function getManifest($app) { $info = $this->appManager->getAppInfo($app, false, $this->l10n->getLanguageCode()); $name = $info['name'] . ' - ' . $this->themingDefaults->getName(); $shortName = $info['name']; - if (strpos($this->request->getRequestUri(), '/index.php/') !== false) { + if (str_contains($this->request->getRequestUri(), '/index.php/')) { $startUrl = $this->urlGenerator->getBaseUrl() . '/index.php/apps/' . $app . '/'; } else { $startUrl = $this->urlGenerator->getBaseUrl() . '/apps/' . $app . '/'; diff --git a/apps/theming/lib/ImageManager.php b/apps/theming/lib/ImageManager.php index d088699fd3c37..87123a6f7c9c0 100644 --- a/apps/theming/lib/ImageManager.php +++ b/apps/theming/lib/ImageManager.php @@ -266,7 +266,7 @@ public function updateImage(string $key, string $tmpFile): string { $newTmpFile = $this->tempManager->getTemporaryFile(); imageinterlace($outputImage, 1); // Keep jpeg images encoded as jpeg - if (strpos($detectedMimeType, 'image/jpeg') !== false) { + if (str_contains($detectedMimeType, 'image/jpeg')) { if (!imagejpeg($outputImage, $newTmpFile, 90)) { throw new \Exception('Could not recompress background image as JPEG'); } @@ -300,16 +300,16 @@ public function updateImage(string $key, string $tmpFile): string { */ private function shouldOptimizeBackgroundImage(string $mimeType, int $contentSize): bool { // Do not touch SVGs - if (strpos($mimeType, 'image/svg') !== false) { + if (str_contains($mimeType, 'image/svg')) { return false; } // GIF does not benefit from converting - if (strpos($mimeType, 'image/gif') !== false) { + if (str_contains($mimeType, 'image/gif')) { return false; } // WebP also does not benefit from converting // We could possibly try to convert to progressive image, but normally webP images are quite small - if (strpos($mimeType, 'image/webp') !== false) { + if (str_contains($mimeType, 'image/webp')) { return false; } // As a rule of thumb background images should be max. 150-300 KiB, small images do not benefit from converting diff --git a/apps/theming/lib/ThemingDefaults.php b/apps/theming/lib/ThemingDefaults.php index 6cb204d1b1bcb..454e050554350 100644 --- a/apps/theming/lib/ThemingDefaults.php +++ b/apps/theming/lib/ThemingDefaults.php @@ -404,7 +404,7 @@ public function replaceImagePath($app, $image) { } $route = $this->urlGenerator->linkToRoute('theming.Theming.getManifest', ['app' => $app ]); } - if (strpos($image, 'filetypes/') === 0 && file_exists(\OC::$SERVERROOT . '/core/img/' . $image)) { + if (str_starts_with($image, 'filetypes/') && file_exists(\OC::$SERVERROOT . '/core/img/' . $image)) { $route = $this->urlGenerator->linkToRoute('theming.Icon.getThemedIcon', ['app' => $app, 'image' => $image]); }