From 501af7cb236beca24f504a3e83edc43f1816a69e Mon Sep 17 00:00:00 2001 From: Wehr Mario Date: Sun, 24 Sep 2023 19:53:48 +0200 Subject: [PATCH 1/2] Minor optimizations. Make Code checker happy. --- filter.php | 37 +++++++++++++++++++++---------------- settings.php | 24 ++++++++++++++---------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/filter.php b/filter.php index beda27c..fe4ee32 100755 --- a/filter.php +++ b/filter.php @@ -33,21 +33,21 @@ class filter_tabs extends moodle_text_filter { /** * Placeholder for tabs. */ - const PLACEHOLDER_PATTERN = '/\{%:([^}]*)\}(.*?)\{%\}/s'; + const PLACEHOLDER_PATTERN = '/\{%:([^}]*)}(.*?)\{%}/s'; /** * Page. * * @var moodle_page $page. */ - private $page; + public moodle_page $page; /** * Plugin renderer. * * @var renderer $renderer. */ - private $renderer; + private static renderer $renderer; /** * Insert JS scripts to page using amd. @@ -55,9 +55,16 @@ class filter_tabs extends moodle_text_filter { * @param moodle_page $page The current page. * @param context $context The current context. */ - public function setup($page, $context) { + public function setup($page, $context): void { $this->page = $page; - $page->requires->js_call_amd('filter_tabs/tabs', 'init'); + + static $initialised = false; + + if (!$initialised) { + $page->requires->js_call_amd('filter_tabs/tabs', 'init'); + $initialised = true; + } + } /** @@ -66,7 +73,7 @@ public function setup($page, $context) { * @param string $text The text to filter. * @param array $options The filter options. */ - public function filter($text, array $options = array() ) { + public function filter($text, array $options = [] ): string { if (!($matches = $this->get_matches($text))) { return $text; } @@ -80,8 +87,8 @@ public function filter($text, array $options = array() ) { * @param string $text * @return null|array */ - private function get_matches(string $text) { - if (!is_string($text) || empty($text) || strpos($text, '{%') === false || + private function get_matches(string $text): ?array { + if (!is_string($text) || empty($text) || !str_contains($text, '{%') || !preg_match_all(self::PLACEHOLDER_PATTERN, $text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE)) { return null; } @@ -95,14 +102,12 @@ private function get_matches(string $text) { * @param array $matches * @return string */ - private function replace_text(string $text, array $matches) { + private function replace_text(string $text, array $matches): string { $textbefore = substr($text, 0, strpos($text, "{%:")); $textafter = substr($text, strpos($text, "{%:")); - $text = preg_replace(self::PLACEHOLDER_PATTERN, "", $textbefore) + return preg_replace(self::PLACEHOLDER_PATTERN, "", $textbefore) . $this->generate_tabs($matches) . preg_replace(self::PLACEHOLDER_PATTERN, "", $textafter); - - return $text; } /** @@ -111,14 +116,14 @@ private function replace_text(string $text, array $matches) { * @param array $matches * @return string */ - private function generate_tabs(array $matches) { - if ($this->renderer === null) { - $this->renderer = $this->page->get_renderer('filter_tabs'); + private function generate_tabs(array $matches): string { + if (!isset(self::$renderer)) { + self::$renderer = $this->page->get_renderer('filter_tabs'); } $config = config::create(get_config('filter_tabs')); $tabs = tab::from_matches($matches); - return $this->renderer->render(new renderable($config->get_template(), $tabs)); + return self::$renderer->render(new renderable($config->get_template(), $tabs)); } } diff --git a/settings.php b/settings.php index 80e8acd..207d021 100755 --- a/settings.php +++ b/settings.php @@ -23,6 +23,9 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +use filter_tabs\config; +use filter_tabs\helper; + defined('MOODLE_INTERNAL') || die(); if ($ADMIN->fulltree) { @@ -37,13 +40,14 @@ '') ); - $tabsconfigsoptions = array( - \filter_tabs\config::YUI_TABS => get_string('enableyui', 'filter_tabs', null, true), - \filter_tabs\config::BOOTSTRAP_2_TABS => get_string('enablebootstrap2', 'filter_tabs', null, true), - \filter_tabs\config::BOOTSTRAP_4_TABS => get_string('enablebootstrap4', 'filter_tabs', null, true) - ); + $tabsconfigsoptions = [ + config::YUI_TABS => get_string('enableyui', 'filter_tabs', null, true), + config::BOOTSTRAP_2_TABS => get_string('enablebootstrap2', 'filter_tabs', null, true), + config::BOOTSTRAP_4_TABS => get_string('enablebootstrap4', 'filter_tabs', null, true), + ]; - if (($bootstrapversion = \filter_tabs\helper::get_bootstrap_version())) { + $version = null; + if (($bootstrapversion = helper::get_bootstrap_version())) { $version = substr($bootstrapversion, 0, 1); } @@ -51,7 +55,7 @@ 'filter_tabs/enablebootstrap', get_string('selecttabs', 'filter_tabs', null, true), get_string('selecttabs_desc', 'filter_tabs', null, true), - '4' === $version ? \filter_tabs\config::BOOTSTRAP_4_TABS : \filter_tabs\config::BOOTSTRAP_2_TABS, + '4' === $version ? config::BOOTSTRAP_4_TABS : config::BOOTSTRAP_2_TABS, $tabsconfigsoptions) ); @@ -59,10 +63,10 @@ $suggestedoption = ''; if ($version !== '4') { $suggestedoption .= '
' . get_string('suggestedoption', 'filter_tabs', null, true) . - ": [ \"{$tabsconfigsoptions[\filter_tabs\config::BOOTSTRAP_2_TABS]}\" ]"; + ": [ \"{$tabsconfigsoptions[config::BOOTSTRAP_2_TABS]}\" ]"; } else { $suggestedoption .= '
' . get_string('suggestedoption', 'filter_tabs', null, true) . - ": [ \"{$tabsconfigsoptions[\filter_tabs\config::BOOTSTRAP_4_TABS]}\" ]"; + ": [ \"{$tabsconfigsoptions[config::BOOTSTRAP_4_TABS]}\" ]"; } // Bootstrap suggestion. @@ -74,7 +78,7 @@ // Preview. $context = context_system::instance(); - $filtertabs = new filter_tabs($context, array()); + $filtertabs = new filter_tabs($context, []); $filtertabs->setup($PAGE, $context); $tabsfilteredtext = $filtertabs->filter('{%:First tab}Some text{%}{%:Second tab}Another text{%}'); $settings->add(new admin_setting_heading( From 0dbed03fb80fdaaddbe8362d654de66344263f92 Mon Sep 17 00:00:00 2001 From: Wehr Mario Date: Sun, 24 Sep 2023 19:53:48 +0200 Subject: [PATCH 2/2] Minor optimizations. Make Code checker happy. --- filter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filter.php b/filter.php index fe4ee32..b75d7f8 100755 --- a/filter.php +++ b/filter.php @@ -40,7 +40,7 @@ class filter_tabs extends moodle_text_filter { * * @var moodle_page $page. */ - public moodle_page $page; + private moodle_page $page; /** * Plugin renderer.