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

OpcacheInvalidateScriptsCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 18.92%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 70
ccs 7
cts 37
cp 0.1892
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 22 2
A processFileList() 0 18 3
A processFilename() 0 10 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\Helper\Table;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class OpcacheInvalidateScriptsCommand extends AbstractCommand
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25 4
    protected function configure()
26
    {
27 4
        $this
28 4
            ->setName('opcache:invalidate:scripts')
29 4
            ->setDescription('Remove scripts from the opcode cache')
30 4
            ->addArgument('path', InputArgument::REQUIRED)
31 4
            ->setHelp('');
32 4
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $this->ensureExtensionLoaded('Zend OPcache');
40
        $path = $input->getArgument('path');
41
42
        $info = $this->getCacheTool()->opcache_get_status(true);
43
44
        if ($info === false) {
45
            throw new \RuntimeException('opcache_get_status(): No Opcache status info available.  Perhaps Opcache is disabled via opcache.enable or opcache.enable_cli?');
46
        }
47
48
        $table = new Table($output);
49
        $table
50
            ->setHeaders(array(
51
                'Cleaned',
52
                'Filename'
53
            ))
54
            ->setRows($this->processFilelist($info['scripts'], $path))
55
        ;
56
57
        $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...
58
    }
59
60
    protected function processFileList(array $cacheList, $path)
61
    {
62
        $list = array();
63
64
        sort($cacheList);
65
66
        foreach ($cacheList as $item) {
67
            $filename = $this->processFilename($item['full_path']);
68
            if (preg_match('|' . $path . '|', $filename)) {
69
                $list[] = array(
70
                    $this->getCacheTool()->opcache_invalidate($filename),
71
                    $filename
72
                );
73
            }
74
        }
75
76
        return $list;
77
    }
78
79
    protected function processFilename($filename)
80
    {
81
        $dir = getcwd();
82
83
        if (0 === strpos($filename, $dir)) {
84
            return "." . substr($filename, strlen($dir));
85
        }
86
87
        return $filename;
88
    }
89
}
90