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 | public function getStoryFields(Story $story, array $fields = null, $locale = null) |
||
| 131 | |||
| 132 | 3 | public function getRecordFields(Record $record, array $fields = null, $locale = null) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param array $map |
||
| 160 | * @param string $alias |
||
| 161 | * @param string $value |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | 3 | private function appendValueToMap($map, $alias, $value) |
|
| 178 | |||
| 179 | 2 | public function getRecordField(Record $record, $field, $locale = null) |
|
| 200 | |||
| 201 | 2 | public function getRecordMultiField(Record $record, $field, $locale = null) |
|
| 219 | } |
||
| 220 |