GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Pull Request — master (#1904)
by Anton
02:21
created

HostSelector::getHosts()   B

Complexity

Conditions 11
Paths 60

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11.0151

Importance

Changes 0
Metric Value
cc 11
nc 60
nop 1
dl 0
loc 46
ccs 19
cts 20
cp 0.95
crap 11.0151
rs 7.3166
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A HostSelector::getAll() 0 8 2
A HostSelector::getByHostnames() 0 5 1
A HostSelector::getByRoles() 0 17 5

How to fix   Complexity   

Long Method

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:

1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Host;
9
10
class HostSelector
11
{
12
    private $hosts;
13
14 5
    public function __construct(HostCollection $hosts)
15
    {
16 5
        $this->hosts = $hosts;
17 5
    }
18
19
    /**
20
     * @param string $hosts
21
     * @param string $roles
22
     * @return Host[]
23
     */
24
    public function select($hosts, $roles)
25
    {
26
        if (!empty($hosts)) {
27
            return $this->getByHostnames($hosts);
28
        }
29
        if (!empty($roles)) {
30
            return $this->getByRoles($roles);
31
        }
32
        return $this->getAll();
33
    }
34
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