Complex classes like FieldHelper 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 FieldHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class FieldHelper |
||
16 | { |
||
17 | const HAS_CONFIG = 'has_config'; |
||
18 | |||
19 | const IDENTITY_ONLY_WHEN_NOT_EMPTY = -1; |
||
20 | |||
21 | /** @var ConfigProvider */ |
||
22 | protected $configProvider; |
||
23 | |||
24 | /** @var EntityFieldProvider */ |
||
25 | protected $fieldProvider; |
||
26 | |||
27 | /** @var FieldTypeHelper */ |
||
28 | protected $fieldTypeHelper; |
||
29 | |||
30 | /** @var PropertyAccessor */ |
||
31 | protected $propertyAccessor; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $fieldsCache = []; |
||
35 | |||
36 | /** @var array */ |
||
37 | protected $relationsCache = []; |
||
38 | |||
39 | /** @var array */ |
||
40 | protected $fieldsConfigCache = []; |
||
41 | |||
42 | /** @var array */ |
||
43 | protected $identityFieldsCache = []; |
||
44 | |||
45 | /** |
||
46 | * @param EntityFieldProvider $fieldProvider |
||
47 | * @param ConfigProvider $configProvider |
||
48 | * @param FieldTypeHelper $fieldTypeHelper |
||
49 | */ |
||
50 | public function __construct( |
||
59 | |||
60 | /** |
||
61 | * @see \Oro\Bundle\EntityBundle\Provider\EntityFieldProvider::getFields |
||
62 | * |
||
63 | * @param string $entityName |
||
64 | * @param bool $withRelations |
||
65 | * @param bool $withVirtualFields |
||
66 | * @param bool $withEntityDetails |
||
67 | * @param bool $withUnidirectional |
||
68 | * @param bool $applyExclusions |
||
69 | * @param bool $translate |
||
70 | * @return array |
||
71 | */ |
||
72 | public function getFields( |
||
97 | |||
98 | /** |
||
99 | * @see \Oro\Bundle\EntityBundle\Provider\EntityFieldProvider::getRelations |
||
100 | * |
||
101 | * @param string $entityName |
||
102 | * @param bool $withEntityDetails |
||
103 | * @param bool $applyExclusions |
||
104 | * @param bool $translate |
||
105 | * @return array |
||
106 | */ |
||
107 | public function getRelations( |
||
126 | |||
127 | /** |
||
128 | * @param string $entityName |
||
129 | * @param string $fieldName |
||
130 | * @param string $parameter |
||
131 | * @param mixed $default |
||
132 | * @return mixed|null |
||
133 | */ |
||
134 | public function getConfigValue($entityName, $fieldName, $parameter, $default = null) |
||
157 | |||
158 | /** |
||
159 | * @param string $entityName |
||
160 | * @param string $fieldName |
||
161 | * @return string |
||
162 | */ |
||
163 | protected function getCacheKey($entityName, $fieldName) |
||
167 | |||
168 | /** |
||
169 | * @param string $className |
||
170 | * @param null|string $fieldName |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function hasConfig($className, $fieldName = null) |
||
182 | |||
183 | /** |
||
184 | * @param array $field |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function isRelation(array $field) |
||
191 | |||
192 | /** |
||
193 | * @param string $className |
||
194 | * @param string $fieldName |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function processRelationAsScalar($className, $fieldName) |
||
202 | |||
203 | /** |
||
204 | * @param array $field |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function isSingleRelation(array $field) |
||
216 | |||
217 | /** |
||
218 | * @param array $field |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function isMultipleRelation(array $field) |
||
230 | |||
231 | /** |
||
232 | * @param array $field |
||
233 | * @return bool |
||
234 | */ |
||
235 | public function isDateTimeField(array $field) |
||
239 | |||
240 | /** |
||
241 | * @param object $object |
||
242 | * @param string $fieldName |
||
243 | * @return mixed |
||
244 | * @throws \Exception |
||
245 | */ |
||
246 | public function getObjectValue($object, $fieldName) |
||
264 | |||
265 | /** |
||
266 | * @param object $object |
||
267 | * @param string $fieldName |
||
268 | * @param mixed $value |
||
269 | * @throws NoSuchPropertyException|\TypeError|\ErrorException |
||
270 | */ |
||
271 | public function setObjectValue($object, $fieldName, $value) |
||
283 | |||
284 | /** |
||
285 | * If Property accessor have type_error |
||
286 | * try added value by ReflectionProperty |
||
287 | * |
||
288 | * @param object $object |
||
289 | * @param string $fieldName |
||
290 | * @param mixed $value |
||
291 | * @param NoSuchPropertyException|\TypeError|\ErrorException $exception |
||
292 | * @throws NoSuchPropertyException|\TypeError|\ErrorException |
||
293 | */ |
||
294 | protected function setObjectValueWithReflection($object, $fieldName, $value, $exception) |
||
308 | |||
309 | /** |
||
310 | * @param mixed $data |
||
311 | * @param string $fieldName |
||
312 | * @return array |
||
313 | */ |
||
314 | public function getItemData($data, $fieldName = null) |
||
326 | |||
327 | /** |
||
328 | * @param object $entity |
||
329 | * @return array |
||
330 | */ |
||
331 | public function getIdentityValues($entity) |
||
338 | |||
339 | /** |
||
340 | * Checks if a field should be used as an identity even if it has empty value |
||
341 | * |
||
342 | * @param string $entityName |
||
343 | * @param string $fieldName |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | public function isRequiredIdentityField($entityName, $fieldName) |
||
353 | |||
354 | /** |
||
355 | * @param string $entityName |
||
356 | * @return string[] |
||
357 | */ |
||
358 | public function getIdentityFieldNames($entityName) |
||
376 | |||
377 | /** |
||
378 | * @param object $entity |
||
379 | * @param array $fieldNames |
||
380 | * @return array |
||
381 | */ |
||
382 | public function getFieldsValues($entity, $fieldNames) |
||
391 | |||
392 | /** |
||
393 | * @return PropertyAccessor |
||
394 | */ |
||
395 | protected function getPropertyAccessor() |
||
403 | } |
||
404 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.