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; |
||
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 ListCommand |
||
13 | * @package N98\Magento\Command\System\Store |
||
14 | */ |
||
15 | class ListCommand extends AbstractMagentoCommand |
||
16 | { |
||
17 | /** |
||
18 | * @var \Magento\Store\Model\StoreManagerInterface |
||
19 | */ |
||
20 | protected $storeManager; |
||
21 | |||
22 | protected function configure() |
||
23 | { |
||
24 | $this |
||
25 | ->setName('sys:store:list') |
||
26 | ->setDescription('Lists all installed store-views') |
||
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 \Symfony\Component\Console\Input\InputInterface $input |
||
45 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
46 | * @return int|void |
||
47 | */ |
||
48 | protected function execute(InputInterface $input, OutputInterface $output) |
||
49 | { |
||
50 | foreach ($this->storeManager->getStores() as $store) { |
||
51 | $table[$store->getId()] = [ |
||
0 ignored issues
–
show
|
|||
52 | $store->getId(), |
||
53 | $store->getCode(), |
||
54 | ]; |
||
55 | } |
||
56 | |||
57 | ksort($table); |
||
58 | $this->getHelper('table') |
||
59 | ->setHeaders(['id', 'code']) |
||
60 | ->renderByFormat($output, $table, $input->getOption('format')); |
||
61 | } |
||
62 | } |
||
63 |
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:
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 thebar
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.