Conditions | 18 |
Paths | 10 |
Total Lines | 44 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
21 | protected function getFieldData($data, $fieldName) |
||
22 | { |
||
23 | if (false !== strpos($fieldName, '.')) { |
||
24 | [$field, $subfield] = explode('.', $fieldName, 2); |
||
25 | |||
26 | return $this->getFieldData($this->getFieldData($data, $field), $subfield); |
||
27 | } |
||
28 | $camelCase = str_replace('_', '', ucwords($fieldName, '_')); |
||
29 | $getter = 'get'.$camelCase; |
||
30 | $isser = 'is'.$camelCase; |
||
31 | $hasser = 'has'.$camelCase; |
||
32 | $camelCaseAccessor = lcfirst($camelCase); |
||
33 | if (is_array($data) && array_key_exists($fieldName, $data)) { |
||
34 | return $data[$fieldName]; |
||
35 | } |
||
36 | |||
37 | if (is_object($data) && method_exists($data, $fieldName)) { |
||
38 | return $data->$fieldName(); |
||
39 | } |
||
40 | if (is_object($data) && method_exists($data, $camelCaseAccessor)) { |
||
41 | return $data->$camelCaseAccessor(); |
||
42 | } |
||
43 | |||
44 | if (is_object($data) && method_exists($data, $getter)) { |
||
45 | return $data->$getter(); |
||
46 | } |
||
47 | |||
48 | if (is_object($data) && method_exists($data, $isser)) { |
||
49 | return $data->$isser(); |
||
50 | } |
||
51 | |||
52 | if (is_object($data) && method_exists($data, $hasser)) { |
||
53 | return $data->$hasser(); |
||
54 | } |
||
55 | |||
56 | if (is_object($data) && property_exists($data, $camelCaseAccessor)) { |
||
57 | return $data->$camelCaseAccessor; |
||
58 | } |
||
59 | |||
60 | if (is_object($data) && property_exists($data, $fieldName)) { |
||
61 | return $data->$fieldName; |
||
62 | } |
||
63 | |||
64 | throw new InvalidFieldException('The field '.$fieldName.' is invalid'); |
||
65 | } |
||
67 |