Passed
Push — nln-php7 ( 5c4f20...6ce259 )
by Nicolas
03:34
created

Display::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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