From 93ec64c3ba2ab250cc720d75d07f8816c7c2ed7a Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Mon, 24 Apr 2023 15:34:16 +0200 Subject: [PATCH] fix nullsafe warnings --- src/Backend.php | 5 +++-- src/BackendBehaviors.php | 26 +++++++++++++++++--------- src/Config.php | 3 ++- src/Settings.php | 9 +++++++++ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/Backend.php b/src/Backend.php index 9815955..7c13a20 100644 --- a/src/Backend.php +++ b/src/Backend.php @@ -21,8 +21,9 @@ class Backend extends dcNsProcess { public static function init(): bool { - static::$init = My::phpCompliant() - && defined('DC_CONTEXT_ADMIN') + static::$init = defined('DC_CONTEXT_ADMIN') + && !is_null(dcCore::app()->auth) && !is_null(dcCore::app()->blog) + && My::phpCompliant() && dcCore::app()->auth->isSuperAdmin() && dcCore::app()->blog->settings->get(My::id())->get('active'); diff --git a/src/BackendBehaviors.php b/src/BackendBehaviors.php index 55f0309..2109f18 100644 --- a/src/BackendBehaviors.php +++ b/src/BackendBehaviors.php @@ -43,7 +43,7 @@ class BackendBehaviors public static function packmanBeforeCreatePackage(array $module): void { - if (!dcCore::app()->blog->settings->get(My::id())->get('packman')) { + if (is_null(dcCore::app()->blog) || !dcCore::app()->blog->settings->get(My::id())->get('packman')) { return; } @@ -56,6 +56,10 @@ class BackendBehaviors public static function modulesToolsHeaders(bool $is_plugin): string { + if (is_null(dcCore::app()->auth) || is_null(dcCore::app()->auth->user_prefs)) { + return ''; + } + return dcPage::jsJson('ts_copied', ['alert' => __('Copied to clipboard')]) . dcPage::jsModuleLoad(My::id() . '/js/backend.js') . @@ -68,16 +72,20 @@ class BackendBehaviors public static function pluginsToolsTabsV2(): void { - self::modulesToolsTabs(dcCore::app()->plugins, explode(',', DC_DISTRIB_PLUGINS), dcCore::app()->adminurl->get('admin.plugins')); + self::modulesToolsTabs(dcCore::app()->plugins, explode(',', DC_DISTRIB_PLUGINS), (string) dcCore::app()->adminurl?->get('admin.plugins')); } public static function themesToolsTabsV2(): void { - self::modulesToolsTabs(dcCore::app()->themes, explode(',', DC_DISTRIB_THEMES), dcCore::app()->adminurl->get('admin.blog.theme')); + self::modulesToolsTabs(dcCore::app()->themes, explode(',', DC_DISTRIB_THEMES), (string) dcCore::app()->adminurl?->get('admin.blog.theme')); } private static function modulesToolsTabs(dcModules $modules, array $excludes, string $page_url): void { + if (is_null(dcCore::app()->adminurl) || is_null(dcCore::app()->auth) || is_null(dcCore::app()->auth->user_prefs)) { + return; + } + $page_url .= '#' . My::id(); $user_ui_colorsyntax = dcCore::app()->auth->user_prefs->get('interface')->get('colorsyntax'); $user_ui_colorsyntax_theme = dcCore::app()->auth->user_prefs->get('interface')->get('colorsyntax_theme'); @@ -303,31 +311,31 @@ class BackendBehaviors if (!$module->isDefined()) { self::$failed[] = 'unknow module'; } - $rsp->id = $module->get('id'); + $rsp->insertAttr('id', $module->get('id')); # name if (empty($module->get('name'))) { self::$failed[] = 'no module name set in _define.php'; } - $rsp->name($module->get('name')); + $rsp->insertNode(new XmlTag('name', $module->get('name'))); # version if (empty($module->get('version'))) { self::$failed[] = 'no module version set in _define.php'; } - $rsp->version($module->get('version')); + $rsp->insertNode(new XmlTag('version', $module->get('version'))); # author if (empty($module->get('author'))) { self::$failed[] = 'no module author set in _define.php'; } - $rsp->author($module->get('author')); + $rsp->insertNode(new XmlTag('author', $module->get('author'))); # desc if (empty($module->get('desc'))) { self::$failed[] = 'no module description set in _define.php'; } - $rsp->desc($module->get('desc')); + $rsp->insertNode(new XmlTag('desc', $module->get('desc'))); # repository if (empty($module->get('repository'))) { @@ -339,7 +347,7 @@ class BackendBehaviors if (empty($file_pattern)) { self::$failed[] = 'no zip file pattern set in Tweak Store configuration'; } - $rsp->file($file_pattern); + $rsp->insertNode(new XmlTag('file', $file_pattern)); # da dc_min or requires core if (!empty($module->get('requires')) && is_array($module->get('requires'))) { diff --git a/src/Config.php b/src/Config.php index ad945cc..129aa6d 100644 --- a/src/Config.php +++ b/src/Config.php @@ -36,6 +36,7 @@ class Config extends dcNsProcess static::$init = My::phpCompliant() && defined('DC_CONTEXT_ADMIN') && defined('DC_CONTEXT_MODULE') + && !is_null(dcCore::app()->auth) && dcCore::app()->auth->isSuperAdmin(); return static::$init; @@ -61,7 +62,7 @@ class Config extends dcNsProcess dcPage::addSuccessNotice( __('Configuration successfully updated') ); - dcCore::app()->adminurl->redirect( + dcCore::app()->adminurl?->redirect( 'admin.plugins', ['module' => My::id(), 'conf' => 1, 'redir' => dcCore::app()->admin->__get('list')->getRedir()] ); diff --git a/src/Settings.php b/src/Settings.php index 6fd8cda..2d6d84b 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Dotclear\Plugin\tweakStores; use dcCore; +use Exception; class Settings { @@ -32,6 +33,10 @@ class Settings */ public function __construct() { + if (is_null(dcCore::app()->blog)) { + throw new Exception('blog is not set'); + } + $s = dcCore::app()->blog->settings->get(My::id()); $this->active = (bool) ($s->get('active') ?? false); @@ -54,6 +59,10 @@ class Settings */ public function writeSetting(string $key, mixed $value): bool { + if (is_null(dcCore::app()->blog)) { + throw new Exception('blog is not set'); + } + if (property_exists($this, $key) && settype($value, gettype($this->{$key})) === true) { dcCore::app()->blog->settings->get(My::id())->drop($key); dcCore::app()->blog->settings->get(My::id())->put($key, $value, gettype($this->{$key}), '', true, true);