| Conditions | 9 |
| Paths | 17 |
| Total Lines | 54 |
| Code Lines | 36 |
| 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 |
||
| 64 | public function map(array $data): array |
||
| 65 | { |
||
| 66 | $attrs = []; |
||
| 67 | |||
| 68 | foreach ($this->map as $attr => $value) { |
||
| 69 | if (array_key_exists($value['attr'], $data)) { |
||
| 70 | $this->logger->info('found attribut mapping ['.$attr.'] => [('.$value['type'].') '.$value['attr'].']', [ |
||
| 71 | 'category' => get_class($this), |
||
| 72 | ]); |
||
| 73 | |||
| 74 | if ($value['type'] == 'array') { |
||
| 75 | $store = $data[$value['attr']]; |
||
| 76 | } else { |
||
| 77 | if (is_array($data[$value['attr']])) { |
||
| 78 | $store = $data[$value['attr']][0]; |
||
| 79 | } else { |
||
| 80 | $store = $data[$value['attr']]; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | switch ($value['type']) { |
||
| 85 | case 'array': |
||
| 86 | $arr = (array)$data[$value['attr']]; |
||
| 87 | unset($arr['count']); |
||
| 88 | $attrs[$attr] = $arr; |
||
| 89 | break; |
||
| 90 | |||
| 91 | case 'string': |
||
| 92 | $attrs[$attr] = (string)$store; |
||
| 93 | break; |
||
| 94 | |||
| 95 | case 'int': |
||
| 96 | $attrs[$attr] = (int)$store; |
||
| 97 | break; |
||
| 98 | |||
| 99 | case 'bool': |
||
| 100 | $attrs[$attr] = (bool)$store; |
||
| 101 | break; |
||
| 102 | |||
| 103 | default: |
||
| 104 | $this->logger->error('unknown attribute type ['.$value['type'].'] for attribute ['.$attr.']; use one of [array,string,int,bool]', [ |
||
| 105 | 'category' => get_class($this), |
||
| 106 | ]); |
||
| 107 | break; |
||
| 108 | } |
||
| 109 | } else { |
||
| 110 | $this->logger->warning('auth attribute ['.$value['attr'].'] was not found from authentication adapter response', [ |
||
| 111 | 'category' => get_class($this), |
||
| 112 | ]); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | return $attrs; |
||
| 117 | } |
||
| 118 | } |
||
| 119 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.