Completed
Push — 1.x ( 8c31f4...5f4811 )
by Samuel
7s
created

ApcCacheInfoCommand::getRows()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 8.8571
nc 16
cc 5
eloc 14
nop 2
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\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class ApcCacheInfoCommand extends AbstractCommand
19
{
20
    protected static $apcFix = array(
21
        'stime' => "start_time",
22
        'atime' => "access_time",
23
        'mtime' => "modification_time",
24
        'ctime' => "creation_time",
25
        'dtime' => "deletion_time",
26
27
        'nslots' => "num_slots",
28
        'nhits' => "num_hits",
29
        'nmisses' => "num_misses",
30
        'ninserts' => "num_inserts",
31
        'nentries' => "num_entries",
32
        'nexpunges' => "expunges",
33
        "num_expunges" => "expunges",
34
35
        'key' => "info",
36
    );
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function configure()
42
    {
43
        $this
44
            ->setName('apc:cache:info')
45
            ->setDescription('Shows APC user & system cache information')
46
            ->setHelp('');
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $this->ensureExtensionLoaded('apc');
55
56
        $user = $this->getCacheTool()->apc_cache_info('user');
57
        $system = $this->getCacheTool()->apc_cache_info('system');
58
59
        $this->normalize($user);
60
        $this->normalize($system);
61
62
        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...
63
            throw new \RuntimeException("Could not fetch info from APC");
64
        }
65
66
        $table = $this->getHelper('table');
67
        $table->setHeaders(array('Name', 'User', 'System'));
68
        $table->setRows($this->getRows($user, $system));
69
        $table->render($output);
70
    }
71
72
    /**
73
     * @param  array $user
74
     * @param  array $system
75
     * @return array
76
     */
77
    protected function getRows($user, $system)
78
    {
79
        // missing: cache_list, deleted_list, slot_distribution
80
        return array(
81
            array('Slots', $user['num_slots'], $system['num_slots']),
82
            array('TTL', $user['ttl'], $system['ttl']),
83
            array('Hits', number_format($user['num_hits']), number_format($system['num_hits'])),
84
            array('Misses', number_format($user['num_misses']), number_format($system['num_misses'])),
85
            array('Inserts', number_format($user['num_inserts']), number_format($system['num_inserts'])),
86
            array('Expunges', number_format($user['expunges']), number_format($system['expunges'])),
87
            array('Start time', Formatter::date($user['start_time'], 'U'), Formatter::date($system['start_time'], 'U')),
88
            array('Memory size', Formatter::bytes($user['mem_size']), Formatter::bytes($system['mem_size'])),
89
            array('Entries', number_format($user['num_entries']), number_format($system['num_entries'])),
90
            array('File upload progress', $user['file_upload_progress'] ? 'Yes' : 'No', $system['file_upload_progress'] ? 'Yes' : 'No'),
91
            array('Memory type', $user['memory_type'], $system['memory_type']),
92
            array('Locking type', (isset($user['locking_type']) ? $user['locking_type'] : 'Not Supported'), (isset($system['locking_type']) ? $system['locking_type'] : 'Not Supported')),
93
        );
94
    }
95
96
    /**
97
     * Fix inconsistencies between APC and APCu
98
     *
99
     * @param  array  &$array
100
     */
101
    protected function normalize(array &$array)
102
    {
103
        foreach ($array as $key => $value) {
104
            if (array_key_exists($key, self::$apcFix)) {
105
                unset($array[$key]);
106
                $array[self::$apcFix[$key]] = $value;
107
            }
108
        }
109
    }
110
}
111