Completed
Push — master ( 3c95a1...ec9ebd )
by juan
02:29
created

ShowProjectsCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Juanber84\Console\Command;
4
5
use Juanber84\Services\DatabaseService;
6
use Juanber84\Texts\ShowProjectsCommandText;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Helper\Table;
11
12
class ShowProjectsCommand extends Command
13
{
14
    const COMMAND_NAME = 'show-projects';
15
    const COMMAND_DESC = 'List Deploy Projects.';
16
17
    private $databaseService;
18
19
    public function __construct(DatabaseService $databaseService)
20
    {
21
        parent::__construct();
22
23
        $this->databaseService = $databaseService;
24
    }
25
26
    protected function configure()
27
    {
28
        $this
29
            ->setName(self::COMMAND_NAME)
30
            ->setDescription(self::COMMAND_DESC);
31
    }
32
33
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35
        $jsonDb = $this->databaseService->getProjects();
36
        if (is_null($jsonDb) || count($jsonDb) == 0) {
37
            $output->writeln('');
38
            $output->writeln('<info>'.ShowProjectsCommandText::OK_0_PROJECTS.'</info>');
39
        } else {
40
            $tableData = [];
41
            foreach ($jsonDb as $k =>$v) {
0 ignored issues
show
Bug introduced by
The expression $jsonDb of type object|integer|double|string|array|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
42
                $tableData[] = [$k,$v];
43
            }
44
45
            $table = new Table($output);
46
            $table
47
                ->setHeaders(array('Name', 'Path'))
48
                ->setRows($tableData)
49
            ;
50
            $table->render();
51
        }
52
    }
53
}