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

OpcacheInvalidateScriptsCommand::processFileList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 0
cts 12
cp 0
rs 9.4285
nc 3
cc 3
eloc 10
nop 2
crap 12
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