ConfigurationGet   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 16.92 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 7
dl 11
loc 65
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 11 11 1
A getValueFlag() 0 10 2
A getValue() 0 14 5
A execute() 0 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Magium\Configuration\Console\Command;
4
5
use Magium\Configuration\Config\Repository\ConfigInterface;
6
use Magium\Configuration\Config\Repository\ConfigurationRepository;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class ConfigurationGet extends AbstractCommand
13
{
14
15
    use ConfigurationFactoryTrait;
16
17
    const COMMAND = 'magium:configuration:get';
18
19 8 View Code Duplication
    protected function configure()
20
    {
21
        $this
22 8
            ->setName(self::COMMAND)
23 8
            ->setDescription('Get a configuration value')
24 8
            ->setHelp("This command retrieves a value for a specific configuration path")
25
        ;
26 8
        $this->addOption('use-flag', 'f', InputOption::VALUE_NONE, 'Get value as flag');
27 8
        $this->addArgument('path', InputArgument::REQUIRED, 'Configuration Path');
28 8
        $this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context', ConfigurationRepository::CONTEXT_DEFAULT);
29 8
    }
30
31 2
    protected function getValueFlag(ConfigInterface $config, $path)
32
    {
33 2
        $value = $config->getValueFlag($path);
34 2
        if ($value) {
35 1
            $value = 'flag:true';
36
        } else {
37 1
            $value = 'flag:false';
38
        }
39 2
        return $value;
40
    }
41
42 4
    protected function getValue(ConfigInterface $config, $path)
43
    {
44 4
        $value = $config->getValue($path);
45 4
        if (is_null($value)) {
46 1
            $value = '<null>';
47 3
        } else if (!is_string($value) && !is_numeric($value)) {
48 1
            $type = gettype($value);
49 1
            $value = json_encode($value);
50 1
            $value = sprintf('%s:%s', $type, $value);
51 2
        } else if ($value == '') {
52 1
            $value = '<empty>';
53
        }
54 4
        return $value;
55
    }
56
57 6
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59 6
        $factory = $this->getConfigurationFactory();
60 6
        $manager = $factory->getManager();
61 6
        $path = $input->getArgument('path');
62 6
        $context = $input->getArgument('context');
63 6
        $config = $manager->getConfiguration($context);
64 6
        $useFlag = $input->getOption('use-flag');
65 6
        if ($useFlag) {
66 2
            $value = $this->getValueFlag($config, $path);
67
        } else {
68 4
            $value = $this->getValue($config, $path);
69
        }
70 6
        $out = sprintf("Value for %s (context: %s): %s", $path, $context, $value);
71
72 6
        $output->writeln($out);
73 6
    }
74
75
76
}
77