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