From 4b00f7996b5b2f7f616b800d7325d65746f02186 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Thu, 13 Dec 2018 21:58:00 +0100 Subject: [PATCH] Early returns --- src/Extension/DefaultLanguagePackGuard.php | 15 +++-- src/Forum/ValidateCustomLess.php | 78 ++++++++++++---------- 2 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/Extension/DefaultLanguagePackGuard.php b/src/Extension/DefaultLanguagePackGuard.php index d8fd3dc8a..88d98c7a8 100644 --- a/src/Extension/DefaultLanguagePackGuard.php +++ b/src/Extension/DefaultLanguagePackGuard.php @@ -42,12 +42,15 @@ class DefaultLanguagePackGuard */ public function whenExtensionWillBeDisabled(Disabling $event) { - if (in_array('flarum-locale', $event->extension->extra)) { - $defaultLocale = $this->settings->get('default_locale'); - $locale = array_get($event->extension->extra, 'flarum-locale.code'); - if ($locale === $defaultLocale) { - throw new ForbiddenException('You cannot disable the default language pack!'); - } + if (! in_array('flarum-locale', $event->extension->extra)) { + return; + } + + $defaultLocale = $this->settings->get('default_locale'); + $locale = array_get($event->extension->extra, 'flarum-locale.code'); + + if ($locale === $defaultLocale) { + throw new ForbiddenException('You cannot disable the default language pack!'); } } } diff --git a/src/Forum/ValidateCustomLess.php b/src/Forum/ValidateCustomLess.php index 190f98a50..532c9b623 100644 --- a/src/Forum/ValidateCustomLess.php +++ b/src/Forum/ValidateCustomLess.php @@ -69,39 +69,41 @@ class ValidateCustomLess */ public function whenSettingsSaving(Saving $event) { - if (isset($event->settings['custom_less'])) { - // We haven't saved the settings yet, but we want to trial a full - // recompile of the CSS to see if this custom LESS will break - // anything. In order to do that, we will temporarily override the - // settings repository with the new settings so that the recompile - // is effective. We will also use a dummy filesystem so that nothing - // is actually written yet. - - $settings = $this->container->make(SettingsRepositoryInterface::class); - - $this->container->extend( - SettingsRepositoryInterface::class, - function ($settings) use ($event) { - return new OverrideSettingsRepository($settings, $event->settings); - } - ); - - $assetsDir = $this->assets->getAssetsDir(); - $this->assets->setAssetsDir(new FilesystemAdapter(new Filesystem(new NullAdapter))); - - try { - $this->assets->makeCss()->commit(); - - foreach ($this->locales->getLocales() as $locale => $name) { - $this->assets->makeLocaleCss($locale)->commit(); - } - } catch (Less_Exception_Parser $e) { - throw new ValidationException(['custom_less' => $e->getMessage()]); - } - - $this->assets->setAssetsDir($assetsDir); - $this->container->instance(SettingsRepositoryInterface::class, $settings); + if (! isset($event->settings['custom_less'])) { + return; } + + // We haven't saved the settings yet, but we want to trial a full + // recompile of the CSS to see if this custom LESS will break + // anything. In order to do that, we will temporarily override the + // settings repository with the new settings so that the recompile + // is effective. We will also use a dummy filesystem so that nothing + // is actually written yet. + + $settings = $this->container->make(SettingsRepositoryInterface::class); + + $this->container->extend( + SettingsRepositoryInterface::class, + function ($settings) use ($event) { + return new OverrideSettingsRepository($settings, $event->settings); + } + ); + + $assetsDir = $this->assets->getAssetsDir(); + $this->assets->setAssetsDir(new FilesystemAdapter(new Filesystem(new NullAdapter))); + + try { + $this->assets->makeCss()->commit(); + + foreach ($this->locales->getLocales() as $locale => $name) { + $this->assets->makeLocaleCss($locale)->commit(); + } + } catch (Less_Exception_Parser $e) { + throw new ValidationException(['custom_less' => $e->getMessage()]); + } + + $this->assets->setAssetsDir($assetsDir); + $this->container->instance(SettingsRepositoryInterface::class, $settings); } /** @@ -109,12 +111,14 @@ class ValidateCustomLess */ public function whenSettingsSaved(Saved $event) { - if (isset($event->settings['custom_less'])) { - $this->assets->makeCss()->flush(); + if (! isset($event->settings['custom_less'])) { + return; + } - foreach ($this->locales->getLocales() as $locale => $name) { - $this->assets->makeLocaleCss($locale)->flush(); - } + $this->assets->makeCss()->flush(); + + foreach ($this->locales->getLocales() as $locale => $name) { + $this->assets->makeLocaleCss($locale)->flush(); } } }