Conditions | 11 |
Paths | 60 |
Total Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Tests | 19 |
CRAP Score | 11.0151 |
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 |
||
35 | 22 | /** |
|
36 | * @return Host[] |
||
37 | 22 | */ |
|
38 | public function getAll() |
||
39 | { |
||
40 | 22 | $hosts = []; |
|
41 | 18 | foreach ($this->hosts as $host) { |
|
42 | $hosts[] = $host; |
||
43 | } |
||
44 | 22 | return $hosts; |
|
45 | } |
||
46 | 4 | ||
47 | /** |
||
48 | 3 | * @param string $hostnames |
|
49 | 1 | * @return Host[] |
|
50 | */ |
||
51 | public function getByHostnames(string $hostnames) |
||
52 | { |
||
53 | $hostnames = Range::expand(array_map('trim', explode(',', $hostnames))); |
||
54 | 4 | return array_map([$this->hosts, 'get'], $hostnames); |
|
55 | 3 | } |
|
56 | 1 | ||
57 | /** |
||
58 | * @param string $roles |
||
59 | 4 | * @return Host[] |
|
60 | */ |
||
61 | public function getByRoles(string $roles) |
||
62 | { |
||
63 | if (is_string($roles)) { |
||
64 | 18 | $roles = array_map('trim', explode(',', $roles)); |
|
65 | 17 | } |
|
66 | 17 | ||
67 | $hosts = []; |
||
68 | foreach ($this->hosts as $host) { |
||
69 | foreach ($host->get('roles', []) as $role) { |
||
70 | if (in_array($role, $roles, true)) { |
||
71 | 20 | $hosts[] = $host; |
|
72 | 1 | } |
|
73 | 1 | } |
|
74 | } |
||
75 | |||
76 | return $hosts; |
||
77 | } |
||
78 | } |
||
79 |