Completed
Push — develop ( 3c1b70...55bb1b )
by Tom
03:03
created

ShowCommand::execute()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 8.867
c 0
b 0
f 0
cc 5
nc 5
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Config\Env;
4
5
use Adbar\Dot;
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
use Symfony\Component\VarDumper\Cloner\VarCloner;
13
use Symfony\Component\VarDumper\Dumper\CliDumper;
14
15
/**
16
 * Class ShowCommand
17
 * @package N98\Magento\Command\Config\Env
18
 */
19
class ShowCommand extends AbstractMagentoCommand
20
{
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('config:env:show')
25
            ->setDescription('List env.php file')
26
            ->addArgument('key', InputArgument::OPTIONAL, 'Key to show.')
27
            ->addOption(
28
                'format',
29
                null,
30
                InputOption::VALUE_OPTIONAL,
31
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
32
            );
33
    }
34
35
    /**
36
     * @param \Symfony\Component\Console\Input\InputInterface $input
37
     * @param \Symfony\Component\Console\Output\OutputInterface $output
38
     * @return int|void
39
     * @throws \Exception
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $this->detectMagento($output);
44
45
        $envFilePath = $this->getApplication()->getMagentoRootFolder() . '/app/etc/env.php';
46
47
        if (!file_exists($envFilePath)) {
48
            throw new \RuntimeException('env.php file does not exist.');
49
        }
50
51
        $keyToShow = $input->getArgument('key');
52
53
        $envConfig = include $envFilePath;
54
        $env = new Dot($envConfig);
55
56
        $cloner = new VarCloner();
57
        $cloner->setMaxItems(-1);
58
        $cloner->setMaxString(-1);
59
        $dumper = new CliDumper();
60
        $dumper->setColors(true);
61
62
        $flattenArray = $env->flatten();
63
64
        ksort($flattenArray);
65
66
        if ($keyToShow !== null) {
67
            if (!isset($flattenArray[$keyToShow])) {
68
                throw new \InvalidArgumentException('Unknown key: ' . $keyToShow);
69
            }
70
71
            $output->writeln($flattenArray[$keyToShow]);
72
        } else {
73
            $table =[];
74
75
            foreach ($flattenArray as $configKey => $configValue) {
76
                $table[] = [
77
                    'key' => $configKey,
78
                    'value' => $configValue,
79
                ];
80
            }
81
82
            $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.

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
                ->setHeaders(['key', 'value'])
84
                ->renderByFormat($output, $table, $input->getOption('format'));
85
        }
86
    }
87
}
88