Completed
Push — develop ( 5d5326...be0e9e )
by Christian
02:27
created

Command/System/Store/Config/BaseUrlListCommand.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace N98\Magento\Command\System\Store\Config;
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
/**
12
 * Class BaseUrlListCommand
13
 * @package N98\Magento\Command\System\Store\Config
14
 */
15
class BaseUrlListCommand extends AbstractMagentoCommand
16
{
17
    /**
18
     * @var \Magento\Framework\Store\StoreManagerInterface
19
     */
20
    protected $storeManager;
21
22
    protected function configure()
23
    {
24
        $this
25
            ->setName('sys:store:config:base-url:list')
26
            ->setDescription('Lists all base urls')
27
            ->addOption(
28
                'format',
29
                null,
30
                InputOption::VALUE_OPTIONAL,
31
                'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']'
32
            );
33
    }
34
35
    /**
36
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
37
     */
38
    public function inject(\Magento\Store\Model\StoreManagerInterface $storeManager)
39
    {
40
        $this->storeManager = $storeManager;
41
    }
42
43
    /**
44
     * @param InputInterface $input
45
     * @param OutputInterface $output
46
     * @return void
47
     * @throws \Exception
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $this->detectMagento($output, true);
52
53
        if (!$input->getOption('format')) {
54
            $this->writeSection($output, 'Magento Stores - Base URLs');
55
        }
56
        $this->initMagento();
57
58
        foreach ($this->storeManager->getStores() as $store) {
59
            $table[$store->getId()] = [
0 ignored issues
show
Coding Style Comprehensibility introduced by
$table was never initialized. Although not strictly required by PHP, it is generally a good practice to add $table = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
60
                $store->getId(),
61
                $store->getCode(),
62
                $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB),
63
                $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB, true),
64
            ];
65
        }
66
67
        ksort($table);
68
        $this->getHelper('table')
69
            ->setHeaders(['id', 'code', 'unsecure_baseurl', 'secure_baseurl'])
70
            ->renderByFormat($output, $table, $input->getOption('format'));
71
    }
72
}
73