Conditions | 10 |
Paths | 20 |
Total Lines | 31 |
Code Lines | 18 |
Lines | 0 |
Ratio | 0 % |
Tests | 19 |
CRAP Score | 10 |
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 |
||
14 | 44 | public function __construct($array = []) |
|
15 | { |
||
16 | 44 | if (is_string($array)) { |
|
17 | 2 | $array = json_decode($array, true); |
|
18 | } |
||
19 | 44 | if (is_object($array)) { |
|
20 | 2 | $array = $array->toArray() ?? (array) $array; |
|
21 | } |
||
22 | |||
23 | 44 | $methods = get_class_methods(self::class); |
|
24 | |||
25 | 44 | foreach ($methods as $method) { |
|
26 | 44 | preg_match('/^(set)(.*?)$/i', $method, $results); |
|
27 | |||
28 | 44 | $pre = $results[1] ?? ''; |
|
29 | |||
30 | 44 | $k = $results[2] ?? ''; |
|
31 | |||
32 | 44 | $k = strtolower(substr($k, 0, 1)).substr($k, 1); |
|
33 | 44 | $k2 = Str::snake($k); |
|
34 | |||
35 | 44 | if ('set' === $pre && ! empty($array[$k])) { |
|
36 | 18 | $this->$method($array[$k]); |
|
37 | 44 | } elseif ('set' === $pre && ! empty($array[$k2])) { |
|
38 | 4 | $this->$method($array[$k2]); |
|
39 | 44 | } elseif ('set' === $pre && ! empty($array[Str::ucfirst($k)])) { |
|
40 | 22 | $this->$method($array[Str::ucfirst($k)]); |
|
41 | } |
||
42 | } |
||
43 | |||
44 | 44 | return $this; |
|
45 | } |
||
47 |