Completed
Push — master ( 2eca99...95d9ac )
by Greg
02:35
created

SiteProcess::chdirToSiteRoot()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 3
nc 2
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
use Consolidation\SiteProcess\Util\ShellOperatorInterface;
10
use Consolidation\SiteProcess\Util\Escape;
11
12
/**
13
 * A wrapper around Symfony Process that uses site aliases
14
 * (https://github.com/consolidation/site-alias)
15
 *
16
 * - Interpolate arguments using values from the alias
17
 *   e.g. `$process = new SiteProcess($alias, ['git', '-C', '{{root}}']);`
18
 * - Make remote calls via ssh as if they were local.
19
 */
20
class SiteProcess extends ProcessBase
21
{
22
    /** @var AliasRecord */
23
    protected $siteAlias;
24
    /** @var string */
25
    protected $args;
26
    /** @var string */
27
    protected $options;
28
    /** @var string */
29
    protected $optionsPassedAsArgs;
30
    /** @var string */
31
    protected $cd;
32
33
    /**
34
     * Process arguments and options per the site alias and build the
35
     * actual command to run.
36
     */
37
    public function __construct(AliasRecord $siteAlias, $args, $options = [], $optionsPassedAsArgs = [])
38
    {
39
        $this->siteAlias = $siteAlias;
40
        $this->args = $args;
41
        $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...
42
        $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...
43
44
        parent::__construct([]);
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function setWorkingDirectory($cwd)
51
    {
52
        $this->cd = $cwd;
53
        return parent::setWorkingDirectory($cwd);
54
    }
55
56
    public function chdirToSiteRoot($shouldUseSiteRoot = true)
57
    {
58
        if (!$shouldUseSiteRoot || !$this->siteAlias->hasRoot()) {
59
            return $this;
60
        }
61
62
        return $this->setWorkingDirectory($this->siteAlias->root());
63
    }
64
65
    /**
66
     * Take all of our individual arguments and process them for use.
67
     */
68
    protected function processArgs()
69
    {
70
        $transport = static::getTransport($this->siteAlias);
71
        $transport->configure($this);
72
73
        $processor = new ArgumentProcessor();
74
        $selectedArgs = $processor->selectArgs(
75
            $this->siteAlias,
76
            $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...
77
            $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...
78
            $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...
79
        );
80
81
        // Ask the transport to drop in a 'cd' if needed.
82
        if ($this->cd) {
83
            $selectedArgs = $transport->addChdir($this->cd, $selectedArgs);
84
        }
85
86
        // Do any necessary interpolation on the selected arguments.
87
        $processedArgs = $this->interpolate($selectedArgs);
88
89
        // Wrap the command with 'ssh' or some other transport if this is
90
        // a remote command; otherwise, leave it as-is.
91
        return $transport->wrap($processedArgs);
92
    }
93
94
    /**
95
     * TODO: Could we perhaps support variable transport mechanisms?
96
     */
97
    protected static function getTransport(AliasRecord $siteAlias)
98
    {
99
        if ($siteAlias->isLocal()) {
100
            return new LocalTransport();
101
        }
102
103
        return new SshTransport($siteAlias);
104
    }
105
106
    /**
107
     * @inheritDoc
108
     */
109
    public function getCommandLine()
110
    {
111
        $commandLine = parent::getCommandLine();
112
        if (empty($commandLine)) {
113
            $processedArgs = $this->processArgs();
114
            $commandLine = Escape::argsForSite($this->siteAlias, $processedArgs);
115
            $commandLine = implode(' ', $commandLine);
116
            $this->setCommandLine($commandLine);
117
        }
118
        return $commandLine;
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    public function start(callable $callback = null)
125
    {
126
        $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...
127
        parent::start($callback);
128
    }
129
130
    /**
131
     * @inheritDoc
132
     */
133
    public function wait(callable $callback = null)
134
    {
135
        $return = parent::wait($callback);
136
        return $return;
137
    }
138
139
    /**
140
     * interpolate examines each of the arguments in the provided argument list
141
     * and replaces any token found therein with the value for that key as
142
     * pulled from the given site alias.
143
     *
144
     * Example: "git -C {{root}} status"
145
     *
146
     * The token "{{root}}" will be converted to a value via $siteAlias->get('root').
147
     * The result will replace the token.
148
     *
149
     * It is possible to use dot notation in the keys to access nested elements
150
     * within the site alias record.
151
     *
152
     * @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...
153
     * @param type $args
154
     * @return type
155
     */
156
    protected function interpolate($args)
157
    {
158
        $interpolator = new Interpolator();
159
        return array_map(
160
            function ($arg) use ($interpolator) {
161
                if ($arg instanceof ShellOperatorInterface) {
162
                    return $arg;
163
                }
164
                return $interpolator->interpolate($this->siteAlias, $arg, false);
165
            },
166
            $args
167
        );
168
    }
169
}
170