Completed
Push — nln-search-for-todo-variables ( d75af6 )
by Nicolas
04:48
created

Display::displayAsCliOutput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Karma\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Karma\Configuration;
9
use Karma\Command;
10
use Karma\Configuration\ValueFilterIterator;
11
12
class Display extends Command
13
{
14
    const
15
        ENV_DEV = 'dev',
16
        NO_FILTERING = 'karma-nofiltering';
17
18 39
    protected function configure()
19
    {
20 39
        parent::configure();
21
22 39
        $this
23 39
            ->setName('display')
24 39
            ->setDescription('Display environment variable set')
25
26 39
            ->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'Target environment', self::ENV_DEV)
27 39
            ->addOption('value', 'f', InputOption::VALUE_REQUIRED, 'Display only variable with this value', self::NO_FILTERING)
28 39
            ->addOption('json', null, InputOption::VALUE_NONE, 'Display output in json format')
29
        ;
30 39
    }
31
32 4
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34 4
        $json = $input->getOption('json');
35
36 4
        if($json)
37 4
        {
38
            $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
39
        }
40
41 4
        parent::execute($input, $output);
42
43 4
        $environment = $input->getOption('env');
44
45 4
        $this->output->writeln(sprintf(
46 4
            "<info>Display <comment>%s</comment> values</info>\n",
47
            $environment
48 4
        ));
49
50 4
        $reader = $this->app['configuration'];
51 4
        $reader->setDefaultEnvironment($input->getOption('env'));
52
53 4
        $this->displayValues($reader, $input->getOption('value'), $json);
54 4
    }
55
56 4
    private function displayValues(Configuration $reader, $filter = self::NO_FILTERING, $json = false)
57 4
    {
58 4
        $values = new \ArrayIterator($reader->getAllValuesForEnvironment());
59
60 4
        if($filter !== self::NO_FILTERING)
61 4
        {
62 1
            $values = new ValueFilterIterator($filter, $values);
63 1
        }
64
65 4
        $this->output->writeln('');
66
67 4
        $values->ksort();
0 ignored issues
show
Bug introduced by
The method ksort does only exist in ArrayIterator, but not in Karma\Configuration\ValueFilterIterator.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
68
69
        if($json)
70 4
        {
71
            $this->output->setVerbosity(OutputInterface::VERBOSITY_NORMAL);
72
73
            return $this->displayAsJson($values);
74
        }
75
76 4
        return $this->displayAsCliOutput($values);
77
    }
78
79 4
    private function displayAsCliOutput(\Iterator $values)
80
    {
81 4
        foreach($values as $variable => $value)
82
        {
83 4
            $this->output->writeln(sprintf(
84 4
               '<fg=cyan>%s</fg=cyan> = %s',
85 4
                $variable,
86 4
                $this->formatValue($value)
87 4
            ));
88 4
        }
89 4
    }
90
91
    private function displayAsJson(\Iterator $values)
92
    {
93
        $this->output->writeln(
94
            json_encode(iterator_to_array($values))
95
        );
96
    }
97
}
98