Issues (14)

src/Command/OpcacheCompileScriptsCommand.php (2 issues)

Labels
Severity
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 24
    protected function configure()
27
    {
28
        $this
29 24
            ->setName('opcache:compile:scripts')
30 24
            ->setDescription('Compile scripts from path to the opcode cache')
31 24
            ->addArgument('path', InputArgument::REQUIRED)
32 24
            ->addOption(
33 24
                'exclude',
34 24
                null,
35 24
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
36 24
                'Exclude files from given path'
37
            )
38 24
            ->addOption(
39 24
                'batch',
40 24
                null,
41 24
                InputOption::VALUE_NONE,
42 24
                'Compile all files at once, could be useful if pm.max_requests is too low'
43
            )
44 24
            ->setHelp('');
45 24
    }
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
It seems like $path can also be of type string[]; however, parameter $path of CacheTool\Command\Opcach...mand::prepareFileList() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $splFiles = $this->prepareFileList(/** @scrutinizer ignore-type */ $path, $exclude);
Loading history...
It seems like $exclude can also be of type boolean and string; however, parameter $exclude of CacheTool\Command\Opcach...mand::prepareFileList() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $splFiles = $this->prepareFileList($path, /** @scrutinizer ignore-type */ $exclude);
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