This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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 Opeyemiabiodun\PotatoORM\Models; |
||
4 | |||
5 | use RuntimeException; |
||
6 | use InvalidArgumentException; |
||
7 | use Opeyemiabiodun\PotatoORM\Connections\Connection; |
||
8 | use Opeyemiabiodun\PotatoORM\Connections\ConnectionFactory; |
||
9 | use Opeyemiabiodun\PotatoORM\Exceptions\AssignmentException; |
||
10 | use Opeyemiabiodun\PotatoORM\Exceptions\PropertyNotFoundException; |
||
11 | |||
12 | trait Model |
||
13 | { |
||
14 | /** |
||
15 | * The model's attributes. |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $_attributes = []; |
||
20 | |||
21 | /** |
||
22 | * The model's database connection. |
||
23 | * |
||
24 | * @var Opeyemiabiodun\PotatoORM\Connections\Connection |
||
25 | */ |
||
26 | protected static $_connection; |
||
27 | |||
28 | /** |
||
29 | * The primary key of the model's database table. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected static $_primaryKey; |
||
34 | |||
35 | /** |
||
36 | * The model's database table. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected static $_table; |
||
41 | |||
42 | /** |
||
43 | * The model's constructor method. |
||
44 | * |
||
45 | * @param Connection|null $connection An Opeyemiabiodun\PotatoORM\Connections\Connection instance or null |
||
46 | * @param string $table The name of the model's table in the database |
||
47 | */ |
||
48 | 8 | public function __construct($array = [], Connection $connection = null, $table = null) |
|
49 | { |
||
50 | 8 | if (null === $connection) { |
|
51 | 8 | $connection = ConnectionFactory::load(); |
|
52 | 8 | } |
|
53 | |||
54 | 8 | if (null === $table) { |
|
55 | 7 | $table = strtolower(substr(get_class($this), strripos(get_class($this), '\\') + 1)).'_table'; |
|
56 | 7 | } |
|
57 | |||
58 | 8 | $this->setConnection($connection); |
|
59 | 8 | $this->setTable($table); |
|
60 | |||
61 | 7 | if (! empty($array)) { |
|
62 | 1 | $this->setProperties($array); |
|
0 ignored issues
–
show
|
|||
63 | 1 | } |
|
64 | 7 | } |
|
65 | |||
66 | /** |
||
67 | * The getter method for the model's properties. |
||
68 | * |
||
69 | * @param string $property The particular property |
||
70 | * |
||
71 | * @return int|float|string|bool The value of the property |
||
72 | */ |
||
73 | 2 | public function __get($property) |
|
74 | { |
||
75 | 2 | View Code Duplication | if (array_key_exists($property, $this->_attributes)) { |
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. ![]() |
|||
76 | 1 | return $this->_attributes[$property]; |
|
77 | } else { |
||
78 | 1 | throw new PropertyNotFoundException('The '.get_class($this)." instance has no {$property} property."); |
|
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * The setter method for the model's properties. |
||
84 | * |
||
85 | * @param string $property The particular property |
||
86 | * @param int|float|string|bool $value The value of the property |
||
87 | */ |
||
88 | 4 | public function __set($property, $value) |
|
89 | { |
||
90 | 4 | if (! is_scalar($value)) { |
|
91 | 1 | throw new AssignmentException("Only scalar values can be assigned to the {$property} property."); |
|
92 | } |
||
93 | |||
94 | 3 | View Code Duplication | if (array_key_exists($property, $this->_attributes)) { |
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. ![]() |
|||
95 | 2 | $this->_attributes[$property] = $value; |
|
96 | 2 | } else { |
|
97 | 1 | throw new PropertyNotFoundException('The '.get_class($this)." instance has no {$property} property."); |
|
98 | } |
||
99 | 2 | } |
|
100 | |||
101 | /** |
||
102 | * Deletes a specified instance of the model in the database. |
||
103 | * |
||
104 | * @param int $number Specifies which model instance to delete; the 1st, 2nd, 3rd, ..... |
||
105 | * |
||
106 | * @return bool Returns boolean true if the instance was successfully deleted or else it returns false. |
||
107 | */ |
||
108 | 3 | public static function destroy($number) |
|
109 | { |
||
110 | 3 | if (! is_int($number)) { |
|
111 | 1 | throw new InvalidArgumentException("The parameter {$number} is not an integer. An integer is required instead."); |
|
112 | } |
||
113 | |||
114 | 2 | if ($number <= 0) { |
|
115 | 1 | throw new InvalidArgumentException("The parameter {$number} is not a positive integer. A positive integer is required instead."); |
|
116 | } |
||
117 | |||
118 | 1 | return self::$_connection->deleteRecord(self::$_table, $number - 1); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Finds a specified instance of the model in the database. |
||
123 | * |
||
124 | * @param int $number Specifies which model instance to find; the 1st, 2nd, 3rd, ..... |
||
125 | * |
||
126 | * @return array Returns the particular instance of the model. |
||
127 | */ |
||
128 | 3 | public static function find($number) |
|
129 | { |
||
130 | 3 | if (! is_int($number)) { |
|
131 | 1 | throw new InvalidArgumentException("The parameter {$number} is not an integer. An integer is required instead."); |
|
132 | } |
||
133 | |||
134 | 2 | if ($number <= 0) { |
|
135 | 1 | throw new InvalidArgumentException("The parameter {$number} is not a positive integer. A positive integer is required instead."); |
|
136 | } |
||
137 | |||
138 | 1 | $result = new self(); |
|
139 | |||
140 | 1 | $record = self::$_connection->findRecord(self::$_table, $number - 1); |
|
141 | |||
142 | 1 | $result->setProperties($record[0]); |
|
143 | |||
144 | 1 | return $result; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Returns all instances of the model in the database. |
||
149 | * |
||
150 | * @return array All instances of the model in the database. |
||
151 | */ |
||
152 | 1 | public static function getAll() |
|
153 | { |
||
154 | 1 | $records = self::$_connection->getAllRecords(self::$_table); |
|
155 | |||
156 | 1 | $count = count($records); |
|
157 | 1 | $result = []; |
|
158 | |||
159 | 1 | for ($i = 0; $i < $count; $i++) { |
|
160 | 1 | array_push($result, new self($records[$i])); |
|
161 | 1 | } |
|
162 | |||
163 | 1 | return $result; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * Checks the attributes of the model to ensure they are not all null. |
||
168 | * |
||
169 | * @return bool true if at least one of the models's attributes is not null else false. |
||
170 | */ |
||
171 | 3 | private function hasAttributes() |
|
172 | { |
||
173 | 3 | $hasAttributes = false; |
|
174 | |||
175 | 3 | foreach ($this->_attributes as $key => $value) { |
|
176 | 3 | if (! is_null($value)) { |
|
177 | 2 | $hasAttributes = true; |
|
178 | 2 | } |
|
179 | 3 | } |
|
180 | |||
181 | 3 | return $hasAttributes; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Saves or updates an instance of the model in the database. |
||
186 | * |
||
187 | * @return bool Returns true if the operation was successfully else returns false. |
||
188 | */ |
||
189 | 3 | public function save() |
|
190 | { |
||
191 | 3 | if (! $this->hasAttributes()) { |
|
192 | 1 | throw new RuntimeException(get_class($this).' model has nothing to persist to the database.'); |
|
193 | } |
||
194 | |||
195 | 2 | $pk = (empty($this->_attributes[self::$_primaryKey])) ? 'NULL' : $this->_attributes[self::$_primaryKey]; |
|
196 | |||
197 | //$record = self::$_connection->findRecord(self::$_table, (string) $pk); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
70% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
198 | |||
199 | 2 | if ('NULL' === $pk) { |
|
200 | 1 | return self::$_connection->createRecord(self::$_table, $this->_attributes); |
|
201 | } else { |
||
202 | 1 | return self::$_connection->updateRecord(self::$_table, (string) $pk, $this->_attributes); |
|
203 | } |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Sets the model's connection. |
||
208 | * |
||
209 | * @param Opeyemiabiodun\PotatoORM\Connections\Connection $connection An instance of Opeyemiabiodun\PotatoORM\Connections\Connection. |
||
210 | */ |
||
211 | 8 | protected function setConnection(Connection $connection) |
|
212 | { |
||
213 | 8 | self::$_connection = $connection; |
|
0 ignored issues
–
show
It seems like
$connection of type object<Opeyemiabiodun\Po...Connections\Connection> is incompatible with the declared type object<Opeyemiabiodun\Po...Connections\Connection> of property $_connection .
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.. ![]() |
|||
214 | 8 | } |
|
215 | |||
216 | /** |
||
217 | * Sets the model's properties. |
||
218 | * |
||
219 | * @param string $array An array containing the model's poperties. |
||
220 | */ |
||
221 | 2 | public function setProperties($array) |
|
222 | { |
||
223 | 2 | foreach ($this->_attributes as $key => $value) { |
|
224 | 2 | if (! empty($array[$key])) { |
|
225 | 2 | $this->_attributes[$key] = $array[$key]; |
|
226 | 2 | } |
|
227 | 2 | } |
|
228 | 2 | } |
|
229 | |||
230 | /** |
||
231 | * Sets the model's table. |
||
232 | * |
||
233 | * @param string $table An existing table in the database. |
||
234 | */ |
||
235 | 8 | protected function setTable($table) |
|
236 | { |
||
237 | 8 | if (gettype($table) !== 'string') { |
|
238 | 1 | throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead."); |
|
239 | } |
||
240 | |||
241 | 7 | self::$_table = $table; |
|
242 | |||
243 | 7 | $columns = self::$_connection->getColumns($table); |
|
244 | |||
245 | 7 | for ($i = 0; $i < count($columns); $i++) { |
|
0 ignored issues
–
show
It seems like you are calling the size function
count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}
// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
![]() |
|||
246 | 7 | $this->_attributes[$columns[$i]['column_name']] = null; |
|
247 | 7 | } |
|
248 | |||
249 | 7 | self::$_primaryKey = self::$_connection->getPrimaryKey($table); |
|
250 | 7 | } |
|
251 | } |
||
252 |
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: