Completed
Push — master ( bfa80e...3adc88 )
by Alexander
02:11
created

DebugWeavingCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * Go! AOP framework
5
 *
6
 * @copyright Copyright 2015, Lisachenko Alexander <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Go\Console\Command;
13
14
use Go\Instrument\ClassLoading\CachePathManager;
15
use Go\Instrument\ClassLoading\CacheWarmer;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\NullOutput;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
21
/**
22
 * Console command for debugging weaving issues due to circular dependencies.
23
 *
24
 * @codeCoverageIgnore
25
 */
26
class DebugWeavingCommand extends BaseAspectCommand
27
{
28
    /**
29
     * {@inheritDoc}
30
     */
31
    protected function configure()
32
    {
33
        parent::configure();
34
        $this
35
            ->setName('debug:weaving')
36
            ->setDescription('Checks consistency in weaving process')
37
            ->setHelp(<<<EOT
38
Allows to check consistency of weaving process, detects circular references and mutual dependencies between
39
subjects of weaving and aspects.
40
EOT
41
            );
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $this->loadAspectKernel($input, $output);
50
51
        $io = new SymfonyStyle($input, $output);
52
53
        $io->title('Weaving debug information');
54
55
        $cachePathManager = $this->aspectKernel->getContainer()->get('aspect.cache.path.manager');
56
        $warmer           = new CacheWarmer($this->aspectKernel, new NullOutput());
0 ignored issues
show
Bug introduced by
It seems like $this->aspectKernel can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
57
        $warmer->warmUp();
58
59
        $proxies = $this->getProxies($cachePathManager);
60
61
        $cachePathManager->clearCacheState();
62
        $warmer->warmUp();
63
64
        $errors = 0;
65
66
        foreach ($this->getProxies($cachePathManager) as $path => $content) {
67
            if (!isset($proxies[$path])) {
68
                $io->error(sprintf('Proxy on path "%s" is generated on second "warmup" pass.', $path));
69
                $errors++;
70
                continue;
71
            }
72
73
            if (isset($proxies[$path]) && $proxies[$path] !== $content) {
74
                $io->error(sprintf('Proxy on path "%s" is weaved differnlty on second "warmup" pass.', $path));
75
                $errors++;
76
                continue;
77
            }
78
79
            $io->note(sprintf('Proxy  on path "%s" is consistently weaved.', $path));
80
        }
81
82
        if ($errors > 0) {
83
            $io->error(sprintf('Weaving is unstable, there are %s reported error(s).', $errors));
84
            return $errors;
85
        }
86
87
        $io->success('Weaving is stable, there are no errors reported.');
88
    }
89
90
    /**
91
     * Get Go! AOP generated proxy classes (paths and their contents) from cache.
92
     *
93
     * @param CachePathManager $cachePathManager
94
     *
95
     * @return array
96
     */
97
    private function getProxies(CachePathManager $cachePathManager)
98
    {
99
        $path     = $cachePathManager->getCacheDir() . '/_proxies';
100
        $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS), \RecursiveIteratorIterator::CHILD_FIRST);
101
        $proxies  = [];
102
103
        /**
104
         * @var \SplFileInfo $value
105
         */
106
        foreach ($iterator as $value) {
107
            if ($value->isFile()) {
108
                $proxies[$value->getPathname()] = file_get_contents($value->getPathname());
109
            }
110
        }
111
112
        return $proxies;
113
    }
114
}
115