helpers.php ➔ copyShared()   A
last analyzed

Complexity

Conditions 5
Paths 9

Size

Total Lines 15

Duplication

Lines 12
Ratio 80 %

Importance

Changes 0
Metric Value
cc 5
nc 9
nop 2
dl 12
loc 15
rs 9.4555
c 0
b 0
f 0
1
<?php
2
3
namespace Deployer;
4
5
/**
6
 * Run an artisan command.
7
 *
8
 * Supported options:
9
 * - 'min' => #.#: The minimum Laravel version required (included).
10
 * - 'max' => #.#: The maximum Laravel version required (included).
11
 * - 'skipIfNoEnv': Skip and warn the user if `.env` file is inexistant or empty.
12
 * - 'failIfNoEnv': Fail the command if `.env` file is inexistant or empty.
13
 * - 'runInCurrent': Run the artisan command in the current directory.
14
 * - 'showOutput': Show the output of the command if given.
15
 *
16
 * @param string $command The artisan command (with cli options if any).
17
 * @param array $options The options that define the behaviour of the command.
18
 * @return callable A function that can be used as a task.
19
 */
20
function artisan($command, $options = [])
21
{
22
    return function () use ($command, $options) {
23
        $versionTooEarly = array_key_exists('min', $options)
24
            && laravel_version_compare($options['min'], '<');
25
26
        $versionTooLate = array_key_exists('max', $options)
27
            && laravel_version_compare($options['max'], '>');
28
29
        if ($versionTooEarly || $versionTooLate) {
30
            return;
31
        }
32
33
        if (in_array('failIfNoEnv', $options) && ! test('[ -s {{release_path}}/.env ]')) {
34
            throw new \Exception('Your .env file is empty! Cannot proceed.');
35
        }
36
37
        if (in_array('skipIfNoEnv', $options) && ! test('[ -s {{release_path}}/.env ]')) {
38
            writeln('<fg=yellow;options=bold;>Warning: </><fg=yellow;>Your .env file is empty! Skipping...</>');
39
40
            return;
41
        }
42
43
        $artisan = in_array('runInCurrent', $options)
0 ignored issues
show
Unused Code introduced by
$artisan is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
            ? '{{deploy_path}}/current/artisan'
45
            : '{{release_path}}/artisan';
46
47
        $output = run("{{bin/php}} {{release_path}}/artisan $command");
48
49
        if (in_array('showOutput', $options)) {
50
            writeln("<info>$output</info>");
51
        }
52
    };
53
}
54
55
function laravel_version_compare($version, $comparator)
56
{
57
    return version_compare(get('laravel_version'), $version, $comparator);
58
}
59
60
function copyShared($from, $to)
61
{
62 View Code Duplication
    foreach (get('shared_dirs') as $dir) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
63
        if (test("[ -d $from/$dir ]")) {
64
            run("mkdir -p $to/$dir");
65
            run("rsync -r --ignore-existing $from/$dir $to/".dirname(parse($dir)));
66
        }
67
    }
68 View Code Duplication
    foreach (get('shared_files') as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
        if (test("[ -f $from/$file ]")) {
70
            run("mkdir -p $to/".dirname(parse($file)));
71
            run("rsync --ignore-existing $from/$file $to/$file");
72
        }
73
    }
74
}
75