Issues (16)

Security Analysis    not enabled

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/Model.php (3 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 Pyjac\ORM;
4
5
use Doctrine\Common\Inflector\Inflector;
6
use PDO;
7
8
abstract class Model implements ModelInterface
9
{
10
    /**
11
     * The table associated with the model.
12
     *
13
     * @var string
14
     */
15
    protected $table;
16
17
    protected $properties = [];
18
19
    /**
20
     * Store instance of database connection used.
21
     *
22
     * @var Pyjac\ORM\DatabaseConnection
23
     */
24
    protected $databaseConnection;
25
26
     /**
27
      *  The id of the model.
28
      *
29
      * @property string $id
30
      */
31
32
     /**
33
      * Create a model instance.
34
      */
35
     public function __construct(DatabaseConnectionInterface $databaseConnection = null)
36
     {
37
         if ($databaseConnection == null) {
38
             $this->databaseConnection = DatabaseConnection::getInstance()->databaseConnection;
39
         } else {
40
             $this->databaseConnection = $databaseConnection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $databaseConnection of type object<Pyjac\ORM\DatabaseConnectionInterface> is incompatible with the declared type object<Pyjac\ORM\Pyjac\ORM\DatabaseConnection> of property $databaseConnection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
         }
42
     }
43
44
    /**
45
     * Sets into $properties the $key => $value pairs.
46
     *
47
     * @param string $key
48
     * @param string $val
49
     */
50
    public function __set($key, $val)
51
    {
52
        $this->properties[$key] = $val;
53
    }
54
55
    /**
56
     * @param string $key
57
     *
58
     * @return array
59
     */
60
    public function __get($key)
61
    {
62
        if (isset($this->properties[$key])) {
63
            return $this->properties[$key];
64
        }
65
    }
66
67
     /**
68
      * Get all the model properties.
69
      *
70
      * @return array
71
      */
72
     public function getProperties()
73
     {
74
         return $this->properties;
75
     }
76
77
     /**
78
      * Set model properties.
79
      */
80
     public function setProperties(array $properties)
81
     {
82
         $this->properties = $properties;
83
     }
84
85
    /**
86
     * Pluralize the name of the child class.
87
     *
88
     * @return string
89
     */
90
    public function getTableName()
91
    {
92
        if (isset($this->table) && !empty($this->table)) {
93
            return $this->table;
94
        }
95
        $className = explode('\\', get_called_class());
96
97
        return Inflector::pluralize(strtolower(end($className)));
98
    }
99
100
    /**
101
     * Find the particular model with the passed id.
102
     *
103
     * @param int $id
104
     *
105
     * @return object
106
     */
107
    public static function find($id)
108
    {
109
        $model = new static();
110
111
        return $model->get($id);
112
    }
113
114
    /**
115
     * Get the particular model with the passed id.
116
     *
117
     * @param int $id
118
     *
119
     * @return object|null
120
     */
121
    public function get($id)
122
    {
123
        $sql = "SELECT * FROM {$this->getTableName()} WHERE id={$id}";
124
        $sqlStatement = $this->databaseConnection->prepare($sql);
125
        $sqlStatement->setFetchMode(PDO::FETCH_CLASS, get_called_class());
126
        $sqlStatement->execute();
127
        if ($sqlStatement->rowCount() < 1) {
128
            return;
129
        }
130
131
        return $sqlStatement->fetch();
132
    }
133
134
    /**
135
     * Get all the models from the database.
136
     *
137
     * @return array
138
     */
139
    public static function getAll()
140
    {
141
        $model = new static();
142
143
        return $model->all();
144
    }
145
146
    /**
147
     * Returns all the models from the database.
148
     *
149
     * @return array
150
     */
151
    public function all()
152
    {
153
        $sql = "SELECT * FROM {$this->getTableName()}";
154
        $sqlStatement = $this->databaseConnection->prepare($sql);
155
        $sqlStatement->execute();
156
157
        return $sqlStatement->fetchAll(PDO::FETCH_CLASS, get_called_class());
158
    }
159
160
    /**
161
     * Update the model in the database.
162
     *
163
     * @return int
164
     */
165
    public function update()
166
    {
167
        $bindNameParameters = [];
168
        $sqlUpdate = 'UPDATE '.$this->getTableName().' SET ';
169 View Code Duplication
        foreach ($this->properties as $columnName => $columnValue) {
0 ignored issues
show
This code seems to be duplicated across 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...
170
            if ($columnName == 'id') {
171
                continue;
172
            }
173
            $bindColumnName = ':'.$columnName;
174
            $sqlUpdate .= "$columnName = $bindColumnName,";
175
            $bindNameParameters[$bindColumnName] = $columnValue;
176
        }
177
        //Remove the last comma in sql command then join it to the other query part.
178
        $sqlUpdate = substr($sqlUpdate, 0, -1).' WHERE id = :id';
179
        $sqlStatement = $this->databaseConnection->prepare($sqlUpdate);
180
        $bindNameParameters[':id'] = $this->properties['id'];
181
        $sqlStatement->execute($bindNameParameters);
182
183
        return $sqlStatement->rowCount();
184
    }
185
186
    /**
187
     * Insert the model values into the database.
188
     *
189
     * @return int
190
     */
191
    public function create()
192
    {
193
        $columnNames = '';
194
        $columnValues = '';
195
        $bindNameParameters = [];
196
        $sqlCreate = 'INSERT'.' INTO '.$this->getTableName().' (';
197 View Code Duplication
        foreach ($this->properties as $columnName => $columnValue) {
0 ignored issues
show
This code seems to be duplicated across 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...
198
            $bindColumnName = ':'.$columnName;
199
            $columnNames .= $columnName.',';
200
            $columnValues .= $bindColumnName.',';
201
            $bindNameParameters[$bindColumnName] = $columnValue;
202
        }
203
        // Remove ending comma and whitespace.
204
        $columnNames = substr($columnNames, 0, -1);
205
        $columnValues = substr($columnValues, 0, -1);
206
207
        $sqlCreate .= $columnNames.') VALUES ('.$columnValues.')';
208
        $sqlStatement = $this->databaseConnection->prepare($sqlCreate);
209
        $sqlStatement->execute($bindNameParameters);
210
211
        return $sqlStatement->rowCount();
212
    }
213
214
    /**
215
     * Save the model data to the database.
216
     *
217
     * @return bool
218
     */
219
    public function save()
220
    {
221
        return isset($this->properties['id']) ? $this->update() : $this->create();
222
    }
223
224
    /**
225
     * Delete a model from the database.
226
     *
227
     * @param int $id
228
     *
229
     * @return bool
230
     */
231
    public static function destroy($id)
232
    {
233
        $model = new static();
234
235
        return $model->delete($id);
236
    }
237
238
    /**
239
     * Delete model from the database.
240
     *
241
     * @param int $id
242
     *
243
     * @return bool
244
     */
245
    public function delete($id)
246
    {
247
        $sql = 'DELETE'.' FROM '.self::getTableName().' WHERE id = '.$id;
248
        $sqlStatment = $this->databaseConnection->prepare($sql);
249
        $sqlStatment->execute();
250
251
        return ($sqlStatment->rowCount() > 0) ? true : false;
252
    }
253
}
254