GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ScpTask::getCommand()   D
last analyzed

Complexity

Conditions 15
Paths 134

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 27
nc 134
nop 0
dl 0
loc 41
rs 4.7582
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * See class comment
4
 *
5
 * PHP Version 5
6
 *
7
 * @category   Netresearch
8
 * @package    Netresearch\Kite
9
 * @subpackage Task
10
 * @author     Christian Opitz <[email protected]>
11
 * @license    http://www.netresearch.de Netresearch Copyright
12
 * @link       http://www.netresearch.de
13
 */
14
15
namespace Netresearch\Kite\Task;
16
use Netresearch\Kite\Exception;
17
use Netresearch\Kite\Node;
18
19
/**
20
 * Up/download via SCP
21
 *
22
 * @category   Netresearch
23
 * @package    Netresearch\Kite
24
 * @subpackage Task
25
 * @author     Christian Opitz <[email protected]>
26
 * @license    http://www.netresearch.de Netresearch Copyright
27
 * @link       http://www.netresearch.de
28
 */
29
class ScpTask extends \Netresearch\Kite\Task\RemoteShellTask
30
{
31
    /**
32
     * Configure the options
33
     *
34
     * @return array
35
     */
36
    protected function configureVariables()
37
    {
38
        return array(
39
            'from' => array(
40
                'type' => 'string',
41
                'required' => true,
42
                'label' => 'Path to the source (prefix with {node}: to download from a node)'
43
            ),
44
            'to' => array(
45
                'type' => 'string',
46
                'required' => true,
47
                'label' => 'Path to the target (prefix with {node}: to upload to a node)'
48
            ),
49
            'options' => null,
50
            'arguments' => null,
51
            'optArg' => null,
52
            '--'
53
        ) + parent::configureVariables();
54
    }
55
56
    /**
57
     * Create all necessary commands
58
     *
59
     * @return array|string
60
     */
61
    protected function getCommand()
62
    {
63
        $to = $this->get('to');
64
        $from = $this->get('from');
65
66
        $toNode = $this->getNodeFromPath($to);
67
        $fromNode = $this->getNodeFromPath($from);
68
        $node = $toNode ?: $fromNode;
69
70
        if (!$node) {
71
            throw new Exception('This task is intended to be used with nodes - use shell task for other sources/targets');
72
        } elseif ($toNode === $fromNode) {
73
            throw new Exception('Transfer on same node not supported');
74
        } elseif ($toNode && $fromNode && $toNode !== $fromNode) {
75
            throw new Exception('Transfer between nodes not (yet) supported');
76
        }
77
78
        $cwd = $this->expand($this->cwd);
79
        $cwdCmd = $cwd ? 'cd ' . escapeshellarg($cwd) . '; ' : null;
80
        $toDirParent = dirname(array_pop(explode(':', $to, 2)));
0 ignored issues
show
Bug introduced by
explode(':', $to, 2) cannot be passed to array_pop() as the parameter $array expects a reference.
Loading history...
81
        if ($toDirParent !== '/') {
82
            $prepareDirCmd = 'mkdir -p ' . escapeshellarg($toDirParent);
83
            if ($toNode) {
84
                $prepareDirCmd = $this->getSshCommand($prepareDirCmd, $toNode);
85
            } elseif ($cwd && $toDirParent[0] !== '/') {
86
                $prepareDirCmd = $cwdCmd . $prepareDirCmd;
87
            }
88
        }
89
90
        $scpCommand = $this->getScpCommand($node) . ' ' . escapeshellarg($from) . ' ' . escapeshellarg($to);
91
92
        if ($node) {
93
            $this->addExpect($scpCommand, $node);
94
        }
95
96
        if ($cwdCmd) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cwdCmd of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
97
            $scpCommand = $cwdCmd . $scpCommand;
98
        }
99
100
        return isset($prepareDirCmd) ? array($prepareDirCmd, $scpCommand) : $scpCommand;
0 ignored issues
show
Bug Compatibility introduced by
The expression isset($prepareDirCmd) ? ...Command) : $scpCommand; of type string[]|string adds the type string[] to the return on line 100 which is incompatible with the return type of the parent method Netresearch\Kite\Task\RemoteShellTask::getCommand of type string.
Loading history...
101
    }
102
103
    /**
104
     * Create the scp command
105
     *
106
     * @param Node $node The node
107
     *
108
     * @return string
109
     */
110
    protected function getScpCommand(Node $node)
111
    {
112
        return rtrim('scp -r ' . trim($node->get('scpOptions')));
113
    }
114
115
    /**
116
     * Get the node from a path (by it's url)
117
     *
118
     * @param string $path The path
119
     *
120
     * @return Node
121
     */
122
    protected function getNodeFromPath($path)
123
    {
124
        if (strpos($path, ':')) {
125
            /* @var Node[] $nodes */
126
            $nodes = $this->get('nodes', array());
127
            $url = array_shift(explode(':', $path, 2));
0 ignored issues
show
Bug introduced by
explode(':', $path, 2) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
128
            foreach ($nodes as $node) {
129
                if ($node->get('url') === $url) {
130
                    return $node;
131
                }
132
            }
133
        }
134
        return null;
135
    }
136
}
137
?>
138