ChecksSSHConnection   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 13
lcom 2
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sshConfigFile() 0 4 1
A getServerName() 0 5 2
A getServer() 0 5 2
B checkSSHConnection() 0 14 5
A abortIfNoSSHConnection() 0 8 3
1
<?php
2
3
namespace Acacha\ForgePublish\Commands\Traits;
4
5
/**
6
 * Trait ChecksSSHConnection.
7
 *
8
 * @package Acacha\ForgePublish\Commands\Traits
9
 */
10
trait ChecksSSHConnection
11
{
12
    use ChecksServer, SSHHostname;
13
14
    /**
15
     * SSH config file path.
16
     *
17
     * @return string
18
     */
19
    protected function sshConfigFile()
0 ignored issues
show
Coding Style introduced by
sshConfigFile uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
20
    {
21
        return $_SERVER['HOME'] . '/.ssh/config';
22
    }
23
24
    /**
25
     * Get Server name.
26
     *
27
     * @return null
28
     */
29
    protected function getServerName()
30
    {
31
        $server_name = isset($this->server_name) ? $this->server_name : fp_env('ACACHA_FORGE_SERVER_NAME');
0 ignored issues
show
Bug introduced by
The property server_name 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...
32
        return $server_name;
33
    }
34
35
    /**
36
     * Get server.
37
     * @return null
38
     */
39
    protected function getServer()
40
    {
41
        $server_name = $this->server ? $this->server : fp_env('ACACHA_FORGE_SERVER');
0 ignored issues
show
Bug introduced by
The property server does not seem to exist. Did you mean server_name?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
42
        return $server_name;
43
    }
44
45
    /**
46
     * Check SSH connection.
47
     *
48
     * @return bool
49
     */
50
    protected function checkSSHConnection($server = null, $ssh_config_file = null, $verbose = false)
51
    {
52
        $server = $server ? $server : $this->hostNameForConfigFile();
53
        $ssh_config_file =  $ssh_config_file ? $ssh_config_file : $this->sshConfigFile();
54
        if ($verbose) {
55
            $this->info("timeout 10 ssh -F $ssh_config_file -q " . $server . ' exit; echo $?');
0 ignored issues
show
Bug introduced by
It seems like info() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
56
        }
57
58
        $ret = exec("timeout 10 ssh -F $ssh_config_file -q " . $server . ' "exit"; echo $?');
59
        if ($ret == 0) {
60
            return true;
61
        }
62
        return false;
63
    }
64
65
    /**
66
     * Abort if no SSH connection.
67
     *
68
     * @return bool
69
     */
70
    protected function abortIfNoSSHConnection($server = null)
71
    {
72
        $server = $server ? $server : $this->hostNameForConfigFile();
73
        if (!$this->checkSSHConnection($server)) {
74
            $this->error("SSH connection to server $server doesn't works. Please run php artisan publish:init or publish:ssh");
0 ignored issues
show
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
75
            die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method abortIfNoSSHConnection() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
76
        }
77
    }
78
}
79