Issues (91)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Database/ActiveRecordModel.php (17 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
13
 */
14
class ActiveRecordModel
15
{
16
    /**
17
     * @var DatabaseQueryBuilder $db the object for persistent
18
     *                               storage.
19
     */
20
    protected $db = null;
21
22
    /**
23
     * @var string $tableName name of the database table.
24
     */
25
    protected $tableName = null;
26
27
28
29
    /**
30
     * Set the database object to use for accessing storage.
31
     *
32
     * @param DatabaseQueryBuilder $db as database access object.
33
     *
34
     * @return void
35
     */
36
    public function setDb(DatabaseQueryBuilder $db)
37
    {
38
        $this->db = $db;
39
    }
40
41
42
43
    /**
44
     * Check if database is injected or throw an exception.
45
     *
46
     * @throws ActiveRecordException when database is not set.
47
     *
48
     * @return void
49
     */
50
    protected function checkDb()
51
    {
52
        if (!$this->db) {
53
            throw new ActiveRecordException("Missing \$db, did you forget to inject/set is?");
54
        }
55
    }
56
57
58
59
    /**
60
     * Get essential object properties.
61
     *
62
     * @return array with object properties.
63
     */
64
    protected function getProperties()
65
    {
66
        $properties = get_object_vars($this);
67
        unset($properties['tableName']);
68
        unset($properties['db']);
69
        unset($properties['di']);
70
        return $properties;
71
    }
72
73
74
75
    /**
76
     * Find and return first object found by search criteria and use
77
     * its data to populate this instance.
78
     *
79
     * @param string $column to use in where statement.
80
     * @param mixed  $value  to use in where statement.
81
     *
82
     * @return this
83
     */
84
    public function find($column, $value)
85
    {
86
        return $this->findWhere("$column = ?", $value);
87
    }
88
89
90
91
    /**
92
     * Find and return first object by its tableIdColumn and use
93
     * its data to populate this instance.
94
     *
95
     * @param integer $id to find or use $this->{$this->tableIdColumn}
96
     *                    as default.
97
     *
98
     * @return this
99
     */
100
    public function findById($id = null)
101
    {
102
        $id = $id ?: $this->{$this->tableIdColumn};
0 ignored issues
show
The property tableIdColumn 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...
103
        return $this->findWhere("{$this->tableIdColumn} = ?", $id);
104
    }
105
106
107
108
    /**
109
     * Find and return first object found by search criteria and use
110
     * its data to populate this instance.
111
     *
112
     * The search criteria `$where` of can be set up like this:
113
     *  `id = ?`
114
     *  `id1 = ? and id2 = ?`
115
     *
116
     * The `$value` can be a single value or an array of values.
117
     *
118
     * @param string $where to use in where statement.
119
     * @param mixed  $value to use in where statement.
120
     *
121
     * @return this
122
     */
123 View Code Duplication
    public function findWhere($where, $value)
0 ignored issues
show
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...
124
    {
125
        $this->checkDb();
126
        $params = is_array($value) ? $value : [$value];
127
        return $this->db->connect()
128
                        ->select()
129
                        ->from($this->tableName)
130
                        ->where($where)
131
                        ->execute($params)
0 ignored issues
show
$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...
132
                        ->fetchInto($this);
133
    }
134
135
136
137
    /**
138
     * Find and return all.
139
     *
140
     * @return array of object of this class
141
     */
142
    public function findAll()
143
    {
144
        $this->checkDb();
145
        return $this->db->connect()
146
                        ->select()
147
                        ->from($this->tableName)
148
                        ->execute()
149
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
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...
150
    }
151
152
153
154 View Code Duplication
    public function findAllLimitOrderBy($order, $number)
0 ignored issues
show
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...
155
    {
156
        $this->checkDb();
157
        return $this->db->connect()
158
                        ->select()
159
                        ->from($this->tableName)
160
                        ->orderBy($order)
161
                        ->limit($number)
162
                        ->execute()
163
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
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...
164
    }
165
166 View Code Duplication
    public function findAllLimit($number)
0 ignored issues
show
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...
167
    {
168
        $this->checkDb();
169
        return $this->db->connect()
170
                        ->select()
171
                        ->from($this->tableName)
172
                        ->limit($number)
173
                        ->execute()
174
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
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...
175
    }
176
177
178
179
180
    /**
181
     * Find and return all matching the search criteria.
182
     *
183
     * The search criteria `$where` of can be set up like this:
184
     *  `id = ?`
185
     *  `id IN [?, ?]`
186
     *
187
     * The `$value` can be a single value or an array of values.
188
     *
189
     * @param string $where to use in where statement.
190
     * @param mixed  $value to use in where statement.
191
     *
192
     * @return array of object of this class
193
     */
194 View Code Duplication
    public function findAllWhere($where, $value)
0 ignored issues
show
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
        $params = is_array($value) ? $value : [$value];
198
        return $this->db->connect()
199
                        ->select()
200
                        ->from($this->tableName)
201
                        ->where($where)
202
                        ->execute($params)
0 ignored issues
show
$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...
203
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
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...
204
    }
205
206
207
    /**
208
     * Execute rawsql
209
     *
210
     * @return array
211
     */
212
    public function findAllSql($sql, $params = [])
213
    {
214
        $this->checkDb();
215
        return $this->db->connect()
216
                        ->execute($sql, $params)
217
                        ->fetchAllClass(get_class($this));
0 ignored issues
show
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...
218
    }
219
220
221
    public function next()
222
    {
223
        return $this->db->next();
0 ignored issues
show
The method next() does not seem to exist on object<Anax\Database\DatabaseQueryBuilder>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
224
    }
225
226
227
    /**
228
     * Execute rawsql
229
     *
230
     * @return array
231
     */
232
    public function findAllSqlTest($sql, $params)
233
    {
234
        $this->checkDb();
235
        return $this->db->connect()
236
                        ->executeFetchAll($sql, $params);
237
    }
238
239
240
241
    /**
242
     * Save current object/row, insert if id is missing and do an
243
     * update if the id exists.
244
     *
245
     * @return void
246
     */
247
    public function save($idName = null, $id = null)
248
    {
249
        if (isset($this->id)) {
250
            return $this->update();
251
        } elseif ($idName !== null) {
252
            return $this->update($idName, $id);
253
        }
254
255
        return $this->create();
256
    }
257
258
259
260
    /**
261
     * Create new row.
262
     *
263
     * @return void
264
     */
265
    protected function create()
266
    {
267
        $this->checkDb();
268
        $properties = $this->getProperties();
269
        unset($properties['id']);
270
        $columns = array_keys($properties);
271
        $values  = array_values($properties);
272
273
        $this->db->connect()
274
                 ->insert($this->tableName, $columns)
275
                 ->execute($values);
0 ignored issues
show
$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...
276
277
        $this->id = $this->db->lastInsertId();
0 ignored issues
show
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...
278
    }
279
280
281
282
    /**
283
     * Update row.
284
     *
285
     * @return void
286
     */
287
    protected function update($idName = null, $id = null)
288
    {
289
        $this->checkDb();
290
        $properties = $this->getProperties();
291
        unset($properties['id']);
292
        $columns = array_keys($properties);
293
        $values  = array_values($properties);
294
        $values[] = isset($this->id) ? $this->id : $id ;
295
        $setId = $idName !== null ? $idName : "id";
296
297
        $this->db->connect()
298
                 ->update($this->tableName, $columns)
299
                 ->where("$setId = ?")
300
                 ->execute($values);
0 ignored issues
show
$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...
301
    }
302
303
304
305
    /**
306
     * Delete row.
307
     *
308
     * @param integer $id to delete or use $this->id as default.
309
     *
310
     * @return void
311
     */
312
    public function delete($idName = null, $id = null)
313
    {
314
        $this->checkDb();
315
        $id = $id ?: $this->id;
316
        $setId = $idName !== null ? $idName : "id";
317
318
        $this->db->connect()
319
                 ->deleteFrom($this->tableName)
320
                 ->where("$setId = ?")
321
                 ->execute([$id]);
0 ignored issues
show
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
        $this->id = null;
324
    }
325
326
327
    public function lastInsertId()
328
    {
329
        return $this->db->lastInsertId();
330
    }
331
}
332