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 | /** |
||
4 | * @author Temitope Olotin <[email protected]> |
||
5 | * @license <https://opensource.org/license/MIT> MIT |
||
6 | */ |
||
7 | namespace Laztopaz\PotatoORM; |
||
8 | |||
9 | class BaseModel implements BaseModelInterface |
||
10 | { |
||
11 | // Inject the inflector trait |
||
12 | use Inflector; |
||
13 | |||
14 | // Private variable that contains instance of database |
||
15 | protected $databaseModel; |
||
16 | |||
17 | // Class variable holding class name pluralized |
||
18 | protected $tableName; |
||
19 | |||
20 | // Properties will later contain key, value pairs from the magic setter, getter methods |
||
21 | protected $properties = []; |
||
22 | |||
23 | public function __construct() |
||
24 | { |
||
25 | $this->tableName = $this->getClassName(); |
||
26 | |||
27 | $this->databaseModel = new DatabaseHandler($this->tableName); |
||
28 | |||
29 | $this->properties['id'] = 0; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * The magic getter method. |
||
34 | * |
||
35 | * @params key |
||
36 | * |
||
37 | * @return array key |
||
38 | */ |
||
39 | public function __get($key) |
||
40 | { |
||
41 | $this->properties[$key]; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * The magic setter method. |
||
46 | * |
||
47 | * @params property, key |
||
48 | * |
||
49 | * @return array associative array properties |
||
50 | */ |
||
51 | public function __set($property, $value) |
||
52 | { |
||
53 | $this->properties[$property] = $value; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * This method gets all the record from a particular table. |
||
58 | * |
||
59 | * @params void |
||
60 | * |
||
61 | * @throws NoRecordFoundException |
||
62 | * |
||
63 | * @return associative array |
||
64 | */ |
||
65 | public static function getAll() |
||
66 | { |
||
67 | $allData = DatabaseHandler::read($id = false, self::getClassName()); |
||
68 | |||
69 | if (count($allData) > 0) { |
||
70 | return $allData; |
||
0 ignored issues
–
show
|
|||
71 | } |
||
72 | |||
73 | throw NoRecordFoundException::create('There is no record to display'); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * This method create or update record in a database table. |
||
78 | * |
||
79 | * @params void |
||
80 | * |
||
81 | * @throws EmptyArrayException |
||
82 | * @throws NoRecordInsertionException |
||
83 | * @throws NoRecordUpdateException |
||
84 | * |
||
85 | * @return bool true or false; |
||
86 | */ |
||
87 | public function save($dbConn = Null) |
||
88 | { |
||
89 | if (is_null($dbConn)) { |
||
90 | $dbConn = new DatabaseConnection(); |
||
91 | } |
||
92 | |||
93 | $boolCommit = false; |
||
0 ignored issues
–
show
$boolCommit is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
94 | |||
95 | if ($this->properties['id']) { |
||
96 | |||
97 | $allData = DatabaseHandler::read($this->properties['id'], self::getClassName(), $dbConn); |
||
98 | |||
99 | if ($this->checkIfRecordIsEmpty($allData)) { |
||
100 | $boolCommit = $this->databaseModel->update(['id' => $this->properties['id']], $this->tableName, $this->properties, $dbConn); |
||
101 | |||
102 | if ($boolCommit) { |
||
103 | return true; |
||
104 | } |
||
105 | |||
106 | throw NoRecordUpdateException::create('Record not updated successfully'); |
||
107 | } |
||
108 | |||
109 | throw EmptyArrayException::create("Value passed didn't match any record"); |
||
110 | } |
||
111 | |||
112 | $boolCommit = $this->databaseModel->create($this->properties, $this->tableName, $dbConn); |
||
113 | |||
114 | if ($boolCommit) { |
||
115 | return true; |
||
116 | } |
||
117 | |||
118 | throw NoRecordInsertionException::create('Record not created successfully'); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * This method find a record by id. |
||
123 | * |
||
124 | * @params int id |
||
125 | * |
||
126 | * @throws NoArgumentPassedToFunctionException |
||
127 | * |
||
128 | * @return object |
||
129 | */ |
||
130 | public static function find($id) |
||
131 | { |
||
132 | $num_args = (int) func_num_args(); // get number of arguments passed to this function |
||
133 | if ($num_args == 0 || $num_args > 1) { |
||
134 | throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed'); |
||
135 | } |
||
136 | |||
137 | if ($id == '') { |
||
138 | throw NullArgumentPassedToFunctionException::create('This function expect a value'); |
||
139 | } |
||
140 | |||
141 | $staticFindInstance = new static(); |
||
142 | $staticFindInstance->id = $id == '' ? false : $id; |
||
0 ignored issues
–
show
The property
id does not exist on object<Laztopaz\PotatoORM\BaseModel> . Since you implemented __set , maybe consider adding a @property annotation.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. ![]() |
|||
143 | |||
144 | return $staticFindInstance; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * This method delete a row from the table by the row id. |
||
149 | * |
||
150 | * @params int id |
||
151 | * |
||
152 | * @throws NoRecordDeletionException; |
||
153 | * |
||
154 | * @return bool true or false |
||
155 | */ |
||
156 | public static function destroy($id) |
||
157 | { |
||
158 | $boolDeleted = false; |
||
0 ignored issues
–
show
$boolDeleted is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the ![]() |
|||
159 | |||
160 | $num_args = (int) func_num_args(); // get number of arguments passed to this function |
||
161 | |||
162 | if ($num_args == 0 || $num_args > 1) { |
||
163 | throw NoArgumentPassedToFunctionException::create('Argument missing: only one argument is allowed'); |
||
164 | } |
||
165 | |||
166 | $boolDeleted = DatabaseHandler::delete($id, self::getClassName()); |
||
167 | |||
168 | if ($boolDeleted) { |
||
169 | return true; |
||
170 | } |
||
171 | |||
172 | throw NoRecordDeletionException::create('Record deletion unsuccessful because id does not match any record'); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * This method return the current class name |
||
177 | * $params void. |
||
178 | * |
||
179 | * @return classname |
||
180 | */ |
||
181 | public static function getClassName() |
||
182 | { |
||
183 | $tableName = preg_split('/(?=[A-Z])/', get_called_class()); |
||
184 | |||
185 | $className = end($tableName); |
||
186 | |||
187 | return self::pluralize(strtolower($className)); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * This method check if the argument passed to this function is an array. |
||
192 | * |
||
193 | * @param $arrayOfRecord |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | public function checkIfRecordIsEmpty($arrayOfRecord) |
||
198 | { |
||
199 | if (count($arrayOfRecord) > 0) { |
||
200 | return true; |
||
201 | } |
||
202 | |||
203 | return false; |
||
204 | } |
||
205 | } |
||
206 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.