Conditions | 24 |
Paths | > 20000 |
Total Lines | 193 |
Code Lines | 134 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
203 | protected function displayCurrentSelection($details_id, $field_parents, $entities) { |
||
204 | $field_type = $this->fieldDefinition->getType(); |
||
205 | $field_settings = $this->fieldDefinition->getSettings(); |
||
206 | $field_machine_name = $this->fieldDefinition->getName(); |
||
207 | $file_settings = $this->configFactory->get('file.settings'); |
||
208 | $widget_settings = $this->getSettings(); |
||
209 | $view_mode = $widget_settings['view_mode']; |
||
210 | $can_edit = (bool) $widget_settings['field_widget_edit']; |
||
211 | $has_file_entity = $this->moduleHandler->moduleExists('file_entity'); |
||
212 | |||
213 | $delta = 0; |
||
214 | |||
215 | $order_class = $field_machine_name . '-delta-order'; |
||
216 | |||
217 | $current = [ |
||
218 | '#type' => 'table', |
||
219 | '#empty' => $this->t('No files yet'), |
||
220 | '#attributes' => ['class' => ['entities-list']], |
||
221 | '#tabledrag' => [ |
||
222 | [ |
||
223 | 'action' => 'order', |
||
224 | 'relationship' => 'sibling', |
||
225 | 'group' => $order_class, |
||
226 | ], |
||
227 | ], |
||
228 | ]; |
||
229 | |||
230 | if ($has_file_entity || $field_type == 'image' && !empty($widget_settings['preview_image_style'])) { |
||
231 | // Had the preview column if we have one. |
||
232 | $current['#header'][] = $this->t('Preview'); |
||
233 | } |
||
234 | |||
235 | // Add the filename if there is no view builder. |
||
236 | if (!$has_file_entity) { |
||
237 | $current['#header'][] = $this->t('Filename'); |
||
238 | } |
||
239 | |||
240 | // Add the remaining columns. |
||
241 | $current['#header'][] = $this->t('Metadata'); |
||
242 | $current['#header'][] = ['data' => $this->t('Operations'), 'colspan' => 2]; |
||
243 | $current['#header'][] = $this->t('Order', [], ['context' => 'Sort order']); |
||
244 | |||
245 | /** @var \Drupal\file\FileInterface[] $entities */ |
||
246 | foreach ($entities as $entity) { |
||
247 | // Check to see if this entity has an edit form. If not, the edit button |
||
248 | // will only throw an exception. |
||
249 | if (!$entity->getEntityType()->getFormClass('edit')) { |
||
250 | $edit_button_access = FALSE; |
||
251 | } |
||
252 | elseif ($has_file_entity) { |
||
253 | $edit_button_access = $can_edit && $entity->access('update', $this->currentUser); |
||
254 | } |
||
255 | |||
256 | $entity_id = $entity->id(); |
||
257 | |||
258 | // Find the default description. |
||
259 | $description = ''; |
||
260 | $display_field = $field_settings['display_default']; |
||
261 | $alt = ''; |
||
262 | $title = ''; |
||
263 | $weight = $delta; |
||
264 | $width = NULL; |
||
265 | $height = NULL; |
||
266 | foreach ($this->items as $item) { |
||
267 | if ($item->target_id == $entity_id) { |
||
268 | if ($field_type == 'file') { |
||
269 | $description = $item->description; |
||
270 | $display_field = $item->display; |
||
271 | } |
||
272 | elseif ($field_type == 'image') { |
||
273 | $alt = $item->alt; |
||
274 | $title = $item->title; |
||
275 | $width = $item->width; |
||
276 | $height = $item->height; |
||
277 | } |
||
278 | $weight = $item->_weight ?: $delta; |
||
279 | } |
||
280 | } |
||
281 | |||
282 | $current[$entity_id] = [ |
||
283 | '#attributes' => [ |
||
284 | 'class' => ['draggable'], |
||
285 | 'data-entity-id' => $entity->getEntityTypeId() . ':' . $entity_id, |
||
286 | 'data-row-id' => $delta, |
||
287 | ], |
||
288 | ]; |
||
289 | |||
290 | // Provide a rendered entity if a view builder is available. |
||
291 | if ($has_file_entity) { |
||
292 | $current[$entity_id]['display'] = $this->entityTypeManager->getViewBuilder('file')->view($entity, $view_mode); |
||
293 | } |
||
294 | // For images, support a preview image style as an alternative. |
||
295 | elseif ($field_type == 'image' && !empty($widget_settings['preview_image_style'])) { |
||
296 | $uri = $entity->getFileUri(); |
||
297 | $current[$entity_id]['display'] = [ |
||
298 | '#weight' => -10, |
||
299 | '#theme' => 'image_style', |
||
300 | '#width' => $width, |
||
301 | '#height' => $height, |
||
302 | '#style_name' => $widget_settings['preview_image_style'], |
||
303 | '#uri' => $uri, |
||
304 | ]; |
||
305 | } |
||
306 | // Assume that the file name is part of the preview output if |
||
307 | // file entity is installed, do not show this column in that case. |
||
308 | if (!$has_file_entity) { |
||
309 | $current[$entity_id]['filename'] = ['#markup' => $entity->label()]; |
||
310 | } |
||
311 | $current[$entity_id] += [ |
||
312 | 'meta' => [ |
||
313 | 'display_field' => [ |
||
314 | '#type' => 'checkbox', |
||
315 | '#title' => $this->t('Include file in display'), |
||
316 | '#default_value' => (bool) $display_field, |
||
317 | '#access' => $field_type == 'file' && $field_settings['display_field'], |
||
318 | ], |
||
319 | 'description' => [ |
||
320 | '#type' => $file_settings->get('description.type'), |
||
321 | '#title' => $this->t('Description'), |
||
322 | '#default_value' => $description, |
||
323 | '#size' => 45, |
||
324 | '#maxlength' => $file_settings->get('description.length'), |
||
325 | '#description' => $this->t('The description may be used as the label of the link to the file.'), |
||
326 | '#access' => $field_type == 'file' && $field_settings['description_field'], |
||
327 | ], |
||
328 | 'alt' => [ |
||
329 | '#type' => 'textfield', |
||
330 | '#title' => $this->t('Alternative text'), |
||
331 | '#default_value' => $alt, |
||
332 | '#size' => 45, |
||
333 | '#maxlength' => 512, |
||
334 | '#description' => $this->t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'), |
||
335 | '#access' => $field_type == 'image' && $field_settings['alt_field'], |
||
336 | '#required' => $field_type == 'image' && $field_settings['alt_field_required'], |
||
337 | ], |
||
338 | 'title' => [ |
||
339 | '#type' => 'textfield', |
||
340 | '#title' => $this->t('Title'), |
||
341 | '#default_value' => $title, |
||
342 | '#size' => 45, |
||
343 | '#maxlength' => 1024, |
||
344 | '#description' => $this->t('The title is used as a tool tip when the user hovers the mouse over the image.'), |
||
345 | '#access' => $field_type == 'image' && $field_settings['title_field'], |
||
346 | '#required' => $field_type == 'image' && $field_settings['title_field_required'], |
||
347 | ], |
||
348 | ], |
||
349 | 'edit_button' => [ |
||
350 | '#type' => 'submit', |
||
351 | '#value' => $this->t('Edit'), |
||
352 | '#ajax' => [ |
||
353 | 'url' => Url::fromRoute('entity_browser.edit_form', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity_id]), |
||
354 | 'options' => ['query' => ['details_id' => $details_id]], |
||
355 | ], |
||
356 | '#attributes' => [ |
||
357 | 'data-entity-id' => $entity->getEntityTypeId() . ':' . $entity->id(), |
||
358 | 'data-row-id' => $delta, |
||
359 | ], |
||
360 | '#access' => $edit_button_access, |
||
|
|||
361 | ], |
||
362 | 'remove_button' => [ |
||
363 | '#type' => 'submit', |
||
364 | '#value' => $this->t('Remove'), |
||
365 | '#ajax' => [ |
||
366 | 'callback' => [get_class($this), 'updateWidgetCallback'], |
||
367 | 'wrapper' => $details_id, |
||
368 | ], |
||
369 | '#submit' => [[get_class($this), 'removeItemSubmit']], |
||
370 | '#name' => $field_machine_name . '_remove_' . $entity_id . '_' . md5(json_encode($field_parents)), |
||
371 | '#limit_validation_errors' => [array_merge($field_parents, [$field_machine_name, 'target_id'])], |
||
372 | '#attributes' => [ |
||
373 | 'data-entity-id' => $entity->getEntityTypeId() . ':' . $entity->id(), |
||
374 | 'data-row-id' => $delta, |
||
375 | ], |
||
376 | '#access' => (bool) $widget_settings['field_widget_remove'], |
||
377 | ], |
||
378 | '_weight' => [ |
||
379 | '#type' => 'weight', |
||
380 | '#title' => $this->t('Weight for row @number', ['@number' => $delta + 1]), |
||
381 | '#title_display' => 'invisible', |
||
382 | // Note: this 'delta' is the FAPI #type 'weight' element's property. |
||
383 | '#delta' => count($entities), |
||
384 | '#default_value' => $weight, |
||
385 | '#attributes' => ['class' => [$order_class]], |
||
386 | ], |
||
387 | ]; |
||
388 | |||
389 | $current['#attached']['library'][] = 'entity_browser/file_browser'; |
||
390 | |||
391 | $delta++; |
||
392 | } |
||
393 | |||
394 | return $current; |
||
395 | } |
||
396 | |||
500 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: