Complex classes like FieldValueManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FieldValueManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class FieldValueManager implements FieldValueManagerInterface { |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The storage plugin for the paragraph entity type. |
||
| 21 | * |
||
| 22 | * @var \Drupal\Core\Entity\EntityStorageInterface |
||
| 23 | */ |
||
| 24 | protected $storage; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The storage plugin for the paragraph type config entity type. |
||
| 28 | * |
||
| 29 | * @var \Drupal\Core\Entity\EntityStorageInterface |
||
| 30 | */ |
||
| 31 | protected $bundleStorage; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The field value manager service for collecting field information. |
||
| 35 | * |
||
| 36 | * @var \Drupal\Core\Entity\EntityFieldManagerInterface |
||
| 37 | */ |
||
| 38 | protected $entityFieldManager; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Element definitions for custom elements that can occur in an editor field. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $elements; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * A static cache of paragraph revisions. |
||
| 49 | * |
||
| 50 | * @var \Drupal\paragraphs\ParagraphInterface[] |
||
| 51 | */ |
||
| 52 | protected $revisionCache = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Creates a field value manager object. |
||
| 56 | * |
||
| 57 | * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager |
||
| 58 | * The field manager service. |
||
| 59 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
||
| 60 | * The entity type manager service. |
||
| 61 | * @param array $elements |
||
| 62 | * An array of widget binder element definitions. |
||
| 63 | */ |
||
| 64 | public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, array $elements) { |
||
| 65 | $this->entityFieldManager = $entity_field_manager; |
||
| 66 | $this->storage = $entity_type_manager->getStorage('paragraph'); |
||
| 67 | $this->bundleStorage = $entity_type_manager->getStorage('paragraphs_type'); |
||
| 68 | $this->elements = $elements; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | public function getReferencedEntities(EntityReferenceRevisionsFieldItemList $items) { |
||
| 75 | $entities = []; |
||
| 76 | foreach ($items as $item) { |
||
| 77 | $value = $item->getValue(); |
||
| 78 | if (!empty($value['entity']) && $value['entity'] instanceof ParagraphInterface) { |
||
| 79 | $entity = $item->entity; |
||
| 80 | } |
||
| 81 | elseif ($item->target_revision_id !== NULL) { |
||
| 82 | if (!empty($this->revisionCache[$item->target_revision_id])) { |
||
| 83 | $entity = $this->revisionCache[$item->target_revision_id]; |
||
| 84 | } |
||
| 85 | else { |
||
| 86 | $entity = $this->storage->loadRevision($item->target_revision_id); |
||
| 87 | $this->revisionCache[$item->target_revision_id] = $entity; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | $entities[] = $entity; |
||
|
|
|||
| 91 | } |
||
| 92 | return $entities; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | public function wrapItems(EntityReferenceRevisionsFieldItemList $items) { |
||
| 99 | $field_definition = $items->getFieldDefinition(); |
||
| 100 | if (!$this->isParagraphsEditorField($field_definition)) { |
||
| 101 | throw new \Exception('Attempt to wrap non-paragraphs editor field.'); |
||
| 102 | } |
||
| 103 | |||
| 104 | // Build a list of refrenced entities and filter out the text entities. |
||
| 105 | $settings = $field_definition->getThirdPartySettings('paragraphs_editor'); |
||
| 106 | $markup = ''; |
||
| 107 | $entities = []; |
||
| 108 | $text_entity = NULL; |
||
| 109 | |||
| 110 | foreach ($this->getReferencedEntities($items) as $entity) { |
||
| 111 | if ($entity->bundle() == $settings['text_bundle']) { |
||
| 112 | $markup .= $entity->{$settings['text_field']}->value; |
||
| 113 | if (!$text_entity) { |
||
| 114 | $text_entity = $entity; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | else { |
||
| 118 | $entities[$entity->uuid()] = $entity; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | // If there is no text entity we need to create one. |
||
| 123 | if (!$text_entity) { |
||
| 124 | $text_entity = $this->ensureParagraphEntity($this->storage->create([ |
||
| 125 | 'type' => $settings['text_bundle'], |
||
| 126 | ])); |
||
| 127 | } |
||
| 128 | |||
| 129 | // Reset the text entity markup in case we merged multiple text entities. |
||
| 130 | $text_entity->{$settings['text_field']}->value = $markup; |
||
| 131 | if (empty($text_entity->{$settings['text_field']}->format) && !empty($settings['filter_format'])) { |
||
| 132 | $text_entity->{$settings['text_field']}->format = $settings['filter_format']; |
||
| 133 | } |
||
| 134 | |||
| 135 | return new FieldValueWrapper($field_definition, $text_entity, $entities); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | public function prepareEntityForSave($entity, $new_revision, $langcode) { |
||
| 142 | $entity->setNewRevision($new_revision); |
||
| 143 | |||
| 144 | if (isset($langcode) && $entity->get('langcode') != $langcode) { |
||
| 145 | if ($entity->hasTranslation($langcode)) { |
||
| 146 | $entity = $entity->getTranslation($langcode); |
||
| 147 | } |
||
| 148 | else { |
||
| 149 | $entity->set('langcode', $langcode); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $entity->setNeedsSave(TRUE); |
||
| 154 | |||
| 155 | return $entity; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | public function setItems(EntityReferenceRevisionsFieldItemList $items, array $entities, $new_revision = FALSE, $langcode = NULL) { |
||
| 162 | $values = []; |
||
| 163 | $delta = 0; |
||
| 164 | foreach ($entities as $entity) { |
||
| 165 | $entity = $this->prepareEntityForSave($entity, $new_revision, $langcode); |
||
| 166 | $values[$delta]['entity'] = $entity; |
||
| 167 | $values[$delta]['target_id'] = $entity->id(); |
||
| 168 | $values[$delta]['target_revision_id'] = $entity->getRevisionId(); |
||
| 169 | $delta++; |
||
| 170 | } |
||
| 171 | |||
| 172 | $items->setValue($values); |
||
| 173 | $items->filterEmptyItems(); |
||
| 174 | return $items; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | public function getTextBundles(array $allowed_bundles = []) { |
||
| 181 | |||
| 182 | if (!empty($allowed_bundles)) { |
||
| 183 | $results = $this->bundleStorage->getQuery()->execute(); |
||
| 184 | if (is_array($results)) { |
||
| 185 | foreach ($results as $name) { |
||
| 186 | $allowed_bundles[$name] = [ |
||
| 187 | 'label' => $this->bundleStorage->load($name)->label(), |
||
| 188 | ]; |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | $bundles = []; |
||
| 194 | foreach ($allowed_bundles as $name => $type) { |
||
| 195 | $text_fields = $this->getTextFields($name); |
||
| 196 | if (count($text_fields) == 1) { |
||
| 197 | $bundles[$name] = [ |
||
| 198 | 'label' => $type['label'], |
||
| 199 | 'text_field' => reset($text_fields), |
||
| 200 | ]; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | return $bundles; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritdoc} |
||
| 208 | */ |
||
| 209 | public function isParagraphsField(FieldDefinitionInterface $field_definition) { |
||
| 210 | $field_definition = $this->ensureFieldConfig($field_definition); |
||
| 211 | |||
| 212 | if ($field_definition->getType() != 'entity_reference_revisions') { |
||
| 213 | return FALSE; |
||
| 214 | } |
||
| 215 | |||
| 216 | if ($field_definition->getFieldStorageDefinition()->getSetting('target_type') != 'paragraph') { |
||
| 217 | return FALSE; |
||
| 218 | } |
||
| 219 | |||
| 220 | return TRUE; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * {@inheritdoc} |
||
| 225 | */ |
||
| 226 | public function isParagraphsEditorField(FieldDefinitionInterface $field_definition) { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function getTextFields($bundle_name) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * {@inheritdoc} |
||
| 268 | */ |
||
| 269 | public function getElement($element_name) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function getAttributeName($element_name, $attribute_name) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * {@inheritdoc} |
||
| 290 | */ |
||
| 291 | public function getSelector($element_name) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Helper function to check if a field is a text field. |
||
| 303 | * |
||
| 304 | * @param \Drupal\Core\Field\FieldDefinitionInterface $field_config |
||
| 305 | * The field to check. |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | * TRUE if it's a paragraphs editor approved text field, FALSE otherwise. |
||
| 309 | */ |
||
| 310 | protected function isTextField(FieldDefinitionInterface $field_config) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Enforces that an entity is a paragraph entity. |
||
| 316 | * |
||
| 317 | * @return \Drupal\paragraphs\ParagraphInterface |
||
| 318 | * The entity, or NULL if it was not a paragraph. |
||
| 319 | */ |
||
| 320 | protected function ensureParagraphEntity(EntityInterface $entity) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Enforces that an entity is a field config entity. |
||
| 329 | * |
||
| 330 | * @return \Drupal\Core\Field\FieldConfigInterface |
||
| 331 | * The entity, or NULL if it was not a field config instance. |
||
| 332 | */ |
||
| 333 | protected function ensureFieldConfig(FieldDefinitionInterface $entity) { |
||
| 339 | |||
| 340 | } |
||
| 341 |
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: