Completed
Push — master ( 2fa00d...5efdd3 )
by Greg
03:04
created

Base::interaction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace Robo\Task\Composer;
3
4
use Robo\Contract\CommandInterface;
5
use Robo\Exception\TaskException;
6
use Robo\Task\BaseTask;
7
8
abstract class Base extends BaseTask implements CommandInterface
9
{
10
    use \Robo\Common\ExecOneCommand;
11
12
    /**
13
     * @var string
14
     */
15
    protected $command = '';
16
17
    /**
18
     * @var boolena
19
     */
20
    protected $built = false;
21
22
    /**
23
     * @var string
24
     */
25
    protected $prefer;
26
27
    /**
28
     * @var string
29
     */
30
    protected $dev;
31
32
    /**
33
     * @var string
34
     */
35
    protected $ansi;
36
37
    /**
38
     * @var string
39
     */
40
    protected $nointeraction;
41
42
    /**
43
     * Action to use
44
     *
45
     * @var string
46
     */
47
    protected $action = '';
48
49
    /**
50
     * @param null|string $pathToComposer
51
     *
52
     * @throws \Robo\Exception\TaskException
53
     */
54 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...
55
    {
56
        $this->command = $pathToComposer;
57
        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...
58
            $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...
59
        }
60
        if (!$this->command) {
61
            throw new TaskException(__CLASS__, "Neither local composer.phar nor global composer installation could be found.");
62
        }
63
    }
64
65
    /**
66
     * adds `prefer-dist` option to composer
67
     *
68
     * @return $this
69
     */
70
    public function preferDist($preferDist = true)
71
    {
72
        if (!$preferDist) {
73
            return $this->preferSource();
74
        }
75
        $this->prefer = '--prefer-dist';
76
        return $this;
77
    }
78
79
    /**
80
     * adds `prefer-source` option to composer
81
     *
82
     * @return $this
83
     */
84
    public function preferSource()
85
    {
86
        $this->prefer = '--prefer-source';
87
        return $this;
88
    }
89
90
    /**
91
     * adds `dev` option to composer
92
     *
93
     * @return $this
94
     */
95
    public function dev($dev = true)
96
    {
97
        if (!$dev) {
98
            return $this->noDev();
99
        }
100
        $this->dev = '--dev';
101
        return $this;
102
    }
103
104
    /**
105
     * adds `no-dev` option to composer
106
     *
107
     * @return $this
108
     */
109
    public function noDev()
110
    {
111
        $this->dev = '--no-dev';
112
        return $this;
113
    }
114
115
    /**
116
     * adds `ansi` option to composer
117
     *
118
     * @return $this
119
     */
120
    public function ansi($ansi = true)
121
    {
122
        if (!$ansi) {
123
            return $this->noAnsi();
124
        }
125
        $this->ansi = '--ansi';
126
        return $this;
127
    }
128
129
    /**
130
     * adds `no-ansi` option to composer
131
     *
132
     * @return $this
133
     */
134
    public function noAnsi()
135
    {
136
        $this->ansi = '--no-ansi';
137
        return $this;
138
    }
139
140
    public function interaction($interaction = true)
141
    {
142
        if (!$interaction) {
143
            return $this->noInteraction();
144
        }
145
        return $this;
146
    }
147
148
    /**
149
     * adds `no-interaction` option to composer
150
     *
151
     * @return $this
152
     */
153
    public function noInteraction()
154
    {
155
        $this->nointeraction = '--no-interaction';
156
        return $this;
157
    }
158
159
    /**
160
     * adds `optimize-autoloader` option to composer
161
     *
162
     * @return $this
163
     */
164
    public function optimizeAutoloader($optimize = true)
165
    {
166
        if ($optimize) {
167
            $this->option('--optimize-autoloader');
168
        }
169
        return $this;
170
    }
171
172
    /**
173
     * adds `ignore-platform-reqs` option to composer
174
     *
175
     * @return $this
176
     */
177
    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...
178
    {
179
        $this->option('--ignore-platform-reqs');
180
        return $this;
181
    }
182
183
    /**
184
     * disable plugins
185
     *
186
     * @return $this
187
     */
188
    public function disablePlugins($disable = true)
189
    {
190
        if ($disable) {
191
            $this->option('--no-plugins');
192
        }
193
        return $this;
194
    }
195
196
    /**
197
     * adds `--working-dir $dir` option to composer
198
     *
199
     * @return $this
200
     */
201
    public function workingDir($dir)
202
    {
203
        $this->option("--working-dir", $dir);
204
        return $this;
205
    }
206
207
    /**
208
     * Copy class fields into command options as directed.
209
     */
210
    public function buildCommand()
211
    {
212
        if (!isset($this->ansi) && $this->getConfig()->isDecorated()) {
213
            $this->ansi();
214
        }
215
        if (!isset($this->nointeraction) && !$this->getConfig()->isInteractive()) {
216
            $this->noInteraction();
217
        }
218
        $this->option($this->prefer)
219
            ->option($this->dev)
220
            ->option($this->nointeraction)
221
            ->option($this->ansi);
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function getCommand()
228
    {
229
        if (!$this->built) {
230
            $this->buildCommand();
231
            $this->built = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type boolean is incompatible with the declared type object<Robo\Task\Composer\boolena> of property $built.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
232
        }
233
        return "{$this->command} {$this->action}{$this->arguments}";
234
    }
235
}
236