From: joshuaruesweg Date: Thu, 4 Mar 2021 18:21:45 +0000 (+0100) Subject: Clean up code X-Git-Tag: 5.4.0_Alpha_1~146^2~34 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=311fbd44519e39b65fdfd4d6c4759b2c1eb6c5c5;p=GitHub%2FWoltLab%2FWCF.git Clean up code --- diff --git a/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php b/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php index 0ee207b4a1..4f3094b5bc 100644 --- a/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php +++ b/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrl.class.php @@ -92,7 +92,7 @@ class UnfurlUrl extends DatabaseObject * * @throws \InvalidArgumentException If the given URL is invalid. */ - public static function getByUrl(string $url): self + public static function getByUrl(string $url): ?self { if (!Url::is($url)) { throw new \InvalidArgumentException("Given URL is not a valid URL."); @@ -105,7 +105,7 @@ class UnfurlUrl extends DatabaseObject $statement->execute([\sha1($url)]); $row = $statement->fetchArray(); if (!$row) { - $row = []; + return null; } return new self(null, $row); diff --git a/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrlAction.class.php b/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrlAction.class.php index 206fb74d07..6342425194 100644 --- a/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrlAction.class.php +++ b/wcfsetup/install/files/lib/data/unfurl/url/UnfurlUrlAction.class.php @@ -46,7 +46,7 @@ class UnfurlUrlAction extends AbstractDatabaseObjectAction { $object = UnfurlUrl::getByUrl($this->parameters['data']['url']); - if (!$object->urlID) { + if (!$object) { $returnValues = (new self([], 'create', [ 'data' => [ 'url' => $this->parameters['data']['url'], diff --git a/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php b/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php index 0b38217688..883d6d8c54 100644 --- a/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php +++ b/wcfsetup/install/files/lib/system/background/job/UnfurlUrlBackgroundJob.class.php @@ -69,52 +69,52 @@ final class UnfurlUrlBackgroundJob extends AbstractBackgroundJob if (empty(StringUtil::trim($unfurlResponse->getTitle()))) { $this->save(UnfurlUrl::STATUS_REJECTED); - } else { - $title = StringUtil::truncate($unfurlResponse->getTitle(), 255); - if ($unfurlResponse->getDescription() !== null) { - $description = StringUtil::truncate($unfurlResponse->getDescription(), 500); - } else { - $description = ""; - } + return; + } - if ($unfurlResponse->getImageUrl()) { - try { - $image = $this->downloadImage($unfurlResponse->getImage()); - $imageData = \getimagesizefromstring($image); - if ($imageData !== false) { - $imageType = $this->validateImage($imageData); - if (!(MODULE_IMAGE_PROXY || IMAGE_ALLOW_EXTERNAL_SOURCE)) { - $imageHash = $this->saveImage($imageData, $image); - } else { - $imageHash = ""; - } - } else { - $imageType = UnfurlUrl::IMAGE_NO_IMAGE; - } + $title = StringUtil::truncate($unfurlResponse->getTitle(), 255); + $description = ""; + if ($unfurlResponse->getDescription()) { + $description = StringUtil::truncate($unfurlResponse->getDescription()); + } - if ($imageType === UnfurlUrl::IMAGE_NO_IMAGE) { - $imageUrl = $imageHash = ""; + if ($unfurlResponse->getImageUrl()) { + try { + $image = $this->downloadImage($unfurlResponse->getImage()); + $imageData = \getimagesizefromstring($image); + if ($imageData !== false) { + $imageType = $this->validateImage($imageData); + if (!(MODULE_IMAGE_PROXY || IMAGE_ALLOW_EXTERNAL_SOURCE)) { + $imageHash = $this->saveImage($imageData, $image); } else { - $imageUrl = $unfurlResponse->getImageUrl(); + $imageHash = ""; } - } catch (UrlInaccessible | DownloadFailed $e) { + } else { $imageType = UnfurlUrl::IMAGE_NO_IMAGE; + } + + if ($imageType === UnfurlUrl::IMAGE_NO_IMAGE) { $imageUrl = $imageHash = ""; + } else { + $imageUrl = $unfurlResponse->getImageUrl(); } - } else { + } catch (UrlInaccessible | DownloadFailed $e) { $imageType = UnfurlUrl::IMAGE_NO_IMAGE; $imageUrl = $imageHash = ""; } - - $this->save( - UnfurlUrl::STATUS_SUCCESSFUL, - $title, - $description, - $imageType, - $imageUrl, - $imageHash - ); + } else { + $imageType = UnfurlUrl::IMAGE_NO_IMAGE; + $imageUrl = $imageHash = ""; } + + $this->save( + UnfurlUrl::STATUS_SUCCESSFUL, + $title, + $description, + $imageType, + $imageUrl, + $imageHash + ); } catch (UrlInaccessible | ParsingFailed $e) { if (\ENABLE_DEBUG_MODE) { \wcf\functions\exception\logThrowable($e); diff --git a/wcfsetup/install/files/lib/system/message/unfurl/exception/DownloadFailed.class.php b/wcfsetup/install/files/lib/system/message/unfurl/exception/DownloadFailed.class.php index 4d163ee162..34cbf2c5d0 100644 --- a/wcfsetup/install/files/lib/system/message/unfurl/exception/DownloadFailed.class.php +++ b/wcfsetup/install/files/lib/system/message/unfurl/exception/DownloadFailed.class.php @@ -2,8 +2,6 @@ namespace wcf\system\message\unfurl\exception; -use Exception; - /** * Denotes a (temporary) download failed. It can be retried later. * @@ -13,6 +11,6 @@ use Exception; * @package WoltLabSuite\Core\System\Message\Unfurl * @since 5.4 */ -class DownloadFailed extends Exception +class DownloadFailed extends \Exception { } diff --git a/wcfsetup/install/files/lib/system/message/unfurl/exception/ParsingFailed.class.php b/wcfsetup/install/files/lib/system/message/unfurl/exception/ParsingFailed.class.php index c11296a81d..b2fe728fff 100644 --- a/wcfsetup/install/files/lib/system/message/unfurl/exception/ParsingFailed.class.php +++ b/wcfsetup/install/files/lib/system/message/unfurl/exception/ParsingFailed.class.php @@ -2,8 +2,6 @@ namespace wcf\system\message\unfurl\exception; -use Exception; - /** * Denotes a permanent parsing body failed. It should not be retried later. * @@ -13,6 +11,6 @@ use Exception; * @package WoltLabSuite\Core\System\Message\Unfurl * @since 5.4 */ -class ParsingFailed extends Exception +class ParsingFailed extends \Exception { } diff --git a/wcfsetup/install/files/lib/system/message/unfurl/exception/UrlInaccessible.class.php b/wcfsetup/install/files/lib/system/message/unfurl/exception/UrlInaccessible.class.php index 3b0586a527..4d0a851973 100644 --- a/wcfsetup/install/files/lib/system/message/unfurl/exception/UrlInaccessible.class.php +++ b/wcfsetup/install/files/lib/system/message/unfurl/exception/UrlInaccessible.class.php @@ -2,8 +2,6 @@ namespace wcf\system\message\unfurl\exception; -use Exception; - /** * Denotes a permanent failing url, because the url is inaccessible. * @@ -13,6 +11,6 @@ use Exception; * @package WoltLabSuite\Core\System\Message\Unfurl * @since 5.4 */ -class UrlInaccessible extends Exception +class UrlInaccessible extends \Exception { }