Passed
Push — master ( 76c092...6f3180 )
by Mikael
01:34
created

ActiveRecordModel   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 181
Duplicated Lines 15.47 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 28
loc 181
ccs 0
cts 64
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setDb() 0 4 1
A checkDb() 0 6 2
A getProperties() 0 8 1
A find() 0 10 1
A findAll() 0 9 1
A save() 0 8 2
A create() 14 14 1
A update() 14 14 1
A delete() 0 12 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
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
    public function setDb(DatabaseQueryBuilder $db)
35
    {
36
        $this->db = $db;
37
    }
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
    protected function checkDb()
49
    {
50
        if (!$this->db) {
51
            throw new ActiveRecordException("Missing \$db, did you forget to inject/set is?");
52
        }
53
    }
54
55
56
57
    /**
58
     * Get essential object properties.
59
     *
60
     * @return array with object properties.
61
     */
62
    private function getProperties()
63
    {
64
        $properties = get_object_vars($this);
65
        unset($properties['tableName']);
66
        unset($properties['db']);
67
        unset($properties['di']);
68
        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
    public function find($column, $value)
83
    {
84
        $this->checkDb();
85
        return $this->db->connect()
86
                        ->select()
87
                        ->from($this->tableName)
88
                        ->where("$column = ?")
89
                        ->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
                        ->fetchInto($this);
91
    }
92
93
94
95
    /**
96
     * Find and return all.
97
     *
98
     * @return array
99
     */
100
    public function findAll()
101
    {
102
        $this->checkDb();
103
        return $this->db->connect()
104
                        ->select()
105
                        ->from($this->tableName)
106
                        ->execute()
107
                        ->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
     * Save current object/row, insert if id is missing and do an
114
     * update if the id exists.
115
     *
116
     * @return void
117
     */
118
    public function save()
119
    {
120
        if (isset($this->id)) {
121
            return $this->update();
122
        }
123
124
        return $this->create();
125
    }
126
127
128
129
    /**
130
     * Create new row.
131
     *
132
     * @return void
133
     */
134 View Code Duplication
    private 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...
135
    {
136
        $this->checkDb();
137
        $properties = $this->getProperties();
138
        unset($properties['id']);
139
        $columns = array_keys($properties);
140
        $values  = array_values($properties);
141
142
        $this->db->connect()
143
                 ->insert($this->tableName, $columns)
144
                 ->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...
145
146
        $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...
147
    }
148
149
150
151
    /**
152
     * Update row.
153
     *
154
     * @return void
155
     */
156 View Code Duplication
    private 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...
157
    {
158
        $this->checkDb();
159
        $properties = $this->getProperties();
160
        unset($properties['id']);
161
        $columns = array_keys($properties);
162
        $values  = array_values($properties);
163
        $values[] = $this->id;
164
165
        $this->db->connect()
166
                 ->update($this->tableName, $columns)
167
                 ->where("id = ?")
168
                 ->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...
169
    }
170
171
172
173
    /**
174
     * Delete row.
175
     *
176
     * @param integer $id to delete or use $this->id as default.
177
     *
178
     * @return void
179
     */
180
    public function delete($id = null)
181
    {
182
        $this->checkDb();
183
        $id = $id ?: $this->id;
184
185
        $this->db->connect()
186
                 ->deleteFrom($this->tableName)
187
                 ->where("id = ?")
188
                 ->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...
189
190
        $this->id = null;
191
    }
192
}
193