Completed
Branch master (9dda00)
by Michael
04:54 queued 02:33
created

NewbbObjectTree::getAllChild_array()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 7
Ratio 31.82 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 6
nop 4
dl 7
loc 22
rs 6.9811
c 0
b 0
f 0
1
<?php
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 35 and the first side effect is on line 29.

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
// This program is free software; you can redistribute it and/or modify     //
5
// it under the terms of the GNU General Public License as published by     //
6
// the Free Software Foundation; either version 2 of the License, or        //
7
// (at your option) any later version.                                      //
8
//                                                                          //
9
// You may not change or alter any portion of this comment or credits       //
10
// of supporting developers from this source code or any supporting         //
11
// source code which is considered copyrighted (c) material of the          //
12
// original comment or credit authors.                                      //
13
//                                                                          //
14
// This program is distributed in the hope that it will be useful,          //
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of           //
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
17
// GNU General Public License for more details.                             //
18
//                                                                          //
19
// You should have received a copy of the GNU General Public License        //
20
// along with this program; if not, write to the Free Software              //
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
22
// ------------------------------------------------------------------------ //
23
// Author: phppp (D.J., [email protected])                                  //
24
//  URL: http://xoops.org                                                    //
25
// Project: Article Project                                                 //
26
// ------------------------------------------------------------------------ //
27
28
// defined('XOOPS_ROOT_PATH') || exit('XOOPS root path not defined');
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...
29
require_once $GLOBALS['xoops']->path('class/tree.php');
30
31
if (!class_exists('NewbbObjectTree')) {
32
    /**
33
     * Class NewbbObjectTree
34
     */
35
    class NewbbObjectTree extends XoopsObjectTree
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
36
    {
37
        /**
38
         * @param      $objectArr
39
         * @param null $rootId
40
         */
41
        public function __construct(&$objectArr, $rootId = null)
42
        {
43
            parent::__construct($objectArr, 'forum_id', 'parent_forum', $rootId);
44
        }
45
46
        /**
47
         * Make options for a select box from
48
         *
49
         * @param int    $key         ID of the object to display as the root of select options
50
         * @param string $ret         (reference to a string when called from outside) Result from previous recursions
51
         * @param string $prefix_orig String to indent items at deeper levels
52
         * @param string $prefix_curr String to indent the current item
53
         * @param null|array   $tags
54
         * @internal  param string $fieldName Name of the member variable from the
55
         *                            node objects that should be used as the title for the options.
56
         * @internal  param string $selected Value to display as selected
57
         * @access    private
58
         */
59
        protected function makeTreeItems($key, &$ret, $prefix_orig, $prefix_curr = '', $tags = null)
60
        {
61
            if ($key > 0) {
62 View Code Duplication
                if (count($tags) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
                    foreach ($tags as $tag) {
0 ignored issues
show
Bug introduced by
The expression $tags of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
64
                        $ret[$key][$tag] = $this->tree[$key]['obj']->getVar($tag);
65
                    }
66
                } else {
67
                    $ret[$key]['forum_name'] = $this->tree[$key]['obj']->getVar('forum_name');
68
                }
69
                $ret[$key]['prefix'] = $prefix_curr;
70
                $prefix_curr         .= $prefix_orig;
71
            }
72
            if (isset($this->tree[$key]['child']) && !empty($this->tree[$key]['child'])) {
73
                foreach ($this->tree[$key]['child'] as $childkey) {
74
                    $this->makeTreeItems($childkey, $ret, $prefix_orig, $prefix_curr, $tags);
75
                }
76
            }
77
        }
78
79
        /**
80
         * Make a select box with options from the tree
81
         *
82
         * @param  string  $prefix         String to indent deeper levels
83
         * @param  integer $key            ID of the object to display as the root of select options
84
         * @param  null    $tags
85
         * @return array|string  HTML select box
86
         * @internal param string $name Name of the select box
87
         * @internal param string $fieldName Name of the member variable from the
88
         *                                 node objects that should be used as the title for the options.
89
         * @internal param string $selected Value to display as selected
90
         * @internal param bool $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy
91
         */
92
        public function &makeTree($prefix = '-', $key = 0, $tags = null)
93
        {
94
            $ret = [];
95
            $this->makeTreeItems($key, $ret, $prefix, '', $tags);
96
97
            return $ret;
98
        }
99
100
        /**
101
         * Make a select box with options from the tree
102
         *
103
         * @param  string  $name           Name of the select box
104
         * @param  string  $fieldName
105
         * @param  string  $prefix         String to indent deeper levels
106
         * @param  string  $selected       Value to display as selected
107
         * @param  bool    $addEmptyOption
108
         * @param  integer $key            ID of the object to display as the root of select options
109
         * @param  string  $extra
110
         * @return string  HTML select box
111
         * @internal param bool $EmptyOption
112
         * @internal param string $fieldName Name of the member variable from the
113
         *                                 node objects that should be used as the title for the options.
114
         * @internal param bool $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy
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
            $ret = '<select name=' . $name . '>';
127
            if (!empty($addEmptyOption)) {
128
                $ret .= '<option value="0">' . (is_string($EmptyOption) ? $EmptyOption : '') . '</option>';
0 ignored issues
show
Bug introduced by
The variable $EmptyOption does not exist. Did you mean $addEmptyOption?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
129
            }
130
            $this->_makeSelBoxOptions('forum_name', $selected, $key, $ret, $prefix);
131
            $ret .= '</select>';
132
133
            return $ret;
134
        }
135
136
        /**
137
         * Make a tree for the array of a given category
138
         *
139
         * @param  string  $key   top key of the tree
140
         * @param  array   $ret   the tree
141
         * @param  integer $depth level of subcategories
142
         * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
143
         * @internal param array $tags fields to be used
144
         */
145
        public function getAllChildObject($key, &$ret, $depth = 0)
146
        {
147
            if (--$depth == 0) {
148
                return;
149
            }
150
151
            if (isset($this->tree[$key]['child'])) {
152
                foreach ($this->tree[$key]['child'] as $childkey) {
153
                    if (isset($this->tree[$childkey]['obj'])) {
154
                        $ret['child'][$childkey] = $this->tree[$childkey]['obj'];
155
                    }
156
                    $this->getAllChild_object($childkey, $ret['child'][$childkey], $depth);
157
                }
158
            }
159
        }
160
161
        /**
162
         * Make a tree for the array
163
         *
164
         * @param  int|string $key   top key of the tree
165
         * @param  integer    $depth level of subcategories
166
         * @return array
167
         * @internal param array $tags fields to be used
168
         */
169
        public function &makeObjectTree($key = 0, $depth = 0)
170
        {
171
            $ret = [];
172
            if ($depth > 0) {
173
                ++$depth;
174
            }
175
            $this->getAllChild_object($key, $ret, $depth);
176
177
            return $ret;
178
        }
179
180
        /**
181
         * Make a tree for the array of a given category
182
         *
183
         * @param  string  $key   top key of the tree
184
         * @param  array   $ret   the tree
185
         * @param  array   $tags  fields to be used
186
         * @param  integer $depth level of subcategories
187
         * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
188
         **/
189
        public function getAllChildArray($key, &$ret, array $tags = [], $depth = 0)
190
        {
191
            if (--$depth == 0) {
192
                return;
193
            }
194
195
            if (isset($this->tree[$key]['child'])) {
196
                foreach ($this->tree[$key]['child'] as $childkey) {
197
                    if (isset($this->tree[$childkey]['obj'])) {
198 View Code Duplication
                        if (count($tags) > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
                            foreach ($tags as $tag) {
200
                                $ret['child'][$childkey][$tag] = $this->tree[$childkey]['obj']->getVar($tag);
201
                            }
202
                        } else {
203
                            $ret['child'][$childkey]['forum_name'] = $this->tree[$childkey]['obj']->getVar('forum_name');
204
                        }
205
                    }
206
207
                    $this->getAllChildArray($childkey, $ret['child'][$childkey], $tags, $depth);
208
                }
209
            }
210
        }
211
212
        /**
213
         * Make a tree for the array
214
         *
215
         * @param  int|string $key   top key of the tree
216
         * @param  array      $tags  fields to be used
0 ignored issues
show
Documentation introduced by
Should the type for parameter $tags not be array|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
217
         * @param  integer    $depth level of subcategories
218
         * @return array
219
         */
220
        public function &makeArrayTree($key = 0, $tags = null, $depth = 0)
221
        {
222
            $ret = [];
223
            if ($depth > 0) {
224
                ++$depth;
225
            }
226
            $this->getAllChildArray($key, $ret, $tags, $depth);
227
228
            return $ret;
229
        }
230
231
        /**#@+
232
         * get all parent forums
233
         *
234
         * @param  string $key     ID of the child object
235
         * @param  array  $ret     (empty when called from outside) Result from previous recursions
236
         * @param  int    $uplevel (empty when called from outside) level of recursion
237
         * @return array  Array of parent nodes.
238
         */
239
        public function &myGetParentForums($key, array $ret = [], $uplevel = 0)
240
        {
241
            if (isset($this->tree[$key]['parent']) && isset($this->tree[$this->tree[$key]['parent']]['obj'])) {
242
                $ret[$uplevel] = $this->tree[$this->tree[$key]['parent']]['obj'];
243
                if ($this->tree[$key]['parent'] !== $key) {
244
                    //$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...
245
                    $parents = $this->getParentForums($this->tree[$key]['parent']);
246
                    foreach (array_keys($parents) as $newkey) {
247
                        $ret[$newkey] = $parents[$newkey];
248
                    }
249
                }
250
            }
251
252
            return $ret;
253
        }
254
255
        /**
256
         * @param        $key
257
         * @param  bool  $reverse
258
         * @return array
259
         */
260
        public function &getParentForums($key, $reverse = true)
261
        {
262
            $ret  = [];
263
            $pids = [];
264
            if (isset($this->tree[$key]['parent']) && isset($this->tree[$this->tree[$key]['parent']]['obj'])) {
265
                $pids[]  = $this->tree[$this->tree[$key]['parent']]['obj']->getVar($this->myId);
266
                $parents = $this->myGetParentForums($this->tree[$key]['parent'], $ret);
267
                foreach (array_keys($parents) as $newkey) {
268
                    if (!is_object($newkey)) {
269
                        continue;
270
                    }
271
                    $ret[] = $parents[$newkey]->getVar($this->myId);
272
                }
273
            }
274
            if ($reverse) {
275
                $pids = array_reverse($ret) + $pids;
276
            } else {
277
                $pids += $ret;
278
            }
279
280
            return $pids;
281
        }
282
        /**#@-*/
283
    }
284
}
285