Passed
Pull Request — master (#5)
by
unknown
01:29
created

ActiveRecordModel::findAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 1
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
    public function findAllWhere($where, $value)
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
     * Execute rawsql
137
     * @param string $sql rawsql
138
     * @param array $params params
139
     *
140
     * @return array
141
     */
142
    public function findAllSql($sql, $params)
143
    {
144
        $this->checkDb();
145
        return $this->db->connect()
146
                        ->execute($sql, $params)
147
                        ->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...
148
    }
149
150
151
152
    /**
153
     * Save current object/row, insert if id is missing and do an
154
     * update if the id exists.
155
     *
156
     * @return void
157
     */
158 4
    public function save()
159
    {
160 4
        if (isset($this->id)) {
161
            return $this->update();
162
        }
163
164 4
        return $this->create();
165
    }
166
167
168
169
    /**
170
     * Create new row.
171
     *
172
     * @return void
173
     */
174 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...
175
    {
176 4
        $this->checkDb();
177 3
        $properties = $this->getProperties();
178 3
        unset($properties['id']);
179 3
        $columns = array_keys($properties);
180 3
        $values  = array_values($properties);
181
182 3
        $this->db->connect()
183 3
                 ->insert($this->tableName, $columns)
184 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...
185
186 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...
187 3
    }
188
189
190
191
    /**
192
     * Update row.
193
     *
194
     * @return void
195
     */
196 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...
197
    {
198
        $this->checkDb();
199
        $properties = $this->getProperties();
200
        unset($properties['id']);
201
        $columns = array_keys($properties);
202
        $values  = array_values($properties);
203
        $values[] = $this->id;
204
205
        $this->db->connect()
206
                 ->update($this->tableName, $columns)
207
                 ->where("id = ?")
208
                 ->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...
209
    }
210
211
212
213
    /**
214
     * Delete row.
215
     *
216
     * @param integer $id to delete or use $this->id as default.
217
     *
218
     * @return void
219
     */
220
    public function delete($id = null)
221
    {
222
        $this->checkDb();
223
        $id = $id ?: $this->id;
224
225
        $this->db->connect()
226
                 ->deleteFrom($this->tableName)
227
                 ->where("id = ?")
228
                 ->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...
229
230
        $this->id = null;
231
    }
232
}
233