WarmupCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 6
dl 0
loc 104
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 18 1
C execute() 0 75 12
1
<?php
2
3
namespace Go\Zend\Framework\Console\Command;
4
5
use Go\Core\AspectKernel;
6
use Go\Instrument\ClassLoading\SourceTransformingLoader;
7
use Go\Instrument\FileSystem\Enumerator;
8
use Go\Instrument\Transformer\FilterInjectorTransformer;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class WarmupCommand extends Command
15
{
16
    /**
17
     * {@inheritDoc}
18
     */
19
    protected function configure()
20
    {
21
        $this
22
            ->setName('goaop:warmup')
23
            ->addArgument(
24
                'applicationConfig',
25
                InputArgument::REQUIRED,
26
                'Path to application config which includes aspects and goaop module'
27
            )
28
            ->setDescription('Warm up the cache with woven aspects')
29
            ->setHelp(<<<EOT
30
Initializes the kernel and, if successful, warm up the cache for PHP
31
files under the application directory.
32
33
By default, the cache directory is taken from configured AspectKernel class.
34
EOT
35
            );
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $output->writeln('Start up application with supplied config...');
44
        $config = $input->getArgument('applicationConfig');
45
        $path   = stream_resolve_include_path($config);
46
        if (!is_readable($path)) {
47
            throw new \InvalidArgumentException("Invalid loader path: {$config}");
48
        }
49
50
        // Init the application once using given config
51
        // This way the late static binding on the AspectKernel
52
        // will be on the goaop-zf2-module kernel
53
        \Zend\Mvc\Application::init(include $path);
54
55
        if (!class_exists(AspectKernel::class, false)) {
56
            $message = "Kernel was not initialized yet. Maybe missing module Go\ZF2\GoAopModule in config {$path}";
57
            throw new \InvalidArgumentException($message);
58
        }
59
60
        $kernel  = AspectKernel::getInstance();
61
        $options = $kernel->getOptions();
62
63
        if (empty($options['cacheDir'])) {
64
            throw new \InvalidArgumentException('Cache warmer require the `cacheDir` options to be configured');
65
        }
66
67
        $enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
68
        $iterator   = $enumerator->enumerate();
69
70
        $totalFiles = iterator_count($iterator);
71
        $output->writeln("Total <info>{$totalFiles}</info> files to process.");
72
        $iterator->rewind();
73
74
        set_error_handler(function($errno, $errstr, $errfile, $errline) {
75
            throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
76
        });
77
78
        $index  = 0;
79
        $errors = [];
80
        foreach ($iterator as $file) {
81
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
82
                $output->writeln("Processing file <info>{$file->getRealPath()}</info>");
83
            }
84
            $isSuccess = null;
0 ignored issues
show
Unused Code introduced by
$isSuccess is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
            try {
86
                // This will trigger creation of cache
87
                file_get_contents(
88
                    FilterInjectorTransformer::PHP_FILTER_READ .
89
                    SourceTransformingLoader::FILTER_IDENTIFIER .
90
                    '/resource=' . $file->getRealPath()
91
                );
92
                $isSuccess = true;
93
            } catch (\Exception $e) {
94
                $isSuccess = false;
95
                $errors[$file->getRealPath()] = $e;
96
            }
97
            if ($output->getVerbosity() == OutputInterface::VERBOSITY_NORMAL) {
98
                $output->write($isSuccess ? '.' : '<error>E</error>');
99
                if (++$index % 50 == 0) {
100
                    $output->writeln("($index/$totalFiles)");
101
                }
102
            }
103
        }
104
105
        restore_error_handler();
106
107
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
108
            foreach ($errors as $file=>$error) {
109
                $message = "File {$file} is not processed correctly due to exception: {$error->getMessage()}";
110
                $output->writeln($message);
111
            }
112
        }
113
114
        $output->writeln('<info>Done</info>');
115
    }
116
117
}
118