Completed
Push — master ( a20e35...2a3e96 )
by Greg
03:21
created

src/Task/Composer/Base.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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)
55
    {
56
        $this->command = $pathToComposer;
57
        if (!$this->command) {
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)
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
     * skip scripts
198
     *
199
     * @return $this
200
     */
201
    public function noScripts($disable = true)
202
    {
203
        if ($disable) {
204
            $this->option('--no-scripts');
205
        }
206
        return $this;
207
    }
208
209
    /**
210
     * adds `--working-dir $dir` option to composer
211
     *
212
     * @return $this
213
     */
214
    public function workingDir($dir)
215
    {
216
        $this->option("--working-dir", $dir);
217
        return $this;
218
    }
219
220
    /**
221
     * Copy class fields into command options as directed.
222
     */
223
    public function buildCommand()
224
    {
225
        if (!isset($this->ansi) && $this->getConfig()->get(\Robo\Config\Config::DECORATED)) {
226
            $this->ansi();
227
        }
228
        if (!isset($this->nointeraction) && !$this->getConfig()->get(\Robo\Config\Config::INTERACTIVE)) {
229
            $this->noInteraction();
230
        }
231
        $this->option($this->prefer)
232
            ->option($this->dev)
233
            ->option($this->nointeraction)
234
            ->option($this->ansi);
235
    }
236
237
    /**
238
     * {@inheritdoc}
239
     */
240
    public function getCommand()
241
    {
242
        if (!$this->built) {
243
            $this->buildCommand();
244
            $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...
245
        }
246
        return "{$this->command} {$this->action}{$this->arguments}";
247
    }
248
}
249