Completed
Push — master ( ed1fe8...e8e5d3 )
by Greg
8s
created

Application   C

Complexity

Total Complexity 58

Size/Duplication

Total Lines 494
Duplicated Lines 3.64 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 58
lcom 1
cbo 2
dl 18
loc 494
rs 6.3005
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 18 3
C getHelpArgValue() 0 23 9
A help() 0 13 2
A setOutputFile() 0 4 1
A parseArgvAndGetCommandList() 0 8 1
A separateProjectAndGetCommandList() 0 13 2
A runCommandList() 0 10 3
A getCommandsToExec() 0 13 2
A getDefaultOptionValues() 0 10 1
A overlayEnvironmentValues() 0 12 3
A parseOutOurOptions() 0 16 4
C separateProjectsFromArgs() 0 48 8
A requireCommand() 0 9 2
A generalCommand() 0 14 2
A configureProjectStability() 0 18 3
A infoCommand() 9 9 2
A updateCommand() 9 9 2
A flipProjectsArray() 0 6 1
A projectWithVersion() 0 7 2
A buildGlobalCommand() 0 6 1
A buildConfigCommand() 0 6 1
A isComposerVersion() 0 9 3

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Application often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Application, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Consolidation\Cgr;
4
5
/**
6
 * Note that this command is deliberately written using only php-native
7
 * libraries, and no external dependencies whatsoever, so that it may
8
 * be installed via `composer global require` without causing any conflicts
9
 * with any other project.
10
 *
11
 * This technique is NOT recommended for other tools. Use Symfony Console
12
 * directly, or, better yet, use Robo (http://robo.li) as a framework.
13
 * See: http://robo.li/framework/
14
 */
15
class Application
16
{
17
    protected $outputFile = '';
18
19
    /**
20
     * Run the cgr tool, a safer alternative to `composer global require`.
21
     *
22
     * @param array $argv The global $argv array passed in by PHP
23
     * @param string $home The path to the composer home directory
24
     * @return integer
25
     */
26
    public function run($argv, $home)
27
    {
28
        $optionDefaultValues = $this->getDefaultOptionValues($home);
29
        $optionDefaultValues = $this->overlayEnvironmentValues($optionDefaultValues);
30
31
        list($argv, $options) = $this->parseOutOurOptions($argv, $optionDefaultValues);
32
33
        $helpArg = $this->getHelpArgValue($argv);
34
        if (!empty($helpArg)) {
35
            return $this->help($helpArg);
36
        }
37
38
        $commandList = $this->separateProjectAndGetCommandList($argv, $home, $options);
39
        if (empty($commandList)) {
40
            return 1;
41
        }
42
        return $this->runCommandList($commandList, $options);
0 ignored issues
show
Documentation introduced by
$commandList is of type object<Consolidation\Cgr\CommandToExec>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
    }
44
45
    /**
46
     * Returns the first argument after `help`, or the
47
     * first argument if `--help` is present. Otherwise,
48
     * returns an empty string.
49
     */
50
    public function getHelpArgValue($argv)
51
    {
52
        $hasHelp = false;
53
        $helpArg = '';
54
55
        foreach ($argv as $arg) {
56
            if (($arg == 'help') || ($arg == '--help') || ($arg == '-h')) {
57
                $hasHelp = true;
58
            } elseif (($arg[0] != '-') && empty($helpArg)) {
59
                $helpArg = $arg;
60
            }
61
        }
62
63
        if (!$hasHelp) {
64
            return false;
65
        }
66
67
        if (empty($helpArg)) {
68
            return 'help';
69
        }
70
71
        return $helpArg;
72
    }
73
74
    public function help($helpArg)
75
    {
76
        $helpFile = dirname(__DIR__) . '/help/' . $helpArg;
77
78
        if (!file_exists($helpFile)) {
79
            print "No help available for '$helpArg'\n";
80
            return 1;
81
        }
82
83
        $helpContents = file_get_contents($helpFile);
84
        print $helpContents;
85
        return 0;
86
    }
87
88
    /**
89
     * Set up output redirection. Used by tests.
90
     */
91
    public function setOutputFile($outputFile)
92
    {
93
        $this->outputFile = $outputFile;
94
    }
95
96
    /**
97
     * Figure out everything we're going to do, but don't do any of it
98
     * yet, just return the command objects to run.
99
     */
100
    public function parseArgvAndGetCommandList($argv, $home)
101
    {
102
        $optionDefaultValues = $this->getDefaultOptionValues($home);
103
        $optionDefaultValues = $this->overlayEnvironmentValues($optionDefaultValues);
104
105
        list($argv, $options) = $this->parseOutOurOptions($argv, $optionDefaultValues);
106
        return $this->separateProjectAndGetCommandList($argv, $home, $options);
107
    }
108
109
    /**
110
     * Figure out everything we're going to do, but don't do any of it
111
     * yet, just return the command objects to run.
112
     */
113
    public function separateProjectAndGetCommandList($argv, $home, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $home 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...
114
    {
115
        list($command, $projects, $composerArgs) = $this->separateProjectsFromArgs($argv, $options);
116
117
        // If command was unknown, then exit with an error message
118
        if (empty($command)) {
119
            print "Unknown command: " . implode(' ', $composerArgs) . "\n";
120
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method separateProjectAndGetCommandList() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
121
        }
122
123
        $commandList = $this->getCommandsToExec($command, $composerArgs, $projects, $options);
124
        return $commandList;
125
    }
126
127
    /**
128
     * Run all of the commands in a list.  Abort early if any fail.
129
     *
130
     * @param array $commandList An array of CommandToExec
131
     * @return integer
132
     */
133
    public function runCommandList($commandList, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options 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...
134
    {
135
        foreach ($commandList as $command) {
136
            $exitCode = $command->run($this->outputFile);
137
            if ($exitCode) {
138
                return $exitCode;
139
            }
140
        }
141
        return 0;
142
    }
143
144
    /**
145
     * Return an array containing a list of commands to execute.  Depending on
146
     * the composition of the aguments and projects parameters, this list will
147
     * contain either a single command string to call through to composer (if
148
     * cgr is being used as a composer alias), or it will contain a list of
149
     * appropriate replacement 'composer global require' commands that install
150
     * each project in its own installation directory, while installing each
151
     * projects' binaries in the global Composer bin directory,
152
     * ~/.composer/vendor/bin.
153
     *
154
     * @param array $composerArgs
155
     * @param array $projects
156
     * @param array $options
157
     * @return CommandToExec
158
     */
159
    public function getCommandsToExec($command, $composerArgs, $projects, $options)
160
    {
161
        $execPath = $options['composer-path'];
162
163
        // Call requireCommand, updateCommand, or removeCommand, as appropriate.
164
        $methodName = "{$command}Command";
165
        if (method_exists($this, $methodName)) {
166
            return $this->$methodName($execPath, $composerArgs, $projects, $options);
167
        } else {
168
            // If there is no specific implementation for the requested command, then call 'generalCommand'.
169
            return $this->generalCommand($command, $execPath, $composerArgs, $projects, $options);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->generalCom..., $projects, $options); (array) is incompatible with the return type documented by Consolidation\Cgr\Application::getCommandsToExec of type Consolidation\Cgr\CommandToExec.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
170
        }
171
    }
172
173
    /**
174
     * Return our list of default option values, with paths relative to
175
     * the provided home directory.
176
     * @param string $home The composer home directory
177
     * @return array
178
     */
179
    public function getDefaultOptionValues($home)
180
    {
181
        return array(
182
            'composer' => false,
183
            'composer-path' => 'composer',
184
            'base-dir' => "$home/global",
185
            'bin-dir' => "$home/vendor/bin",
186
            'stability' => false,
187
        );
188
    }
189
190
    /**
191
     * Replace option default values with the corresponding
192
     * environment variable value, if it is set.
193
     */
194
    protected function overlayEnvironmentValues($defaults)
195
    {
196
        foreach ($defaults as $key => $value) {
197
            $envKey = 'CGR_' . strtoupper(strtr($key, '-', '_'));
198
            $envValue = getenv($envKey);
199
            if ($envValue) {
200
                $defaults[$key] = $envValue;
201
            }
202
        }
203
204
        return $defaults;
205
    }
206
207
    /**
208
     * We use our own special-purpose argv parser. The options that apply
209
     * to this tool are identified by a simple associative array, where
210
     * the key is the option name, and the value is its default value.
211
     * The result of this function is an array of two items containing:
212
     *  - An array of the items in $argv not used to set an option value
213
     *  - An array of options containing the user-specified or default values
214
     *
215
     * @param array $argv The global $argv passed in by php
216
     * @param array $optionDefaultValues An associative array
217
     * @return array
218
     */
219
    public function parseOutOurOptions($argv, $optionDefaultValues)
220
    {
221
        $argv0 = array_shift($argv);
222
        $options['composer'] = (strpos($argv0, 'composer') !== false);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
223
        $passAlongArgvItems = array();
224
        $options = array();
225
        while (!empty($argv)) {
226
            $arg = array_shift($argv);
227
            if ((substr($arg, 0, 2) == '--') && array_key_exists(substr($arg, 2), $optionDefaultValues)) {
228
                $options[substr($arg, 2)] = array_shift($argv);
229
            } else {
230
                $passAlongArgvItems[] = $arg;
231
            }
232
        }
233
        return array($passAlongArgvItems, $options + $optionDefaultValues);
234
    }
235
236
    /**
237
     * After our options are removed by parseOutOurOptions, those items remaining
238
     * in $argv will be separated into a list of projects and versions, and
239
     * anything else that is not a project:version. Returns an array of two
240
     * items containing:
241
     *  - An associative array, where the key is the project name and the value
242
     *    is the version (or an empty string, if no version was specified)
243
     *  - The remaining $argv items not used to build the projects array.
244
     *
245
     * @param array $argv The $argv array from parseOutOurOptions()
246
     * @return array
247
     */
248
    public function separateProjectsFromArgs($argv, $options)
249
    {
250
        $cgrCommands = array('info', 'require', 'update', 'remove');
251
        $command = 'require';
252
        $composerArgs = array();
253
        $projects = array();
254
        $globalMode = !$options['composer'];
255
        foreach ($argv as $arg) {
256
            if ($arg[0] == '-') {
257
                // Any flags (first character is '-') will just be passed
258
                // through to to composer. Flags interpreted by cgr have
259
                // already been removed from $argv.
260
                $composerArgs[] = $arg;
261
            } elseif (strpos($arg, '/') !== false) {
262
                // Arguments containing a '/' name projects.  We will split
263
                // the project from its version, allowing the separator
264
                // character to be either a '=' or a ':', and then store the
265
                // result in the $projects array.
266
                $projectAndVersion = explode(':', strtr($arg, '=', ':'), 2) + array('', '');
267
                list($project, $version) = $projectAndVersion;
268
                $projects[$project] = $version;
269
            } elseif ($this->isComposerVersion($arg)) {
270
                // If an argument is a composer version, then we will alter
271
                // the last project we saw, attaching this version to it.
272
                // This allows us to handle 'a/b:1.0' and 'a/b 1.0' equivalently.
273
                $keys = array_keys($projects);
274
                $lastProject = array_pop($keys);
275
                unset($projects[$lastProject]);
276
                $projects[$lastProject] = $arg;
277
            } elseif ($arg == 'global') {
278
                // Make note if we see the 'global' command.
279
                $globalMode = true;
280
            } else {
281
                // If we see any command other than 'global [require|update|remove]',
282
                // then we will pass *all* of the arguments through to
283
                // composer unchanged. We return an empty projects array
284
                // to indicate that this should be a pass-through call
285
                // to composer, rather than one or more calls to
286
                // 'composer require' to install global projects.
287
                if ((!$globalMode) || (!in_array($arg, $cgrCommands))) {
288
                    return array('', array(), $argv);
289
                }
290
                // Remember which command we saw
291
                $command = $arg;
292
            }
293
        }
294
        return array($command, $projects, $composerArgs);
295
    }
296
297
    /**
298
     * Provide a safer version of `composer global require`.  Each project
299
     * listed in $projects will be installed into its own project directory.
300
     * The binaries from each project will still be placed in the global
301
     * composer bin directory.
302
     *
303
     * @param string $execPath The path to composer
304
     * @param array $composerArgs Anything from the global $argv to be passed
305
     *   on to Composer
306
     * @param array $projects A list of projects to install, with the key
307
     *   specifying the project name, and the value specifying its version.
308
     * @param array $options User options from the command line; see
309
     *   $optionDefaultValues in the main() function.
310
     * @return array
311
     */
312
    public function requireCommand($execPath, $composerArgs, $projects, $options)
313
    {
314
        $stabilityCommands = array();
315
        if ($options['stability']) {
316
            $stabilityCommands = $this->configureProjectStability($execPath, $composerArgs, $projects, $options);
317
        }
318
        $requireCommands = $this->generalCommand('require', $execPath, $composerArgs, $projects, $options);
319
        return array_merge($stabilityCommands, $requireCommands);
320
    }
321
322
    /**
323
     * General command handler.
324
     *
325
     * @param string $composerCommand The composer command to run e.g. require
326
     * @param string $execPath The path to composer
327
     * @param array $composerArgs Anything from the global $argv to be passed
328
     *   on to Composer
329
     * @param array $projects A list of projects to install, with the key
330
     *   specifying the project name, and the value specifying its version.
331
     * @param array $options User options from the command line; see
332
     *   $optionDefaultValues in the main() function.
333
     * @return array
334
     */
335
    public function generalCommand($composerCommand, $execPath, $composerArgs, $projects, $options)
336
    {
337
        $globalBaseDir = $options['base-dir'];
338
        $binDir = $options['bin-dir'];
339
        $env = array("COMPOSER_BIN_DIR" => $binDir);
340
        $result = array();
341
        foreach ($projects as $project => $version) {
342
            $installLocation = "$globalBaseDir/$project";
343
            $projectWithVersion = $this->projectWithVersion($project, $version);
344
            $commandToExec = $this->buildGlobalCommand($composerCommand, $execPath, $composerArgs, $projectWithVersion, $env, $installLocation);
345
            $result[] = $commandToExec;
346
        }
347
        return $result;
348
    }
349
350
    /**
351
     * If --stability VALUE is provided, then run a `composer config minimum-stability VALUE`
352
     * command to configure composer.json appropriately.
353
     *
354
     * @param string $execPath The path to composer
355
     * @param array $composerArgs Anything from the global $argv to be passed
356
     *   on to Composer
357
     * @param array $projects A list of projects to install, with the key
358
     *   specifying the project name, and the value specifying its version.
359
     * @param array $options User options from the command line; see
360
     *   $optionDefaultValues in the main() function.
361
     * @return array
362
     */
363
    public function configureProjectStability($execPath, $composerArgs, $projects, $options)
364
    {
365
        $globalBaseDir = $options['base-dir'];
366
        $stability = $options['stability'];
367
        $result = array();
368
        $env = array();
369
370
        foreach ($projects as $project => $version) {
371
            $installLocation = "$globalBaseDir/$project";
372
            FileSystemUtils::mkdirParents($installLocation);
373
            if (!file_exists("$installLocation/composer.json")) {
374
                file_put_contents("$installLocation/composer.json", '{}');
375
            }
376
            $result[] = $this->buildConfigCommand($execPath, $composerArgs, 'minimum-stability', $stability, $env, $installLocation);
377
        }
378
379
        return $result;
380
    }
381
382
    /**
383
     * Run `composer info`. Not only do we want to display the information of
384
     * the "global" Composer project, we also want to get the infomation of
385
     * all the "isolated" projects installed via cgr in ~/.composer/global.
386
     *
387
     * @param string $command The path to composer
0 ignored issues
show
Bug introduced by
There is no parameter named $command. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
388
     * @param array $composerArgs Anything from the global $argv to be passed
389
     *   on to Composer
390
     * @param array $projects A list of projects to update.
391
     * @param array $options User options from the command line; see
392
     *   $optionDefaultValues in the main() function.
393
     * @return array
394
     */
395 View Code Duplication
    public function infoCommand($execPath, $composerArgs, $projects, $options)
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...
396
    {
397
        // If 'projects' list is empty, make a list of everything currently installed
398
        if (empty($projects)) {
399
            $projects = FileSystemUtils::allInstalledProjectsInBaseDir($options['base-dir']);
400
            $projects = $this->flipProjectsArray($projects);
401
        }
402
        return $this->generalCommand('info', $execPath, $composerArgs, $projects, $options);
403
    }
404
405
406
    /**
407
     * Run `composer global update`. Not only do we want to update the
408
     * "global" Composer project, we also want to update all of the
409
     * "isolated" projects installed via cgr in ~/.composer/global.
410
     *
411
     * @param string $command The path to composer
0 ignored issues
show
Bug introduced by
There is no parameter named $command. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
412
     * @param array $composerArgs Anything from the global $argv to be passed
413
     *   on to Composer
414
     * @param array $projects A list of projects to update.
415
     * @param array $options User options from the command line; see
416
     *   $optionDefaultValues in the main() function.
417
     * @return array
418
     */
419 View Code Duplication
    public function updateCommand($execPath, $composerArgs, $projects, $options)
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...
420
    {
421
        // If 'projects' list is empty, make a list of everything currently installed
422
        if (empty($projects)) {
423
            $projects = FileSystemUtils::allInstalledProjectsInBaseDir($options['base-dir']);
424
            $projects = $this->flipProjectsArray($projects);
425
        }
426
        return $this->generalCommand('update', $execPath, $composerArgs, $projects, $options);
427
    }
428
429
    /**
430
     * Convert from an array of projects to an array where the key is the
431
     * project name, and the value (version) is an empty string.
432
     *
433
     * @param string[] $projects
434
     * @return array
435
     */
436
    public function flipProjectsArray($projects)
437
    {
438
        return array_map(function () {
439
            return '';
440
        }, array_flip($projects));
441
    }
442
443
    /**
444
     * Return $project:$version, or just $project if there is no $version.
445
     *
446
     * @param string $project The project to install
447
     * @param string $version The version desired
448
     * @return string
449
     */
450
    public function projectWithVersion($project, $version)
451
    {
452
        if (empty($version)) {
453
            return $project;
454
        }
455
        return "$project:$version";
456
    }
457
458
    /**
459
     * Generate command string to call `composer COMMAND` to install one project.
460
     *
461
     * @param string $command The path to composer
0 ignored issues
show
Documentation introduced by
There is no parameter named $command. Did you maybe mean $composerCommand?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
462
     * @param array $composerArgs The arguments to pass to composer
463
     * @param string $projectWithVersion The project:version to install
464
     * @param array $env Environment to set prior to exec
465
     * @param string $installLocation Location to install the project
466
     * @return CommandToExec
467
     */
468
    public function buildGlobalCommand($composerCommand, $execPath, $composerArgs, $projectWithVersion, $env, $installLocation)
469
    {
470
        $projectSpecificArgs = array("--working-dir=$installLocation", $composerCommand, $projectWithVersion);
471
        $arguments = array_merge($composerArgs, $projectSpecificArgs);
472
        return new CommandToExec($execPath, $arguments, $env, $installLocation);
473
    }
474
475
    /**
476
     * Generate command string to call `composer config KEY VALUE` to install one project.
477
     *
478
     * @param string $execPath The path to composer
479
     * @param array $composerArgs The arguments to pass to composer
480
     * @param string $key The config item to set
481
     * @param string $value The value to set the config item to
482
     * @param array $env Environment to set prior to exec
483
     * @param string $installLocation Location to install the project
484
     * @return CommandToExec
485
     */
486
    public function buildConfigCommand($execPath, $composerArgs, $key, $value, $env, $installLocation)
487
    {
488
        $projectSpecificArgs = array("--working-dir=$installLocation", 'config', $key, $value);
489
        $arguments = array_merge($composerArgs, $projectSpecificArgs);
490
        return new CommandToExec($execPath, $arguments, $env, $installLocation);
491
    }
492
493
    /**
494
     * Identify an argument that could be a Composer version string.
495
     *
496
     * @param string $arg The argument to test
497
     * @return boolean
498
     */
499
    public function isComposerVersion($arg)
500
    {
501
        // Allow for 'dev-master', et. al.
502
        if (substr($arg, 0, 4) == 'dev-') {
503
            return true;
504
        }
505
        $specialVersionChars = array('^', '~', '<', '>');
506
        return is_numeric($arg[0]) || in_array($arg[0], $specialVersionChars);
507
    }
508
}
509