Passed
Push — master ( 4ab603...30b82a )
by Mauro
02:08
created

DebugCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
3
namespace SimpleDIC\Console;
4
5
use SimpleDIC\DIC;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Helper\Table;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
13
class DebugCommand extends Command
14
{
15
    protected function configure()
16
    {
17
        $this
18
                ->setName('dic:debug')
19
                ->setDescription('Dumps the entry list in the DIC.')
20
                ->setHelp('This command shows you to complete entry list in the DIC from a valid config array.')
21
                ->addArgument('config_file', InputArgument::REQUIRED)
22
        ;
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output)
26
    {
27
        $configFile = $input->getArgument('config_file');
28
29
        if (false === file_exists($configFile)) {
0 ignored issues
show
Bug introduced by
It seems like $configFile can also be of type string[]; however, parameter $filename of file_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        if (false === file_exists(/** @scrutinizer ignore-type */ $configFile)) {
Loading history...
30
            throw new \InvalidArgumentException($configFile . ' is not a valid file');
0 ignored issues
show
Bug introduced by
Are you sure $configFile of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            throw new \InvalidArgumentException(/** @scrutinizer ignore-type */ $configFile . ' is not a valid file');
Loading history...
31
        }
32
33
        DIC::initFromFile($configFile);
0 ignored issues
show
Bug introduced by
It seems like $configFile can also be of type string[]; however, parameter $filename of SimpleDIC\DIC::initFromFile() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        DIC::initFromFile(/** @scrutinizer ignore-type */ $configFile);
Loading history...
34
35
        $keys = DIC::keys();
36
        asort($keys);
37
38
        $table = new Table($output);
39
        $table->setHeaders(['#', 'Alias', 'Content']);
40
41
        $i = 1;
42
        foreach ($keys as $key) {
43
            $table->setRow($i, [$i, $key, $this->getValue($key)]);
44
            $i++;
45
        }
46
47
        $table->render();
48
    }
49
50
    /**
51
     * @param string $key
52
     *
53
     * @return mixed|string
54
     */
55
    private function getValue($key)
56
    {
57
        $dicKey = DIC::get($key);
58
59
        if (false === $dicKey) {
60
            return  '<fg=red>Invalid Entry</>';
61
        }
62
63
        if (is_object($dicKey)) {
64
            return '<fg=cyan>' . get_class($dicKey) . '</>';
65
        }
66
67
        if (is_array($dicKey)) {
68
            return '<fg=green>' . implode("|", $dicKey) . '</>';
69
        }
70
71
        return '<fg=yellow>' . $dicKey . '</>';
72
    }
73
}
74