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.
Passed
Push — master ( 9c3470...c852c0 )
by
unknown
02:52
created

HierarchicalTrait::hasChilds()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
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\Framework\Models\Sql\Traits;
15
16
// ------------------------------------------------------------------------
17
18
/**
19
 * Class HierarchicalTrait
20
 *
21
 * @package O2System\Framework\Models\Sql\Traits
22
 */
23
trait HierarchicalTrait
24
{
25
    /**
26
     * Parent Key Field
27
     *
28
     * @access  public
29
     * @type    string
30
     */
31
    public $parentKey = 'id_parent';
32
33
    /**
34
     * Hierarchical Enabled Flag
35
     *
36
     * @access  protected
37
     * @type    bool
38
     */
39
    protected $hierarchical = true;
40
41
    /**
42
     * Rebuild Tree
43
     *
44
     * Rebuild self hierarchical table
45
     *
46
     * @access public
47
     *
48
     * @param string $table Working database table
49
     *
50
     * @return int  Right column value
51
     */
52
    protected function rebuildTree($idParent = 0, $left = 1, $depth = 0)
53
    {
54
        ini_set('xdebug.max_nesting_level', 10000);
55
        ini_set('memory_limit', '-1');
56
57
        if ($result = $this->qb
58
            ->table($this->table)
59
            ->select('id')
60
            ->where('id_parent', $idParent)
61
            ->orderBy('id')
62
            ->get()) {
63
64
            $right = $left + 1;
65
66
            $i = 0;
67
            foreach ($result as $row) {
68
                if ($i == 0) {
69
                    $this->qb
70
                        ->from($this->table)
71
                        ->where('id', $row->id)
72
                        ->update($update = [
73
                            'record_left'  => $left,
74
                            'record_right' => $right,
75
                            'record_depth' => $depth,
76
                        ]);
77
                } else {
78
                    $this->qb
79
                        ->from($this->table)
80
                        ->where('id', $row->id)
81
                        ->update($update = [
82
                            'record_left'  => $left = $right + 1,
83
                            'record_right' => $right = $left + 1,
84
                            'record_depth' => $depth,
85
                        ]);
86
                }
87
88
                $update[ 'id' ] = $row->id;
89
90
                if ($this->hasChilds($row->id)) {
91
                    $right = $this->rebuildTree($row->id, $right, $depth + 1);
92
                    $this->qb
93
                        ->from($this->table)
94
                        ->where('id', $row->id)
95
                        ->update($update = [
96
                            'record_right' => $right,
97
                        ]);
98
                    $update[ 'id' ] = $row->id;
99
                }
100
101
                $i++;
102
            }
103
        }
104
105
        return $right + 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $right does not seem to be defined for all execution paths leading up to this point.
Loading history...
106
    }
107
108
    // ------------------------------------------------------------------------
109
110
    /**
111
     * Has Childs
112
     *
113
     * Check if there is a child rows
114
     *
115
     * @param string $table Working database table
116
     *
117
     * @access public
118
     * @return bool
119
     */
120
    protected function hasChilds($idParent)
121
    {
122
        if ($result = $this->qb
123
            ->table($this->table)
124
            ->select('id')
125
            ->where('id_parent', $idParent)
126
            ->get()) {
127
            return (bool)($result->count() == 0 ? false : true);
128
        }
129
130
        return false;
131
    }
132
133
    // ------------------------------------------------------------------------
134
135
    /**
136
     * Find Parents
137
     *
138
     * Retreive parents of a record
139
     *
140
     * @param numeric $id Record ID
0 ignored issues
show
Bug introduced by
The type O2System\Framework\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...
141
     *
142
     * @access public
143
     * @return array
144
     */
145
    protected function getParents($id, &$parents = [])
146
    {
147
        if ($result = $this->qb
148
            ->from($this->table)
149
            ->whereIn('id', $this->qb
150
                ->subQuery()
151
                ->from($this->table)
152
                ->select('id_parent')
153
                ->where('id', $id)
154
            )
155
            ->get()) {
156
            if ($result->count()) {
157
                $parents[] = $row = $result->first();
158
159
                if ($this->hasParent($row->id_parent)) {
160
                    $this->getParents($row->id, $parents);
161
                }
162
            }
163
        }
164
165
        return array_reverse($parents);
166
    }
167
168
    protected function hasParent($idParent)
169
    {
170
        if ($result = $this->qb
171
            ->table($this->table)
172
            ->select('id')
173
            ->where('id', $idParent)
174
            ->get()) {
175
            return (bool)($result->count() == 0 ? false : true);
176
        }
177
178
        return false;
179
    }
180
    // ------------------------------------------------------------------------
181
182
    /**
183
     * Find Childs
184
     *
185
     * Retreive all childs
186
     *
187
     * @param numeric $idParent Parent ID
188
     *
189
     * @access public
190
     * @return array
191
     */
192
    protected function getChilds($idParent)
193
    {
194
        $childs = [];
195
196
        if($result = $this->qb
197
            ->table($this->table)
198
            ->where('id_parent', $idParent)
199
            ->get()) {
200
            foreach ($result as $row) {
201
                $childs[] = $row;
202
                if ($this->hasChild($row->id)) {
0 ignored issues
show
Bug introduced by
The method hasChild() does not exist on O2System\Framework\Model...raits\HierarchicalTrait. Did you maybe mean hasChilds()? ( Ignorable by Annotation )

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

202
                if ($this->/** @scrutinizer ignore-call */ hasChild($row->id)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
203
                    $row->childs = $this->getChilds($row->id);
204
                }
205
            }
206
        }
207
208
        return $childs;
209
    }
210
    // ------------------------------------------------------------------------
211
212
    /**
213
     * Count Childs
214
     *
215
     * Num childs of a record
216
     *
217
     * @param numeric $idParent Record Parent ID
218
     *
219
     * @access public
220
     * @return bool
221
     */
222
    protected function getNumChilds($idParent)
223
    {
224
        if($result = $this->qb
225
            ->table($this->table)
226
            ->select('id')
227
            ->where('id_parent', $idParent)
228
            ->get()) {
229
            return $result->count();
230
        }
231
232
        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...
233
    }
234
}