AptCommandBuilder   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 0
dl 100
loc 100
ccs 28
cts 28
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A install() 11 11 2
A update() 7 7 2
A uninstall() 11 11 2
A assumeYes() 6 6 1
A quiet() 6 6 1
A getCommand() 12 12 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
/**
3
 * This file is part of RoboSystemPackage.
4
 *
5
 * @author      Aitor García Martínez (Falc) <[email protected]>
6
 * @copyright   2015 Aitor García Martínez (Falc) <[email protected]>
7
 * @license     MIT
8
 */
9
10
namespace Falc\Robo\Package\CommandBuilder;
11
12
/**
13
 * Apt command builder.
14
 */
15 View Code Duplication
class AptCommandBuilder implements CommandBuilderInterface
0 ignored issues
show
Duplication introduced by
This class 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...
16
{
17
    /**
18
     * Command action.
19
     *
20
     * @var string
21
     */
22
    protected $action;
23
24
    /**
25
     * Option list.
26
     *
27
     * @var string[]
28
     */
29
    protected $options = [];
30
31
    /**
32
     * Package list.
33
     *
34
     * @var string[]
35
     */
36
    protected $packages = [];
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 5
    public function install(array $packages)
42
    {
43 5
        if (empty($packages)) {
44 1
            throw new \Exception('No packages selected to be installed');
45
        }
46
47 4
        $this->packages = $packages;
48 4
        $this->action = 'install';
49
50 4
        return $this;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 3
    public function update(array $packages = [])
57
    {
58 3
        $this->packages = $packages;
59 3
        $this->action = empty($packages) ? 'upgrade' : 'install --only-upgrade';
60
61 3
        return $this;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 3
    public function uninstall(array $packages)
68
    {
69 3
        if (empty($packages)) {
70 1
            throw new \Exception('No packages selected to be uninstalled');
71
        }
72
73 2
        $this->packages = $packages;
74 2
        $this->action = 'remove';
75
76 2
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function assumeYes()
83
    {
84 1
        $this->options[] = '-y';
85
86 1
        return $this;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 1
    public function quiet()
93
    {
94 1
        $this->options[] = '-qq';
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 9
    public function getCommand()
103
    {
104 9
        $packages = implode(' ', array_unique($this->packages));
105 9
        $options = implode(' ', array_unique($this->options));
106
107 9
        $command = "apt-get {$options} {$this->action} {$packages}";
108
109
        // Remove extra whitespaces
110 9
        $command = preg_replace('/\s+/', ' ', trim($command));
111
112 9
        return $command;
113
    }
114
}
115