Completed
Push — 2.x ( 22d5c7 )
by Samuel
10s
created

ApcuCacheInfoCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 15.38%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 90
ccs 6
cts 39
cp 0.1538
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 17 2
A getRows() 0 18 3
A normalize() 0 9 3
1
<?php
2
3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CacheTool\Command;
13
14
use CacheTool\Util\Formatter;
15
use Symfony\Component\Console\Helper\Table;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class ApcuCacheInfoCommand extends AbstractCommand
20
{
21
    protected static $apcFix = array(
22
        'stime' => "start_time",
23
        'atime' => "access_time",
24
        'mtime' => "modification_time",
25
        'ctime' => "creation_time",
26
        'dtime' => "deletion_time",
27
28
        'nslots' => "num_slots",
29
        'nhits' => "num_hits",
30
        'nmisses' => "num_misses",
31
        'ninserts' => "num_inserts",
32
        'nentries' => "num_entries",
33
        'nexpunges' => "expunges",
34
        "num_expunges" => "expunges",
35
36
        'key' => "info",
37
    );
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 4
    protected function configure()
43
    {
44 4
        $this
45 4
            ->setName('apcu:cache:info')
46 4
            ->setDescription('Shows APCu user & system cache information')
47 4
            ->setHelp('');
48 4
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $this->ensureExtensionLoaded('apcu');
56
57
        $info = $this->getCacheTool()->apcu_cache_info(true);
58
59
        $this->normalize($info);
60
61
        if (!$info) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $info 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...
62
            throw new \RuntimeException("Could not fetch info from APCu");
63
        }
64
65
        $table = new Table($output);
66
        $table->setHeaders(array('Name', 'Info'));
67
        $table->setRows($this->getRows($info));
68
        $table->render($output);
0 ignored issues
show
Unused Code introduced by
The call to Table::render() has too many arguments starting with $output.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
69
    }
70
71
    /**
72
     * @param  array $info
73
     * @return array
74
     */
75
    protected function getRows($info)
76
    {
77
        // missing: cache_list, deleted_list, slot_distribution
78
        return array(
79
            array('Slots', $info['num_slots']),
80
            array('TTL', $info['ttl']),
81
            array('Hits', number_format($info['num_hits'])),
82
            array('Misses', number_format($info['num_misses'])),
83
            array('Inserts', number_format($info['num_inserts'])),
84
            array('Expunges', number_format($info['expunges'])),
85
            array('Start time', Formatter::date($info['start_time'], 'U')),
86
            array('Memory size', Formatter::bytes($info['mem_size'])),
87
            array('Entries', number_format($info['num_entries'])),
88
            array('File upload progress', ini_get('apcu.rfc1867') ? 'Yes' : 'No'),
89
            array('Memory type', $info['memory_type']),
90
            array('Locking type', (isset($info['locking_type']) ? $info['locking_type'] : 'Not Supported')),
91
        );
92
    }
93
94
    /**
95
     * Fix inconsistencies between APC and APCu
96
     *
97
     * @param  array  &$array
98
     */
99
    protected function normalize(array &$array)
100
    {
101
        foreach ($array as $key => $value) {
102
            if (array_key_exists($key, self::$apcFix)) {
103
                unset($array[$key]);
104
                $array[self::$apcFix[$key]] = $value;
105
            }
106
        }
107
    }
108
}
109