From 6e656d65f8cbaebe94dc77d04ea3839d97f0e3c1 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 22 Apr 2023 01:18:46 +0200 Subject: [PATCH] fix nullsafe warning --- src/BackendBehaviors.php | 27 +++++++++++++++++++-------- src/Config.php | 16 ++++++++++++++-- src/Frontend.php | 37 +++++++++++++++++++++---------------- src/Install.php | 4 ++++ 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/src/BackendBehaviors.php b/src/BackendBehaviors.php index e12df5b..0f78992 100644 --- a/src/BackendBehaviors.php +++ b/src/BackendBehaviors.php @@ -16,7 +16,6 @@ namespace Dotclear\Plugin\fac; use cursor; use ArrayObject; -use dcAuth; use dcCore; use dcPage; use dcPostsActions; @@ -79,6 +78,9 @@ class BackendBehaviors */ public static function adminBlogPreferencesFormV2(dcSettings $blog_settings): void { + if (is_null(dcCore::app()->auth) || is_null(dcCore::app()->adminurl)) { + return; + } $lines = ''; $fac_public_tpltypes = json_decode($blog_settings->get(My::id())->get('public_tpltypes'), true); if (!is_array($fac_public_tpltypes)) { @@ -168,7 +170,7 @@ class BackendBehaviors */ public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, ?dcRecord $post): void { - if (!dcCore::app()->blog->settings->get(My::id())->get('active')) { + if (is_null(dcCore::app()->blog) || !dcCore::app()->blog->settings->get(My::id())->get('active')) { return; } @@ -231,7 +233,7 @@ class BackendBehaviors */ public static function adminPostsActions(dcPostsActions $pa): void { - if (!dcCore::app()->blog->settings->get(My::id())->get('active')) { + if (is_null(dcCore::app()->blog) || is_null(dcCore::app()->auth) || !dcCore::app()->blog->settings->get(My::id())->get('active')) { return; } @@ -241,8 +243,8 @@ class BackendBehaviors ); if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ - dcAuth::PERMISSION_DELETE, - dcAuth::PERMISSION_CONTENT_ADMIN, + dcCore::app()->auth::PERMISSION_DELETE, + dcCore::app()->auth::PERMISSION_CONTENT_ADMIN, ]), dcCore::app()->blog->id)) { return; } @@ -260,6 +262,9 @@ class BackendBehaviors */ public static function callbackRemove(dcPostsActions $pa, ArrayObject $post): void { + if (is_null(dcCore::app()->blog) || is_null(dcCore::app()->auth)) { + return; + } # No entry $posts_ids = $pa->getIDs(); if (empty($posts_ids)) { @@ -268,8 +273,8 @@ class BackendBehaviors # No right if (!dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ - dcAuth::PERMISSION_DELETE, - dcAuth::PERMISSION_CONTENT_ADMIN, + dcCore::app()->auth::PERMISSION_DELETE, + dcCore::app()->auth::PERMISSION_CONTENT_ADMIN, ]), dcCore::app()->blog->id)) { throw new Exception(__('No enough right')); } @@ -291,6 +296,9 @@ class BackendBehaviors */ public static function callbackAdd(dcPostsActions $pa, ArrayObject $post): void { + if (is_null(dcCore::app()->blog)) { + return; + } # No entry $posts_ids = $pa->getIDs(); if (empty($posts_ids)) { @@ -341,7 +349,7 @@ class BackendBehaviors */ protected static function formFeed(string $url = '', string $format = ''): string { - if (!dcCore::app()->blog->settings->get(My::id())->get('active')) { + if (is_null(dcCore::app()->blog) || !dcCore::app()->blog->settings->get(My::id())->get('active')) { return ''; } @@ -369,6 +377,9 @@ class BackendBehaviors */ protected static function comboFac(): array { + if (is_null(dcCore::app()->blog)) { + return []; + } $formats = json_decode(dcCore::app()->blog->settings->get(My::id())->get('formats'), true); if (!is_array($formats) || empty($formats)) { return []; diff --git a/src/Config.php b/src/Config.php index c6e8f8f..3f1e158 100644 --- a/src/Config.php +++ b/src/Config.php @@ -35,7 +35,8 @@ class Config extends dcNsProcess public static function init(): bool { static::$init == defined('DC_CONTEXT_ADMIN') - && dcCore::app()->auth?->isSuperAdmin(); + && !is_null(dcCore::app()->auth) + && dcCore::app()->auth->isSuperAdmin(); return static::$init; } @@ -46,6 +47,11 @@ class Config extends dcNsProcess return false; } + //nullsafe + if (is_null(dcCore::app()->blog) || is_null(dcCore::app()->adminurl)) { + return false; + } + $redir = empty($_REQUEST['redir']) ? dcCore::app()->admin->__get('list')->getURL() . '#plugins' : $_REQUEST['redir']; @@ -85,7 +91,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()] ); @@ -102,6 +108,12 @@ class Config extends dcNsProcess if (!static::$init) { return; } + + //nullsafe + if (is_null(dcCore::app()->blog)) { + return; + } + $s = dcCore::app()->blog->settings->get(My::id()); $fac_formats = json_decode($s->get('formats'), true); diff --git a/src/Frontend.php b/src/Frontend.php index 4bced90..d7b0888 100644 --- a/src/Frontend.php +++ b/src/Frontend.php @@ -33,24 +33,29 @@ class Frontend extends dcNsProcess public static function process(): bool { - if (!static::$init || !dcCore::app()->blog->settings->get(My::id())->get('active')) { + if (!static::$init || is_null(dcCore::app()->blog) || !dcCore::app()->blog->settings->get(My::id())->get('active')) { return false; } dcCore::app()->addBehavior('publicEntryAfterContent', function (dcCore $core, context $_ctx): void { - # Not a post + //nullsafe + if (is_null(dcCore::app()->blog) || is_null(dcCore::app()->ctx)) { + return; + } + + // Not a post if (!dcCore::app()->ctx->exists('posts')) { return; } - # Not in page to show + // Not in page to show $types = json_decode((string) dcCore::app()->blog->settings->get(My::id())->get('public_tpltypes'), true); if (!is_array($types) || !in_array(dcCore::app()->url->type, $types)) { return; } - # Get related feed + // Get related feed $fac_url = dcCore::app()->meta->getMetadata([ 'meta_type' => 'fac', 'post_id' => dcCore::app()->ctx->__get('posts')->f('post_id'), @@ -60,7 +65,7 @@ class Frontend extends dcNsProcess return; } - # Get related format + // Get related format $fac_format = dcCore::app()->meta->getMetadata([ 'meta_type' => 'facformat', 'post_id' => dcCore::app()->ctx->__get('posts')->f('post_id'), @@ -70,7 +75,7 @@ class Frontend extends dcNsProcess return; } - # Get format info + // Get format info $default_format = [ 'name' => 'default', 'dateformat' => '', @@ -98,7 +103,7 @@ class Frontend extends dcNsProcess ); } - # Read feed url + // Read feed url $cache = is_dir(DC_TPL_CACHE . '/fac') ? DC_TPL_CACHE . '/fac' : null; try { @@ -107,12 +112,12 @@ class Frontend extends dcNsProcess $feed = null; } - # No entries + // No entries if (!$feed) { return; } - # Feed title + // Feed title $feedtitle = ''; if ('' != dcCore::app()->blog->settings->get(My::id())->get('defaultfeedtitle')) { $feedtitle = '

' . Html::escapeHTML( @@ -130,7 +135,7 @@ class Frontend extends dcNsProcess ) . '

'; } - # Feed desc + // Feed desc $feeddesc = ''; if (dcCore::app()->blog->settings->get(My::id())->get('showfeeddesc') && '' != $feed->description) { @@ -140,12 +145,12 @@ class Frontend extends dcNsProcess ) . '

'; } - # Date format + // Date format $dateformat = '' != $format['dateformat'] ? $format['dateformat'] : dcCore::app()->blog->settings->get('system')->get('date_format') . ',' . dcCore::app()->blog->settings->get('system')->get('time_format'); - # Enrties limit + // Enrties limit $entrieslimit = abs((int) $format['lineslimit']); $uselimit = $entrieslimit > 0 ? true : false; @@ -159,7 +164,7 @@ class Frontend extends dcNsProcess # Format date $date = Date::dt2str($dateformat, $item->pubdate); - # Entries title + // Entries title $title = context::global_filters( str_replace( [ @@ -181,7 +186,7 @@ class Frontend extends dcNsProcess ['remove_html', 'cut_string' => abs((int) $format['linestitlelength'])], ); - # Entries over title + // Entries over title $overtitle = context::global_filters( str_replace( [ @@ -203,7 +208,7 @@ class Frontend extends dcNsProcess ['remove_html', 'cut_string' => 350], ); - # Entries description + // Entries description $description = ''; if ($format['showlinesdescription'] && '' != $item->description) { @@ -214,7 +219,7 @@ class Frontend extends dcNsProcess ) . ''; } - # Entries content + // Entries content $content = ''; if ($format['showlinescontent'] && '' != $item->content) { diff --git a/src/Install.php b/src/Install.php index fa2ff15..a240acd 100644 --- a/src/Install.php +++ b/src/Install.php @@ -35,6 +35,10 @@ class Install extends dcNsProcess return false; } + if (is_null(dcCore::app()->blog)) { + return false; + } + // Module specs $mod_conf = [ [