| Conditions | 10 |
| Paths | 35 |
| Total Lines | 40 |
| Code Lines | 29 |
| 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 |
||
| 37 | public function loadUserByUsername($username) |
||
| 38 | { |
||
| 39 | $providerAttributes = $this->attributesProvider->getAttributesByUsername($username); |
||
| 40 | if (empty($providerAttributes)) { |
||
| 41 | throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username)); |
||
| 42 | } |
||
| 43 | |||
| 44 | $attributeDefinitions = $this->attributeDefinitionsProvider->getAttributeDefinitions(); |
||
| 45 | $attributes = []; |
||
| 46 | foreach ($attributeDefinitions as $idOrAlias => $attributeDefinition) { |
||
| 47 | switch (true) { |
||
| 48 | case isset($providerAttributes[$idOrAlias]): |
||
| 49 | $value = $providerAttributes[$idOrAlias]; |
||
| 50 | break; |
||
| 51 | case isset($providerAttributes[strtolower($idOrAlias)]): |
||
| 52 | $value = $providerAttributes[strtolower($idOrAlias)]; |
||
| 53 | break; |
||
| 54 | default: |
||
| 55 | continue 2; // switch is considered a looping structure, we have to continue the foreach |
||
| 56 | } |
||
| 57 | $charset = isset($attributeDefinition['charset']) ? $attributeDefinition['charset'] : 'UTF-8'; |
||
| 58 | if ($charset == 'UTF-8') { |
||
| 59 | $value = utf8_decode($value); |
||
| 60 | } |
||
| 61 | if (isset($attributeDefinition['multivalue']) && $attributeDefinition['multivalue']) { |
||
| 62 | $value = explode(';', $value); // $value is an array |
||
| 63 | } |
||
| 64 | $id = $attributeDefinition['id']; |
||
| 65 | $aliases = $attributeDefinition['aliases']; |
||
| 66 | $attributes[$id] = $value; |
||
| 67 | foreach ($aliases as $alias) { |
||
| 68 | $attributes[$alias] = $value; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | return new KuleuvenUser( |
||
| 73 | $username, |
||
| 74 | $attributes |
||
| 75 | ); |
||
| 76 | } |
||
| 77 | |||
| 105 |