Passed
Pull Request — master (#125)
by
unknown
03:02
created

QtModel::prop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.6.0
13
 */
14
15
namespace Quantum\Mvc;
16
17
use Quantum\Libraries\Database\DbalInterface;
18
use Quantum\Exceptions\ModelException;
19
20
/**
21
 * Class QtModel
22
 * @package Quantum\Mvc
23
 * @method string getTable()
24
 * @method DbalInterface select(...$columns)
25
 * @method DbalInterface findOne(int $id)
26
 * @method DbalInterface findOneBy(string $column, $value)
27
 * @method DbalInterface first()
28
 * @method DbalInterface criteria(string $column, string $operator, $value = null)
29
 * @method DbalInterface criterias(...$criterias)
30
 * @method DbalInterface having(string $column, string $operator, string $value = null)
31
 * @method DbalInterface orderBy(string $column, string $direction)
32
 * @method DbalInterface limit(int $limit)
33
 * @method mixed get()
34
 * @method array asArray()
35
 * @method DbalInterface create()
36
 * @method bool save()
37
 * @method bool delete()
38
 * @method bool deleteMany()
39
 * @method DbalInterface joinTo(QtModel $model, bool $switch = true)
40
 * @method DbalInterface joinThrough(QtModel $model, bool $switch = true)
41
 */
42
abstract class QtModel
43
{
44
45
    /**
46
     * The database table associated with model
47
     * @var string
48
     */
49
    public $table;
50
51
    /**
52
     * Id column of table
53
     * @var string
54
     */
55
    public $idColumn = 'id';
56
57
    /**
58
     * Foreign keys
59
     * @var array
60
     */
61
    public $foreignKeys = [];
62
63
    /**
64
     * Models fillable properties
65
     * @var array
66
     */
67
    protected $fillable = [];
68
69
    /**
70
     * Models hidden properties
71
     * @var array
72
     */
73
    protected $hidden = [];
74
75
    /**
76
     * ORM database abstract layer object
77
     * @var \Quantum\Libraries\Database\DbalInterface
78
     */
79
    private $orm;
80
81
    /**
82
     * Sets the ORM
83
     * @param \Quantum\Libraries\Database\DbalInterface $orm
84
     */
85
    public function setOrm(DbalInterface $orm)
86
    {
87
        $this->orm = $orm;
88
    }
89
90
    /**
91
     * Fills the object properties
92
     * @param array $props
93
     * @return \Quantum\Mvc\QtModel
94
     * @throws \Quantum\Exceptions\ModelException
95
     */
96
    public function fillObjectProps(array $props): QtModel
97
    {
98
        foreach ($props as $key => $value) {
99
            if ($key == $this->idColumn) {
100
                continue;
101
            }
102
103
            if (!in_array($key, $this->fillable)) {
104
                throw ModelException::inappropriateProperty($key);
105
            }
106
107
            $this->prop($key, $value);
108
        }
109
110
        return $this;
111
    }
112
113
    /**
114
     * @inheritDoc
115
     * @throws DatabaseException
116
     */
117
    public function get(?int $returnType = self::TYPE_ARRAY)
0 ignored issues
show
Bug introduced by
The constant Quantum\Mvc\QtModel::TYPE_ARRAY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
118
    {
119
        $result = $this->orm->get($returnType);
0 ignored issues
show
Unused Code introduced by
The call to Quantum\Libraries\Database\DbalInterface::get() has too many arguments starting with $returnType. ( Ignorable by Annotation )

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

119
        /** @scrutinizer ignore-call */ 
120
        $result = $this->orm->get($returnType);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
120
121
        if (count($this->hidden) > 0) {
122
            for ($i = 0; $i < count($result); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
123
                $result[$i] = array_diff_key($result[$i], array_flip($this->hidden));
124
            }
125
        }
126
127
        return $result;
128
    }
129
130
    /**
131
     * Sets or gets the model property
132
     * @param string $property
133
     * @param mixed|null $value
134
     * @return mixed
135
     */
136
    public function prop(string $property, $value = null)
137
    {
138
        return $this->orm->prop(...func_get_args());
0 ignored issues
show
Bug introduced by
func_get_args() is expanded, but the parameter $key of Quantum\Libraries\Database\DbalInterface::prop() does not expect variable arguments. ( Ignorable by Annotation )

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

138
        return $this->orm->prop(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
139
    }
140
141
    /**
142
     * Gets the model property with magic
143
     * @param string $property
144
     * @return mixed
145
     */
146
    public function __get(string $property)
147
    {
148
        return $this->prop($property);
149
    }
150
151
    /**
152
     * Sets a value to the model property with magic
153
     * @param string $property
154
     * @param mixed $value
155
     */
156
    public function __set(string $property, $value)
157
    {
158
        $this->prop($property, $value);
159
    }
160
161
    /**
162
     * Allows calling the model methods
163
     * @param string $method
164
     * @param mixed|null $args
165
     * @return $this|array|int|string
166
     * @throws \Quantum\Exceptions\ModelException
167
     */
168
    public function __call(string $method, $args = null)
169
    {
170
        if (!method_exists($this->orm, $method)) {
171
            throw ModelException::undefinedMethod($method);
172
        }
173
174
        $result = $this->orm->{$method}(...$args);
175
176
        if (!is_object($result)) {
177
            return $result;
178
        }
179
180
        return $this;
181
    }
182
183
    /**
184
     * Keeps only relevant props at serialization
185
     * @return string[]
186
     */
187
    public function __sleep()
188
    {
189
        return [
190
            'table',
191
            'idColumn',
192
            'foreignKeys'
193
        ];
194
    }
195
}
196