Passed
Push — master ( 780718...e5056f )
by Mikael
01:39
created

ActiveRecordModel   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 343
Duplicated Lines 17.49 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.12%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 2
dl 60
loc 343
ccs 101
cts 104
cp 0.9712
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setDb() 0 4 1
A checkDb() 0 6 2
A getProperties() 0 11 1
A find() 0 4 1
A findById() 0 5 2
A findWhere() 11 11 2
A findAll() 0 9 1
A findAllWhere() 11 11 2
A save() 0 8 2
A saveWhere() 0 4 1
A create() 14 14 1
A update() 14 14 1
A updateWhere() 0 16 2
A delete() 0 12 2
A deleteWhere() 10 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Anax\Database;
4
5
use \Anax\Database\DatabaseQueryBuilder;
6
use \Anax\Database\Exception\ActiveRecordException;
7
8
/**
9
 * An implementation of the Active Record pattern to be used as
10
 * base class for database driven models.
11
 */
12
class ActiveRecordModel
13
{
14
    /**
15
     * @var DatabaseQueryBuilder $db the object for persistent
16
     *                               storage.
17
     */
18
    protected $db = null;
19
20
    /**
21
     * @var string $tableName name of the database table.
22
     */
23
    protected $tableName = null;
24
25
    /**
26
     * @var string $tableIdColumn name of the id column in the database table.
27
     */
28
    protected $tableIdColumn = "id";
29
30
31
32
    /**
33
     * Set the database object to use for accessing storage.
34
     *
35
     * @param DatabaseQueryBuilder $db as database access object.
36
     *
37
     * @return void
38
     */
39 6
    public function setDb(DatabaseQueryBuilder $db)
40
    {
41 6
        $this->db = $db;
42 6
    }
43
44
45
46
    /**
47
     * Check if database is injected or throw an exception.
48
     *
49
     * @throws ActiveRecordException when database is not set.
50
     *
51
     * @return void
52
     */
53 7
    protected function checkDb()
54
    {
55 7
        if (!$this->db) {
56 1
            throw new ActiveRecordException("Missing \$db, did you forget to inject/set is?");
57
        }
58 6
    }
59
60
61
62
    /**
63
     * Get essential object properties.
64
     *
65
     * @return array with object properties.
66
     */
67 6
    protected function getProperties()
68
    {
69 6
        $properties = get_object_vars($this);
70
        unset(
71 6
            $properties['tableName'],
72
            $properties['db'],
73
            $properties['di'],
74
            $properties['tableIdColumn']
75
        );
76 6
        return $properties;
77
    }
78
79
80
81
    /**
82
     * Find and return first object found by search criteria and use
83
     * its data to populate this instance.
84
     *
85
     * @param string $column to use in where statement.
86
     * @param mixed  $value  to use in where statement.
87
     *
88
     * @return this
89
     */
90 1
    public function find($column, $value)
91
    {
92 1
        return $this->findWhere("$column = ?", $value);
93
    }
94
95
96
97
    /**
98
     * Find and return first object by its tableIdColumn and use
99
     * its data to populate this instance.
100
     *
101
     * @param integer $id to find or use $this->{$this->tableIdColumn}
102
     *                    as default.
103
     *
104
     * @return this
105
     */
106 4
    public function findById($id = null)
107
    {
108 4
        $id = $id ?: $this->{$this->tableIdColumn};
109 4
        return $this->findWhere("{$this->tableIdColumn} = ?", $id);
110
    }
111
112
113
114
    /**
115
     * Find and return first object found by search criteria and use
116
     * its data to populate this instance.
117
     *
118
     * The search criteria `$where` of can be set up like this:
119
     *  `id = ?`
120
     *  `id1 = ? and id2 = ?`
121
     *
122
     * The `$value` can be a single value or an array of values.
123
     *
124
     * @param string $where to use in where statement.
125
     * @param mixed  $value to use in where statement.
126
     *
127
     * @return this
128
     */
129 4 View Code Duplication
    public function findWhere($where, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131 4
        $this->checkDb();
132 4
        $params = is_array($value) ? $value : [$value];
133 4
        return $this->db->connect()
134 4
                        ->select()
135 4
                        ->from($this->tableName)
136 4
                        ->where($where)
137 4
                        ->execute($params)
0 ignored issues
show
Documentation introduced by
$params is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138 4
                        ->fetchInto($this);
139
    }
140
141
142
143
    /**
144
     * Find and return all.
145
     *
146
     * @return array of object of this class
147
     */
148 1
    public function findAll()
149
    {
150 1
        $this->checkDb();
151 1
        return $this->db->connect()
152 1
                        ->select()
153 1
                        ->from($this->tableName)
154 1
                        ->execute()
155 1
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
Documentation introduced by
get_class($this) is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
156
    }
157
158
159
160
    /**
161
     * Find and return all matching the search criteria.
162
     *
163
     * The search criteria `$where` of can be set up like this:
164
     *  `id = ?`
165
     *  `id IN [?, ?]`
166
     *
167
     * The `$value` can be a single value or an array of values.
168
     *
169
     * @param string $where to use in where statement.
170
     * @param mixed  $value to use in where statement.
171
     *
172
     * @return array of object of this class
173
     */
174 1 View Code Duplication
    public function findAllWhere($where, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176 1
        $this->checkDb();
177 1
        $params = is_array($value) ? $value : [$value];
178 1
        return $this->db->connect()
179 1
                        ->select()
180 1
                        ->from($this->tableName)
181 1
                        ->where($where)
182 1
                        ->execute($params)
0 ignored issues
show
Documentation introduced by
$params is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
183 1
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
Documentation introduced by
get_class($this) is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
184
    }
185
186
187
188
    /**
189
     * Save current object/row, insert if id is missing and do an
190
     * update if the id exists.
191
     *
192
     * @return void
193
     */
194 7
    public function save()
195
    {
196 7
        if (isset($this->{$this->tableIdColumn})) {
197 2
            return $this->update();
198
        }
199
200 7
        return $this->create();
201
    }
202
203
204
205
    /**
206
     * Save/update current object/row using a custom where-statement.
207
     *
208
     * The criteria `$where` of can be set up like this:
209
     *  `id = ?`
210
     *  `id1 = ? AND id2 = ?`
211
     *
212
     * The `$value` can be a single value or an array of values.
213
     *
214
     * @param string $where to use in where statement.
215
     * @param mixed  $value to use in where statement.
216
     *
217
     * @return void
218
     */
219 1
    public function saveWhere($where, $value)
220
    {
221 1
        return $this->updateWhere($where, $value);
222
    }
223
224
225
226
    /**
227
     * Create new row.
228
     *
229
     * @return void
230
     */
231 7 View Code Duplication
    protected function create()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
    {
233 7
        $this->checkDb();
234 6
        $properties = $this->getProperties();
235 6
        unset($properties[$this->tableIdColumn]);
236 6
        $columns = array_keys($properties);
237 6
        $values  = array_values($properties);
238
239 6
        $this->db->connect()
240 6
                 ->insert($this->tableName, $columns)
241 6
                 ->execute($values);
0 ignored issues
show
Documentation introduced by
$values is of type array<integer,?>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
242
243 6
        $this->{$this->tableIdColumn} = $this->db->lastInsertId();
244 6
    }
245
246
247
248
    /**
249
     * Update row using $tableIdColumn as where.
250
     *
251
     * @return void
252
     */
253 2 View Code Duplication
    protected function update()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
    {
255 2
        $this->checkDb();
256 2
        $properties = $this->getProperties();
257 2
        unset($properties[$this->tableIdColumn]);
258 2
        $columns = array_keys($properties);
259 2
        $values  = array_values($properties);
260 2
        $values[] = $this->{$this->tableIdColumn};
261
262 2
        $this->db->connect()
263 2
                 ->update($this->tableName, $columns)
264 2
                 ->where("{$this->tableIdColumn} = ?")
265 2
                 ->execute($values);
0 ignored issues
show
Documentation introduced by
$values is of type array<integer,?>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
266 2
    }
267
268
269
270
    /**
271
     * Update row using a custom where-statement.
272
     *
273
     * The criteria `$where` of can be set up like this:
274
     *  `id = ?`
275
     *  `id1 = ? AND id2 = ?`
276
     *  `id IN (?, ?)`
277
     *
278
     * The `$value` can be a single value or an array of values.
279
     *
280
     * @param string $where to use in where statement.
281
     * @param mixed  $value to use in where statement.
282
     *
283
     * @return void
284
     */
285 1
    protected function updateWhere($where, $value)
286
    {
287 1
        $this->checkDb();
288 1
        $properties = $this->getProperties();
289 1
        $columns = array_keys($properties);
290 1
        $values  = array_values($properties);
291 1
        $values1 = is_array($value)
292 1
            ? $value
293 1
            : [$value];
294 1
        $values = array_merge($values, $values1);
295
296 1
        $this->db->connect()
297 1
                 ->update($this->tableName, $columns)
298 1
                 ->where($where)
299 1
                 ->execute($values);
0 ignored issues
show
Documentation introduced by
$values is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
300 1
    }
301
302
303
304
    /**
305
     * Update row using $tableIdColumn as where and clear value of
306
     * `$tableIdColumn`.
307
     *
308
     * @param integer $id to delete or use $this->{$this->tableIdColumn}
309
     *                    as default.
310
     *
311
     * @return void
312
     */
313 2
    public function delete($id = null)
314
    {
315 2
        $this->checkDb();
316 2
        $id = $id ?: $this->{$this->tableIdColumn};
317
318 2
        $this->db->connect()
319 2
                 ->deleteFrom($this->tableName)
320 2
                 ->where("{$this->tableIdColumn} = ?")
321 2
                 ->execute([$id]);
0 ignored issues
show
Documentation introduced by
array($id) is of type array<integer,?,{"0":"?"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
322
323 2
        $this->{$this->tableIdColumn} = null;
324 2
    }
325
326
327
328
    /**
329
     * Delete row using a custom where-statement and leave value of
330
     * `$tableIdColumn` as it is.
331
     *
332
     * The criteria `$where` of can be set up like this:
333
     *  `id = ?`
334
     *  `id1 = ? AND id2 = ?`
335
     *  `id IN (?, ?)`
336
     *
337
     * The `$value` can be a single value or an array of values.
338
     *
339
     * @param string $where to use in where statement.
340
     * @param mixed  $value to use in where statement.
341
     *
342
     * @return void
343
     */
344 1 View Code Duplication
    public function deleteWhere($where, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
345
    {
346 1
        $this->checkDb();
347 1
        $values = is_array($value) ? $value : [$value];
348
349 1
        $this->db->connect()
350 1
                 ->deleteFrom($this->tableName)
351 1
                 ->where($where)
352 1
                 ->execute($values);
0 ignored issues
show
Documentation introduced by
$values is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
353 1
    }
354
}
355