Conditions | 17 |
Paths | 3073 |
Total Lines | 166 |
Code Lines | 123 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
204 | protected function displayCurrentSelection($details_id, $field_parents, $entities) { |
||
205 | $field_type = $this->fieldDefinition->getType(); |
||
206 | $field_settings = $this->fieldDefinition->getSettings(); |
||
207 | $field_machine_name = $this->fieldDefinition->getName(); |
||
208 | $file_settings = $this->configFactory->get('file.settings'); |
||
209 | $widget_settings = $this->getSettings(); |
||
210 | $view_mode = $widget_settings['view_mode']; |
||
211 | $can_edit = (bool) $widget_settings['field_widget_edit']; |
||
212 | |||
213 | $delta = 0; |
||
214 | |||
215 | $order_class = $field_machine_name . '-delta-order'; |
||
216 | |||
217 | $current = [ |
||
218 | '#type' => 'table', |
||
219 | '#header' => [ |
||
220 | $this->t('Preview'), |
||
221 | $this->t('Filename'), |
||
222 | $this->t('Metadata'), |
||
223 | ['data' => $this->t('Operations'), 'colspan' => 2], |
||
224 | $this->t('Order', [], ['context' => 'Sort order']), |
||
225 | ], |
||
226 | '#empty' => $this->t('No files yet'), |
||
227 | '#attributes' => ['class' => ['entities-list']], |
||
228 | '#tabledrag' => [ |
||
229 | [ |
||
230 | 'action' => 'order', |
||
231 | 'relationship' => 'sibling', |
||
232 | 'group' => $order_class, |
||
233 | ], |
||
234 | ], |
||
235 | ]; |
||
236 | /** @var \Drupal\file\FileInterface[] $entities */ |
||
237 | foreach ($entities as $entity) { |
||
238 | // Check to see if this entity has an edit form. If not, the edit button |
||
239 | // will only throw an exception. |
||
240 | if (!$entity->getEntityType()->getFormClass('edit')) { |
||
241 | $can_edit = FALSE; |
||
242 | } |
||
243 | |||
244 | $entity_id = $entity->id(); |
||
245 | $uri = $entity->getFileUri(); |
||
246 | if ($field_type == 'image' && $view_mode == 'default') { |
||
247 | $image = $this->imageFactory->get($uri); |
||
248 | if ($image->isValid()) { |
||
249 | $width = $image->getWidth(); |
||
250 | $height = $image->getHeight(); |
||
251 | } |
||
252 | else { |
||
253 | $width = $height = NULL; |
||
254 | } |
||
255 | |||
256 | $display = [ |
||
257 | '#weight' => -10, |
||
258 | '#theme' => 'image_style', |
||
259 | '#width' => $width, |
||
260 | '#height' => $height, |
||
261 | '#style_name' => $widget_settings['preview_image_style'], |
||
262 | '#uri' => $uri, |
||
263 | ]; |
||
264 | } |
||
265 | else { |
||
266 | $display = $this->entityTypeManager->getViewBuilder('file')->view($entity, $view_mode); |
||
267 | } |
||
268 | |||
269 | // Find the default description. |
||
270 | $description = ''; |
||
271 | $display_field = $field_settings['display_default']; |
||
272 | $alt = ''; |
||
273 | $title = ''; |
||
274 | $weight = $delta; |
||
275 | foreach ($this->items as $item) { |
||
276 | if ($item->target_id == $entity_id) { |
||
277 | if ($field_type == 'file') { |
||
278 | $description = $item->description; |
||
279 | $display_field = $item->display; |
||
280 | } |
||
281 | elseif ($field_type == 'image') { |
||
282 | $alt = $item->alt; |
||
283 | $title = $item->title; |
||
284 | } |
||
285 | $weight = $item->_weight ?: $delta; |
||
286 | } |
||
287 | } |
||
288 | |||
289 | $current[$entity_id] = [ |
||
290 | '#attributes' => [ |
||
291 | 'class' => ['draggable'], |
||
292 | 'data-entity-id' => $entity->getEntityTypeId() . ':' . $entity_id, |
||
293 | ], |
||
294 | 'display' => $display, |
||
295 | 'filename' => ['#markup' => $entity->label()], |
||
296 | 'meta' => [ |
||
297 | 'display_field' => [ |
||
298 | '#type' => 'checkbox', |
||
299 | '#title' => $this->t('Include file in display'), |
||
300 | '#default_value' => (bool) $display_field, |
||
301 | '#access' => $field_type == 'file' && $field_settings['display_field'], |
||
302 | ], |
||
303 | 'description' => [ |
||
304 | '#type' => $file_settings->get('description.type'), |
||
305 | '#title' => $this->t('Description'), |
||
306 | '#default_value' => $description, |
||
307 | '#size' => 45, |
||
308 | '#maxlength' => $file_settings->get('description.length'), |
||
309 | '#description' => $this->t('The description may be used as the label of the link to the file.'), |
||
310 | '#access' => $field_type == 'file' && $field_settings['description_field'], |
||
311 | ], |
||
312 | 'alt' => [ |
||
313 | '#type' => 'textfield', |
||
314 | '#title' => $this->t('Alternative text'), |
||
315 | '#default_value' => $alt, |
||
316 | '#size' => 45, |
||
317 | '#maxlength' => 512, |
||
318 | '#description' => $this->t('This text will be used by screen readers, search engines, or when the image cannot be loaded.'), |
||
319 | '#access' => $field_type == 'image' && $field_settings['alt_field'], |
||
320 | '#required' => $field_type == 'image' && $field_settings['alt_field_required'], |
||
321 | ], |
||
322 | 'title' => [ |
||
323 | '#type' => 'textfield', |
||
324 | '#title' => $this->t('Title'), |
||
325 | '#default_value' => $title, |
||
326 | '#size' => 45, |
||
327 | '#maxlength' => 1024, |
||
328 | '#description' => $this->t('The title is used as a tool tip when the user hovers the mouse over the image.'), |
||
329 | '#access' => $field_type == 'image' && $field_settings['title_field'], |
||
330 | '#required' => $field_type == 'image' && $field_settings['title_field_required'], |
||
331 | ], |
||
332 | ], |
||
333 | 'edit_button' => [ |
||
334 | '#type' => 'submit', |
||
335 | '#value' => $this->t('Edit'), |
||
336 | '#ajax' => [ |
||
337 | 'url' => Url::fromRoute('entity_browser.edit_form', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity_id]), |
||
338 | ], |
||
339 | '#access' => $can_edit, |
||
340 | ], |
||
341 | 'remove_button' => [ |
||
342 | '#type' => 'submit', |
||
343 | '#value' => $this->t('Remove'), |
||
344 | '#ajax' => [ |
||
345 | 'callback' => [get_class($this), 'updateWidgetCallback'], |
||
346 | 'wrapper' => $details_id, |
||
347 | ], |
||
348 | '#submit' => [[get_class($this), 'removeItemSubmit']], |
||
349 | '#name' => $field_machine_name . '_remove_' . $entity_id, |
||
350 | '#limit_validation_errors' => [array_merge($field_parents, [$field_machine_name, 'target_id'])], |
||
351 | '#attributes' => ['data-entity-id' => $entity->getEntityTypeId() . ':' . $entity_id], |
||
352 | '#access' => (bool) $widget_settings['field_widget_remove'], |
||
353 | ], |
||
354 | '_weight' => [ |
||
355 | '#type' => 'weight', |
||
356 | '#title' => $this->t('Weight for row @number', ['@number' => $delta + 1]), |
||
357 | '#title_display' => 'invisible', |
||
358 | // Note: this 'delta' is the FAPI #type 'weight' element's property. |
||
359 | '#delta' => count($entities), |
||
360 | '#default_value' => $weight, |
||
361 | '#attributes' => ['class' => [$order_class]], |
||
362 | ], |
||
363 | ]; |
||
364 | |||
365 | $delta++; |
||
366 | } |
||
367 | |||
368 | return $current; |
||
369 | } |
||
370 | |||
470 |
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: