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.

AbstractCommand   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 5.66 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 6
loc 106
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 34 3
doExecute() 0 1 ?
C doPreExecute() 3 27 7
A doPostExecute() 3 8 2
A addDefaults() 0 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
namespace Kunstmaan\Skylab\Command;
3
4
use Cilex\Command\Command;
5
use Kunstmaan\Skylab\Application;
6
use Kunstmaan\Skylab\Exceptions\AccessDeniedException;
7
use Kunstmaan\Skylab\Exceptions\SkylabException;
8
use Kunstmaan\Skylab\Provider\UsesProviders;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
abstract class AbstractCommand extends Command
14
{
15
16
    use UsesProviders;
17
18
    /**
19
     * @param  InputInterface  $input
20
     * @param  OutputInterface $output
21
     * @return int|null|void
22
     */
23
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Coding Style introduced by
execute 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...
24
    {
25
26
        // handle everything that is not an actual exception
27
        set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) use ($input) {
0 ignored issues
show
Unused Code introduced by
The parameter $errcontext is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
            // error was suppressed with the @-operator
29
            if (0 === error_reporting()) {
30
                return false;
31
            }
32
            $extra = array();
33
            $tags = array();
34
            $extra["full_command"] = implode(" ", $_SERVER['argv']);
35
            $arguments = $input->getArguments();
36
            $tags["command"] = $arguments["command"];
37
            $this->dialogProvider->logException(new SkylabException($errstr, 0, $errno, $errfile, $errline, null, array()), $tags, $extra);
38
        }, E_ALL);
39
40
        /** @var \Cilex\Application $app */
41
        $app = $this->getContainer();
42
43
        try {
44
            $this->setup($app, $input, $output, true);
45
            $this->doPreExecute();
46
            $this->doExecute();
47
            $this->doPostExecute();
48
        } catch (\Exception $ex){
49
            $extra = array();
50
            $tags = array();
51
            $extra["full_command"] = implode(" ", $_SERVER['argv']);
52
            $arguments = $input->getArguments();
53
            $tags["command"] = $arguments["command"];
54
            $this->dialogProvider->logException($ex, $tags, $extra);
55
        }
56
    }
57
58
    /**
59
     * @return void
60
     */
61
    abstract protected function doExecute();
62
63
    /**
64
     *
65
     */
66
    private function doPreExecute()
0 ignored issues
show
Coding Style introduced by
doPreExecute 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...
67
    {
68 View Code Duplication
        if (!$this->input->getOption('hideLogo')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
69
            $this->dialogProvider->logo($this->output, OutputInterface::VERBOSITY_NORMAL, "Executing " . get_class($this));
70
        }
71
72
        $this->processProvider->executeCommand('sudo -p "Please enter your sudo password: " -v', true);
73
74
        if ('phar:' === substr(__FILE__, 0, 5) || getenv("SU")) {
75
            try {
76
               $json = $this->remoteProvider->curl('https://api.github.com/repos/kunstmaan/skylab/releases', null, null, 60);
77
            } catch (AccessDeniedException $e) {
78
               return;
79
            }
80
            $data = json_decode($json, true);
81
82
            usort($data, function ($a, $b) {
83
                return version_compare($a["tag_name"], $b["tag_name"]) * -1;
84
            });
85
86
            $latest = $data[0];
87
88
            if ($this->getName() !== 'self-update' && version_compare(Application::VERSION, $latest["tag_name"]) < 0) {
89
                $this->dialogProvider->logWarning('Warning: There is a new release available of Skylab. It is recommended to update it by running "' . $_SERVER['PHP_SELF'] . ' self-update" to get the latest version.');
90
            }
91
        }
92
    }
93
94
    /**
95
     *
96
     */
97
    protected function doPostExecute()
98
    {
99
        $this->dialogProvider->clearLine();
100
101 View Code Duplication
        if (!$this->input->getOption('hideLogo')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
102
            $this->dialogProvider->logStatistics($this->output, OutputInterface::VERBOSITY_NORMAL, $this->app['skylab.starttime']);
103
        }
104
    }
105
106
    /**
107
     * @return $this
108
     */
109
    public function addDefaults()
110
    {
111
        $this
112
            ->addOption("--hideLogo", null, InputOption::VALUE_NONE, 'If set, no logo or statistics will be shown')
113
            ->addOption("--no-interactive", null, InputOption::VALUE_NONE, 'If set, no questions will be asked');
114
115
        return $this;
116
    }
117
118
}
119