Passed
Push — master ( c54b8c...2ac8f3 )
by Vladimir
05:52
created

Check::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 21
ccs 10
cts 10
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\rabbitmq\QueueConsumer;
13
14
use JMS\Serializer\Annotation as JMS;
15
use Tvi\MonitorBundle\Check\rabbitmq\RabbitMQClient;
16
use Tvi\MonitorBundle\Check\CheckAbstract;
17
use ZendDiagnostics\Result\Failure;
18
use ZendDiagnostics\Result\Success;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @JMS\ExclusionPolicy("all")
22
 *
23
 * @author Vladimir Turnaev <[email protected]>
24
 */
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...
25
class Check extends CheckAbstract
26
{
27
    /**
28
     * Count that will cause a warning.
29
     *
30
     * @var int
31
     */
32
    protected $warningThreshold;
33
34
    /**
35
     * Count that will cause a fail.
36
     *
37
     * @var int
38
     */
39
    protected $criticalThreshold;
40
41
    /**
42
     * check queue name.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
43
     *
44
     * @var string
45
     */
46
    protected $queue;
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
49
     * @var RabbitMQClient
50
     */
51
    private $client;
0 ignored issues
show
Coding Style introduced by
Private member variable "client" must be prefixed with an underscore
Loading history...
52
53
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
54
     * @param ?string $queue
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
55
     * @param ?int    $criticalThreshold
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
56
     * @param ?int    $warningThreshold
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
57
     * @param ?string $host
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
58
     * @param ?int    $port
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
59
     * @param ?string $user
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
60
     * @param ?string $password
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     * @param ?string $vhost
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     * @param ?string $dsn
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
63
     */
64 2
    public function __construct(
65
        $queue,
66
        $criticalThreshold,
67
        $warningThreshold = null,
68
        $host = 'localhost',
69
        $port = 5672,
70
        $user = 'guest',
71
        $password = 'guest',
72
        $vhost = '/',
73
        $dsn = null)
0 ignored issues
show
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
74
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
75 2
        $this->queue = $queue;
76 2
        $this->criticalThreshold = $criticalThreshold;
77 2
        $this->warningThreshold = $warningThreshold;
78
79 2
        $this->client = new RabbitMQClient($host,
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...
80 2
            $port,
81 2
            $user,
82 2
            $password,
83 2
            $vhost,
84 2
            $dsn);
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
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...
85 2
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
90 2
    public function check()
91
    {
92
        try {
93 2
            $conn = $this->client->getConnect();
94
            $conn->channel();
95
96
            $channel = $conn->channel();
97
98
            list($queue, $messageCount, $consumerCount) = $channel->queue_declare($this->queue, true);
99
100
            if ($consumerCount <= $this->criticalThreshold) {
101
                $msg = sprintf('Consumer(s) %d got queue to few less them critical level %d.', $consumerCount, $this->criticalThreshold);
102
103
                return new Failure($msg, $messageCount);
104
            }
105
106
            if ($this->warningThreshold != null && $consumerCount <= $this->warningThreshold) {
107
                $msg = sprintf('Consumer(s) %d got queue to few less them critical level %d.', $consumerCount, $this->warningThreshold);
108
109
                return new Warning($msg, $messageCount);
0 ignored issues
show
Bug introduced by
The type Tvi\MonitorBundle\Check\...q\QueueConsumer\Warning was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
110
            }
111
112
            return new Success(sprintf('Consumer(s) %d for queue.', $consumerCount), $consumerCount);
113 2
        } catch (\Exception $e) {
114 2
            return new Failure($e->getMessage());
115
        }
116
    }
117
}
118