Completed
Push — master ( 5017d8...561e2a )
by Christian
07:36 queued 03:33
created

ListCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 55
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A inject() 0 4 1
A configure() 0 13 1
A execute() 0 20 3
1
<?php
2
3
namespace N98\Magento\Command\SearchEngine;
4
5
use N98\Magento\Command\AbstractMagentoCommand;
6
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class ListCommand extends AbstractMagentoCommand
12
{
13
    /**
14
     * @var \Magento\Search\Model\Adminhtml\System\Config\Source\Engine
15
     */
16
    private $searchEngineConfig;
17
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('search:engine:list')
22
            ->setDescription('Lists all registered search engines')
23
            ->addOption(
24
                'format',
25
                null,
26
                InputOption::VALUE_OPTIONAL,
27
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
28
            )
29
        ;
30
    }
31
32
    /**
33
     * @param \Magento\Search\Model\Adminhtml\System\Config\Source\Engine $searchEngineConfig
34
     */
35
    public function inject(\Magento\Search\Model\Adminhtml\System\Config\Source\Engine $searchEngineConfig)
36
    {
37
        $this->searchEngineConfig = $searchEngineConfig;
38
    }
39
40
    /**
41
     * @param \Symfony\Component\Console\Input\InputInterface $input
42
     * @param \Symfony\Component\Console\Output\OutputInterface $output
43
     * @return int|void
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        $this->detectMagento($output, true);
48
        if (!$this->initMagento()) {
49
            return;
50
        }
51
52
        $searchEngines = $this->searchEngineConfig->toOptionArray();
53
54
        $table = array();
55
        foreach ($searchEngines as $searchEngine) {
56
            $table[] = [
57
                $searchEngine['value'],
58
                $searchEngine['label'],
59
            ];
60
        }
61
        $this->getHelper('table')
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method setHeaders() does only exist in the following implementations of said interface: N98\Util\Console\Helper\TableHelper, Symfony\Component\Console\Helper\TableHelper.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
62
            ->setHeaders(array('code', 'label'))
63
            ->renderByFormat($output, $table, $input->getOption('format'));
64
    }
65
}
66