Completed
Push — master ( 17dfca...2f9de9 )
by Samuel
01:17
created

OpcacheCompileScriptsCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 27.12%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 4
dl 0
loc 113
ccs 16
cts 59
cp 0.2712
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 20 1
A execute() 0 19 3
A compile() 0 17 2
A compileBatch() 0 17 2
A prepareFileList() 0 13 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 Symfony\Component\Console\Helper\Table;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Finder\Finder;
20
21
class OpcacheCompileScriptsCommand extends AbstractCommand
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 19
    protected function configure()
27
    {
28
        $this
29 19
            ->setName('opcache:compile:scripts')
30 19
            ->setDescription('Compile scripts from path to the opcode cache')
31 19
            ->addArgument('path', InputArgument::REQUIRED)
32 19
            ->addOption(
33 19
                'exclude',
34 19
                null,
35 19
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
36 19
                'Exclude files from given path'
37
            )
38 19
            ->addOption(
39 19
                'batch',
40 19
                null,
41 19
                InputOption::VALUE_NONE,
42 19
                'Compile all files at once, could be useful if pm.max_requests is too low'
43
            )
44 19
            ->setHelp('');
45 19
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output): int
51
    {
52
        $this->ensureExtensionLoaded('Zend OPcache');
53
        $path = $input->getArgument('path');
54
55
        $exclude = [];
56
        if ($input->hasOption('exclude')) {
57
            $exclude = $input->getOption('exclude');
58
        }
59
60
        $splFiles = $this->prepareFileList($path, $exclude);
0 ignored issues
show
Bug introduced by
It seems like $path defined by $input->getArgument('path') on line 53 can also be of type array<integer,string> or null; however, CacheTool\Command\Opcach...mand::prepareFileList() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
        if ($input->getOption('batch')) {
62
            $this->compileBatch($splFiles, $output);
63
        } else {
64
            $this->compile($splFiles, $output);
65
        }
66
67
        return 0;
68
    }
69
70
    /**
71
     * @param \Traversable|\SplFileInfo[] $splFiles
72
     * @param OutputInterface $output
73
     */
74
    protected function compile($splFiles, OutputInterface $output)
75
    {
76
        $rows = [];
77
        foreach ($splFiles as $file) {
78
            $rows[] = [
79
                $this->getCacheTool()->opcache_compile_file($file->getRealPath()),
80
                $file->getRealPath()
81
            ];
82
        }
83
84
        $table = new Table($output);
85
        $table
86
            ->setHeaders(['Compiled', 'Filename'])
87
            ->setRows($rows)
88
        ;
89
        $table->render();
90
    }
91
92
    /**
93
     * @param \Traversable|\SplFileInfo[] $splFiles
94
     * @param OutputInterface $output
95
     */
96
    protected function compileBatch($splFiles, OutputInterface $output)
97
    {
98
        $paths = [];
99
        foreach ($splFiles as $file) {
100
            $paths []= $file->getRealPath();
101
        }
102
103
        $compiled = $this->getCacheTool()->opcache_compile_files($paths);
104
105
        $table = new Table($output);
106
        $table
107
            ->setHeaders(['Compiled'])
108
            ->setRows([[$compiled]])
109
        ;
110
111
        $table->render();
112
    }
113
114
    /**
115
     * @param string $path
116
     * @param array $exclude
117
     *
118
     * @return \Traversable|\SplFileInfo[]
119
     */
120
    private function prepareFileList($path, $exclude = [])
121
    {
122
        return Finder::create()
123
            ->files()
124
            ->in($path)
125
            ->name('*.php')
126
            ->notPath('/Tests/')
127
            ->notPath('/tests/')
128
            ->notPath($exclude)
129
            ->ignoreUnreadableDirs()
130
            ->ignoreDotFiles(true)
131
            ->ignoreVCS(true);
132
    }
133
}
134