Completed
Push — master ( 924f7d...1d4235 )
by Greg
99:30
created

SiteProcess::interpolate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Consolidation\SiteProcess;
3
4
use Consolidation\SiteAlias\AliasRecord;
5
use Consolidation\SiteProcess\Util\ArgumentProcessor;
6
use Consolidation\SiteProcess\Transport\LocalTransport;
7
use Consolidation\SiteProcess\Transport\SshTransport;
8
use Consolidation\Config\Util\Interpolator;
9
10
/**
11
 * A wrapper around Symfony Process that uses site aliases
12
 * (https://github.com/consolidation/site-alias)
13
 *
14
 * - Interpolate arguments using values from the alias
15
 *   e.g. `$process = new SiteProcess($alias, ['git', '-C', '{{root}}']);`
16
 * - Make remote calls via ssh as if they were local.
17
 */
18
class SiteProcess extends ProcessBase
19
{
20
    /** @var AliasRecord */
21
    protected $siteAlias;
22
    /** @var string */
23
    protected $args;
24
    /** @var string */
25
    protected $options;
26
    /** @var string */
27
    protected $optionsPassedAsArgs;
28
    /** @var string */
29
    protected $cd;
30
31
    /**
32
     * Process arguments and options per the site alias and build the
33
     * actual command to run.
34
     */
35
    public function __construct(AliasRecord $siteAlias, $args, $options = [], $optionsPassedAsArgs = [])
36
    {
37
        $this->siteAlias = $siteAlias;
38
        $this->args = $args;
39
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options of type array is incompatible with the declared type string of property $options.

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...
40
        $this->optionsPassedAsArgs = $optionsPassedAsArgs;
0 ignored issues
show
Documentation Bug introduced by
It seems like $optionsPassedAsArgs of type array is incompatible with the declared type string of property $optionsPassedAsArgs.

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...
41
42
        parent::__construct([]);
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function setWorkingDirectory($cwd)
49
    {
50
        $this->cd = $cwd;
51
        return parent::setWorkingDirectory($cwd);
52
    }
53
54
    public function useSiteRoot()
55
    {
56
        if (!$this->siteAlias->hasRoot()) {
57
            return $this;
58
        }
59
60
        return $this->setWorkingDirectory($this->siteAlias->root());
61
    }
62
63
    /**
64
     * Take all of our individual arguments and process them for use.
65
     */
66
    protected function processArgs()
67
    {
68
        $transport = static::getTransport($this->siteAlias);
69
        $transport->configure($this);
70
71
        $processor = new ArgumentProcessor();
72
        $selectedArgs = $processor->selectArgs(
73
            $this->siteAlias,
74
            $this->args,
0 ignored issues
show
Documentation introduced by
$this->args is of type string, 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...
75
            $this->options,
0 ignored issues
show
Documentation introduced by
$this->options is of type string, 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...
76
            $this->optionsPassedAsArgs
0 ignored issues
show
Documentation introduced by
$this->optionsPassedAsArgs is of type string, 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...
77
        );
78
79
        // Ask the transport to drop in a 'cd' if needed.
80
        if ($this->cd) {
81
            $selectedArgs = $transport->addChdir($this->cd, $selectedArgs);
82
        }
83
84
        // Do any necessary interpolation on the selected arguments.
85
        $processedArgs = $this->interpolate($selectedArgs);
86
87
        // Wrap the command with 'ssh' or some other transport if this is
88
        // a remote command; otherwise, leave it as-is.
89
        return $transport->wrap($processedArgs);
90
    }
91
92
    /**
93
     * TODO: Could we perhaps support variable transport mechanisms?
94
     */
95
    protected static function getTransport(AliasRecord $siteAlias)
96
    {
97
        if ($siteAlias->isLocal()) {
98
            return new LocalTransport();
99
        }
100
101
        return new SshTransport($siteAlias);
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function getCommandLine()
108
    {
109
        $commandLine = parent::getCommandLine();
110
        if (empty($commandLine)) {
111
            $this->setCommandLine($this->processArgs());
112
            $commandLine = parent::getCommandLine();
113
        }
114
        return $commandLine;
115
    }
116
117
    /**
118
     * @inheritDoc
119
     */
120
    public function start(callable $callback = null)
121
    {
122
        $cmd = $this->getCommandLine();
0 ignored issues
show
Unused Code introduced by
$cmd 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...
123
        parent::start($callback);
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function wait(callable $callback = null)
130
    {
131
        $return = parent::wait($callback);
132
        return $return;
133
    }
134
135
    /**
136
     * interpolate examines each of the arguments in the provided argument list
137
     * and replaces any token found therein with the value for that key as
138
     * pulled from the given site alias.
139
     *
140
     * Example: "git -C {{root}} status"
141
     *
142
     * The token "{{root}}" will be converted to a value via $siteAlias->get('root').
143
     * The result will replace the token.
144
     *
145
     * It is possible to use dot notation in the keys to access nested elements
146
     * within the site alias record.
147
     *
148
     * @param AliasRecord $siteAlias
0 ignored issues
show
Bug introduced by
There is no parameter named $siteAlias. 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...
149
     * @param type $args
150
     * @return type
151
     */
152
    protected function interpolate($args)
153
    {
154
        $interpolator = new Interpolator();
155
        return array_map(
156
            function ($arg) use ($interpolator) {
157
                return $interpolator->interpolate($this->siteAlias, $arg, false);
158
            },
159
            $args
160
        );
161
    }
162
}
163