Completed
Pull Request — master (#1)
by Michal
01:58
created

InfoHelper   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 111
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createInfoArray() 0 11 2
A initializeKeyspace() 0 14 3
A createInfoForPredis() 0 9 3
C createInfoForRedis() 0 24 7
1
<?php
2
3
namespace RedisProxy;
4
5
use Predis\Client;
6
use Redis;
7
8
class InfoHelper
9
{
10
    private static $keyStartToSectionMap = [
11
        'redis_' => 'server',
12
        'uptime_' => 'server',
13
        'client_' => 'clients',
14
        'used_memory' => 'memory',
15
        'mem_' => 'memory',
16
        'rdb_' => 'persistence',
17
        'aof_' => 'persistence',
18
        'total_' => 'stats',
19
        'sync_' => 'stats',
20
        'keyspace_' => 'stats',
21
        'pubsub_' => 'stats',
22
        'repl_backlog_' => 'replication',
23
        'used_cpu_' => 'cpu',
24
        'db' => 'keyspace',
25
    ];
26
27
    private static $keyToSectionMap = [
28
        'os' => 'server',
29
        'arch_bits' => 'server',
30
        'multiplexing_api'  => 'server',
31
        'gcc_version' => 'server',
32
        'process_id' => 'server',
33
        'run_id' => 'server',
34
        'tcp_port' => 'server',
35
        'hz' => 'server',
36
        'lru_clock' => 'server',
37
        'config_file' => 'server',
38
        'connected_clients' => 'clients',
39
        'blocked_clients' => 'clients',
40
        'loading' => 'persistence',
41
        'instantaneous_ops_per_sec' => 'stats',
42
        'rejected_connections' => 'stats',
43
        'expired_keys' => 'stats',
44
        'evicted_keys' => 'stats',
45
        'latest_fork_usec' => 'stats',
46
        'role' => 'replication',
47
        'connected_slaves' => 'replication',
48
        'master_repl_offset' => 'replication',
49
    ];
50
51
    /**
52
     * @param Client|Redis $driver
53
     * @param array $result
54
     * @param integer|null $databases
55
     * @return array
56
     */
57 8
    public static function createInfoArray($driver, array $result, $databases = null)
58
    {
59 8
        $groupedResult = [];
60 8
        self::initializeKeyspace($groupedResult, $databases);
61
62 8
        if ($driver instanceof Client) {
63 4
            return self::createInfoForPredis($result, $groupedResult);
64
        }
65
66 4
        return self::createInfoForRedis($result, $groupedResult);
67
    }
68
69 8
    private static function initializeKeyspace(&$groupedResult, $databases = null)
70
    {
71 8
        if ($databases === null) {
72 8
            return;
73
        }
74 4
        $groupedResult['keyspace'] = [];
75 4
        for ($db = 0; $db < $databases; ++$db) {
76 4
            $groupedResult['keyspace']["db$db"] = [
77 2
                'keys' => 0,
78 2
                'expires' => null,
79 2
                'avg_ttl' => null,
80
            ];
81 2
        }
82 4
    }
83
84 4
    private static function createInfoForPredis(array $result, array $groupedResult)
85
    {
86 4
        $result = array_change_key_case($result, CASE_LOWER);
87 4
        if (isset($groupedResult['keyspace']) && isset($result['keyspace'])) {
88 2
            $groupedResult['keyspace'] = array_merge($groupedResult['keyspace'], $result['keyspace']);
89 2
            unset($result['keyspace']);
90 1
        }
91 4
        return array_merge($groupedResult, $result);
92
    }
93
94 4
    private static function createInfoForRedis(array $result, array $groupedResult)
95
    {
96 4
        foreach ($result as $key => $value) {
97 2
            if (isset(self::$keyToSectionMap[$key])) {
98 2
                $groupedResult[self::$keyToSectionMap[$key]][$key] = $value;
99 2
                continue;
100
            }
101
102 2
            foreach (self::$keyStartToSectionMap as $keyStart => $targetSection) {
103 2
                if (strpos($key, $keyStart) === 0 && $keyStart === 'db') {
104 2
                    $dbKeyspace = explode(',', $value);
105 2
                    $info['keys'] = explode('=', $dbKeyspace[0])[1];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$info was never initialized. Although not strictly required by PHP, it is generally a good practice to add $info = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
106 2
                    $info['expires'] = explode('=', $dbKeyspace[1])[1];
0 ignored issues
show
Bug introduced by
The variable $info does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
107 2
                    $info['avg_ttl'] = explode('=', $dbKeyspace[2])[1];
108 2
                    $value = $info;
109 1
                }
110 2
                if (strpos($key, $keyStart) === 0) {
111 2
                    $groupedResult[$targetSection][$key] = $value;
112 2
                    continue;
113
                }
114 1
            }
115 2
        }
116 4
        return $groupedResult;
117
    }
118
}
119