Completed
Pull Request — master (#257)
by Alexander
16:28
created

CacheWarmupCommand   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 83
ccs 0
cts 62
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 14 1
C execute() 0 58 10
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2013, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Console\Command;
12
13
use Go\Instrument\ClassLoading\SourceTransformingLoader;
14
use Go\Instrument\FileSystem\Enumerator;
15
use Go\Instrument\Transformer\FilterInjectorTransformer;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * Console command for warming the cache
21
 */
22
class CacheWarmupCommand extends BaseAspectCommand
23
{
24
25
    /**
26
     * {@inheritDoc}
27
     */
28
    protected function configure()
29
    {
30
        parent::configure();
31
        $this
32
            ->setName('cache:warmup:aop')
33
            ->setDescription("Warm up the cache with woven aspects")
34
            ->setHelp(<<<EOT
35
Initializes the kernel and, if successful, warm up the cache for PHP
36
files under the application directory.
37
38
By default, the cache directory is taken from configured AspectKernel class.
39
EOT
40
            );
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        parent::execute($input, $output);
49
        $options = $this->aspectKernel->getOptions();
50
51
        if (empty($options['cacheDir'])) {
52
            throw new \InvalidArgumentException("Cache warmer require the `cacheDir` options to be configured");
53
        }
54
55
        $enumerator = new Enumerator($options['appDir'], $options['includePaths'], $options['excludePaths']);
56
        $iterator   = $enumerator->enumerate();
57
58
        $totalFiles = iterator_count($iterator);
59
        $output->writeln("Total <info>{$totalFiles}</info> files to process.");
60
        $iterator->rewind();
61
62
        set_error_handler(function($errno, $errstr, $errfile, $errline) {
63
            throw new \ErrorException($errstr, $errno, 0, $errfile, $errline);
64
        });
65
66
        $index  = 0;
67
        $errors = [];
68
        foreach ($iterator as $file) {
69
            if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
70
                $output->writeln("Processing file <info>{$file->getRealPath()}</info>");
71
            }
72
            $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...
73
            try {
74
                // This will trigger creation of cache
75
                file_get_contents(
76
                    FilterInjectorTransformer::PHP_FILTER_READ .
77
                    SourceTransformingLoader::FILTER_IDENTIFIER .
78
                    "/resource=" . $file->getRealPath()
79
                );
80
                $isSuccess = true;
81
            } catch (\Exception $e) {
82
                $isSuccess = false;
83
                $errors[$file->getRealPath()] = $e;
84
            }
85
            if ($output->getVerbosity() == OutputInterface::VERBOSITY_NORMAL) {
86
                $output->write($isSuccess ? '.' : '<error>E</error>');
87
                if (++$index % 50 == 0) {
88
                    $output->writeln("($index/$totalFiles)");
89
                }
90
            }
91
        }
92
93
        restore_error_handler();
94
95
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERY_VERBOSE) {
96
            foreach ($errors as $file=>$error) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "=>"; 0 found
Loading history...
Coding Style introduced by
Expected 1 space after "=>"; 0 found
Loading history...
97
                $message = "File {$file} is not processed correctly due to exception: {$error->getMessage()}";
98
                $output->writeln($message);
99
            }
100
        }
101
102
        $output->writeln("<info>Done</info>");
103
    }
104
}
105