Completed
Push — master ( f3c7b7...780718 )
by Mikael
01:53
created

ActiveRecordModel::updateWhere()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 2
crap 6
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 5
    public function setDb(DatabaseQueryBuilder $db)
40
    {
41 5
        $this->db = $db;
42 5
    }
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 6
    protected function checkDb()
54
    {
55 6
        if (!$this->db) {
56 1
            throw new ActiveRecordException("Missing \$db, did you forget to inject/set is?");
57
        }
58 5
    }
59
60
61
62
    /**
63
     * Get essential object properties.
64
     *
65
     * @return array with object properties.
66
     */
67 5
    protected function getProperties()
68
    {
69 5
        $properties = get_object_vars($this);
70
        unset(
71 5
            $properties['tableName'],
72
            $properties['db'],
73
            $properties['di'],
74
            $properties['tableIdColumn']
75
        );
76 5
        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 string $column to use in where statement.
0 ignored issues
show
Bug introduced by
There is no parameter named $column. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
102
     * @param mixed  $value  to use in where statement.
103
     *
104
     * @return this
105
     */
106 3
    public function findById($value)
107
    {
108 3
        return $this->findWhere("{$this->tableIdColumn} = ?", $value);
109
    }
110
111
112
113
    /**
114
     * Find and return first object found by search criteria and use
115
     * its data to populate this instance.
116
     *
117
     * The search criteria `$where` of can be set up like this:
118
     *  `id = ?`
119
     *  `id1 = ? and id2 = ?`
120
     *
121
     * The `$value` can be a single value or an array of values.
122
     *
123
     * @param string $where to use in where statement.
124
     * @param mixed  $value to use in where statement.
125
     *
126
     * @return this
127
     */
128 3 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...
129
    {
130 3
        $this->checkDb();
131 3
        $params = is_array($value) ? $value : [$value];
132 3
        return $this->db->connect()
133 3
                        ->select()
134 3
                        ->from($this->tableName)
135 3
                        ->where($where)
136 3
                        ->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...
137 3
                        ->fetchInto($this);
138
    }
139
140
141
142
    /**
143
     * Find and return all.
144
     *
145
     * @return array of object of this class
146
     */
147 1
    public function findAll()
148
    {
149 1
        $this->checkDb();
150 1
        return $this->db->connect()
151 1
                        ->select()
152 1
                        ->from($this->tableName)
153 1
                        ->execute()
154 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...
155
    }
156
157
158
159
    /**
160
     * Find and return all matching the search criteria.
161
     *
162
     * The search criteria `$where` of can be set up like this:
163
     *  `id = ?`
164
     *  `id IN [?, ?]`
165
     *
166
     * The `$value` can be a single value or an array of values.
167
     *
168
     * @param string $where to use in where statement.
169
     * @param mixed  $value to use in where statement.
170
     *
171
     * @return array of object of this class
172
     */
173 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...
174
    {
175 1
        $this->checkDb();
176 1
        $params = is_array($value) ? $value : [$value];
177 1
        return $this->db->connect()
178 1
                        ->select()
179 1
                        ->from($this->tableName)
180 1
                        ->where($where)
181 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...
182 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...
183
    }
184
185
186
187
    /**
188
     * Save current object/row, insert if id is missing and do an
189
     * update if the id exists.
190
     *
191
     * @return void
192
     */
193 6
    public function save()
194
    {
195 6
        if (isset($this->{$this->tableIdColumn})) {
196 2
            return $this->update();
197
        }
198
199 6
        return $this->create();
200
    }
201
202
203
204
    /**
205
     * Save current object/row, update with where.
206
     *
207
     * @param string $where to use in where statement.
208
     * @param mixed  $params to use in where statement.
0 ignored issues
show
Documentation introduced by
There is no parameter named $params. Did you maybe mean $param?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
209
     *
210
     * @return void
211
     */
212
    public function saveWhere($where, $param)
213
    {
214
        return $this->updateWhere($where, $param);
215
    }
216
217
218
219
    /**
220
     * Create new row.
221
     *
222
     * @return void
223
     */
224 6 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...
225
    {
226 6
        $this->checkDb();
227 5
        $properties = $this->getProperties();
228 5
        unset($properties[$this->tableIdColumn]);
229 5
        $columns = array_keys($properties);
230 5
        $values  = array_values($properties);
231
232 5
        $this->db->connect()
233 5
                 ->insert($this->tableName, $columns)
234 5
                 ->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...
235
236 5
        $this->{$this->tableIdColumn} = $this->db->lastInsertId();
237 5
    }
238
239
240
241
    /**
242
     * Update row.
243
     *
244
     * @return void
245
     */
246 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...
247
    {
248 2
        $this->checkDb();
249 2
        $properties = $this->getProperties();
250 2
        unset($properties[$this->tableIdColumn]);
251 2
        $columns = array_keys($properties);
252 2
        $values  = array_values($properties);
253 2
        $values[] = $this->{$this->tableIdColumn};
254
255 2
        $this->db->connect()
256 2
                 ->update($this->tableName, $columns)
257 2
                 ->where("{$this->tableIdColumn} = ?")
258 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...
259 2
    }
260
261
262
263
    /**
264
     * Update row where.
265
     *
266
     * @param string $where to use in where statement.
267
     * @param mixed  $value to use in where statement.
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
268
     *
269
     * @return void
270
     */
271
    protected function updateWhere($where, $param)
272
    {
273
        $this->checkDb();
274
        $properties = $this->getProperties();
275
        $columns = array_keys($properties);
276
        $values  = array_values($properties);
277
278
        $params = is_array($param) ? $param : [$param];
279
280
        $what = array_merge($values, $params);
281
282
        $this->db->connect()
283
                 ->update($this->tableName, $columns)
284
                 ->where($where)
285
                 ->execute($what);
0 ignored issues
show
Documentation introduced by
$what 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...
286
    }
287
288
289
290
    /**
291
     * Delete row.
292
     *
293
     * @param integer $id to delete or use $this->{$this->tableIdColumn} as default.
294
     *
295
     * @return void
296
     */
297 1
    public function delete($id = null)
298
    {
299 1
        $this->checkDb();
300 1
        $id = $id ?: $this->{$this->tableIdColumn};
301
302 1
        $this->db->connect()
303 1
                 ->deleteFrom($this->tableName)
304 1
                 ->where("{$this->tableIdColumn} = ?")
305 1
                 ->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...
306
307 1
        $this->{$this->tableIdColumn} = null;
308 1
    }
309
310
311
312
    /**
313
     * Delete row where.
314
     *
315
     * @param string $where to use in where statement.
316
     * @param mixed  $value to use in where statement.
317
     *
318
     * @return void
319
     */
320 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...
321
    {
322
        $this->checkDb();
323
        $params = is_array($value) ? $value : [$value];
324
325
        $this->db->connect()
326
                 ->deleteFrom($this->tableName)
327
                 ->where($where)
328
                 ->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...
329
330
        $this->id = null;
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
331
    }
332
}
333