VagrantTransport   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 8
loc 72
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 4 1
A wrap() 0 13 1
A addChdir() 0 11 1
A getTransportOptions() 0 4 2
A getCommandToExecute() 8 8 1

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace Consolidation\SiteProcess\Transport;
4
5
use Consolidation\SiteProcess\SiteProcess;
6
use Consolidation\SiteProcess\Util\Escape;
7
use Consolidation\SiteAlias\SiteAliasInterface;
8
use Consolidation\SiteProcess\Util\Shell;
9
10
/**
11
 * VagrantTransport knows how to wrap a command such that it runs on a remote
12
 * system via the vagrant cli.
13
 */
14
class VagrantTransport implements TransportInterface
15
{
16
    protected $tty;
17
    protected $siteAlias;
18
19
    public function __construct(SiteAliasInterface $siteAlias)
20
    {
21
        $this->siteAlias = $siteAlias;
22
    }
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function configure(SiteProcess $process)
28
    {
29
        $this->tty = $process->isTty();
30
    }
31
32
    /**
33
     * inheritdoc
34
     */
35
    public function wrap($args)
36
    {
37
        $transport = ['vagrant', 'ssh'];
38
        $transportOptions = $this->getTransportOptions();
39
        $commandToExecute = $this->getCommandToExecute($args);
40
41
        return array_merge(
42
            $transport,
43
            $transportOptions,
44
            ['-c'],
45
            $commandToExecute
46
        );
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function addChdir($cd_remote, $args)
53
    {
54
        return array_merge(
55
            [
56
                'cd',
57
                $cd_remote,
58
                Shell::op('&&'),
59
            ],
60
            $args
61
        );
62
    }
63
64
    /**
65
     * getTransportOptions returns the transport options for the tranport
66
     * mechanism itself
67
     */
68
    protected function getTransportOptions()
69
    {
70
        return $this->tty ? ['-t'] : [];
71
    }
72
73
    /**
74
     * getCommandToExecute processes the arguments for the command to
75
     * be executed such that they are appropriate for the transport mechanism.
76
     */
77 View Code Duplication
    protected function getCommandToExecute($args)
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...
78
    {
79
        // Escape each argument for the target system and then join
80
        $args = Escape::argsForSite($this->siteAlias, $args);
81
        $commandToExecute = implode(' ', $args);
82
83
        return [$commandToExecute];
84
    }
85
}
86