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 Tacone\Bees\Widget; |
|||||||||||
4 | ||||||||||||
5 | use App; |
|||||||||||
6 | use ArrayAccess; |
|||||||||||
7 | use Countable; |
|||||||||||
8 | use Illuminate\Contracts\Support\Arrayable; |
|||||||||||
9 | use Illuminate\Contracts\Support\Jsonable; |
|||||||||||
10 | use IteratorAggregate; |
|||||||||||
11 | use Tacone\Bees\Base\DelegatedArrayTrait; |
|||||||||||
12 | use Tacone\Bees\Collection\FieldCollection; |
|||||||||||
13 | use Tacone\Bees\Field\Field; |
|||||||||||
14 | use Tacone\Bees\Helper\Error; |
|||||||||||
15 | use Tacone\DataSource\AbstractDataSource; |
|||||||||||
16 | use Tacone\DataSource\DataSource; |
|||||||||||
17 | ||||||||||||
18 | class Endpoint implements Countable, IteratorAggregate, ArrayAccess, Arrayable, Jsonable |
|||||||||||
19 | { |
|||||||||||
20 | use DelegatedArrayTrait; |
|||||||||||
21 | ||||||||||||
22 | /** |
|||||||||||
23 | * @var FieldCollection |
|||||||||||
24 | */ |
|||||||||||
25 | protected $fields; |
|||||||||||
26 | ||||||||||||
27 | /** |
|||||||||||
28 | * @var AbstractDataSource |
|||||||||||
29 | */ |
|||||||||||
30 | protected $source; |
|||||||||||
31 | ||||||||||||
32 | protected $auto = false; |
|||||||||||
33 | ||||||||||||
34 | public function __construct($source = []) |
|||||||||||
35 | { |
|||||||||||
36 | $this->fields = new FieldCollection(); |
|||||||||||
37 | $this->initSource($source); |
|||||||||||
38 | } |
|||||||||||
39 | ||||||||||||
40 | public static function auto($source) |
|||||||||||
41 | { |
|||||||||||
42 | $endpoint = new static($source); |
|||||||||||
43 | $endpoint->auto = true; |
|||||||||||
44 | ||||||||||||
45 | return $endpoint; |
|||||||||||
46 | } |
|||||||||||
47 | ||||||||||||
48 | protected function initSource($source) |
|||||||||||
49 | { |
|||||||||||
50 | $this->source = DataSource::make($source); |
|||||||||||
51 | } |
|||||||||||
52 | ||||||||||||
53 | /** |
|||||||||||
54 | * @param string $name |
|||||||||||
55 | * @param array $arguments |
|||||||||||
56 | * |
|||||||||||
57 | * @return Field|static|mixed |
|||||||||||
58 | */ |
|||||||||||
59 | public function __call($name, $arguments) |
|||||||||||
60 | { |
|||||||||||
61 | // Is it a field name? |
|||||||||||
62 | try { |
|||||||||||
63 | $binding = "bees.$name"; |
|||||||||||
64 | $field = App::make($binding, $arguments); |
|||||||||||
65 | $this->fields->add($field); |
|||||||||||
66 | ||||||||||||
67 | return $field; |
|||||||||||
68 | } catch (\ReflectionException $e) { |
|||||||||||
69 | // not a field name |
|||||||||||
70 | } |
|||||||||||
71 | ||||||||||||
72 | // oh, well, then ... |
|||||||||||
73 | throw Error::missingMethod($this, $name); |
|||||||||||
74 | } |
|||||||||||
75 | ||||||||||||
76 | public function source($newSource = null) |
|||||||||||
77 | { |
|||||||||||
78 | if (func_num_args()) { |
|||||||||||
79 | $this->source = $newSource; |
|||||||||||
80 | ||||||||||||
81 | return $this; |
|||||||||||
82 | } |
|||||||||||
83 | ||||||||||||
84 | return $this->source; |
|||||||||||
85 | } |
|||||||||||
86 | ||||||||||||
87 | /** |
|||||||||||
88 | * Collection containing all the fields in the form. |
|||||||||||
89 | * |
|||||||||||
90 | * @return FieldCollection |
|||||||||||
91 | */ |
|||||||||||
92 | public function fields() |
|||||||||||
93 | { |
|||||||||||
94 | return $this->fields; |
|||||||||||
95 | } |
|||||||||||
96 | ||||||||||||
97 | /** |
|||||||||||
98 | * Get a field by name (dotted offset). |
|||||||||||
99 | * |
|||||||||||
100 | * (you can also use array notation like: |
|||||||||||
101 | * <code>$form['author.name']</code> |
|||||||||||
102 | * |
|||||||||||
103 | * @param string $name |
|||||||||||
104 | * |
|||||||||||
105 | * @return Field |
|||||||||||
106 | */ |
|||||||||||
107 | public function field($name) |
|||||||||||
108 | { |
|||||||||||
109 | return $this->fields->get($name); |
|||||||||||
110 | } |
|||||||||||
111 | ||||||||||||
112 | /** |
|||||||||||
113 | * Get the fields value as an associative array. |
|||||||||||
114 | * By default a nested array is returned. |
|||||||||||
115 | * Passing true as the first parameter, a flat |
|||||||||||
116 | * array will be returned, with dotted offsets |
|||||||||||
117 | * as the keys. |
|||||||||||
118 | * |
|||||||||||
119 | * @param bool $flat |
|||||||||||
120 | * |
|||||||||||
121 | * @return array |
|||||||||||
122 | */ |
|||||||||||
123 | public function toArray($flat = false) |
|||||||||||
124 | { |
|||||||||||
125 | return $this->fields()->toArray($flat); |
|||||||||||
126 | } |
|||||||||||
127 | ||||||||||||
128 | /** |
|||||||||||
129 | * Required by DelegatedArrayTrait. |
|||||||||||
130 | * |
|||||||||||
131 | * @return FieldCollection |
|||||||||||
132 | */ |
|||||||||||
133 | protected function getDelegatedStorage() |
|||||||||||
134 | { |
|||||||||||
135 | return $this->fields; |
|||||||||||
136 | } |
|||||||||||
137 | ||||||||||||
138 | /** |
|||||||||||
139 | * Sets the fields values back to the models. |
|||||||||||
140 | */ |
|||||||||||
141 | public function writeSource() |
|||||||||||
142 | { |
|||||||||||
143 | foreach ($this->fields as $name => $field) { |
|||||||||||
144 | $this->source[$name] = $field->cast(); |
|||||||||||
145 | } |
|||||||||||
146 | } |
|||||||||||
147 | ||||||||||||
148 | /** |
|||||||||||
149 | * Fills the form with the values coming from the DB |
|||||||||||
150 | * and HTTP input. |
|||||||||||
151 | */ |
|||||||||||
152 | public function populate() |
|||||||||||
153 | { |
|||||||||||
154 | $this->fromSource(); |
|||||||||||
155 | $this->fromInput(); |
|||||||||||
156 | } |
|||||||||||
157 | ||||||||||||
158 | public function fromSource() |
|||||||||||
159 | { |
|||||||||||
160 | return $this->from($this->source); |
|||||||||||
161 | } |
|||||||||||
162 | ||||||||||||
163 | public function fromInput() |
|||||||||||
164 | { |
|||||||||||
165 | $request = method_exists($request = \Request::instance(), 'all') |
|||||||||||
166 | ? $request |
|||||||||||
167 | : \Input::instance(); |
|||||||||||
168 | ||||||||||||
169 | return $this->from($request->all()); |
|||||||||||
170 | } |
|||||||||||
171 | ||||||||||||
172 | public function from($source) |
|||||||||||
173 | { |
|||||||||||
174 | if (!$source instanceof AbstractDataSource) { |
|||||||||||
175 | $source = DataSource::make($source); |
|||||||||||
176 | } |
|||||||||||
177 | ||||||||||||
178 | return $this->fields->from($source); |
|||||||||||
179 | } |
|||||||||||
180 | ||||||||||||
181 | /** |
|||||||||||
182 | * Saves the models back to the database. |
|||||||||||
183 | */ |
|||||||||||
184 | public function save() |
|||||||||||
185 | { |
|||||||||||
186 | return $this->source->save(); |
|||||||||||
0 ignored issues
–
show
|
||||||||||||
187 | } |
|||||||||||
188 | ||||||||||||
189 | /** |
|||||||||||
190 | * Validates the form, then sets eventual errors on each field. |
|||||||||||
191 | * |
|||||||||||
192 | * @return mixed |
|||||||||||
193 | */ |
|||||||||||
194 | public function validate() |
|||||||||||
195 | { |
|||||||||||
196 | $arguments = func_get_args(); |
|||||||||||
197 | ||||||||||||
198 | return call_user_func_array([$this->fields, 'validate'], $arguments); |
|||||||||||
199 | } |
|||||||||||
200 | ||||||||||||
201 | /** |
|||||||||||
202 | * @return mixed |
|||||||||||
203 | */ |
|||||||||||
204 | public function errors() |
|||||||||||
205 | { |
|||||||||||
206 | $arguments = func_get_args(); |
|||||||||||
207 | ||||||||||||
208 | return call_user_func_array([$this->fields, 'errors'], $arguments); |
|||||||||||
209 | } |
|||||||||||
210 | ||||||||||||
211 | /** |
|||||||||||
212 | * Convert the object to its JSON representation. |
|||||||||||
213 | * |
|||||||||||
214 | * @param int $options |
|||||||||||
215 | * |
|||||||||||
216 | * @return string |
|||||||||||
217 | */ |
|||||||||||
218 | public function toJson($options = 0) |
|||||||||||
219 | { |
|||||||||||
220 | if ($this->auto) { |
|||||||||||
221 | $this->process(); |
|||||||||||
222 | } |
|||||||||||
223 | ||||||||||||
224 | return json_encode( |
|||||||||||
225 | $this->toArray(), |
|||||||||||
226 | JSON_UNESCAPED_SLASHES |
|||||||||||
227 | ); |
|||||||||||
228 | } |
|||||||||||
229 | ||||||||||||
230 | protected function getKey() |
|||||||||||
231 | { |
|||||||||||
232 | $parameters = \Route::current()->parameters(); |
|||||||||||
233 | ||||||||||||
234 | return reset($parameters); |
|||||||||||
235 | } |
|||||||||||
236 | ||||||||||||
237 | protected function process() |
|||||||||||
238 | { |
|||||||||||
239 | $key = $this->getKey(); |
|||||||||||
240 | $this->load($key); |
|||||||||||
0 ignored issues
–
show
The call to
Endpoint::load() has too many arguments starting with $key .
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 ![]() |
||||||||||||
241 | $this->fromSource(); |
|||||||||||
242 | ||||||||||||
243 | if (\Request::method() == 'GET') { |
|||||||||||
244 | if (!$key) { |
|||||||||||
245 | App::abort(404); |
|||||||||||
246 | } |
|||||||||||
247 | } elseif (\Request::method() == 'POST') { |
|||||||||||
248 | $this->fromInput(); |
|||||||||||
249 | if ($this->validate()) { |
|||||||||||
250 | $this->writeSource(); |
|||||||||||
251 | $this->save(); |
|||||||||||
252 | ||||||||||||
253 | // we need to update the ID and timestamps |
|||||||||||
254 | $this->fromSource(); |
|||||||||||
255 | } else { |
|||||||||||
256 | // TODO: move this to the middleware |
|||||||||||
257 | // HTTP_UNPROCESSABLE_ENTITY |
|||||||||||
258 | App::abort(422, $this->errors()); |
|||||||||||
259 | } |
|||||||||||
260 | } elseif (\Request::method() == 'DELETE') { |
|||||||||||
261 | $this->source->unwrap()->delete(); |
|||||||||||
262 | } |
|||||||||||
263 | } |
|||||||||||
264 | ||||||||||||
265 | protected function load() |
|||||||||||
266 | { |
|||||||||||
267 | $key = $this->getKey(); |
|||||||||||
268 | if ($key) { |
|||||||||||
269 | $instance = $this->source->unwrap()->find($key) or App::abort(404); |
|||||||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Using logical operators such as
or instead of || is generally not recommended.
PHP has two types of connecting operators (logical operators, and boolean operators):
The difference between these is the order in which they are executed. In most cases,
you would want to use a boolean operator like Let’s take a look at a few examples: // Logical operators have lower precedence:
$f = false or true;
// is executed like this:
($f = false) or true;
// Boolean operators have higher precedence:
$f = false || true;
// is executed like this:
$f = (false || true);
Logical Operators are used for Control-FlowOne case where you explicitly want to use logical operators is for control-flow such as this: $x === 5
or die('$x must be 5.');
// Instead of
if ($x !== 5) {
die('$x must be 5.');
}
Since // The following is currently a parse error.
$x === 5
or throw new RuntimeException('$x must be 5.');
These limitations lead to logical operators rarely being of use in current PHP code. ![]() |
||||||||||||
270 | $this->initSource($instance); |
|||||||||||
271 | } |
|||||||||||
272 | } |
|||||||||||
273 | } |
|||||||||||
274 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: