Completed
Push — develop ( 3c1b70...55bb1b )
by Tom
03:03
created

CheckCompatibility::checkCompatibility()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.5559
c 0
b 0
f 0
cc 6
nc 11
nop 1
1
<?php
2
3
namespace N98\Magento\Application\Console\EventSubscriber;
4
5
use N98\Magento\Application;
6
use N98\Magento\Application\ApplicationAwareInterface;
7
use N98\Magento\Application\Console\Event;
8
use N98\Magento\Application\Console\Events;
9
use Symfony\Component\Console\Event\ConsoleEvent;
10
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12
/**
13
 * Class CheckCompatibility
14
 * @package N98\Magento\Application\Console\EventSubscriber
15
 */
16
class CheckCompatibility implements EventSubscriberInterface, ApplicationAwareInterface
17
{
18
    /**
19
     * @var Application
20
     */
21
    private $application;
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
            Events::RUN_BEFORE => 'checkCompatibility',
34
        ];
35
    }
36
37
    /**
38
     * Display a warning if a running n98-magerun as root user
39
     *
40
     * @param ConsoleEvent $event
41
     * @return void
42
     */
43
    public function checkCompatibility(ConsoleEvent $event)
44
    {
45
        if ($event->getInput()->hasParameterOption('--skip-magento-compatibility-check')) {
46
            return;
47
        }
48
49
        try {
50
            $this->application->initMagento(true);
51
            $objectManager = $this->application->getObjectManager();
52
            if (!$objectManager) {
53
                return;
54
            }
55
56
            $productMetadata = $objectManager->get(\Magento\Framework\App\ProductMetadataInterface::class);
57
58
            $currentMagentoVersion = $productMetadata->getVersion();
59
60
            if ($this->isStableVersion($currentMagentoVersion) && version_compare($currentMagentoVersion, '2.3.0', '<')) {
61
                $output = $event->getOutput();
62
                $output->writeln([
0 ignored issues
show
Documentation introduced by
array('', '<error>You ar...gerun2 v2.3.3', '', '') is of type array<integer,string,{"0...string","17":"string"}>, but the function expects a string|object<Symfony\Co...onsole\Output\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
                    '',
64
                    '<error>You are running an incompatible version of n98-magerun2!</error>',
65
                    '<error>Your shop version has to be >2.3.0</error>',
66
                    '',
67
                    '',
68
                    '<comment>Current Magento Version     : ' . $currentMagentoVersion . '</comment>',
69
                    '<comment>Current n98-magerun2 Version: ' . $this->application->getVersion() . '</comment>',
70
                    '',
71
                    '',
72
                    '<info>Please download an older version of n98-magerun2.</info>',
73
                    '',
74
                    '<info>Visit: https://files.magerun.net/old_versions.php</info>',
75
                    '',
76
                    '    Magento 2.2.x => n98-magerun2 v3.2.0',
77
                    '    Magento 2.1.x => n98-magerun2 v3.2.0',
78
                    '    Magento 2.0.x => n98-magerun2 v2.3.3',
79
                    '',
80
                    '',
81
                ]);
82
83
                exit(1);
84
            }
85
        } catch (\Exception $e) {
86
            //
87
        }
88
    }
89
90
    public function setApplication($application)
91
    {
92
        $this->application = $application;
93
    }
94
95
    /**
96
     * @param string $currentMagentoVersion
97
     * @return bool
98
     */
99
    private function isStableVersion(string $currentMagentoVersion): bool
100
    {
101
        return preg_match('/^\d+\.\d+\.\d+$/', $currentMagentoVersion);
102
    }
103
}
104