Completed
Push — feature/merge-build-stop-step ( d59daf )
by Tom
8s
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 N98\Magento\Command\Developer\Console\Structure\ModuleNameStructure;
6
use N98\Util\BinaryString;
7
use Psy\VarDumper\Presenter;
8
use Psy\VarDumper\PresenterAware;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Magento\Framework\Module\ModuleListInterface;
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
    /**
32
     * PresenterAware interface.
33
     *
34
     * @param Presenter $presenter
35
     */
36
    public function setPresenter(Presenter $presenter)
37
    {
38
        $this->presenter = $presenter;
39
    }
40
41
    /**
42
     * @param InputInterface  $input
43
     * @param OutputInterface $output
44
     *
45
     * @return int|void
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $module = $input->getArgument('module');
50
51
        if (!empty($module)) {
52
            $moduleName = new ModuleNameStructure($module);
53
            $this->setCurrentModuleContext($output, $moduleName);
54
        } else {
55
            try {
56
                $module = $this->getCurrentModuleName();
57
                $output->writeln('<info>Current module </info><comment>' . $module . '</comment>');
58
            } catch (\InvalidArgumentException $e) {
59
                $output->writeln('<info>No module context defined</info>');
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param OutputInterface $output
66
     * @param ModuleNameStructure $moduleName
67
     */
68
    protected function setCurrentModuleContext(OutputInterface $output, ModuleNameStructure $moduleName)
69
    {
70
        $moduleList = $this->create(ModuleListInterface::class);
71
        /** @var $moduleList ModuleListInterface */
72
73
        $detectedModule = $moduleList->getOne($moduleName->getFullModuleName());
74
75
        if (is_array($detectedModule)) {
76
            $detectedModule = $detectedModule['name'];
77
        }
78
79
        if (!$detectedModule) {
80
            // Try to load first matching module
81
            foreach ($moduleList->getAll() as $moduleListItem) {
82
                if (BinaryString::startsWith($moduleListItem['name'], $moduleName->getFullModuleName())) {
83
                    $detectedModule = $moduleListItem['name'];
84
                    break;
85
                }
86
            }
87
        }
88
89
        if ($detectedModule) {
90
            $this->setCurrentModuleName($detectedModule);
91
            $output->writeln('<info>Use module </info><comment>' . $detectedModule . '</comment>');
92
            $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...
93
        } else {
94
            $output->writeln('<error>Invalid module</error>');
95
        }
96
    }
97
}