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 | * This file is part of the ICanBoogie package. |
||
5 | * |
||
6 | * (c) Olivier Laviale <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace ICanBoogie\Module\Operation; |
||
13 | |||
14 | use ICanBoogie\ActiveRecord; |
||
15 | use ICanBoogie\ActiveRecord\SchemaColumn; |
||
16 | use ICanBoogie\ErrorCollection; |
||
17 | use ICanBoogie\Module; |
||
18 | use ICanBoogie\Operation; |
||
19 | |||
20 | /** |
||
21 | * The "save" operation is used to create or update a record. |
||
22 | * |
||
23 | * @property array $properties The properties to save. |
||
24 | */ |
||
25 | class SaveOperation extends Operation |
||
26 | { |
||
27 | /** |
||
28 | * Change controls: |
||
29 | * |
||
30 | * - CONTROL_PERMISSION: Module::PERMISSION_CREATE |
||
31 | * - CONTROL_OWNERSHIP: true |
||
32 | * - CONTROL_FORM: true |
||
33 | * |
||
34 | * @return array |
||
35 | */ |
||
36 | View Code Duplication | protected function get_controls() |
|
0 ignored issues
–
show
|
|||
37 | { |
||
38 | return [ |
||
0 ignored issues
–
show
The return type of
return array(self::CONTR...parent::get_controls(); (boolean[] ) is incompatible with the return type of the parent method ICanBoogie\Operation::get_controls of type false[] .
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: class Author {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Post {
public function getAuthor() {
return 'Johannes';
}
}
class BlogPost extends Post {
public function getAuthor() {
return new Author('Johannes');
}
}
class ForumPost extends Post { /* ... */ }
function my_function(Post $post) {
echo strtoupper($post->getAuthor());
}
Our function
Loading history...
|
|||
39 | |||
40 | self::CONTROL_PERMISSION => Module::PERMISSION_CREATE, |
||
41 | self::CONTROL_RECORD => true, |
||
42 | self::CONTROL_OWNERSHIP => true, |
||
43 | self::CONTROL_FORM => true |
||
44 | |||
45 | ] + parent::get_controls(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Filters out the operation's parameters, which are not defined as fields by the |
||
50 | * primary model of the module, and take care of filtering or resolving properties values. |
||
51 | * |
||
52 | * Fields defined as 'boolean' |
||
53 | * --------------------------- |
||
54 | * |
||
55 | * The value of the property is filtered using the filter_var() function and the |
||
56 | * FILTER_VALIDATE_BOOLEAN filter. If the property in the operation params is empty, the |
||
57 | * property value is set the `false`. |
||
58 | * |
||
59 | * Fields defined as 'varchar' |
||
60 | * --------------------------- |
||
61 | * |
||
62 | * If the property is not empty in the operation params, the property value is trimmed using the |
||
63 | * trim() function, ensuring that there is no leading or trailing white spaces. |
||
64 | * |
||
65 | * **Note::** The getter should only be called during the {@link process()} method. |
||
66 | * |
||
67 | * @return array The properties of the operation. |
||
68 | */ |
||
69 | protected function lazy_get_properties() |
||
70 | { |
||
71 | $schema = $this->module->model->extended_schema; |
||
72 | $request = $this->request; |
||
73 | $properties = array_intersect_key($request->params, $schema->columns); |
||
74 | |||
75 | foreach ($schema as $identifier => $column) |
||
76 | { |
||
77 | $type = $column->type; |
||
78 | |||
79 | if ($type == SchemaColumn::TYPE_BOOLEAN) |
||
80 | { |
||
81 | if ($column->null && ($request[$identifier] === null || $request[$identifier] === '')) |
||
82 | { |
||
83 | $properties[$identifier] = null; |
||
84 | } |
||
85 | else |
||
86 | { |
||
87 | if (empty($properties[$identifier])) |
||
88 | { |
||
89 | $properties[$identifier] = false; |
||
90 | |||
91 | continue; |
||
92 | } |
||
93 | |||
94 | $properties[$identifier] = filter_var($properties[$identifier], FILTER_VALIDATE_BOOLEAN); |
||
95 | } |
||
96 | } |
||
97 | else if ($type == SchemaColumn::TYPE_VARCHAR) |
||
98 | { |
||
99 | if (empty($properties[$identifier]) || !is_string($properties[$identifier])) |
||
100 | { |
||
101 | continue; |
||
102 | } |
||
103 | |||
104 | $properties[$identifier] = trim($properties[$identifier]); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | unset($properties[$schema->primary]); |
||
109 | |||
110 | return $properties; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Overrides the getter to prevent exceptions when the operation key is empty. |
||
115 | */ |
||
116 | protected function lazy_get_record() |
||
117 | { |
||
118 | return $this->key ? parent::lazy_get_record() : null; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Overrides the method in order for the control to pass if the operation key is empty, which |
||
123 | * is the case when creating a new record. |
||
124 | */ |
||
125 | protected function control_record() |
||
126 | { |
||
127 | return $this->key ? parent::control_record() : true; |
||
0 ignored issues
–
show
The expression
$this->key ? parent::control_record() : true; of type ICanBoogie\ActiveRecord|boolean adds the type boolean to the return on line 127 which is incompatible with the return type of the parent method ICanBoogie\Operation::control_record of type ICanBoogie\ActiveRecord|null .
Loading history...
|
|||
128 | } |
||
129 | |||
130 | /** |
||
131 | * The method simply returns true. |
||
132 | * |
||
133 | * @param ErrorCollection $errors |
||
134 | * |
||
135 | * @return bool |
||
136 | */ |
||
137 | protected function validate(ErrorCollection $errors) |
||
138 | { |
||
139 | return true; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Creates or updates a record in the module's primary model. |
||
144 | * |
||
145 | * A record is created if the operation's key is empty, otherwise an existing record is |
||
146 | * updated. |
||
147 | * |
||
148 | * The method uses the `properties` property to get the properties used to create or update |
||
149 | * the record. |
||
150 | * |
||
151 | * @return array An array composed of the save mode ('update' or 'new') and the record's |
||
152 | * key. |
||
153 | * |
||
154 | * @throws \RuntimeException when saving the record fails. |
||
155 | */ |
||
156 | protected function process() |
||
157 | { |
||
158 | $key = $this->key; |
||
159 | $properties = $this->properties; |
||
160 | $log_params = [ 'key' => $key, 'module' => $this->module->title ]; |
||
161 | |||
162 | try |
||
163 | { |
||
164 | $record_key = $key |
||
165 | ? $this->update_record($properties) |
||
166 | : $this->create_record($properties, $this->record); |
||
167 | } |
||
168 | catch (ActiveRecord\RecordNotValid $e) |
||
0 ignored issues
–
show
The class
ICanBoogie\ActiveRecord\RecordNotValid does not exist. Did you forget a USE statement, or did you not list all dependencies?
Scrutinizer analyzes your It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.
Loading history...
|
|||
169 | { |
||
170 | $this->response->errors->merge($e->errors->to_error_collection()); |
||
171 | |||
172 | throw $e; |
||
173 | } |
||
174 | |||
175 | if (!$record_key) |
||
176 | { |
||
177 | throw new \RuntimeException($this->format($key ? 'Unable to update record %key in %module.' : 'Unable to create record in %module.', $log_params)); |
||
178 | } |
||
179 | |||
180 | $this->response->location = $this->request->uri; |
||
181 | $this->response->message = $this->format($key ? 'The record %key in %module has been saved.' : 'A new record has been saved in %module.', $log_params); |
||
0 ignored issues
–
show
It seems like
$this->format($key ? 'Th...%module.', $log_params) of type object<ICanBoogie\I18n\FormattedString> is incompatible with the declared type string of property $message .
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...
|
|||
182 | |||
183 | return [ 'mode' => $key ? 'update' : 'new', 'key' => $record_key ]; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Update the operation record with properties. |
||
188 | * |
||
189 | * @param array $properties |
||
190 | * |
||
191 | * @return bool|int |
||
192 | */ |
||
193 | protected function update_record(array $properties) |
||
194 | { |
||
195 | return $this->record->assign($properties)->save(); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Creates a record from properties. |
||
200 | * |
||
201 | * @param array $properties |
||
202 | * @param ActiveRecord $record The new record is saved in that variable. |
||
203 | * |
||
204 | * @return bool|int |
||
205 | */ |
||
206 | protected function create_record(array $properties, &$record) |
||
207 | { |
||
208 | $record = $this->module->model->new($properties); |
||
0 ignored issues
–
show
The call to
Model::new() has too many arguments starting with $properties .
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. In this case you can add the
Loading history...
|
|||
209 | |||
210 | return $record->save(); |
||
211 | } |
||
212 | } |
||
213 |
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.