ViewCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace N98\Magento\Command\Eav\Attribute;
4
5
use InvalidArgumentException;
6
use N98\Magento\Command\AbstractMagentoCommand;
7
use N98\Util\Console\Helper\Table\Renderer\RendererFactory;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class ViewCommand extends AbstractMagentoCommand
14
{
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('eav:attribute:view')
19
            ->addArgument('entityType', InputArgument::REQUIRED, 'Entity Type Code like catalog_product')
20
            ->addArgument('attributeCode', InputArgument::REQUIRED, 'Attribute Code')
21
            ->setDescription('View informations about an EAV attribute')
22
            ->addOption(
23
                'format',
24
                null,
25
                InputOption::VALUE_OPTIONAL,
26
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
27
            );
28
    }
29
30
    /**
31
     * @param InputInterface  $input
32
     * @param OutputInterface $output
33
     *
34
     * @return int|void
35
     * @throws InvalidArgumentException
36
     */
37
    protected function execute(InputInterface $input, OutputInterface $output)
38
    {
39
        $this->detectMagento($output);
40
        if (!$this->initMagento()) {
41
            return;
42
        }
43
44
        $entityType = $input->getArgument('entityType');
45
        $attributeCode = $input->getArgument('attributeCode');
46
47
        $attribute = $this->getAttribute($entityType, $attributeCode);
48
        if (!$attribute) {
49
            throw new InvalidArgumentException('Attribute was not found.');
50
        }
51
52
        $table = array(
53
            array('ID', $attribute->getId()),
54
            array('Code', $attribute->getName()),
55
            array('Attribute-Set-ID', $attribute->getAttributeSetId()),
56
            array('Visible-On-Front', $attribute->getIsVisibleOnFront() ? 'yes' : 'no'),
57
            array('Attribute-Model', $attribute->getAttributeModel() ? $attribute->getAttributeModel() : ''),
58
            array('Backend-Model', $attribute->getBackendModel() ? $attribute->getBackendModel() : ''),
59
            array('Backend-Table', $attribute->getBackendTable() ? $attribute->getBackendTable() : ''),
60
            array('Backend-Type', $attribute->getBackendType() ? $attribute->getBackendType() : ''),
61
            array('Source-Model', $attribute->getSourceModel() ? $attribute->getSourceModel() : ''),
62
            array('Cache-ID-Tags', $attribute->getCacheIdTags() ? implode(',', $attribute->getCacheIdTags()) : ''),
63
            array('Cache-Tags', $attribute->getCacheTags() ? implode(',', $attribute->getCacheTags()) : ''),
64
            array('Default-Value', $attribute->getDefaultValue() ? $attribute->getDefaultValue() : ''),
65
            array(
66
                'Flat-Columns',
67
                $attribute->getFlatColumns() ? implode(',', array_keys($attribute->getFlatColumns())) : '',
68
            ),
69
            array('Flat-Indexes', $attribute->getFlatIndexes() ? implode(',', $attribute->getFlatIndexes()) : ''),
70
        );
71
72
        if ($attribute->getFrontend()) {
73
            $table[] = array('Frontend-Label', $attribute->getFrontend()->getLabel());
74
            $table[] = array('Frontend-Class', trim($attribute->getFrontend()->getClass()));
75
            $table[] = array('Frontend-Input', trim($attribute->getFrontend()->getInputType()));
76
            $table[] = array(
77
                'Frontend-Input-Renderer-Class',
78
                trim($attribute->getFrontend()->getInputRendererClass()),
79
            );
80
        }
81
82
        $this
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...
83
            ->getHelper('table')
84
            ->setHeaders(array('Type', 'Value'))
85
            ->renderByFormat($output, $table, $input->getOption('format'));
86
    }
87
88
    /**
89
     * @param string $entityType
90
     * @param string $attributeCode
91
     *
92
     * @return \Mage_Eav_Model_Entity_Attribute_Abstract|false
93
     */
94
    protected function getAttribute($entityType, $attributeCode)
95
    {
96
        return \Mage::getModel('eav/config')->getAttribute($entityType, $attributeCode);
97
    }
98
}
99