Completed
Push — develop ( 73bd0a...ccb498 )
by Tom
05:04
created

ModuleCommand::setCurrentModuleContext()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 3
Metric Value
c 4
b 1
f 3
dl 0
loc 29
rs 8.439
cc 6
eloc 16
nc 16
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Developer\Console;
4
5
use Magento\Framework\Module\ModuleListInterface;
6
use N98\Magento\Command\Developer\Console\Structure\ModuleNameStructure;
7
use N98\Util\BinaryString;
8
use Psy\VarDumper\Presenter;
9
use Psy\VarDumper\PresenterAware;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class ModuleCommand extends AbstractGeneratorCommand implements PresenterAware
15
{
16
    /**
17
     * @var Presenter
18
     */
19
    private $presenter;
20
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('module')
25
            ->addArgument('module', InputArgument::OPTIONAL)
26
            ->setAliases(['mod'])
27
            ->setDescription('Set current module context');
28
    }
29
30
    /**
31
     * PresenterAware interface.
32
     *
33
     * @param Presenter $presenter
34
     */
35
    public function setPresenter(Presenter $presenter)
36
    {
37
        $this->presenter = $presenter;
38
    }
39
40
    /**
41
     * @param InputInterface $input
42
     * @param OutputInterface $output
43
     *
44
     * @return int|void
45
     */
46
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48
        $module = $input->getArgument('module');
49
50
        if (!empty($module)) {
51
            $moduleName = new ModuleNameStructure($module);
52
            $this->setCurrentModuleContext($output, $moduleName);
53
        } else {
54
            try {
55
                $module = $this->getCurrentModuleName();
56
                $output->writeln('<info>Current module </info><comment>' . $module . '</comment>');
57
            } catch (\InvalidArgumentException $e) {
58
                $output->writeln('<info>No module context defined</info>');
59
            }
60
        }
61
    }
62
63
    /**
64
     * @param OutputInterface $output
65
     * @param ModuleNameStructure $moduleName
66
     */
67
    protected function setCurrentModuleContext(OutputInterface $output, ModuleNameStructure $moduleName)
68
    {
69
        $moduleList = $this->create(ModuleListInterface::class);
70
        /** @var $moduleList ModuleListInterface */
71
72
        $detectedModule = $moduleList->getOne($moduleName->getFullModuleName());
73
74
        if (is_array($detectedModule)) {
75
            $detectedModule = $detectedModule['name'];
76
        }
77
78
        if (!$detectedModule) {
79
            // Try to load first matching module
80
            foreach ($moduleList->getAll() as $moduleListItem) {
81
                if (BinaryString::startsWith($moduleListItem['name'], $moduleName->getFullModuleName())) {
82
                    $detectedModule = $moduleListItem['name'];
83
                    break;
84
                }
85
            }
86
        }
87
88
        if ($detectedModule) {
89
            $this->setCurrentModuleName($detectedModule);
90
            $output->writeln('<info>Use module </info><comment>' . $detectedModule . '</comment>');
91
            $this->getApplication()->setPrompt('Module: ' . $detectedModule . ' >>> ');
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method setPrompt() does only exist in the following sub-classes of Symfony\Component\Console\Application: N98\Magento\Command\Developer\Console\Shell. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
92
        } else {
93
            $output->writeln('<error>Invalid module</error>');
94
        }
95
    }
96
}
97