Code cleanup
authorjoshuaruesweg <ruesweg@woltlab.com>
Wed, 10 Mar 2021 15:09:14 +0000 (16:09 +0100)
committerjoshuaruesweg <ruesweg@woltlab.com>
Tue, 16 Mar 2021 15:19:15 +0000 (16:19 +0100)
wcfsetup/install/files/acp/database/update_com.woltlab.wcf_5.4.php
wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php
wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php

index cc9c1a2d4d698ed35541ac7a2e6253bf19b02d3e..2e1bd84ea9ba7efb4fde9d13e20bfe4a6876a0e6 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 
 /**
    * Makes non-critical database adjustments (i.e. everything that is not related
+ * Makes non-critical database adjustments (i.e. everything that is not related
  * to sessions).
  *
  * @author  Tim Duesterhus
index 4756f0017e18d92625e80a88ec6d038be063a536..a6169d304375fa88fcf47002bbde1f01970a09ff 100644 (file)
@@ -122,12 +122,12 @@ class UnfurlUrl extends DatabaseObject
 
     public function hasCoverImage(): bool
     {
-        return $this->getImageType() == self::IMAGE_COVER && !empty($this->getImageUrl());
+        return $this->getImageType() === self::IMAGE_COVER && !empty($this->getImageUrl());
     }
 
     public function hasSquaredImage(): bool
     {
-        return $this->getImageType() == self::IMAGE_SQUARED && !empty($this->getImageUrl());
+        return $this->getImageType() === self::IMAGE_SQUARED && !empty($this->getImageUrl());
     }
 
     private function getImageType(): string
@@ -136,7 +136,7 @@ class UnfurlUrl extends DatabaseObject
             return self::IMAGE_NO_IMAGE;
         }
 
-        if ($this->width == $this->height) {
+        if ($this->width === $this->height) {
             return self::IMAGE_SQUARED;
         }
 
index 376ff17b8e0d2746fe436fbac39fa711764a9818..c8f1427c19cca589e732391dc34e8ddb2c96f82a 100644 (file)
@@ -12,6 +12,7 @@ use wcf\system\message\unfurl\exception\UrlInaccessible;
 use wcf\system\message\unfurl\UnfurlResponse;
 use wcf\system\WCF;
 use wcf\util\FileUtil;
+use wcf\util\ImageUtil;
 use wcf\util\StringUtil;
 use wcf\util\Url;
 
@@ -116,7 +117,7 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob
         $imageSaveData = [];
 
         if (empty($unfurlResponse->getImageUrl()) || !Url::is($unfurlResponse->getImageUrl())) {
-            throw new BadMethodCallException("Invalid image given.");
+            throw new \InvalidArgumentException("The given response does not have an image.");
         }
 
         try {
@@ -212,15 +213,13 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob
 
     private function getImageExtension(array $imageData): ?string
     {
-        switch ($imageData[2]) {
-            case \IMAGETYPE_PNG:
-                return 'png';
-
-            case \IMAGETYPE_GIF:
-                return 'gif';
-
-            case \IMAGETYPE_JPEG:
-                return 'jpg';
+        $extension = ImageUtil::getExtensionByMimeType($imageData['mime']);
+        switch ($extension) {
+            case 'gif':
+            case 'jpg':
+            case 'png':
+            case 'webp':
+                return $extension;
 
             default:
                 return null;
@@ -241,7 +240,7 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob
                 break;
 
             default:
-                throw new BadMethodCallException("Invalid status '{$status}' given.");
+                throw new \InvalidArgumentException("Invalid status '{$status}' given.");
         }
 
         if ($imageID !== null && !empty($imageData)) {