Installer::getDestinationPath()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Consigliere\Components\Process;
4
5
use Illuminate\Console\Command as ComponentCommand;
6
use Illuminate\Support\Str;
7
use Consigliere\Components\Repository;
8
use Symfony\Component\Process\Process;
9
10
class Installer
11
{
12
    /**
13
     * The component name.
14
     *
15
     * @var string
16
     */
17
    protected $name;
18
19
    /**
20
     * The version of component being installed.
21
     *
22
     * @var string
23
     */
24
    protected $version;
25
26
    /**
27
     * The component repository instance.
28
     *
29
     * @var \Consigliere\Components\Repository
30
     */
31
    protected $repository;
32
33
    /**
34
     * The console command instance.
35
     *
36
     * @var \Illuminate\Console\Command
37
     */
38
    protected $console;
39
40
    /**
41
     * The destionation path.
42
     *
43
     * @var string
44
     */
45
    protected $path;
46
47
    /**
48
     * The process timeout.
49
     *
50
     * @var int
51
     */
52
    protected $timeout = 3360;
53
54
    /**
55
     * The constructor.
56
     *
57
     * @param string $name
58
     * @param string $version
59
     * @param string $type
60
     * @param bool $tree
61
     */
62
    public function __construct($name, $version = null, $type = null, $tree = false)
63
    {
64
        $this->name    = $name;
65
        $this->version = $version;
66
        $this->type    = $type;
0 ignored issues
show
Bug introduced by
The property type does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67
        $this->tree    = $tree;
0 ignored issues
show
Bug introduced by
The property tree does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
68
    }
69
70
    /**
71
     * Set destination path.
72
     *
73
     * @param string $path
74
     *
75
     * @return $this
76
     */
77
    public function setPath($path)
78
    {
79
        $this->path = $path;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Set the component repository instance.
86
     *
87
     * @param \Consigliere\Components\Repository $repository
88
     *
89
     * @return $this
90
     */
91
    public function setRepository(Repository $repository)
92
    {
93
        $this->repository = $repository;
94
95
        return $this;
96
    }
97
98
    /**
99
     * Set console command instance.
100
     *
101
     * @param \Consigliere\Components\Process\Command $console
102
     *
103
     * @return $this
104
     */
105
    public function setConsole(Command $console)
106
    {
107
        $this->console = $console;
0 ignored issues
show
Documentation Bug introduced by
It seems like $console of type object<Consigliere\Components\Process\Command> is incompatible with the declared type object<Illuminate\Console\Command> of property $console.

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...
108
109
        return $this;
110
    }
111
112
    /**
113
     * Set process timeout.
114
     *
115
     * @param int $timeout
116
     *
117
     * @return $this
118
     */
119
    public function setTimeout($timeout)
120
    {
121
        $this->timeout = $timeout;
122
123
        return $this;
124
    }
125
126
    /**
127
     * Run the installation process.
128
     *
129
     * @return \Symfony\Component\Process\Process
130
     */
131
    public function run()
132
    {
133
        $process = $this->getProcess();
134
135
        $process->setTimeout($this->timeout);
136
137
        if ($this->console instanceof Command) {
0 ignored issues
show
Bug introduced by
The class Consigliere\Components\Process\Command does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
138
            $process->run(function($type, $line) {
139
                $this->console->line($line);
140
            });
141
        }
142
143
        return $process;
144
    }
145
146
    /**
147
     * Get process instance.
148
     *
149
     * @return \Symfony\Component\Process\Process
150
     */
151
    public function getProcess()
152
    {
153
        switch ($this->type) {
154
            case 'github':
155
            case 'github-https':
156
            case 'bitbucket':
157
                if ($this->tree) {
158
                    $process = $this->installViaSubtree();
0 ignored issues
show
Unused Code introduced by
$process 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...
159
                }
160
161
                $process = $this->installViaGit();
162
                break;
163
164
            default:
165
                $process = $this->installViaComposer();
166
                break;
167
        }
168
169
        return $process;
170
    }
171
172
    /**
173
     * Get destination path.
174
     *
175
     * @return string
176
     */
177
    public function getDestinationPath()
178
    {
179
        if ($this->path) {
180
            return $this->path;
181
        }
182
183
        return $this->repository->getComponentPath($this->getComponentName());
184
    }
185
186
    /**
187
     * Get git repo url.
188
     *
189
     * @return string|null
190
     */
191
    public function getRepoUrl()
192
    {
193
        switch ($this->type) {
194
            case 'github':
195
                return "[email protected]:{$this->name}.git";
196
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
197
198
            case 'github-https':
199
                return "https://github.com/{$this->name}.git";
200
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
201
202
            case 'bitbucket':
203
                return "[email protected]:{$this->name}.git";
204
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
205
206
            default:
207
                return;
208
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
209
        }
210
    }
211
212
    /**
213
     * Get branch name.
214
     *
215
     * @return string
216
     */
217
    public function getBranch()
218
    {
219
        return is_null($this->version) ? 'master' : $this->version;
220
    }
221
222
    /**
223
     * Get component name.
224
     *
225
     * @return string
226
     */
227
    public function getComponentName()
228
    {
229
        $parts = explode('/', $this->name);
230
231
        return Str::studly(end($parts));
232
    }
233
234
    /**
235
     * Get composer package name.
236
     *
237
     * @return string
238
     */
239
    public function getPackageName()
240
    {
241
        if (is_null($this->version)) {
242
            return $this->name . ':dev-master';
243
        }
244
245
        return $this->name . ':' . $this->version;
246
    }
247
248
    /**
249
     * Install the component via git.
250
     *
251
     * @return \Symfony\Component\Process\Process
252
     */
253 View Code Duplication
    public function installViaGit()
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...
254
    {
255
        return new Process(sprintf(
256
            'cd %s && git clone %s %s && cd %s && git checkout %s',
257
            base_path(),
258
            $this->getRepoUrl(),
259
            $this->getDestinationPath(),
260
            $this->getDestinationPath(),
261
            $this->getBranch()
262
        ));
263
    }
264
265
    /**
266
     * Install the component via git subtree.
267
     *
268
     * @return \Symfony\Component\Process\Process
269
     */
270 View Code Duplication
    public function installViaSubtree()
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...
271
    {
272
        return new Process(sprintf(
273
            'cd %s && git remote add %s %s && git subtree add --prefix=%s --squash %s %s',
274
            base_path(),
275
            $this->getComponentName(),
276
            $this->getRepoUrl(),
277
            $this->getDestinationPath(),
278
            $this->getComponentName(),
279
            $this->getBranch()
280
        ));
281
    }
282
283
    /**
284
     * Install the component via composer.
285
     *
286
     * @return \Symfony\Component\Process\Process
287
     */
288
    public function installViaComposer()
289
    {
290
        return new Process(sprintf(
291
            'cd %s && composer require %s',
292
            base_path(),
293
            $this->getPackageName()
294
        ));
295
    }
296
}
297