Passed
Push — master ( faf2d6...18ab35 )
by Vladimir
06:03
created

Manager::findGroups()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 10
c 0
b 0
f 0
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
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @param array $tagsMap
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
36
     * @param array $checkServiceMap
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
37
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
38 36
    public function init(array $tagsMap, array $checkServiceMap)
39
    {
40 36
        $this->setTagsMap($tagsMap);
41 36
        $this->setCheckServiceMap($checkServiceMap);
42 36
    }
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $alias should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $groups should have a doc-comment as per coding-style.
Loading history...
45
     * @param null|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...
46
     *
47
     * @return Tag[]
48
     */
49 1
    public function findChecks($alias = null, $groups = null, $tags = null): array
0 ignored issues
show
Unused Code introduced by
The parameter $groups is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
    public function findChecks($alias = null, /** @scrutinizer ignore-unused */ $groups = null, $tags = null): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tags is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
    public function findChecks($alias = null, $groups = null, /** @scrutinizer ignore-unused */ $tags = null): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $alias is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

49
    public function findChecks(/** @scrutinizer ignore-unused */ $alias = null, $groups = null, $tags = null): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51 1
        $out = [];
52 1
        foreach ($this as $id => $check) {
53
            $out[] = $check;
54
        }
55
56 1
        return $out;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $out returns an array which contains values of type Tvi\MonitorBundle\Check\CheckInterface which are incompatible with the documented value type Tvi\MonitorBundle\Check\Tag.
Loading history...
57
    }
58
59
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
60
     * @param null|string|string[] $groups
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     *
62
     * @return Group[]
63
     */
64 1
    public function findGroups($groups = null): array
65
    {
66 1
        if ($groups) {
67 1
            $groups = \is_string($groups) ? [$groups] : $groups;
68
69 1
            return array_filter($this->groups, 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...
70 1
                return \in_array($t->getName(), $groups);
71 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...
72
        }
73
74 1
        return $this->groups;
75
    }
76
77
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
78
     * @param null|string|string[] $tags
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
79
     *
80
     * @return Group[]
81
     */
82 2
    public function findTags($tags = null): array
83
    {
84 2
        if ($tags) {
85 1
            $tags = \is_string($tags) ? [$tags] : $tags;
86
87 1
            return array_filter($this->tags, function ($t) use ($tags) {
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
88 1
                return \in_array($t->getName(), $tags);
89 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...
90
        }
91
92 2
        return $this->tags;
93
    }
94
95
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
96
     * @param Group $group
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
97
     *
98
     * @return Group
99
     */
100 32
    public function addGroup(Group $group): Group
101
    {
102 32
        return empty($this->groups[$group->getName()]) ? $this->groups[$group->getName()] = $group : $this->groups[$group->getName()];
103
    }
104
105
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
106
     * @param Tag $tag
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
107
     *
108
     * @return Tag
109
     */
110 4
    public function addTag(Tag $tag): Tag
111
    {
112 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...
113
    }
114
115
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
116
     * @param Group $group
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
117
     *
118
     * @return Group
119
     */
120
    public function getTags(Group $group): Group
121
    {
122
        return empty($this->groups[$group->getName()]) ? $this->groups[$group->getName()] = $group : $this->groups[$group->getName()];
123
    }
124
125
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
126
     * @param array $tagsMap
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
127
     * @param array $checksMap
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Superfluous parameter comment
Loading history...
128
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
129 36
    protected function setTagsMap(array $tagsMap)
130
    {
131 36
        foreach ($tagsMap as $tag => $tagSetting) {
132 2
            $this->addTag(new Tag($tag));
133
        }
134 36
    }
135
136
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
137
     * @param array $checkServiceMap
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
138
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
139 36
    protected function setCheckServiceMap($checkServiceMap)
140
    {
141 36
        foreach ($checkServiceMap as $checkId => $check) {
142 32
            $checkServiceId = $check['serviceId'];
143 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...
144 32
                $this->checks[$checkId] = $this->container->get($checkServiceId);
145
146 32
                return $this->checks[$checkId];
147 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...
148
149 32
            $this->checks[$checkId] = $checkProxy;
150
151 32
            foreach ($check['tags'] as $tagName) {
152 3
                $tag = $this->addTag(new Tag($tagName));
153 3
                $tag->addCheck($checkId, $this->checks[$checkId]);
154
            }
155
156 32
            $group = $this->addGroup(new Group($check['group']));
157 32
            $group->addCheck($checkId, $this->checks[$checkId]);
158 32
            $this->groups[$group->getName()] = $group;
159
        }
160
161 36
        foreach ($this->tags as $id => $tag) {
162 4
            if (!\count($tag)) {
163 4
                unset($this->tags[$id]);
164
            }
165
        }
166 36
    }
167
}
168