Completed
Pull Request — master (#586)
by Greg
05:01
created

Base::noInteraction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
     * adds `prefer-dist` option to composer
51
     *
52
     * @return $this
53
     */
54
    public function preferDist()
55
    {
56
        $this->prefer = '--prefer-dist';
57
        return $this;
58
    }
59
60
    /**
61
     * adds `prefer-source` option to composer
62
     *
63
     * @return $this
64
     */
65
    public function preferSource()
66
    {
67
        $this->prefer = '--prefer-source';
68
        return $this;
69
    }
70
71
    /**
72
     * adds `no-dev` option to composer
73
     *
74
     * @return $this
75
     */
76
    public function noDev()
77
    {
78
        $this->dev = '--no-dev';
79
        return $this;
80
    }
81
82
    /**
83
     * adds `dev` option to composer
84
     *
85
     * @return $this
86
     */
87
    public function dev()
88
    {
89
        $this->dev = '--dev';
90
        return $this;
91
    }
92
93
    /**
94
     * adds `no-ansi` option to composer
95
     *
96
     * @return $this
97
     */
98
    public function noAnsi()
99
    {
100
        $this->ansi = '--no-ansi';
101
        return $this;
102
    }
103
104
    /**
105
     * adds `ansi` option to composer
106
     *
107
     * @return $this
108
     */
109
    public function ansi()
110
    {
111
        $this->ansi = '--ansi';
112
        return $this;
113
    }
114
115
    /**
116
     * adds `no-interaction` option to composer
117
     *
118
     * @return $this
119
     */
120
    public function noInteraction()
121
    {
122
        $this->nointeraction = '--no-interaction';
123
    }
124
125
    /**
126
     * adds `optimize-autoloader` option to composer
127
     *
128
     * @return $this
129
     */
130
    public function optimizeAutoloader()
131
    {
132
        $this->option('--optimize-autoloader');
133
        return $this;
134
    }
135
136
    /**
137
     * adds `ignore-platform-reqs` option to composer
138
     *
139
     * @return $this
140
     */
141
    public function ignorePlatformRequirements()
142
    {
143
        $this->option('--ignore-platform-reqs');
144
        return $this;
145
    }
146
147
    /**
148
     * disable plugins
149
     *
150
     * @return $this
151
     */
152
    public function disablePlugins()
153
    {
154
        $this->option('--no-plugins');
155
        return $this;
156
    }
157
158
    /**
159
     * adds `--working-dir $dir` option to composer
160
     *
161
     * @return $this
162
     */
163
    public function workingDir($dir)
164
    {
165
        $this->option("--working-dir", $dir);
166
        return $this;
167
    }
168
169
    /**
170
     * @param null|string $pathToComposer
171
     *
172
     * @throws \Robo\Exception\TaskException
173
     */
174 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...
175
    {
176
        $this->command = $pathToComposer;
177
        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...
178
            $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...
179
        }
180
        if (!$this->command) {
181
            throw new TaskException(__CLASS__, "Neither local composer.phar nor global composer installation could be found.");
182
        }
183
    }
184
185
    /**
186
     * Copy class fields into command options as directed.
187
     */
188
    public function buildCommand()
189
    {
190
        if (!isset($this->ansi) && $this->getConfig()->isDecorated()) {
191
            $this->ansi();
192
        }
193
        if (!isset($this->nointeraction) && !$this->getConfig()->isInteractive()) {
194
            $this->noInteraction();
195
        }
196
        $this->option($this->prefer)
197
            ->option($this->dev)
198
            ->option($this->nointeraction)
199
            ->option($this->ansi);
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function getCommand()
206
    {
207
        if (!$this->built) {
208
            $this->buildCommand();
209
            $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...
210
        }
211
        return "{$this->command} {$this->action}{$this->arguments}";
212
    }
213
}
214