Passed
Push — master ( d3e687...4990f6 )
by Michael
02:43
created

ObjectTree::getAllChildArray()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 6
nop 4
dl 0
loc 19
rs 8.2222
c 0
b 0
f 0
1
<?php namespace XoopsModules\Newbb;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 36 and the first side effect is on line 30.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
//
4
// ------------------------------------------------------------------------ //
5
// This program is free software; you can redistribute it and/or modify     //
6
// it under the terms of the GNU General Public License as published by     //
7
// the Free Software Foundation; either version 2 of the License, or        //
8
// (at your option) any later version.                                      //
9
//                                                                          //
10
// You may not change or alter any portion of this comment or credits       //
11
// of supporting developers from this source code or any supporting         //
12
// source code which is considered copyrighted (c) material of the          //
13
// original comment or credit authors.                                      //
14
//                                                                          //
15
// This program is distributed in the hope that it will be useful,          //
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
18
// GNU General Public License for more details.                             //
19
//                                                                          //
20
// You should have received a copy of the GNU General Public License        //
21
// along with this program; if not, write to the Free Software              //
22
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
23
// ------------------------------------------------------------------------ //
24
// Author: phppp (D.J., [email protected])                                  //
25
//  URL: https://xoops.org                                                    //
26
// Project: Article Project                                                 //
27
// ------------------------------------------------------------------------ //
28
29
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
require_once $GLOBALS['xoops']->path('class/tree.php');
31
32
if (!class_exists('ObjectTree')) {
33
    /**
34
     * Class ObjectTree
35
     */
36
    class ObjectTree extends \XoopsObjectTree
0 ignored issues
show
Bug introduced by
The type XoopsObjectTree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
    {
38
        /**
39
         * @param      $objectArr
40
         * @param null $rootId
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $rootId is correct as it would always require null to be passed?
Loading history...
41
         */
42
        public function __construct(&$objectArr, $rootId = null)
43
        {
44
            parent::__construct($objectArr, 'forum_id', 'parent_forum', $rootId);
45
        }
46
47
        /**
48
         * Make options for a select box from
49
         *
50
         * @param int        $key         ID of the object to display as the root of select options
51
         * @param string     $ret         (reference to a string when called from outside) Result from previous recursions
52
         * @param string     $prefix_orig String to indent items at deeper levels
53
         * @param string     $prefix_curr String to indent the current item
54
         * @param null|array $tags
55
         * @internal  param string $fieldName Name of the member variable from the
56
         *                                node objects that should be used as the title for the options.
57
         * @internal  param string $selected Value to display as selected
58
         * @access    private
59
         */
60
        protected function makeTreeItems($key, &$ret, $prefix_orig, $prefix_curr = '', $tags = null)
61
        {
62
            if ($key > 0) {
63
                if (count($tags) > 0) {
64
                    foreach ($tags as $tag) {
65
                        $ret[$key][$tag] = $this->tree[$key]['obj']->getVar($tag);
66
                    }
67
                } else {
68
                    $ret[$key]['forum_name'] = $this->tree[$key]['obj']->getVar('forum_name');
69
                }
70
                $ret[$key]['prefix'] = $prefix_curr;
71
                $prefix_curr         .= $prefix_orig;
72
            }
73
            if (isset($this->tree[$key]['child']) && !empty($this->tree[$key]['child'])) {
74
                foreach ($this->tree[$key]['child'] as $childkey) {
75
                    $this->makeTreeItems($childkey, $ret, $prefix_orig, $prefix_curr, $tags);
76
                }
77
            }
78
        }
79
80
        /**
81
         * Make a select box with options from the tree
82
         *
83
         * @param  string  $prefix         String to indent deeper levels
84
         * @param  integer $key            ID of the object to display as the root of select options
85
         * @param  null    $tags
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $tags is correct as it would always require null to be passed?
Loading history...
86
         * @return array|string  HTML select box
87
         * @internal param string $name Name of the select box
88
         * @internal param string $fieldName Name of the member variable from the
89
         *                                 node objects that should be used as the title for the options.
90
         * @internal param string $selected Value to display as selected
91
         * @internal param bool $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy
92
         */
93
        public function &makeTree($prefix = '-', $key = 0, $tags = null)
94
        {
95
            $ret = [];
96
            $this->makeTreeItems($key, $ret, $prefix, '', $tags);
0 ignored issues
show
Bug introduced by
$ret of type array is incompatible with the type string expected by parameter $ret of XoopsModules\Newbb\ObjectTree::makeTreeItems(). ( Ignorable by Annotation )

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

96
            $this->makeTreeItems($key, /** @scrutinizer ignore-type */ $ret, $prefix, '', $tags);
Loading history...
97
98
            return $ret;
99
        }
100
101
        /**
102
         * Make a select box with options from the tree
103
         *
104
         * @param  string  $name           Name of the select box
105
         * @param  string  $fieldName      Name of the member variable from the
106
         *                                 node objects that should be used as the title for the options.
107
         * @param  string  $prefix         String to indent deeper levels
108
         * @param  string  $selected       Value to display as selected
109
         * @param  bool    $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy
110
         * @param  integer $key            ID of the object to display as the root of select options
111
         * @param  string  $extra
112
         * @return string  HTML select box
113
         *
114
         * @deprecated since 2.5.9, please use makeSelectElement()
115
         */
116
        public function makeSelBox(
117
            $name,
118
            $fieldName,
119
            $prefix = '-',
120
            $selected = '',
121
            $addEmptyOption = false,
122
            $key = 0,
123
            $extra = ''
124
        ) //makeSelBox($name, $prefix = '-', $selected = '', $EmptyOption = false, $key = 0)
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
125
        {
126
            $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
127
            trigger_error("makeSelBox() is deprecated since 2.5.9, please use makeSelectElement(), accessed from {$trace[0]['file']} line {$trace[0]['line']},");
128
129
            $ret = '<select name=' . $name . '>';
130
            if (!empty($addEmptyOption)) {
131
                $ret .= '<option value="0">' . (is_string($EmptyOption) ? $EmptyOption : '') . '</option>';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $EmptyOption does not exist. Did you maybe mean $addEmptyOption?
Loading history...
132
            }
133
            $this->_makeSelBoxOptions('forum_name', $selected, $key, $ret, $prefix);
134
            $ret .= '</select>';
135
136
            return $ret;
137
        }
138
139
        /**
140
         * Make a tree for the array of a given category
141
         *
142
         * @param  string  $key   top key of the tree
143
         * @param  array   $ret   the tree
144
         * @param  integer $depth level of subcategories
145
         * @return void
146
         * @internal param array $tags fields to be used
147
         */
148
        public function getAllChildObject($key, &$ret, $depth = 0)
149
        {
150
            if (0 == --$depth) {
151
                return;
152
            }
153
154
            if (isset($this->tree[$key]['child'])) {
155
                foreach ($this->tree[$key]['child'] as $childkey) {
156
                    if (isset($this->tree[$childkey]['obj'])) {
157
                        $ret['child'][$childkey] = $this->tree[$childkey]['obj'];
158
                    }
159
                    $this->getAllChild_object($childkey, $ret['child'][$childkey], $depth);
160
                }
161
            }
162
        }
163
164
        /**
165
         * Make a tree for the array
166
         *
167
         * @param  int|string $key   top key of the tree
168
         * @param  integer    $depth level of subcategories
169
         * @return array
170
         * @internal param array $tags fields to be used
171
         */
172
        public function &makeObjectTree($key = 0, $depth = 0)
173
        {
174
            $ret = [];
175
            if ($depth > 0) {
176
                ++$depth;
177
            }
178
            $this->getAllChild_object($key, $ret, $depth);
179
180
            return $ret;
181
        }
182
183
        /**
184
         * Make a tree for the array of a given category
185
         *
186
         * @param  string  $key   top key of the tree
187
         * @param  array   $ret   the tree
188
         * @param  array   $tags  fields to be used
189
         * @param  integer $depth level of subcategories
190
         * @return void
191
         */
192
        public function getAllChildArray($key, &$ret, array $tags = [], $depth = 0)
193
        {
194
            if (0 == --$depth) {
195
                return;
196
            }
197
198
            if (isset($this->tree[$key]['child'])) {
199
                foreach ($this->tree[$key]['child'] as $childkey) {
200
                    if (isset($this->tree[$childkey]['obj'])) {
201
                        if (count($tags) > 0) {
202
                            foreach ($tags as $tag) {
203
                                $ret['child'][$childkey][$tag] = $this->tree[$childkey]['obj']->getVar($tag);
204
                            }
205
                        } else {
206
                            $ret['child'][$childkey]['forum_name'] = $this->tree[$childkey]['obj']->getVar('forum_name');
207
                        }
208
                    }
209
210
                    $this->getAllChildArray($childkey, $ret['child'][$childkey], $tags, $depth);
211
                }
212
            }
213
        }
214
215
        /**
216
         * Make a tree for the array
217
         *
218
         * @param  int|string $key   top key of the tree
219
         * @param  array      $tags  fields to be used
220
         * @param  integer    $depth level of subcategories
221
         * @return array
222
         */
223
        public function &makeArrayTree($key = 0, $tags = null, $depth = 0)
224
        {
225
            $ret = [];
226
            if ($depth > 0) {
227
                ++$depth;
228
            }
229
            $this->getAllChildArray($key, $ret, $tags, $depth);
0 ignored issues
show
Bug introduced by
It seems like $tags can also be of type null; however, parameter $tags of XoopsModules\Newbb\ObjectTree::getAllChildArray() 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

229
            $this->getAllChildArray($key, $ret, /** @scrutinizer ignore-type */ $tags, $depth);
Loading history...
230
231
            return $ret;
232
        }
233
234
        /**#@+
235
         * get all parent forums
236
         *
237
         * @param  string $key     ID of the child object
238
         * @param  array  $ret     (empty when called from outside) Result from previous recursions
239
         * @param  int    $uplevel (empty when called from outside) level of recursion
240
         * @return array  Array of parent nodes.
241
         */
242
        public function &myGetParentForums($key, array $ret = [], $uplevel = 0)
243
        {
244
            if (isset($this->tree[$key]['parent']) && isset($this->tree[$this->tree[$key]['parent']]['obj'])) {
245
                $ret[$uplevel] = $this->tree[$this->tree[$key]['parent']]['obj'];
246
                if ($this->tree[$key]['parent'] !== $key) {
247
                    //$parents = $this->getParentForums($this->tree[$key]['parent'], $ret, $uplevel+1);
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
248
                    $parents = $this->getParentForums($this->tree[$key]['parent']);
249
                    foreach (array_keys($parents) as $newkey) {
250
                        $ret[$newkey] = $parents[$newkey];
251
                    }
252
                }
253
            }
254
255
            return $ret;
256
        }
257
258
        /**
259
         * @param        $key
260
         * @param  bool  $reverse
261
         * @return array
262
         */
263
        public function &getParentForums($key, $reverse = true)
264
        {
265
            $ret  = [];
266
            $pids = [];
267
            if (isset($this->tree[$key]['parent']) && isset($this->tree[$this->tree[$key]['parent']]['obj'])) {
268
                $pids[]  = $this->tree[$this->tree[$key]['parent']]['obj']->getVar($this->myId);
269
                $parents = $this->myGetParentForums($this->tree[$key]['parent'], $ret);
270
                foreach (array_keys($parents) as $newkey) {
271
                    if (!is_object($newkey)) {
272
                        continue;
273
                    }
274
                    $ret[] = $parents[$newkey]->getVar($this->myId);
275
                }
276
            }
277
            if ($reverse) {
278
                $pids = array_reverse($ret) + $pids;
279
            } else {
280
                $pids += $ret;
281
            }
282
283
            return $pids;
284
        }
285
        /**#@-*/
286
    }
287
}
288