Base   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 262
Duplicated Lines 3.82 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 4
dl 10
loc 262
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A dev() 0 8 2
A interaction() 0 7 2
A noInteraction() 0 5 1
A ignorePlatformRequirements() 0 5 1
A disablePlugins() 0 7 2
A noScripts() 0 7 2
A workingDir() 0 5 1
A buildCommand() 0 13 5
A __construct() 10 10 3
A preferDist() 0 8 2
A preferSource() 0 5 1
A noDev() 0 5 1
A ansi() 0 8 2
A noAnsi() 0 5 1
A optimizeAutoloader() 0 7 2
A getCommand() 0 8 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 Robo\Task\Composer;
4
5
use Robo\Contract\CommandInterface;
6
use Robo\Exception\TaskException;
7
use Robo\Task\BaseTask;
8
9
abstract class Base extends BaseTask implements CommandInterface
10
{
11
    use \Robo\Common\ExecOneCommand;
12
13
    /**
14
     * @var string
15
     */
16
    protected $command = '';
17
18
    /**
19
     * @var bool
20
     */
21
    protected $built = false;
22
23
    /**
24
     * @var string
25
     */
26
    protected $prefer;
27
28
    /**
29
     * @var string
30
     */
31
    protected $dev;
32
33
    /**
34
     * @var string
35
     */
36
    protected $ansi;
37
38
    /**
39
     * @var string
40
     */
41
    protected $nointeraction;
42
43
    /**
44
     * Action to use
45
     *
46
     * @var string
47
     */
48
    protected $action = '';
49
50
    /**
51
     * @param null|string $pathToComposer
52
     *
53
     * @throws \Robo\Exception\TaskException
54
     */
55 View Code Duplication
    public function __construct($pathToComposer = null)
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...
56
    {
57
        $this->command = $pathToComposer;
58
        if (!$this->command) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->command of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
59
            $this->command = $this->findExecutablePhar('composer');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->findExecutablePhar('composer') can also be of type boolean. However, the property $command is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
60
        }
61
        if (!$this->command) {
62
            throw new TaskException(__CLASS__, "Neither local composer.phar nor global composer installation could be found.");
63
        }
64
    }
65
66
    /**
67
     * adds `prefer-dist` option to composer
68
     *
69
     * @param bool $preferDist
70
     *
71
     * @return $this
72
     */
73
    public function preferDist($preferDist = true)
74
    {
75
        if (!$preferDist) {
76
            return $this->preferSource();
77
        }
78
        $this->prefer = '--prefer-dist';
79
        return $this;
80
    }
81
82
    /**
83
     * adds `prefer-source` option to composer
84
     *
85
     * @return $this
86
     */
87
    public function preferSource()
88
    {
89
        $this->prefer = '--prefer-source';
90
        return $this;
91
    }
92
93
    /**
94
     * adds `dev` option to composer
95
     *
96
     * @param bool $dev
97
     *
98
     * @return $this
99
     */
100
    public function dev($dev = true)
101
    {
102
        if (!$dev) {
103
            return $this->noDev();
104
        }
105
        $this->dev = '--dev';
106
        return $this;
107
    }
108
109
    /**
110
     * adds `no-dev` option to composer
111
     *
112
     * @return $this
113
     */
114
    public function noDev()
115
    {
116
        $this->dev = '--no-dev';
117
        return $this;
118
    }
119
120
    /**
121
     * adds `ansi` option to composer
122
     *
123
     * @param bool $ansi
124
     *
125
     * @return $this
126
     */
127
    public function ansi($ansi = true)
128
    {
129
        if (!$ansi) {
130
            return $this->noAnsi();
131
        }
132
        $this->ansi = '--ansi';
133
        return $this;
134
    }
135
136
    /**
137
     * adds `no-ansi` option to composer
138
     *
139
     * @return $this
140
     */
141
    public function noAnsi()
142
    {
143
        $this->ansi = '--no-ansi';
144
        return $this;
145
    }
146
147
    /**
148
     * @param bool $interaction
149
     *
150
     * @return $this
151
     */
152
    public function interaction($interaction = true)
153
    {
154
        if (!$interaction) {
155
            return $this->noInteraction();
156
        }
157
        return $this;
158
    }
159
160
    /**
161
     * adds `no-interaction` option to composer
162
     *
163
     * @return $this
164
     */
165
    public function noInteraction()
166
    {
167
        $this->nointeraction = '--no-interaction';
168
        return $this;
169
    }
170
171
    /**
172
     * adds `optimize-autoloader` option to composer
173
     *
174
     * @param bool $optimize
175
     *
176
     * @return $this
177
     */
178
    public function optimizeAutoloader($optimize = true)
179
    {
180
        if ($optimize) {
181
            $this->option('--optimize-autoloader');
182
        }
183
        return $this;
184
    }
185
186
    /**
187
     * adds `ignore-platform-reqs` option to composer
188
     *
189
     * @param bool $ignore
190
     *
191
     * @return $this
192
     */
193
    public function ignorePlatformRequirements($ignore = true)
0 ignored issues
show
Unused Code introduced by
The parameter $ignore is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
194
    {
195
        $this->option('--ignore-platform-reqs');
196
        return $this;
197
    }
198
199
    /**
200
     * disable plugins
201
     *
202
     * @param bool $disable
203
     *
204
     * @return $this
205
     */
206
    public function disablePlugins($disable = true)
207
    {
208
        if ($disable) {
209
            $this->option('--no-plugins');
210
        }
211
        return $this;
212
    }
213
214
    /**
215
     * skip scripts
216
     *
217
     * @param bool $disable
218
     *
219
     * @return $this
220
     */
221
    public function noScripts($disable = true)
222
    {
223
        if ($disable) {
224
            $this->option('--no-scripts');
225
        }
226
        return $this;
227
    }
228
229
    /**
230
     * adds `--working-dir $dir` option to composer
231
     *
232
     * @param string $dir
233
     *
234
     * @return $this
235
     */
236
    public function workingDir($dir)
237
    {
238
        $this->option("--working-dir", $dir);
239
        return $this;
240
    }
241
242
    /**
243
     * Copy class fields into command options as directed.
244
     */
245
    public function buildCommand()
246
    {
247
        if (!isset($this->ansi) && $this->getConfig()->get(\Robo\Config\Config::DECORATED)) {
248
            $this->ansi();
249
        }
250
        if (!isset($this->nointeraction) && !$this->getConfig()->get(\Robo\Config\Config::INTERACTIVE)) {
251
            $this->noInteraction();
252
        }
253
        $this->option($this->prefer)
254
            ->option($this->dev)
255
            ->option($this->nointeraction)
256
            ->option($this->ansi);
257
    }
258
259
    /**
260
     * {@inheritdoc}
261
     */
262
    public function getCommand()
263
    {
264
        if (!$this->built) {
265
            $this->buildCommand();
266
            $this->built = true;
267
        }
268
        return "{$this->command} {$this->action}{$this->arguments}";
269
    }
270
}
271