Completed
Pull Request — master (#38)
by Boris
04:23 queued 02:11
created

ApcCacheInfoCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
nc 1
cc 1
eloc 5
nop 0
crap 1
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 ApcCacheInfoCommand 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('apc:cache:info')
46 4
            ->setDescription('Shows APC 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('apc');
56
57
        $user = $this->getCacheTool()->apc_cache_info('user');
58
        $system = $this->getCacheTool()->apc_cache_info('system');
59
60
        $this->normalize($user);
61
        $this->normalize($system);
62
63
        if (!$user || !$system) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $user 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...
Bug Best Practice introduced by
The expression $system 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...
64
            throw new \RuntimeException("Could not fetch info from APC");
65
        }
66
67
        $table = new Table($output);
68
        $table->setHeaders(array('Name', 'User', 'System'));
69
        $table->setRows($this->getRows($user, $system));
70
        $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...
71
    }
72
73
    /**
74
     * @param  array $user
75
     * @param  array $system
76
     * @return array
77
     */
78
    protected function getRows($user, $system)
79
    {
80
        // missing: cache_list, deleted_list, slot_distribution
81
        return array(
82
            array('Slots', $user['num_slots'], $system['num_slots']),
83
            array('TTL', $user['ttl'], $system['ttl']),
84
            array('Hits', number_format($user['num_hits']), number_format($system['num_hits'])),
85
            array('Misses', number_format($user['num_misses']), number_format($system['num_misses'])),
86
            array('Inserts', number_format($user['num_inserts']), number_format($system['num_inserts'])),
87
            array('Expunges', number_format($user['expunges']), number_format($system['expunges'])),
88
            array('Start time', Formatter::date($user['start_time'], 'U'), Formatter::date($system['start_time'], 'U')),
89
            array('Memory size', Formatter::bytes($user['mem_size']), Formatter::bytes($system['mem_size'])),
90
            array('Entries', number_format($user['num_entries']), number_format($system['num_entries'])),
91
            array('File upload progress', $user['file_upload_progress'] ? 'Yes' : 'No', $system['file_upload_progress'] ? 'Yes' : 'No'),
92
            array('Memory type', $user['memory_type'], $system['memory_type']),
93
            array('Locking type', (isset($user['locking_type']) ? $user['locking_type'] : 'Not Supported'), (isset($system['locking_type']) ? $system['locking_type'] : 'Not Supported')),
94
        );
95
    }
96
97
    /**
98
     * Fix inconsistencies between APC and APCu
99
     *
100
     * @param  array  &$array
101
     */
102
    protected function normalize(array &$array)
103
    {
104
        foreach ($array as $key => $value) {
105
            if (array_key_exists($key, self::$apcFix)) {
106
                unset($array[$key]);
107
                $array[self::$apcFix[$key]] = $value;
108
            }
109
        }
110
    }
111
}
112