Utility::getWysiwygForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 8
dl 0
loc 16
rs 9.8666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace XoopsModules\Extgallery;
4
5
use XoopsModules\Extgallery;
6
use XoopsModules\Extgallery\Common;
7
8
//use XoopsModules\Extgallery\Constants;
9
10
/**
11
 * Class Utility
12
 */
13
class Utility extends Common\SysUtility
14
{
15
    //--------------- Custom module methods -----------------------------
16
    /**
17
     * @param $option
18
     * @return bool|mixed
19
     */
20
    public static function getModuleOption($option)
21
    {
22
        global $xoopsModule;
23
        /** @var \XoopsModules\Extgallery\Helper $helper */
24
        $helper = \XoopsModules\Extgallery\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
25
        static $tbloptions = [];
26
        if (\is_array($tbloptions) && \array_key_exists($option, $tbloptions)) {
27
            return $tbloptions[$option];
28
        }
29
30
        $retval = false;
31
        if (isset($xoopsModuleConfig)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $xoopsModuleConfig seems to never exist and therefore isset should always be false.
Loading history...
32
            && (\is_object($xoopsModule) && 'extgallery' === $xoopsModule->getVar('dirname')
33
                && $xoopsModule->getVar('isactive'))) {
34
            if (isset($xoopsModuleConfig[$option])) {
35
                $retval = $xoopsModuleConfig[$option];
36
            }
37
        } else {
38
            /** @var \XoopsModuleHandler $moduleHandler */
39
            $moduleHandler = \xoops_getHandler('module');
40
            $module        = $moduleHandler->getByDirname('extgallery');
41
42
            /** @var \XoopsModuleHandler $moduleHandler */
43
            $configHandler = \xoops_getHandler('config');
44
            if ($module) {
45
                $configurator = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
0 ignored issues
show
Bug introduced by
The method getConfigsByCat() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
                /** @scrutinizer ignore-call */ 
46
                $configurator = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
Loading history...
46
                if (isset($configurator[$option])) {
47
                    $retval = $configurator[$option];
48
                }
49
            }
50
        }
51
        $tbloptions[$option] = $retval;
52
53
        return $retval;
54
    }
55
56
    /**
57
     * @param $caption
58
     * @param $name
59
     * @param $value
60
     * @param $rows
61
     * @param $cols
62
     * @param $width
63
     * @param $height
64
     * @param $supplemental
65
     *
66
     * @return bool|\XoopsFormEditor
67
     */
68
    public static function getWysiwygForm($caption, $name, $value, $rows, $cols, $width, $height, $supplemental)
0 ignored issues
show
Unused Code introduced by
The parameter $supplemental is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

68
    public static function getWysiwygForm($caption, $name, $value, $rows, $cols, $width, $height, /** @scrutinizer ignore-unused */ $supplemental)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        $editor_option            = mb_strtolower(static::getModuleOption('form_options'));
0 ignored issues
show
Bug introduced by
It seems like static::getModuleOption('form_options') can also be of type boolean; however, parameter $string of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $editor_option            = mb_strtolower(/** @scrutinizer ignore-type */ static::getModuleOption('form_options'));
Loading history...
71
        $editor                   = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $editor is dead and can be removed.
Loading history...
72
        $editor_configs           = [];
73
        $editor_configs['name']   = $name;
74
        $editor_configs['value']  = $value;
75
        $editor_configs['rows']   = $rows;
76
        $editor_configs['cols']   = $cols;
77
        $editor_configs['width']  = $width;
78
        $editor_configs['height'] = $height;
79
        $editor_configs['editor'] = $editor_option;
80
81
        $editor = new \XoopsFormEditor($caption, $name, $editor_configs);
82
83
        return $editor;
84
    }
85
86
    /**
87
     * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags
88
     * www.gsdesign.ro/blog/cut-html-string-without-breaking-the-tags
89
     * www.cakephp.org
90
     *
91
     * @param string $text         String to truncate.
92
     * @param int    $length       Length of returned string, including ellipsis.
93
     * @param string $ending       Ending to be appended to the trimmed string.
94
     * @param bool   $exact        If false, $text will not be cut mid-word
95
     * @param bool   $considerHtml If true, HTML tags would be handled correctly
96
     *
97
     * @return string Trimmed string.
98
     */
99
    public static function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true)
100
    {
101
        $open_tags = [];
102
        if ($considerHtml) {
103
            // if the plain text is shorter than the maximum length, return the whole text
104
            if (mb_strlen(\preg_replace('/<.*?' . '>/', '', $text)) <= $length) {
105
                return $text;
106
            }
107
            // splits all html-tags to scanable lines
108
            \preg_match_all('/(<.+?' . '>)?([^<>]*)/s', $text, $lines, \PREG_SET_ORDER);
109
            $total_length = mb_strlen($ending);
110
            $truncate     = '';
111
            foreach ($lines as $line_matchings) {
112
                // if there is any html-tag in this line, handle it and add it (uncounted) to the output
113
                if (!empty($line_matchings[1])) {
114
                    // if it's an "empty element" with or without xhtml-conform closing slash
115
                    if (\preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
116
                        // do nothing
117
                        // if tag is a closing tag
118
                    } elseif (\preg_match('/^<\s*\/(\S+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
119
                        // delete tag from $open_tags list
120
                        $pos = \array_search($tag_matchings[1], $open_tags, true);
121
                        if (false !== $pos) {
122
                            unset($open_tags[$pos]);
123
                        }
124
                        // if tag is an opening tag
125
                    } elseif (\preg_match('/^<\s*([^\s>!]+).*?' . '>$/s', $line_matchings[1], $tag_matchings)) {
126
                        // add tag to the beginning of $open_tags list
127
                        \array_unshift($open_tags, mb_strtolower($tag_matchings[1]));
128
                    }
129
                    // add html-tag to $truncate'd text
130
                    $truncate .= $line_matchings[1];
131
                }
132
                // calculate the length of the plain text part of the line; handle entities as one character
133
                $content_length = mb_strlen(\preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
134
                if ($total_length + $content_length > $length) {
135
                    // the number of characters which are left
136
                    $left            = $length - $total_length;
137
                    $entities_length = 0;
138
                    // search for html entities
139
                    if (\preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, \PREG_OFFSET_CAPTURE)) {
140
                        // calculate the real length of all entities in the legal range
141
                        foreach ($entities[0] as $entity) {
142
                            if ($left >= $entity[1] + 1 - $entities_length) {
143
                                $left--;
144
                                $entities_length += mb_strlen($entity[0]);
145
                            } else {
146
                                // no more characters left
147
                                break;
148
                            }
149
                        }
150
                    }
151
                    $truncate .= mb_substr($line_matchings[2], 0, $left + $entities_length);
152
                    // maximum lenght is reached, so get off the loop
153
                    break;
154
                }
155
                $truncate     .= $line_matchings[2];
156
                $total_length += $content_length;
157
158
                // if the maximum length is reached, get off the loop
159
                if ($total_length >= $length) {
160
                    break;
161
                }
162
            }
163
        } else {
164
            if (mb_strlen($text) <= $length) {
165
                return $text;
166
            }
167
            $truncate = mb_substr($text, 0, $length - mb_strlen($ending));
168
        }
169
        // if the words shouldn't be cut in the middle...
170
        if (!$exact) {
171
            // ...search the last occurance of a space...
172
            $spacepos = mb_strrpos($truncate, ' ');
173
            if (isset($spacepos)) {
174
                // ...and cut the text in this position
175
                $truncate = mb_substr($truncate, 0, $spacepos);
176
            }
177
        }
178
        // add the defined ending to the text
179
        $truncate .= $ending;
180
        if ($considerHtml) {
181
            // close all unclosed html-tags
182
            foreach ($open_tags as $tag) {
183
                $truncate .= '</' . $tag . '>';
184
            }
185
        }
186
187
        return $truncate;
188
    }
189
}
190