ComposerConfigSetting   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 17.02 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 8
loc 47
ccs 23
cts 23
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 8 18 4
A resetConfig() 0 9 1
A retrieveCurrentConfig() 0 4 1

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
namespace Samurai\Project\Task;
3
4
use Samurai\Project\Composer\ComposerConfigMerger;
5
use Samurai\Task\ITask;
6
use Samurai\Task\Task;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class ComposerConfigSetting
12
 * @package Samurai\Project\Task
13
 * @author Raphaël Lefebvre <[email protected]>
14
 */
15
class ComposerConfigSetting extends Task
16
{
17
    /**
18
     * @param InputInterface $input
19
     * @param OutputInterface $output
20
     * @return bool
21
     */
22 4
    public function execute(InputInterface $input, OutputInterface $output)
23
    {
24 4
        $output->writeln('<info>Initializing composer config</info>');
25
26 4
        $this->resetConfig();
27
28 4
        $hasError = false;
29 4 View Code Duplication
        if($this->getService('composer')->validateConfig($this->getService('project')->getDirectoryPath())) {
30 1
            $output->writeln('<error>Error: Composer config is not valid</error>');
31 1
            $hasError = true;
32 1
        }
33 4 View Code Duplication
        if($this->getService('composer')->dumpAutoload($this->getService('project')->getDirectoryPath())) {
34 1
            $output->writeln('<error>Error: autoload is not up-to-date. Process to "composer dump-autoload".</error>');
35 1
            $hasError = true;
36 4
        }
37
38 4
        return $hasError ? ITask::NO_ERROR_CODE : ITask::NON_BLOCKING_ERROR_CODE;
39
    }
40
41
    /**
42
     *
43
     */
44 4
    public function resetConfig()
45
    {
46 4
        $merger = new ComposerConfigMerger();
47 4
        $this->getService('composer')->setConfig(
48 4
            $merger->merge($this->retrieveCurrentConfig(), $this->getService('project')->toConfig()),
49 4
            $this->getService('project')->getDirectoryPath()
50 4
        );
51 4
        $this->getService('composer')->flushConfig($this->getService('project')->getDirectoryPath());
52 4
    }
53
54
    /**
55
     * @return array
56
     */
57 4
    public function retrieveCurrentConfig()
58
    {
59 4
        return $this->getService('composer')->getConfig($this->getService('project')->getDirectoryPath());
60
    }
61
}
62