GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

HierarchicalTrait::getParents()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 23
rs 9.7998
1
<?php
2
/**
3
 * This file is part of the O2System PHP Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Reactor\Models\Sql\Traits;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class HierarchicalTrait
20
 *
21
 * @package O2System\Reactor\Models\Sql\Traits
22
 */
23
trait HierarchicalTrait
24
{
25
    /**
26
     * Rebuild Tree
27
     *
28
     * Rebuild self hierarchical table
29
     *
30
     * @access public
31
     *
32
     * @param string $table Working database table
33
     *
34
     * @return int  Right column value
35
     */
36
    public function rebuild($idParent = 0, $left = 1, $depth = 0)
37
    {
38
        ini_set('xdebug.max_nesting_level', 10000);
39
        ini_set('memory_limit', '-1');
40
41
        $result = $this->qb
42
            ->table($this->table)
43
            ->select('id')
44
            ->where('id_parent', $idParent)
45
            ->orderBy('id')
46
            ->get();
47
48
        $right = $left + 1;
49
50
        if ($result) {
51
            $i = 0;
52
            foreach ($result as $row) {
53
                if ($i == 0) {
54
                    $this->qb
55
                        ->table($this->table)
56
                        ->where('id', $row->id)
57
                        ->update($update = [
58
                            'record_left'  => $left,
59
                            'record_right' => $right,
60
                            'record_depth' => $depth,
61
                        ]);
62
                } else {
63
                    $this->qb
64
                        ->table($this->table)
65
                        ->where('id', $row->id)
66
                        ->update($update = [
67
                            'record_left'  => $left = $right + 1,
68
                            'record_right' => $right = $left + 1,
69
                            'record_depth' => $depth,
70
                        ]);
71
                }
72
73
                $update[ 'id' ] = $row->id;
74
75
                if ($this->hasChild($row->id)) {
76
                    $right = $this->rebuild($row->id, $right, $depth + 1);
77
                    $this->qb
78
                        ->table($this->table)
79
                        ->where('id', $row->id)
80
                        ->update($update = [
81
                            'record_right' => $right,
82
                        ]);
83
                    $update[ 'id' ] = $row->id;
84
                }
85
86
                $i++;
87
            }
88
        }
89
90
        return $right + 1;
91
    }
92
93
    /**
94
     * Has Childs
95
     *
96
     * Check if there is a child rows
97
     *
98
     * @param string $table Working database table
99
     *
100
     * @access public
101
     * @return bool
102
     */
103
    public function hasChild($idParent)
104
    {
105
        $result = $this->qb
106
            ->table($this->table)
107
            ->select('id')
108
            ->where('id_parent', $idParent)
109
            ->get();
110
111
        if ($result) {
112
            return (bool)($result->count() == 0 ? false : true);
113
        }
114
115
        return false;
116
    }
117
118
    // ------------------------------------------------------------------------
119
120
    /**
121
     * Find Parents
122
     *
123
     * Retreive parents of a record
124
     *
125
     * @param numeric $id Record ID
0 ignored issues
show
Bug introduced by
The type O2System\Reactor\Models\Sql\Traits\numeric 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...
126
     *
127
     * @access public
128
     * @return array
129
     */
130
    public function getParents($id, &$parents = [])
131
    {
132
        $result = $this->qb
133
            ->table($this->table)
134
            ->whereIn('id', $this->qb
135
                ->subQuery()
136
                ->table($this->table)
137
                ->select('id_parent')
138
                ->where('id', $id)
139
            )
140
            ->get();
141
142
        if ($result) {
143
            if ($result->count()) {
144
                $parents[] = $row = $result->first();
145
146
                if ($this->hasParent($row->id_parent)) {
147
                    $this->getParents($row->id, $parents);
148
                }
149
            }
150
        }
151
152
        return array_reverse($parents);
153
    }
154
155
    public function hasParent($idParent)
156
    {
157
        $result = $this->qb
158
            ->table($this->table)
159
            ->select('id')
160
            ->where('id', $idParent)
161
            ->get();
162
163
        if ($result) {
164
            return (bool)($result->count() == 0 ? false : true);
165
        }
166
167
        return false;
168
    }
169
    // ------------------------------------------------------------------------
170
171
    /**
172
     * Find Childs
173
     *
174
     * Retreive all childs
175
     *
176
     * @param numeric $idParent Parent ID
177
     *
178
     * @access public
179
     * @return array
180
     */
181
    public function getChilds($idParent)
182
    {
183
        $result = $this->qb
184
            ->table($this->table)
185
            ->where('id_parent', $idParent)
186
            ->get();
187
188
        $childs = [];
189
190
        if ($result) {
191
            foreach ($result as $row) {
192
                $childs[] = $row;
193
                if ($this->hasChild($row->id)) {
194
                    $row->childs = $this->getChilds($row->id);
195
                }
196
            }
197
        }
198
199
        return $childs;
200
    }
201
    // ------------------------------------------------------------------------
202
203
    /**
204
     * Count Childs
205
     *
206
     * Num childs of a record
207
     *
208
     * @param numeric $idParent Record Parent ID
209
     *
210
     * @access public
211
     * @return bool
212
     */
213
    public function getNumChilds($idParent)
214
    {
215
        $result = $this->qb
216
            ->table($this->table)
217
            ->select('id')
218
            ->where('id_parent', $idParent)
219
            ->get();
220
221
        if ($result) {
222
            return $result->count();
223
        }
224
225
        return 0;
0 ignored issues
show
Bug Best Practice introduced by
The expression return 0 returns the type integer which is incompatible with the documented return type boolean.
Loading history...
226
    }
227
}