Completed
Push — master ( be473f...8253c0 )
by Vladimir
05:20
created

Manager::findChecks()   C

Complexity

Conditions 12
Paths 64

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 18.922

Importance

Changes 0
Metric Value
cc 12
eloc 9
nc 64
nop 3
dl 0
loc 15
ccs 7
cts 11
cp 0.6364
crap 18.922
rs 6.9666
c 0
b 0
f 0

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
3
/**
4
 * This file is part of the `tvi/monitor-bundle` project.
5
 *
6
 * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
11
12
namespace Tvi\MonitorBundle\Check;
13
14
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
15
16
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
17
 * @author Vladimir Turnaev <[email protected]>
18
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
19
class Manager implements \ArrayAccess, \Iterator, \Countable
20
{
21
    use ContainerAwareTrait;
22
    use CheckArraybleTrait;
23
24
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
25
     * @var Group[]
26
     */
27
    protected $groups = [];
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @var Group[]
31
     */
32
    protected $tags = [];
33
34 36
    public function init(array $tagsMap, array $checkServiceMap)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function init()
Loading history...
35
    {
36 36
        $this->setTagsMap($tagsMap);
37 36
        $this->setCheckServiceMap($checkServiceMap);
38 36
    }
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @param ?string|string[] $tags
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $tags does not match actual variable name $alias
Loading history...
42
     * @param ?string|string[] $alias
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $alias does not match actual variable name $groups
Loading history...
43
     * @param ?string|string[] $groups
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Doc comment for parameter $groups does not match actual variable name $tags
Loading history...
44
     *
45
     * @return Tag[]
46
     */
47 1
    public function findChecks($alias = null, $groups = null, $tags = null): array
48
    {
49 1
        $alias = (array) (null === $alias ? [] : (\is_string($alias) ? [$alias] : $alias));
50 1
        $groups = (array) (null === $groups ? [] : (\is_string($groups) ? [$groups] : $groups));
51 1
        $tags = (array) (null === $tags ? [] : (\is_string($tags) ? [$tags] : $tags));
52
53 1
        $check = array_filter($this->toArray(), static function (CheckInterface $c) use ($alias, $groups, $tags) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
54
            $inAlias = ($alias) ? \in_array($c->getId(), $alias, true) : true;
55
            $inGroups = ($groups) ? \in_array($c->getGroup(), $groups, true) : true;
56
            $inTags = ($tags) ? (bool) array_intersect($c->getTags(), $tags) : true;
57
58
            return $inAlias && $inGroups && $inTags;
59 1
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
60
61 1
        return $check;
62
    }
63
64
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
65
     * @param ?string|string[] $groups
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
66
     *
67
     * @return Group[]
68
     */
69 1
    public function findGroups($groups = null): array
70
    {
71 1
        if ($groups) {
72 1
            $groups = \is_string($groups) ? [$groups] : $groups;
73
74 1
            return array_filter($this->groups, static function ($t) use ($groups) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
75 1
                return \in_array($t->getName(), $groups, true);
76 1
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
77
        }
78
79 1
        return $this->groups;
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
83
     * @param ?string|string[] $tags
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
84
     *
85
     * @return Group[]
86
     */
87 2
    public function findTags($tags = null): array
88
    {
89 2
        if ($tags) {
90 1
            $tags = \is_string($tags) ? [$tags] : $tags;
91
92 1
            return array_filter($this->tags, static function ($t) use ($tags) {
93 1
                return \in_array($t->getName(), $tags, true);
94 1
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
95
        }
96
97 2
        return $this->tags;
98
    }
99
100 32
    public function addGroup(Group $group): Group
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addGroup()
Loading history...
101
    {
102 32
        return empty($this->groups[$group->getName()]) ? $this->groups[$group->getName()] = $group : $this->groups[$group->getName()];
103
    }
104
105 4
    public function addTag(Tag $tag): Tag
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addTag()
Loading history...
106
    {
107 4
        return empty($this->tags[$tag->getName()]) ? $this->tags[$tag->getName()] = $tag : $this->tags[$tag->getName()];
0 ignored issues
show
Bug Best Practice introduced by
The expression return empty($this->tags...->tags[$tag->getName()] could return the type Tvi\MonitorBundle\Check\Group which is incompatible with the type-hinted return Tvi\MonitorBundle\Check\Tag. Consider adding an additional type-check to rule them out.
Loading history...
108
    }
109
110 36
    protected function setTagsMap(array $tagsMap)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setTagsMap()
Loading history...
111
    {
112 36
        foreach ($tagsMap as $tag => $tagSetting) {
113 2
            $this->addTag(new Tag($tag));
114
        }
115 36
    }
116
117
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
118
     * @param array $checkServiceMap
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
119
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
120 36
    protected function setCheckServiceMap($checkServiceMap)
121
    {
122 36
        foreach ($checkServiceMap as $checkId => $check) {
123 32
            $checkServiceId = $check['serviceId'];
124 32
            $checkProxy = new Proxy(function () use ($checkServiceId, $checkId) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
125 32
                $this->checks[$checkId] = $this->container->get($checkServiceId);
126
127 32
                return $this->checks[$checkId];
128 32
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
129
130 32
            $this->checks[$checkId] = $checkProxy;
131
132 32
            foreach ($check['tags'] as $tagName) {
133 3
                $tag = $this->addTag(new Tag($tagName));
134 3
                $tag->addCheck($checkId, $this->checks[$checkId]);
135
            }
136
137 32
            $group = $this->addGroup(new Group($check['group']));
138 32
            $group->addCheck($checkId, $this->checks[$checkId]);
139 32
            $this->groups[$group->getName()] = $group;
140
        }
141
142 36
        foreach ($this->tags as $id => $tag) {
143 4
            if (!\count($tag)) {
144 4
                unset($this->tags[$id]);
145
            }
146
        }
147 36
    }
148
}
149