Completed
Branch master (f2efac)
by John
02:31
created

MemcachedHealthIndicator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 2
dl 42
loc 42
ccs 14
cts 14
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A doHealthCheck() 16 16 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Actuator\Health\Indicator;
4
5
use Actuator\Health\HealthBuilder;
6
7
/**
8
 * Simple implementation of a HealthIndicator returning status information for
9
 * Memcached in-memory data stores.
10
 *
11
 * @package Actuator\Health\Indicator
12
 */
13 View Code Duplication
class MemcachedHealthIndicator extends AbstractHealthIndicator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    /**
16
     * @var \Memcached
17
     */
18
    private $memcached;
19
20
    /**
21
     * MemcacheHealthIndicator constructor.
22
     * @param \Memcached $memcached
23
     */
24 9
    public function __construct(\Memcached $memcached)
25
    {
26 9
        assert(!is_null($memcached), 'Memcached must not be null');
27
28 9
        $this->memcached = $memcached;
29 9
    }
30
31
    /**
32
     * Actual health check logic.
33
     *
34
     * @param HealthBuilder $builder
35
     * @throws \Exception any Exception that should create a Status::DOWN
36
     * system status.
37
     */
38 9
    protected function doHealthCheck(HealthBuilder $builder)
39
    {
40
        try {
41 9
            $version = $this->memcached->getversion();
42 9
        } catch (\Exception $e) {
43 3
            $builder->down($e);
44 3
            return;
45
        }
46
47 6
        if (!$version) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $version of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48 3
            $builder->down();
49 3
            return;
50
        }
51
52 3
        $builder->up()->withDetail('version', $version);
0 ignored issues
show
Documentation introduced by
$version is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53 3
    }
54
}
55