| Conditions | 11 |
| Paths | 20 |
| Total Lines | 41 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 63 | public function extract($entity, $fieldOrFields = null) { |
||
| 64 | $filterFields = null; |
||
| 65 | $data = []; |
||
| 66 | /** @var \ReflectionProperty[] $classProperties */ |
||
| 67 | $classProperties = $this->reflectionPropertiesGetter->getProperties(get_class($entity)); |
||
| 68 | |||
| 69 | if (!is_array($fieldOrFields) && is_string($fieldOrFields)) { |
||
| 70 | $filterFields = [$fieldOrFields]; |
||
| 71 | } |
||
| 72 | |||
| 73 | foreach ($classProperties as $prop) { |
||
| 74 | if (is_array($filterFields)) { |
||
| 75 | $filtered = in_array($prop->name, $filterFields); |
||
| 76 | $filtered |= in_array(self::decamelizeString($prop->name), $filterFields); |
||
| 77 | |||
| 78 | if (!$filtered) { |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | $prop->setAccessible(true); |
||
| 84 | $value = $prop->getValue($entity); |
||
| 85 | |||
| 86 | $decamelName = self::decamelizeString($prop->name); |
||
| 87 | |||
| 88 | if ($decamelName == $fieldOrFields |
||
| 89 | || $prop->name == $fieldOrFields |
||
| 90 | || $prop->name == self::camelizeString($fieldOrFields) |
||
| 91 | ) { |
||
| 92 | return $value; |
||
| 93 | } |
||
| 94 | |||
| 95 | $data[$decamelName] = $value; |
||
| 96 | } |
||
| 97 | |||
| 98 | if (empty($data) && is_string($fieldOrFields)) { |
||
| 99 | return null; |
||
| 100 | } |
||
| 101 | |||
| 102 | return $data; |
||
| 103 | } |
||
| 104 | |||
| 133 |