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.

ProcessProvider   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 3
dl 0
loc 87
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 5 1
A executeCommand() 0 4 1
A executeSudoCommand() 0 9 2
A commandExists() 0 4 1
B performCommand() 0 27 6
1
<?php
2
3
namespace Kunstmaan\Skylab\Provider;
4
5
use Cilex\Application;
6
use Symfony\Component\Process\Process;
7
use Symfony\Component\Process\ProcessBuilder;
8
9
/**
10
 * ProcessProvider
11
 */
12
class ProcessProvider extends AbstractProvider
13
{
14
    /**
15
     * Registers services on the given app.
16
     *
17
     * @param Application $app An Application instance
18
     */
19
    public function register(Application $app)
20
    {
21
        $app['process'] = $this;
22
        $this->app = $app;
23
    }
24
25
    /**
26
     * @param string $command The command
27
     * @param bool   $silent  Be silent or not
28
     *
29
     * @param  \Closure    $callback
0 ignored issues
show
Documentation introduced by
Should the type for parameter $callback not be null|\Closure?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
30
     * @return bool|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
31
     */
32
    public function executeCommand($command, $silent = false, \Closure $callback = null, $env=array())
33
    {
34
        return $this->performCommand($command, $silent, $callback, $env);
35
    }
36
37
    /**
38
     * @param string $command The command
39
     * @param bool   $silent  Be silent or not
40
     * @param string $sudoAs  Sudo as a different user then the root user
0 ignored issues
show
Documentation introduced by
Should the type for parameter $sudoAs not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
41
     *
42
     * @return bool|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
43
     */
44
    public function executeSudoCommand($command, $silent = false, $sudoAs = null, \Closure $callback = null, $env=array())
45
    {
46
        if (empty($sudoAs)) {
47
            $command = 'sudo -s -p "Please enter your sudo password:" ' . $command;
48
        } else {
49
            $command = 'sudo -s -p "Please enter your sudo password:" -u ' . $sudoAs . ' ' . $command;
50
        }
51
        return $this->performCommand($command, $silent, $callback, $env);
52
    }
53
54
    /**
55
     * @param  string $cmd
56
     * @return bool
57
     */
58
    public function commandExists($cmd)
59
    {
60
        return shell_exec("hash " . $cmd . " 2>&1") == '';
61
    }
62
63
    /**
64
     * @param $command
65
     * @param $silent
66
     * @param \Closure $callback
0 ignored issues
show
Documentation introduced by
Should the type for parameter $callback not be null|\Closure?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
67
     * @param $env
68
     * @return bool|string
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
69
     * @throws \Kunstmaan\Skylab\Exceptions\SkylabException
70
     */
71
    private function performCommand($command, $silent=false, \Closure $callback = null, $env=array())
0 ignored issues
show
Coding Style introduced by
performCommand uses the super-global variable $_ENV 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...
Coding Style introduced by
performCommand 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...
72
    {
73
        $startTime = microtime(true);
74
75
        if (!$silent) {
76
            $this->dialogProvider->logCommand($command);
77
        }
78
79
        $env = array_replace($_ENV, $_SERVER, $env);
80
        $process = new Process($command, null, $env);
81
        $process->setTimeout(14400*100);
82
        $process->run($callback);
83
        if (!$silent) {
84
            $this->dialogProvider->logCommandTime($startTime);
85
        }
86
        if (!$process->isSuccessful()) {
87
            if ($process->getExitCode() == 23){
88
                return $process->getOutput();
89
            } else {
90
                if (!$silent) {
91
                    $this->dialogProvider->logError($process->getErrorOutput());
92
                }
93
                return false;
94
            }
95
        }
96
        return $process->getOutput();
97
    }
98
}
99