Songs::toArray()   F
last analyzed

Complexity

Conditions 28
Paths 8232

Size

Total Lines 113
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 28
eloc 74
c 1
b 0
f 0
nc 8232
nop 1
dl 0
loc 113
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Songlist;
4
5
use Criteria;
6
use MyTextSanitizer;
7
use XoopsObject;
8
9
require_once \dirname(__DIR__) . '/include/songlist.object.php';
10
// require_once \dirname(__DIR__) . '/include/songlist.form.php';
11
use  XoopsModules\Songlist\Form\FormController;
12
13
/**
14
 * Class Songs
15
 */
16
class Songs extends XoopsObject
17
{
18
    /**
19
     * Songs constructor.
20
     * @param null $fid
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fid is correct as it would always require null to be passed?
Loading history...
21
     */
22
    public function __construct($fid = null)
23
    {
24
        $this->initVar('sid', \XOBJ_DTYPE_INT, 0, false);
25
        $this->initVar('cid', \XOBJ_DTYPE_INT, 0, false);
26
        $this->initVar('gids', \XOBJ_DTYPE_ARRAY, 0, false);
27
        $this->initVar('vcid', \XOBJ_DTYPE_INT, 0, false);
28
        $this->initVar('aids', \XOBJ_DTYPE_ARRAY, [], false);
29
        $this->initVar('abid', \XOBJ_DTYPE_INT, 0, false);
30
        $this->initVar('songid', \XOBJ_DTYPE_TXTBOX, null, false, 32);
31
        $this->initVar('traxid', \XOBJ_DTYPE_TXTBOX, null, false, 32);
32
        $this->initVar('title', \XOBJ_DTYPE_TXTBOX, null, false, 128);
33
        $this->initVar('lyrics', \XOBJ_DTYPE_OTHER, null, false);
34
        $this->initVar('hits', \XOBJ_DTYPE_INT, 0, false);
35
        $this->initVar('rank', \XOBJ_DTYPE_DECIMAL, 0, false);
36
        $this->initVar('votes', \XOBJ_DTYPE_INT, 0, false);
37
        $this->initVar('tags', \XOBJ_DTYPE_TXTBOX, null, false, 255);
38
        $this->initVar('mp3', \XOBJ_DTYPE_OTHER, null, false, 500);
39
        $this->initVar('created', \XOBJ_DTYPE_INT, 0, false);
40
        $this->initVar('updated', \XOBJ_DTYPE_INT, 0, false);
41
    }
42
43
    /**
44
     * @param bool $as_array
45
     * @return array|string
46
     */
47
    public function getForm($as_array = false)
48
    {
49
        return FormController::getFormSongs($this, $as_array);
50
    }
51
52
    /**
53
     * @param bool $extra
54
     * @return array
55
     */
56
    public function toArray($extra = true): array
57
    {
58
        $ret = parent::toArray();
59
60
        $GLOBALS['myts'] = MyTextSanitizer::getInstance();
61
62
        $ret['lyrics'] = $GLOBALS['myts']->displayTarea($this->getVar('lyrics'), true, true, true, true, true);
63
64
        $form = $this->getForm(true);
65
        foreach ($form as $key => $element) {
66
            $ret['form'][$key] = $element->render();
67
        }
68
        foreach (['created', 'updated'] as $key) {
69
            if ($this->getVar($key) > 0) {
70
                $ret['form'][$key] = \date(_DATESTRING, $this->getVar($key));
71
                $ret[$key]         = \date(_DATESTRING, $this->getVar($key));
72
            }
73
        }
74
75
        $ret['url'] = $this->getURL();
76
77
        $ret['rank'] = \number_format(($this->getVar('rank') > 0 && $this->getVar('votes') > 0 ? $this->getVar('rank') / $this->getVar('votes') : 0), 2) . \_MI_SONGLIST_OFTEN;
78
79
        if (!empty($ret['mp3'])) {
80
            $ret['mp3'] = '<embed flashvars="playerID=1&amp;bg=0xf8f8f8&amp;leftbg=0x3786b3&amp;lefticon=0x78bee3&amp;rightbg=0x3786b3&amp;rightbghover=0x78bee3&amp;righticon=0x78bee3&amp;righticonhover=0x3786b3&amp;text=0x666666&amp;slider=0x3786b3&amp;track=0xcccccc&amp;border=0x666666&amp;loader=0x78bee3&amp;loop=no&amp;soundFile='
81
                          . $ret['mp3']
82
                          . "\" quality='high' menu='false' wmode='transparent' pluginspage='https://www.macromedia.com/go/getflashplayer' src='"
83
                          . XOOPS_URL
84
                          . "/images/form/player.swf'  width=290 height=24 type='application/x-shockwave-flash'></embed>";
85
        }
86
87
        $helper = Helper::getInstance();
88
        if (1 == $helper->getConfig('tags')
89
            && \class_exists(\XoopsModules\Tag\Tagbar::class)
90
            && \xoops_isActiveModule('tag')) {
91
            $tagbarObj     = new \XoopsModules\Tag\Tagbar();
92
            $ret['tagbar'] = $tagbarObj->getTagbar($this->getVar('sid'), $this->getVar('cid'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('sid') can also be of type boolean and string; however, parameter $tags of XoopsModules\Tag\Tagbar::getTagbar() does only seem to accept array|integer, 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

92
            $ret['tagbar'] = $tagbarObj->getTagbar(/** @scrutinizer ignore-type */ $this->getVar('sid'), $this->getVar('cid'));
Loading history...
93
        }
94
95
        $extrasHandler     = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Extras');
96
        $fieldHandler      = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Field');
97
        $visibilityHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Visibility');
98
99
        $extras = $extrasHandler->get($this->getVar('sid'));
100
        if ($extras) {
0 ignored issues
show
introduced by
$extras is of type object, thus it always evaluated to true.
Loading history...
101
            if (\is_object($GLOBALS['xoopsUser'])) {
102
                $fields_id = $visibilityHandler->getVisibleFields([], $GLOBALS['xoopsUser']->getGroups());
103
            } elseif (!\is_object($GLOBALS['xoopsUser'])) {
104
                $fields_id = $visibilityHandler->getVisibleFields([], []);
105
            }
106
107
            if (\count($fields_id) > 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $fields_id does not seem to be defined for all execution paths leading up to this point.
Loading history...
108
                $criteria = new Criteria('field_id', '(' . \implode(',', $fields_id) . ')', 'IN');
109
                $criteria->setSort('field_weight');
110
                $fields = $fieldHandler->getObjects($criteria, true);
111
                foreach ($fields as $id => $field) {
112
                    if (\in_array($this->getVar('cid'), $field->getVar('cids'), true)) {
113
                        $ret['fields'][$id]['title'] = $field->getVar('field_title');
114
                        if (\is_object($GLOBALS['xoopsUser'])) {
115
                            $ret['fields'][$id]['value'] = htmlspecialchars_decode($field->getOutputValue($GLOBALS['xoopsUser'], $extras));
116
                        } elseif (!\is_object($GLOBALS['xoopsUser'])) {
117
                            $ret['fields'][$id]['value'] = htmlspecialchars_decode($extras->getVar($field->getVar('field_name')));
118
                        }
119
                    }
120
                }
121
            }
122
        }
123
124
        if (!$extra) {
125
            return $ret;
126
        }
127
128
        if (0 != $this->getVar('cid')) {
129
            $categoryHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Category');
130
            $category        = $categoryHandler->get($this->getVar('cid'));
131
            $ret['category'] = $category->toArray(false);
0 ignored issues
show
Unused Code introduced by
The call to XoopsObject::toArray() has too many arguments starting with false. ( Ignorable by Annotation )

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

131
            /** @scrutinizer ignore-call */ 
132
            $ret['category'] = $category->toArray(false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
132
        }
133
134
        if (0 != \count($this->getVar('gids'))) {
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('gids') can also be of type boolean and null and string; however, parameter $value of count() does only seem to accept Countable|array, 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

134
        if (0 != \count(/** @scrutinizer ignore-type */ $this->getVar('gids'))) {
Loading history...
135
            $i            = 0;
136
            $genreHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Genre');
137
            $ret['genre'] = '';
138
            $genres       = $genreHandler->getObjects(new Criteria('gid', '(' . \implode(',', $this->getVar('gids')) . ')', 'IN'), true);
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('gids') can also be of type boolean and null and string; however, parameter $pieces of implode() does only seem to accept array, 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

138
            $genres       = $genreHandler->getObjects(new Criteria('gid', '(' . \implode(',', /** @scrutinizer ignore-type */ $this->getVar('gids')) . ')', 'IN'), true);
Loading history...
139
            foreach ($genres as $gid => $genre) {
140
                $ret['genre_array'][$gid] = $genre->toArray(false);
141
                ++$i;
142
                $ret['genre'] .= $genre->getVar('name') . ($i < \count($genres) ? ', ' : '');
143
            }
144
        }
145
        if (0 != $this->getVar('vcid')) {
146
            $voiceHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Voice');
147
            $voice        = $voiceHandler->get($this->getVar('vcid'));
148
            $ret['voice'] = $voice->toArray(false);
149
        }
150
151
        if (0 != \count($this->getVar('aids'))) {
152
            $artistsHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Artists');
153
            foreach ($this->getVar('aids') as $aid) {
154
                $artist                     = $artistsHandler->get($aid);
155
                $ret['artists_array'][$aid] = $artist->toArray(false);
156
            }
157
        }
158
159
        if (0 != $this->getVar('abid')) {
160
            $albumsHandler = \XoopsModules\Songlist\Helper::getInstance()->getHandler('Albums');
161
            $albums        = $albumsHandler->get($this->getVar('abid'));
162
            if (null !== $albums) {
163
                $ret['albums'] = $albums->toArray(false);
164
            }
165
166
        }
167
168
        return $ret;
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function getURL(): string
175
    {
176
        global $file, $op, $fct, $id, $value, $vcid, $gid, $cid, $start, $limit;
177
        if ($GLOBALS['songlistModuleConfig']['htaccess']) {
178
            return XOOPS_URL . '/' . $GLOBALS['songlistModuleConfig']['baseofurl'] . '/index/' . \urlencode(\str_replace([' ', \chr(9)], '-', $this->getVar('title'))) . '/item-item-' . $this->getVar('sid') . $GLOBALS['songlistModuleConfig']['endofurl'];
179
        }
180
181
        return XOOPS_URL . '/modules/songlist/index.php?op=item&fct=item&id=' . $this->getVar('sid') . '&value=' . \urlencode($value ?? '') . '&vcid=' . $vcid . '&gid=' . $gid . '&cid=' . $cid;
182
    }
183
}
184