Ssh   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 38
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getConnection() 0 18 7
A exec() 0 8 2
1
<?php
2
3
namespace Fabrica\Tools;
4
5
use phpseclib3\Net\SSH2;
6
7
use Fabrica\Models\Infra\Computer;
8
9
/**
10
 * Bash Class
11
 *
12
 * @class Bash
13
 */
14
class Ssh extends Bash
15
{
16
17
    protected $computer = false;
18
19
    public function __construct(Computer $computer)
20
    {
21
        $this->computer = $computer;
0 ignored issues
show
Documentation Bug introduced by
It seems like $computer of type object<Fabrica\Models\Infra\Computer> is incompatible with the declared type boolean of property $computer.

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...
22
    }
23
24
    public function getConnection()
25
    {
26
        if (!$this->connection) {
27
            $this->connection = new SSH2($this->computer->host);
0 ignored issues
show
Bug introduced by
The property connection 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...
28
            if ($this->computer->key) {
29
                if ($this->connection->login($this->computer->username, $this->computer->key->getKey())) {
30
                    return $this->connection;
31
                }
32
            }
33
34
            if (isset($this->computer->password) && $this->computer->password && $this->connection->login($this->computer->username, $this->computer->password)) {
35
                return $this->connection;
36
            }
37
            
38
            exit('Login Failed');
39
        }
40
        return $this->connection;
41
    }
42
43
    public function exec($exec, $path = false)
44
    {
45
        if (!$path) {
46
            $exec = 'cd '.$this->computer.' && '.$exec;
47
        }
48
        
49
        return $this->getConnection()->exec($exec);
50
    }
51
}
52