Complex classes like MetadataHelper 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 MetadataHelper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class MetadataHelper |
||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | private $defaultLocale; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var FieldMap |
||
| 19 | */ |
||
| 20 | private $fieldsMap; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $fallbackLocale; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param FieldMap $fieldsMap |
||
| 29 | * @param string $defaultLocale |
||
| 30 | * @param string $fallbackLocale |
||
| 31 | */ |
||
| 32 | 11 | public function __construct(FieldMap $fieldsMap, $defaultLocale, $fallbackLocale) |
|
| 38 | |||
| 39 | public function getFieldName($alias, $locale = null) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param $fieldName |
||
| 56 | * @param null $locale |
||
| 57 | * @param bool $fallback Whether the lookup should fallback to the default locale |
||
| 58 | * @return string |
||
| 59 | */ |
||
| 60 | public function getFieldAlias($fieldName, $locale = null, $fallback = true) |
||
| 76 | |||
| 77 | 3 | public function getStoryField(Story $story, $field, $locale = null) |
|
| 106 | |||
| 107 | 3 | public function getStoryFields(Story $story, array $fields = null, $locale = null) |
|
| 108 | { |
||
| 109 | if ($locale == null) { |
||
| 110 | 3 | $locale = $this->defaultLocale; |
|
| 111 | } |
||
| 112 | |||
| 113 | $map = []; |
||
| 114 | 3 | ||
| 115 | foreach ($story->getMetadata() as $metadata) { |
||
| 116 | 3 | if (!$this->fieldsMap->isFieldMapped($metadata->getName(), $locale)) { |
|
| 117 | 3 | continue; |
|
| 118 | 3 | } |
|
| 119 | |||
| 120 | $alias = $this->fieldsMap->getAliasFromFieldName($metadata->getName(), $locale); |
||
| 121 | 3 | ||
| 122 | if ($fields !== null && !in_array($alias, $fields)) { |
||
| 123 | 3 | continue; |
|
| 124 | 2 | } |
|
| 125 | |||
| 126 | $map = $this->appendValueToMap($map, $alias, $metadata->getValue()); |
||
| 127 | 3 | } |
|
| 128 | 3 | ||
| 129 | return $map; |
||
| 130 | 3 | } |
|
| 131 | |||
| 132 | public function getRecordFields(Record $record, array $fields = null, $locale = null) |
||
| 133 | { |
||
| 134 | // @todo Clean up code |
||
| 135 | if ($locale == null) { |
||
| 136 | $locale = $this->defaultLocale; |
||
| 137 | } |
||
| 138 | |||
| 139 | 3 | $map = []; |
|
| 140 | |||
| 141 | 3 | foreach ($record->getMetadata() as $metadata) { |
|
| 142 | 1 | if (!$this->fieldsMap->isFieldMapped($metadata->getName(), $locale)) { |
|
| 143 | 1 | continue; |
|
| 144 | 1 | } |
|
| 145 | |||
| 146 | 1 | $alias = $this->fieldsMap->getAliasFromFieldName($metadata->getName(), $locale); |
|
| 147 | 1 | ||
| 148 | 3 | if ($fields !== null && !in_array($alias, $fields)) { |
|
| 149 | continue; |
||
| 150 | } |
||
| 151 | 3 | ||
| 152 | $map = $this->appendValueToMap($map, $alias, $metadata->getValue()); |
||
| 153 | } |
||
| 154 | 2 | ||
| 155 | return $map; |
||
| 156 | 2 | } |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param array $map |
||
| 160 | 2 | * @param string $alias |
|
| 161 | 1 | * @param string $value |
|
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | 2 | private function appendValueToMap($map, $alias, $value) |
|
| 178 | 2 | ||
| 179 | 1 | public function getRecordField(Record $record, $field, $locale = null) |
|
| 180 | { |
||
| 200 | |||
| 201 | public function getRecordMultiField(Record $record, $field, $locale = null) |
||
| 219 | } |
||
| 220 |