SetSetting   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 7
dl 8
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setPath() 0 4 1
A configure() 8 8 1
B execute() 0 24 4

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\Cli\Command;
4
5
use Magium\Cli\ConfigurationPathInterface;
6
use Magium\NotFoundException;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Zend\Config\Config;
13
use Zend\Config\Writer\Json;
14
15
class SetSetting extends Command implements ConfigurationPathInterface
16
{
17
18
    protected $path;
19
20
    public function setPath($path)
21
    {
22
        $this->path = $path;
23
    }
24
25 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        $this->setName('config:set');
28
        $this->setDescription('Modifies a setting');
29
        $this->addArgument('name', InputArgument::REQUIRED, 'The name of the setting');
30
        $this->addArgument('value', InputArgument::REQUIRED, 'The value of the setting');
31
        $this->addOption('json', null, InputOption::VALUE_NONE, 'If set, this will filter the value through json_decode().');
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        if (!file_exists($this->path . '/magium.json')) {
37
            throw new NotFoundException('Configuration file not found.  Please execute magium:init.');
38
        }
39
        $reader = new \Zend\Config\Reader\Json();
40
        $config = new Config($reader->fromFile($this->path . '/magium.json'), true);
41
42
        $name = $input->getArgument('name');
43
        $value = $input->getArgument('value');
44
        $output->writeln($value);
45
46
        if ($input->getOption('json')) {
47
            $value = json_decode($value);
48
        }
49
        if (!$config->config) {
50
            $config->config = [];
51
        }
52
        $config->config->$name = $value;
53
54
        $writer = new Json();
55
        $writer->toFile($this->path . '/magium.json', $config);
56
        $output->writeln(sprintf('Wrote value for "%s" to %s/magium.json', $name, $this->path));
57
    }
58
59
60
61
}
62
63