Completed
Push — master ( 1dd7d1...0c5b7b )
by Tom
09:54 queued 05:02
created

DevUrnCatalogAutoPath::autosetIdeaMiscXmlPath()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.439
cc 6
eloc 20
nc 6
nop 1
1
<?php
2
/*
3
 * @author Tom Klingenberg <https://github.com/ktomk>
4
 */
5
6
namespace N98\Magento\Application\Console\EventSubscriber;
7
8
use Magento\Developer\Console\Command\XmlCatalogGenerateCommand;
9
use Symfony\Component\Console\ConsoleEvents;
10
use Symfony\Component\Console\Event\ConsoleCommandEvent;
11
use Symfony\Component\Console\Input\ArgvInput;
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
/**
15
 * Class DevUrnCatalogAutoPath
16
 *
17
 * Comfort option to automatically set path to ".idea/misc.xml
18
 *
19
 * @package N98\Magento\Application\Console\EventSubscriber
20
 */
21
class DevUrnCatalogAutoPath implements EventSubscriberInterface
22
{
23
    /**
24
     * Returns an array of event names this subscriber wants to listen to.
25
     *
26
     * @return array The event names to listen to
27
     *
28
     * @api
29
     */
30
    public static function getSubscribedEvents()
31
    {
32
        return [
33
            ConsoleEvents::COMMAND => 'autosetIdeaMiscXmlPath',
34
        ];
35
    }
36
37
    /**
38
     * Display a warning if a running n98-magerun as root user
39
     *
40
     * @param ConsoleCommandEvent $event
41
     *
42
     * @return void
43
     */
44
    public function autosetIdeaMiscXmlPath(ConsoleCommandEvent $event)
45
    {
46
        if (!$event->getCommand() instanceof XmlCatalogGenerateCommand) {
1 ignored issue
show
Bug introduced by
The class Magento\Developer\Consol...lCatalogGenerateCommand does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
47
            return;
48
        }
49
50
        $input = clone $event->getInput();
51
        if (!$input instanceof ArgvInput) {
52
            return;
53
        }
54
55
        $command = clone $event->getCommand();
56
57
        $input->bind($command->getDefinition());
58
59
        $path = null;
60
        try {
61
            $path = $input->getArgument('path');
62
        } catch (\Exception $e) {
63
            return;
64
        }
65
66
        if ('dev:urn-catalog:generate' !== $path) {
67
            return;
68
        }
69
70
        $file = $this->detectFile($event);
71
        if (null === $file) {
72
            return;
73
        }
74
75
        $event->getOutput()->writeln("<info>automatically setting path to <comment>'$file'</comment></info>");
76
        $this->addToken($event->getInput(), $file);
0 ignored issues
show
Compatibility introduced by
$event->getInput() of type object<Symfony\Component...e\Input\InputInterface> is not a sub-type of object<Symfony\Component\Console\Input\ArgvInput>. It seems like you assume a concrete implementation of the interface Symfony\Component\Console\Input\InputInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
77
    }
78
79
    private function detectFile(ConsoleCommandEvent $event)
80
    {
81
        /** @var \N98\Magento\Application $app */
82
        $app = $event->getCommand()->getApplication();
83
84
        $root = $app->getMagentoRootFolder();
85
        $down = 1;
86
        do {
87
            if (is_dir($root . '/.idea')) {
88
                return $root . '/.idea/misc.xml';
89
            }
90
            $root .= '/..';
91
        } while (is_dir($root) && $down--);
92
93
        return null;
94
    }
95
96
    /**
97
     * @param ArgvInput $arg
98
     * @param string $file
99
     */
100
    private function addToken(ArgvInput $arg, $file)
101
    {
102
        $refl = new \ReflectionObject($arg);
103
        $prop = $refl->getProperty('tokens');
104
        $prop->setAccessible(true);
105
        $tokens = $prop->getValue($arg);
106
        $tokens[] = $file;
107
        $prop->setValue($arg, $tokens);
108
    }
109
}
110