Completed
Pull Request — master (#7)
by
unknown
01:31
created

ActiveRecordModel::deleteWhere()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
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
27
    /**
28
     * Set the database object to use for accessing storage.
29
     *
30
     * @param DatabaseQueryBuilder $db as database access object.
31
     *
32
     * @return void
33
     */
34 3
    public function setDb(DatabaseQueryBuilder $db)
35
    {
36 3
        $this->db = $db;
37 3
    }
38
39
40
41
    /**
42
     * Check if database is injected or throw an exception.
43
     *
44
     * @throws ActiveRecordException when database is not set.
45
     *
46
     * @return void
47
     */
48 4
    protected function checkDb()
49
    {
50 4
        if (!$this->db) {
51 1
            throw new ActiveRecordException("Missing \$db, did you forget to inject/set is?");
52
        }
53 3
    }
54
55
56
57
    /**
58
     * Get essential object properties.
59
     *
60
     * @return array with object properties.
61
     */
62 3
    protected function getProperties()
63
    {
64 3
        $properties = get_object_vars($this);
65 3
        unset($properties['tableName']);
66 3
        unset($properties['db']);
67 3
        unset($properties['di']);
68 3
        return $properties;
69
    }
70
71
72
73
    /**
74
     * Find and return first object found by search criteria and use
75
     * its data to populate this instance.
76
     *
77
     * @param string $column to use in where statement.
78
     * @param mixed  $value  to use in where statement.
79
     *
80
     * @return this
81
     */
82 1
    public function find($column, $value)
83
    {
84 1
        $this->checkDb();
85 1
        return $this->db->connect()
86 1
                        ->select()
87 1
                        ->from($this->tableName)
88 1
                        ->where("$column = ?")
89 1
                        ->execute([$value])
0 ignored issues
show
Documentation introduced by
array($value) 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...
90 1
                        ->fetchInto($this);
91
    }
92
93
94
95
    /**
96
     * Find and return all.
97
     *
98
     * @return array of object of this class
99
     */
100 1
    public function findAll()
101
    {
102 1
        $this->checkDb();
103 1
        return $this->db->connect()
104 1
                        ->select()
105 1
                        ->from($this->tableName)
106 1
                        ->execute()
107 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...
108
    }
109
110
111
112
    /**
113
     * Find and return all matching a search criteria of
114
     * for example `id = ?` or `id IN [?, ?]`.
115
     *
116
     * @param string $where to use in where statement.
117
     * @param mixed  $value to use in where statement.
118
     *
119
     * @return array of object of this class
120
     */
121 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...
122
    {
123 1
        $this->checkDb();
124 1
        $params = is_array($value) ? $value : [$value];
125 1
        return $this->db->connect()
126 1
                        ->select()
127 1
                        ->from($this->tableName)
128 1
                        ->where($where)
129 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...
130 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...
131
    }
132
133
134
135
    /**
136
     * Save current object/row, insert if id is missing and do an
137
     * update if the id exists.
138
     *
139
     * @return void
140
     */
141 4
    public function save()
142
    {
143 4
        if (isset($this->id)) {
144
            return $this->update();
145
        }
146
147 4
        return $this->create();
148
    }
149
150
151
152
    /**
153
     * Save current object/row, update with where.
154
     *
155
     * @param string $where to use in where statement.
156
     * @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...
157
     *
158
     * @return void
159
     */
160
    public function saveWhere($where, $param)
161
    {
162
        return $this->updateWhere($where, $param);
163
    }
164
165
166
167
    /**
168
     * Create new row.
169
     *
170
     * @return void
171
     */
172 4 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...
173
    {
174 4
        $this->checkDb();
175 3
        $properties = $this->getProperties();
176 3
        unset($properties['id']);
177 3
        $columns = array_keys($properties);
178 3
        $values  = array_values($properties);
179
180 3
        $this->db->connect()
181 3
                 ->insert($this->tableName, $columns)
182 3
                 ->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...
183
184 3
        $this->id = $this->db->lastInsertId();
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...
185 3
    }
186
187
188
189
    /**
190
     * Update row.
191
     *
192
     * @return void
193
     */
194 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...
195
    {
196
        $this->checkDb();
197
        $properties = $this->getProperties();
198
        unset($properties['id']);
199
        $columns = array_keys($properties);
200
        $values  = array_values($properties);
201
        $values[] = $this->id;
202
203
        $this->db->connect()
204
                 ->update($this->tableName, $columns)
205
                 ->where("id = ?")
206
                 ->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...
207
    }
208
209
210
211
    /**
212
     * Update row where.
213
     *
214
     * @param string $where to use in where statement.
215
     * @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...
216
     *
217
     * @return void
218
     */
219
    protected function updateWhere($where, $param)
220
    {
221
        $this->checkDb();
222
        $properties = $this->getProperties();
223
        $columns = array_keys($properties);
224
        $values  = array_values($properties);
225
226
        $params = is_array($param) ? $param : [$param];
227
228
        $what = array_merge($values, $params);
229
230
        $this->db->connect()
231
                 ->update($this->tableName, $columns)
232
                 ->where($where)
233
                 ->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...
234
    }
235
236
237
238
    /**
239
     * Delete row.
240
     *
241
     * @param integer $id to delete or use $this->id as default.
242
     *
243
     * @return void
244
     */
245
    public function delete($id = null)
246
    {
247
        $this->checkDb();
248
        $id = $id ?: $this->id;
249
250
        $this->db->connect()
251
                 ->deleteFrom($this->tableName)
252
                 ->where("id = ?")
253
                 ->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...
254
255
        $this->id = null;
256
    }
257
258
259
260
    /**
261
     * Delete row where.
262
     *
263
     * @param string $where to use in where statement.
264
     * @param mixed  $value to use in where statement.
265
     *
266
     * @return void
267
     */
268 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...
269
    {
270
        $this->checkDb();
271
        $params = is_array($value) ? $value : [$value];
272
273
        $this->db->connect()
274
                 ->deleteFrom($this->tableName)
275
                 ->where($where)
276
                 ->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...
277
278
        $this->id = null;
279
    }
280
}
281