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 |
||
| 16 | class FieldValueManager implements FieldValueManagerInterface { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The storage plugin for the paragraph entity type. |
||
| 20 | * |
||
| 21 | * @var \Drupal\Core\Entity\EntityStorageInterface |
||
| 22 | */ |
||
| 23 | protected $storage; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The storage plugin for the paragraph type config entity type. |
||
| 27 | * |
||
| 28 | * @var \Drupal\Core\Entity\EntityStorageInterface |
||
| 29 | */ |
||
| 30 | protected $bundleStorage; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The field value manager service for collecting field information. |
||
| 34 | * |
||
| 35 | * @var \Drupal\Core\Entity\EntityFieldManagerInterface |
||
| 36 | */ |
||
| 37 | protected $entityFieldManager; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Element definitions for custom elements that can occur in an editor field. |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $elements; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * A static cache of paragraph revisions. |
||
| 48 | * |
||
| 49 | * @var \Drupal\paragraphs\ParagraphInterface[] |
||
| 50 | */ |
||
| 51 | protected $revisionCache = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Creates a field value manager object. |
||
| 55 | * |
||
| 56 | * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager |
||
| 57 | * The field manager service. |
||
| 58 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
||
| 59 | * The entity type manager service. |
||
| 60 | * @param array $elements |
||
| 61 | * An array of widget binder element definitions. |
||
| 62 | */ |
||
| 63 | public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_type_manager, array $elements) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * {@inheritdoc} |
||
| 72 | */ |
||
| 73 | public function getReferencedEntities(EntityReferenceRevisionsFieldItemList $items) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | public function wrapItems(EntityReferenceRevisionsFieldItemList $items) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * {@inheritdoc} |
||
| 139 | */ |
||
| 140 | public function prepareEntityForSave($entity, $new_revision, $langcode) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * {@inheritdoc} |
||
| 159 | */ |
||
| 160 | public function setItems(EntityReferenceRevisionsFieldItemList $items, array $entities, $new_revision = FALSE, $langcode = NULL) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | public function getTextBundles(array $allowed_bundles = []) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | public function isParagraphsField(FieldDefinitionInterface $field_definition) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * {@inheritdoc} |
||
| 223 | */ |
||
| 224 | public function isParagraphsEditorField(FieldDefinitionInterface $field_definition) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | public function getTextFields($bundle_name) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | public function getElement($element_name) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * {@inheritdoc} |
||
| 273 | */ |
||
| 274 | public function getAttributeName($element_name, $attribute_name) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | public function getSelector($element_name) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Helper function to check if a field is a text field. |
||
| 301 | * |
||
| 302 | * @param \Drupal\Core\Field\FieldDefinitionInterface $field_config |
||
| 303 | * The field to check. |
||
| 304 | * |
||
| 305 | * @return bool |
||
| 306 | * TRUE if it's a paragraphs editor approved text field, FALSE otherwise. |
||
| 307 | */ |
||
| 308 | protected function isTextField(FieldDefinitionInterface $field_config) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Enforces that an entity is a paragraph entity. |
||
| 314 | * |
||
| 315 | * @return \Drupal\paragraphs\ParagraphInterface|null |
||
| 316 | * The entity, or NULL if it was not a paragraph. |
||
| 317 | */ |
||
| 318 | protected function ensureParagraphEntity(EntityInterface $entity) { |
||
| 321 | |||
| 322 | } |
||
| 323 |
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: