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.

FinderTrait   B
last analyzed

Complexity

Total Complexity 48

Size/Duplication

Total Lines 261
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 104
c 1
b 0
f 0
dl 0
loc 261
rs 8.5599
wmc 48

6 Methods

Rating   Name   Duplication   Size   Complexity  
B findNotIn() 0 33 9
A all() 0 28 6
B findIn() 0 33 9
B find() 0 37 10
B findWhere() 0 31 8
A page() 0 27 6

How to fix   Complexity   

Complex Class

Complex classes like FinderTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FinderTrait, and based on these observations, apply Extract Interface, too.

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\Files\Traits;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Reactor\Models\Sql\DataObjects;
19
use O2System\Spl\DataStructures\SplArrayObject;
20
use O2System\Spl\Iterators\ArrayIterator;
21
22
/**
23
 * Class FinderTrait
24
 *
25
 * @package O2System\Reactor\Models\Sql\Traits
26
 */
27
trait FinderTrait
28
{
29
    /**
30
     * FinderTrait::all
31
     *
32
     * @param null $fields
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fields is correct as it would always require null to be passed?
Loading history...
33
     * @param null $limit
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $limit is correct as it would always require null to be passed?
Loading history...
34
     *
35
     * @return bool|DataObjects\Result
36
     */
37
    public function all($fields = null, $limit = null)
38
    {
39
        $result = $this->storage;
40
41
        if (isset($limit)) {
42
            $result = array_slice($this->storage, $limit);
43
        }
44
45
        if (empty($fields)) {
46
            return $this->result = new ArrayIterator($result);
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
        } else {
48
            $this->result = new ArrayIterator();
49
50
            foreach ($result as $row) {
51
                $item = new SplArrayObject();
52
                foreach ($fields as $field) {
53
                    if ($row->offsetExists($field)) {
54
                        $item[ $field ] = $row->offsetGet($field);
55
                    }
56
                }
57
58
                $this->result[] = $item;
59
            }
60
61
            return $this->result;
62
        }
63
64
        return false;
0 ignored issues
show
Unused Code introduced by
return false is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
65
    }
66
    // ------------------------------------------------------------------------
67
68
    /**
69
     * FinderTrait::page
70
     *
71
     * Find record by page.
72
     *
73
     * @param int $page
74
     */
75
    public function page($fields = null, $page = 1, $entries = 5)
76
    {
77
        $chunks = array_chunk($this->storage, $entries);
78
        $offset = $page - 1;
79
80
        if (isset($chunks[ $offset ])) {
81
            $result = new ArrayIterator($chunks[ $offset ]);
82
83
            if (empty($fields)) {
84
                return $this->result = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
85
            } else {
86
                foreach ($result as $row) {
87
                    $item = new SplArrayObject();
88
                    foreach ($fields as $field) {
89
                        if ($row->offsetExists($field)) {
90
                            $item[ $field ] = $row->offsetGet($field);
91
                        }
92
                    }
93
94
                    $this->result[] = $item;
95
                }
96
97
                return $this->result;
98
            }
99
        }
100
101
        return false;
102
    }
103
    // ------------------------------------------------------------------------
104
105
    /**
106
     * Find
107
     *
108
     * Find single or many record base on criteria by specific field
109
     *
110
     * @param   string      $criteria Criteria value
111
     * @param   string|null $field    Table column field name | set to primary key by default
112
     *
113
     * @return  DataObjects\Result|DataObjects\Result\Row|bool Returns FALSE if failed.
114
     */
115
    public function find($criteria, $field = null, $limit = null)
116
    {
117
        if (is_array($criteria)) {
0 ignored issues
show
introduced by
The condition is_array($criteria) is always false.
Loading history...
118
            return $this->findIn($criteria, $field);
119
        }
120
121
        $field = isset($field) ? $field : $this->primaryKey;
122
123
        $result = new ArrayIterator();
124
125
        $counter = 0;
126
        foreach ($this->storage as $row) {
127
            if ($row->offsetExists($field)) {
128
                if ($row->offsetGet($field) === $criteria) {
129
                    $result[] = $row;
130
                    $counter++;
131
                }
132
            }
133
134
            if (isset($limit)) {
135
                if ($counter == $limit) {
136
                    break;
137
                }
138
            }
139
        }
140
141
        if ($result->count() > 0) {
142
            $this->result = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
143
144
            if ($result->count() == 1) {
145
                return $result->first();
146
            } else {
147
                return $this->result;
148
            }
149
        }
150
151
        return false;
152
    }
153
    // ------------------------------------------------------------------------
154
155
    /**
156
     * Find In
157
     *
158
     * Find many records within criteria on specific field
159
     *
160
     * @param   array  $inCriteria List of criteria
161
     * @param   string $field      Table column field name | set to primary key by default
162
     *
163
     * @return  DataObjects\Result|bool Returns FALSE if failed.
164
     */
165
    public function findIn(array $inCriteria, $field = null)
166
    {
167
        $field = isset($field) ? $field : $this->primaryKey;
168
169
        $result = new ArrayIterator();
170
171
        $counter = 0;
172
        foreach ($this->storage as $row) {
173
            if ($row->offsetExists($field)) {
174
                if (in_array($row->offsetGet($field), $inCriteria)) {
175
                    $result[] = $row;
176
                    $counter++;
177
                }
178
            }
179
180
            if (isset($limit)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $limit seems to never exist and therefore isset should always be false.
Loading history...
181
                if ($counter == $limit) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $limit seems to be never defined.
Loading history...
182
                    break;
183
                }
184
            }
185
        }
186
187
        if ($result->count() > 0) {
188
            $this->result = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
189
190
            if ($result->count() == 1) {
191
                return $result->first();
192
            } else {
193
                return $this->result;
194
            }
195
        }
196
197
        return false;
198
    }
199
    // ------------------------------------------------------------------------
200
201
    /**
202
     * Find By
203
     *
204
     * Find single record based on certain conditions
205
     *
206
     * @param   array $conditions List of conditions with criteria
207
     *
208
     * @access  protected
209
     * @return  DataObjects\Result|bool Returns FALSE if failed.
210
     */
211
    public function findWhere(array $conditions, $limit = null)
212
    {
213
        $result = new ArrayIterator();
214
215
        $counter = 0;
216
        foreach ($this->storage as $row) {
217
            foreach ($conditions as $field => $criteria) {
218
                if ($row->offsetGet($field) === $criteria) {
219
                    $result[] = $row;
220
                    $counter++;
221
                }
222
            }
223
224
            if (isset($limit)) {
225
                if ($counter == $limit) {
226
                    break;
227
                }
228
            }
229
        }
230
231
        if ($result->count() > 0) {
232
            $this->result = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
233
234
            if ($result->count() == 1) {
235
                return $result->first();
236
            } else {
237
                return $this->result;
238
            }
239
        }
240
241
        return false;
242
    }
243
    // ------------------------------------------------------------------------
244
245
    /**
246
     * Find In
247
     *
248
     * Find many records not within criteria on specific field
249
     *
250
     * @param   array  $notInCriteria List of criteria
251
     * @param   string $field         Table column field name | set to primary key by default
252
     *
253
     * @return  DataObjects\Result|bool Returns FALSE if failed.
254
     */
255
    public function findNotIn(array $notInCriteria, $field = null)
256
    {
257
        $field = isset($field) ? $field : $this->primaryKey;
258
259
        $result = new ArrayIterator();
260
261
        $counter = 0;
262
        foreach ($this->storage as $row) {
263
            if ($row->offsetExists($field)) {
264
                if ( ! in_array($row->offsetGet($field), $notInCriteria)) {
265
                    $result[] = $row;
266
                    $counter++;
267
                }
268
            }
269
270
            if (isset($limit)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $limit seems to never exist and therefore isset should always be false.
Loading history...
271
                if ($counter == $limit) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $limit seems to be never defined.
Loading history...
272
                    break;
273
                }
274
            }
275
        }
276
277
        if ($result->count() > 0) {
278
            $this->result = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property result does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
279
280
            if ($result->count() == 1) {
281
                return $result->first();
282
            } else {
283
                return $this->result;
284
            }
285
        }
286
287
        return false;
288
    }
289
}