| Conditions | 14 |
| Paths | 14 |
| Total Lines | 119 |
| Code Lines | 74 |
| Lines | 8 |
| Ratio | 6.72 % |
| Changes | 16 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 202 | function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { |
||
| 203 | $entity_type = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type'); |
||
| 204 | $entity_storage = $this->entityManager->getStorage($entity_type); |
||
| 205 | $field_widget_display = $this->fieldDisplayManager->createInstance( |
||
| 206 | $this->getSetting('field_widget_display'), |
||
| 207 | $this->getSetting('field_widget_display_settings') + ['entity_type' => $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type')] |
||
| 208 | ); |
||
| 209 | |||
| 210 | $ids = []; |
||
| 211 | if (($trigger = $form_state->getTriggeringElement()) && in_array($this->fieldDefinition->getName(), $trigger['#parents'])) { |
||
| 212 | // Submit was triggered by hidden "target_id" element when entities were |
||
| 213 | // added via entity browser. |
||
| 214 | View Code Duplication | if (!empty($trigger['#ajax']['event']) && $trigger['#ajax']['event'] == 'entity_browser_value_updated') { |
|
| 215 | $parents = $trigger['#parents']; |
||
| 216 | } |
||
| 217 | // Submit was triggered by one of the "Remove" buttons. We need to walk |
||
| 218 | // few levels up to read value of "target_id" element. |
||
| 219 | elseif ($trigger['#type'] == 'submit' && strpos($trigger['#name'], $this->fieldDefinition->getName() . '_remove_') === 0) { |
||
| 220 | $parents = array_merge(array_slice($trigger['#parents'], 0, -4), ['target_id']); |
||
| 221 | } |
||
| 222 | |||
| 223 | if (isset($parents) && $value = $form_state->getValue($parents)) { |
||
| 224 | $ids = explode(' ', $value); |
||
| 225 | $entities = $entity_storage->loadMultiple($ids); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | // We are loading for for the first time so we need to load any existing |
||
| 229 | // values that might already exist on the entity. Also, remove any leftover |
||
| 230 | // data from removed entity references. |
||
| 231 | else { |
||
| 232 | foreach ($items as $item) { |
||
| 233 | $entity = $entity_storage->load($item->target_id); |
||
| 234 | if (!empty($entity)) { |
||
| 235 | $entities[$item->target_id] = $entity; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | $ids = array_keys($entities); |
||
| 239 | } |
||
| 240 | $ids = array_filter($ids); |
||
| 241 | |||
| 242 | $hidden_id = Html::getUniqueId('edit-' . $this->fieldDefinition->getName() . '-target-id'); |
||
| 243 | $details_id = Html::getUniqueId('edit-' . $this->fieldDefinition->getName()); |
||
| 244 | /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */ |
||
| 245 | $entity_browser = $this->entityManager->getStorage('entity_browser')->load($this->getSetting('entity_browser')); |
||
| 246 | |||
| 247 | $element += [ |
||
| 248 | '#id' => $details_id, |
||
| 249 | '#type' => 'details', |
||
| 250 | '#open' => !empty($ids), |
||
| 251 | 'target_id' => [ |
||
| 252 | '#type' => 'hidden', |
||
| 253 | '#id' => $hidden_id, |
||
| 254 | // We need to repeat ID here as it is otherwise skipped when rendering. |
||
| 255 | '#attributes' => ['id' => $hidden_id], |
||
| 256 | '#default_value' => $ids, |
||
| 257 | // #ajax is officially not supported for hidden elements but if we |
||
| 258 | // specify event manually it works. |
||
| 259 | '#ajax' => [ |
||
| 260 | 'callback' => [get_class($this), 'updateWidgetCallback'], |
||
| 261 | 'wrapper' => $details_id, |
||
| 262 | 'event' => 'entity_browser_value_updated', |
||
| 263 | ], |
||
| 264 | ], |
||
| 265 | ]; |
||
| 266 | |||
| 267 | $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); |
||
| 268 | if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || count($ids) < $cardinality) { |
||
| 269 | $element['entity_browser'] = $entity_browser->getDisplay()->displayEntityBrowser(); |
||
| 270 | $element['#attached']['library'][] = 'entity_browser/entity_reference'; |
||
| 271 | $element['#attached']['drupalSettings']['entity_browser'] = [ |
||
| 272 | $entity_browser->getDisplay()->getUuid() => [ |
||
| 273 | 'cardinality' => $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(), |
||
| 274 | 'selector' => '#'.$element['target_id']['#attributes']['id'], |
||
| 275 | ] |
||
| 276 | ]; |
||
| 277 | } |
||
| 278 | |||
| 279 | $field_parents = $element['#field_parents']; |
||
| 280 | |||
| 281 | $element['current'] = [ |
||
| 282 | '#theme_wrappers' => ['container'], |
||
| 283 | '#attributes' => ['class' => ['entities-list']], |
||
| 284 | 'items' => array_map( |
||
| 285 | function($id) use ($entity_storage, $field_widget_display, $details_id, $field_parents, $entities) { |
||
| 286 | $entity = $entities[$id]; |
||
| 287 | |||
| 288 | $display = $field_widget_display->view($entity); |
||
| 289 | if (is_string($display)) { |
||
| 290 | $display = ['#markup' => $display]; |
||
| 291 | } |
||
| 292 | |||
| 293 | return [ |
||
| 294 | '#theme_wrappers' => ['container'], |
||
| 295 | '#attributes' => [ |
||
| 296 | 'class' => ['item-container'], |
||
| 297 | 'data-entity-id' => $entity->id() |
||
| 298 | ], |
||
| 299 | 'display' => $display, |
||
| 300 | 'remove_button' => [ |
||
| 301 | '#type' => 'submit', |
||
| 302 | '#value' => $this->t('Remove'), |
||
| 303 | '#ajax' => [ |
||
| 304 | 'callback' => [get_class($this), 'updateWidgetCallback'], |
||
| 305 | 'wrapper' => $details_id, |
||
| 306 | ], |
||
| 307 | '#submit' => [[get_class($this), 'removeItemSubmit']], |
||
| 308 | '#name' => $this->fieldDefinition->getName() . '_remove_' . $id, |
||
| 309 | '#limit_validation_errors' => [array_merge($field_parents, [$this->fieldDefinition->getName()])], |
||
| 310 | '#attributes' => ['data-entity-id' => $id], |
||
| 311 | ] |
||
| 312 | ]; |
||
| 313 | |||
| 314 | }, |
||
| 315 | $ids |
||
| 316 | ), |
||
| 317 | ]; |
||
| 318 | |||
| 319 | return $element; |
||
| 320 | } |
||
| 321 | |||
| 382 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.