Conditions | 12 |
Paths | 9 |
Total Lines | 33 |
Code Lines | 21 |
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 |
||
67 | public function getUserInfo(UserAccount $userAccount, array $claims, array $claimLocales): array |
||
68 | { |
||
69 | $result = []; |
||
70 | $claimLocale[] = null; |
||
|
|||
71 | foreach ($claims as $claimName => $config) { |
||
72 | if ($this->has($claimName)) { |
||
73 | $claim = $this->get($claimName); |
||
74 | foreach ($claimLocales as $claimLocale) { |
||
75 | if ($claim->isAvailableForUserAccount($userAccount, $claimLocale)) { |
||
76 | $value = $claim->getForUserAccount($userAccount, $claimLocale); |
||
77 | switch (true) { |
||
78 | case is_array($config) && array_key_exists('value', $config): |
||
79 | if ($claim === $config['value']) { |
||
80 | $result[$claimName] = $value; |
||
81 | } |
||
82 | |||
83 | break; |
||
84 | case is_array($config) && array_key_exists('values', $config) && is_array($config['values']): |
||
85 | if (in_array($claim, $config['values'])) { |
||
86 | $result[$claimName] = $value; |
||
87 | } |
||
88 | |||
89 | break; |
||
90 | default: |
||
91 | $result[$claimName] = $value; |
||
92 | } |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 | |||
98 | return $result; |
||
99 | } |
||
100 | } |
||
101 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.