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 ( bbcfbe...c28c6c )
by Steeven
02:22
created

Row::__call()   D

Complexity

Conditions 18
Paths 18

Size

Total Lines 47
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 18
eloc 31
c 2
b 1
f 0
nc 18
nop 2
dl 0
loc 47
rs 4.8666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the O2System 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\DataObjects\Result;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Database;
19
use O2System\Framework\Models\Sql\Model;
20
use O2System\Spl\DataStructures\SplArrayObject;
21
use O2System\Spl\Info\SplClassInfo;
22
23
/**
24
 * Class Row
25
 *
26
 * @package O2System\Framework\Models\Sql\DataObjects
27
 */
28
class Row extends SplArrayObject
29
{
30
    /**
31
     * Row::$model
32
     *
33
     * @var \O2System\Spl\Info\SplClassInfo
34
     */
35
    private $model;
36
37
    /**
38
     * Row::$record
39
     *
40
     * @var \O2System\Database\DataObjects\Result\Row\Record
0 ignored issues
show
Bug introduced by
The type O2System\Database\DataObjects\Result\Row\Record 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...
41
     */
42
    private $record;
43
44
    // ------------------------------------------------------------------------
45
46
    /**
47
     * Row::__construct
48
     *
49
     * @param mixed  $row
50
     * @param Model  $model
51
     */
52
    public function __construct($row, Model &$model)
53
    {
54
        $this->model = new SplClassInfo($model);
55
        $this->record = $row->getRecord();
56
57
        if ( ! models()->has($this->model->getClass())) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on O2System\Framework\Models\NoSql\Model. ( Ignorable by Annotation )

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

57
        if ( ! models()->/** @scrutinizer ignore-call */ has($this->model->getClass())) {

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...
Bug introduced by
The method has() does not exist on O2System\Framework\Models\Sql\Model. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

57
        if ( ! models()->/** @scrutinizer ignore-call */ has($this->model->getClass())) {
Loading history...
58
            models()->add($model, $this->model->getClass());
0 ignored issues
show
Bug introduced by
The method add() does not exist on O2System\Framework\Models\Sql\Model. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

58
            models()->/** @scrutinizer ignore-call */ add($model, $this->model->getClass());
Loading history...
Bug introduced by
The method add() does not exist on O2System\Framework\Models\NoSql\Model. ( Ignorable by Annotation )

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

58
            models()->/** @scrutinizer ignore-call */ add($model, $this->model->getClass());

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...
59
        }
60
61
        if ($row instanceof Database\DataObjects\Result\Row) {
62
            parent::__construct($row->getArrayCopy());
63
        } elseif (is_array($row)) {
64
            parent::__construct($row);
65
        }
66
67
        // Append Columns
68
        if (count($model->appendColumns)) {
69
            foreach ($model->appendColumns as $appendColumn) {
70
                $this->offsetSet($appendColumn, $this->offsetGet($appendColumn));
71
            }
72
        }
73
74
        models($this->model->getClass())->row = $this;
0 ignored issues
show
Bug introduced by
The property row does not seem to exist on O2System\Framework\Containers\Models.
Loading history...
75
    }
76
77
    // ------------------------------------------------------------------------
78
79
    /**
80
     * Row::offsetGet
81
     *
82
     * @param string $offset
83
     *
84
     * @return mixed
85
     */
86
    public function offsetGet($offset)
87
    {
88
        if ($this->offsetExists($offset)) {
89
            return parent::offsetGet($offset);
90
        } elseif(strpos($offset, 'record') !== false) {
91
            switch ($offset) {
92
                case 'record':
93
                    return $this->record;
94
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
95
                case 'record_status':
96
                    return $this->record->status;
97
                    break;
98
                case 'record_left':
99
                    return $this->record->left;
100
                    break;
101
                case 'record_right':
102
                    return $this->record->right;
103
                    break;
104
                case 'record_depth':
105
                    return $this->record->depth;
106
                    break;
107
                case 'record_ordering':
108
                    return $this->record->ordering;
109
                    break;
110
                case 'record_create_user':
111
                    return $this->record->create->user;
112
                    break;
113
                case 'record_create_timestamp':
114
                    return $this->record->create->timestamp;
115
                    break;
116
                case 'record_update_user':
117
                    return $this->record->update->user;
118
                    break;
119
                case 'record_update_timestamp':
120
                    return $this->record->update->timestamp;
121
                    break;
122
            }
123
        } elseif (null !== ($result = $this->__call($offset))) {
124
            return $result;
125
        }
126
127
        return null;
128
    }
129
130
    // ------------------------------------------------------------------------
131
132
    /**
133
     * Row::__call
134
     *
135
     * @param string $method
136
     * @param array  $args
137
     *
138
     * @return bool|\O2System\Framework\Models\Sql\DataObjects\Result|\O2System\Framework\Models\Sql\DataObjects\Result\Row|null
139
     */
140
    public function __call($method, $args = [])
141
    {
142
        $model = models($this->model->getClass());
143
144
        if (method_exists($model, $method)) {
145
            $model->row = $this;
0 ignored issues
show
Bug introduced by
The property row does not seem to exist on O2System\Framework\Containers\Models.
Loading history...
146
147
            if (false !== ($result = call_user_func_array([&$model, $method], $args))) {
148
                $this->offsetSet($method, $result);
149
150
                return $result;
151
            }
152
        } elseif (strpos($method, 'Url')) {
153
            $key = str_replace('Url', '', $method);
154
155
            if ($key === $model->uploadedImageKey) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedImageKey does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedImageKey does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
156
                if (isset($model->uploadedImageFilePath)) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedImageFilePath does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedImageFilePath does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
157
                    if (is_file($filePath = $model->uploadedImageFilePath . $this->offsetGet($key))) {
158
                        return images_url($filePath);
159
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.jpg')) {
160
                        return images_url($filePath);
161
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.png')) {
162
                        return images_url($filePath);
163
                    }
164
                }
165
            } elseif (in_array($key, $model->uploadedImageKeys)) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedImageKeys does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedImageKeys does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like $model->uploadedImageKeys can also be of type boolean; however, parameter $haystack of in_array() 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

165
            } elseif (in_array($key, /** @scrutinizer ignore-type */ $model->uploadedImageKeys)) {
Loading history...
166
                if (isset($model->uploadedImageFilePath)) {
167
                    if (is_file($filePath = $model->uploadedImageFilePath . $this->offsetGet($key))) {
168
                        return images_url($filePath);
169
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.jpg')) {
170
                        return images_url($filePath);
171
                    } elseif (is_file($filePath = PATH_STORAGE . 'images/default/not-found.png')) {
172
                        return images_url($filePath);
173
                    }
174
                }
175
            } elseif ($key === $model->uploadedFileKey) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedFileKey does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedFileKey does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
176
                if (isset($model->uploadedFileFilepath)) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedFileFilepath does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedFileFilepath does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
177
                    return storage_url($model->uploadedFileFilepath . $this->offsetGet($key));
178
                }
179
            } elseif (in_array($key, $model->uploadedFileKeys)) {
0 ignored issues
show
Bug Best Practice introduced by
The property uploadedFileKeys does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property uploadedFileKeys does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
180
                if (isset($model->uploadedFileFilepath)) {
181
                    return storage_url($model->uploadedFileFilepath . $this->offsetGet($key));
182
                }
183
            }
184
        }
185
186
        return null;
187
    }
188
189
    // ------------------------------------------------------------------------
190
191
    /**
192
     * Row::delete
193
     *
194
     * @return bool
195
     * @throws \O2System\Spl\Exceptions\RuntimeException
196
     * @throws \Psr\Cache\InvalidArgumentException
197
     */
198
    public function delete()
199
    {
200
        $model = models($this->model->getClass());
201
202
        $primaryKey = isset($model->primaryKey) ? $model->primaryKey : 'id';
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
203
        $id = $this->offsetGet($primaryKey);
204
205
        if (method_exists($model, 'beforeDelete')) {
206
            $model->beforeDelete();
0 ignored issues
show
Bug introduced by
The method beforeDelete() does not exist on O2System\Framework\Models\Sql\Model. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

206
            $model->/** @scrutinizer ignore-call */ 
207
                    beforeDelete();
Loading history...
207
        }
208
209
        if ($model->qb->table($model->table)->limit(1)->delete([$primaryKey => $id])) {
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property qb does not exist on O2System\Framework\Containers\Models. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property table does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property qb does not exist on O2System\Framework\Models\NoSql\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
210
            if (method_exists($model, 'afterDelete')) {
211
                return $model->afterDelete();
0 ignored issues
show
Bug introduced by
The method afterDelete() does not exist on O2System\Framework\Models\Sql\Model. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

211
                return $model->/** @scrutinizer ignore-call */ afterDelete();
Loading history...
212
            }
213
214
            return true;
215
        }
216
217
        return false;
218
    }
219
}