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

ListCommand::execute()   C

Complexity

Conditions 10
Paths 33

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 55
rs 6.8372
c 1
b 0
f 0
cc 10
eloc 36
nc 33
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace N98\Magento\Command\Eav\Attribute;
4
5
use Magento\Eav\Model\Attribute;
6
use Magento\Eav\Model\Entity\Type as EntityType;
7
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection as AttributeCollection;
8
use N98\Magento\Command\AbstractMagentoCommand;
9
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class ListCommand extends AbstractMagentoCommand
15
{
16
    /**
17
     * @var AttributeCollection
18
     */
19
    private $attributeCollection;
20
21
    /**
22
     * @param AttributeCollection $attributeCollection
23
     * @return void
24
     */
25
    public function inject(
26
        AttributeCollection $attributeCollection
27
    ) {
28
        $this->attributeCollection = $attributeCollection;
29
    }
30
31
    /**
32
     * @return void
33
     */
34 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
    {
36
        $this
37
            ->setName('eav:attribute:list')
38
            ->addOption(
39
                'add-source',
40
                null,
41
                InputOption::VALUE_NONE,
42
                'Add source models to list'
43
            )
44
            ->addOption(
45
                'add-backend',
46
                null,
47
                InputOption::VALUE_NONE,
48
                'Add backend type to list'
49
            )
50
            ->addOption(
51
                'filter-type',
52
                null,
53
                InputOption::VALUE_OPTIONAL,
54
                'Filter attributes by entity type'
55
            )
56
            ->addOption(
57
                'format',
58
                null,
59
                InputOption::VALUE_OPTIONAL,
60
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
61
            )
62
            ->setDescription('List EAV attributes');
63
    }
64
65
    /**
66
     * @param InputInterface $input
67
     * @param OutputInterface $output
68
     * @return int|void
69
     */
70
    protected function execute(InputInterface $input, OutputInterface $output)
71
    {
72
        $this->detectMagento($output);
73
        if (!$this->initMagento()) {
74
            return;
75
        }
76
77
        $table = [];
78
        $addSource = $input->getOption('add-source');
79
        $addBackend = $input->getOption('add-backend');
80
        $filterType = $input->getOption('filter-type');
81
        $this->attributeCollection->setOrder('attribute_code', 'asc');
82
83
        /** @var Attribute $attribute */
84
        foreach ($this->attributeCollection as $attribute) {
85
            /** @var EntityType $entityType */
86
            $entityType = $attribute->getEntityType();
87
            if ($filterType &&
88
                $entityType->getEntityTypeCode() !== $filterType) {
89
                continue;
90
            }
91
92
            $row = [
93
                $attribute->getAttributeCode(),
94
                $attribute->getId(),
95
                $entityType->getEntityTypeCode() . ' (#' . $entityType->getEntityTypeId() . ')',
96
                $attribute->getFrontendLabel(),
97
            ];
98
            if ($addBackend) {
99
                $row[] = $attribute->getBackendType();
100
            }
101
            if ($addSource) {
102
                $row[] = $attribute->getSourceModel() ? $attribute->getSourceModel() : '';
103
            }
104
105
            $table[] = $row;
106
        }
107
108
        $headers = [
109
            'code',
110
            'id',
111
            'entity_type',
112
            'label',
113
        ];
114
        if ($addBackend) {
115
            $headers[] = 'backend_type';
116
        }
117
        if ($addSource) {
118
            $headers[] = 'source';
119
        }
120
121
        $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...
122
            ->setHeaders($headers)
123
            ->renderByFormat($output, $table, $input->getOption('format'));
124
    }
125
}
126