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.
Completed
Push — master ( 3308b4...466970 )
by Anton
02:28
created

Application::addCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Console;
9
10
use Deployer\Component\PharUpdate\Console\Command as PharUpdateCommand;
11
use Deployer\Component\PharUpdate\Console\Helper as PharUpdateHelper;
12
use Symfony\Component\Console\Application as Console;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputDefinition;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class Application extends Console
20
{
21
    /**
22
     * Input definition for user specific arguments and options.
23
     *
24
     * @var InputDefinition
25
     */
26
    private $userDefinition;
27
28 36
    /**
29
     * @var callable
30 36
     */
31
    private $callback;
32 36
33 36
    /**
34 36
     * {@inheritdoc}
35
     */
36 36
    protected function getDefaultInputDefinition()
37
    {
38
        $inputDefinition = parent::getDefaultInputDefinition();
39
40
        $inputDefinition->addOption(
41
            new InputOption('--file', '-f', InputOption::VALUE_OPTIONAL, 'Specify Deployer file')
42 36
        );
43
44 36
        return $inputDefinition;
45 36
    }
46 36
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function getDefaultCommands()
51
    {
52 36
        $commands = parent::getDefaultCommands();
53
54 36
        if ($this->isPharArchive()) {
55 36
            $commands[] = $this->selfUpdateCommand();
56 36
        }
57 36
58
        return $commands;
1 ignored issue
show
Best Practice introduced by
The expression return $commands; seems to be an array, but some of its elements' types (Deployer\Component\PharUpdate\Console\Command) are incompatible with the return type of the parent method Symfony\Component\Consol...ion::getDefaultCommands of type array<Symfony\Component\...le\Command\ListCommand>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
    }
60
61
    /**
62
     * {@inheritdoc}
63 36
     */
64
    private function selfUpdateCommand()
65 36
    {
66 36
        $selfUpdate = new PharUpdateCommand('self-update');
67 36
        $selfUpdate->setDescription('Updates deployer.phar to the latest version');
68
        $selfUpdate->setManifestUri('https://deployer.org/manifest.json');
69
        return $selfUpdate;
70
    }
71
72
    /**
73 14
     * {@inheritdoc}
74
     */
75 14
    protected function getDefaultHelperSet()
76 14
    {
77 14
        $helperSet = parent::getDefaultHelperSet();
78
79 14
        if ($this->isPharArchive()) {
80
            $helperSet->set(new PharUpdateHelper());
81
        }
82
        return $helperSet;
83
    }
84
85 14
    /**
86
     * @return InputDefinition
87 14
     */
88 14
    public function getUserDefinition()
89 14
    {
90
        if (null === $this->userDefinition) {
91
            $this->userDefinition = new InputDefinition();
92
        }
93
94
        return $this->userDefinition;
95
    }
96
97
    /**
98
     * Add user definition arguments and options to definition.
99
     */
100
    public function addUserArgumentsAndOptions()
101
    {
102
        $this->getDefinition()->addArguments($this->getUserDefinition()->getArguments());
103
        $this->getDefinition()->addOptions($this->getUserDefinition()->getOptions());
104
    }
105
106
    /**
107
     * @return bool
108
     */
109
    public function isPharArchive()
110
    {
111
        return 'phar:' === substr(__FILE__, 0, 5);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
118
    {
119
        $exception = null;
120
        $exitCode = 0;
121
122
        try {
123
            $exitCode = parent::doRunCommand($command, $input, $output);
124
        } catch (\Exception $x) {
125
            $exception = $x;
126
        } catch (\Throwable $x) {
1 ignored issue
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
127
            $exception = $x;
128
        }
129
130
        if (!empty($this->callback)) {
131
            call_user_func($this->callback, new CommandEvent($command, $input, $output, $exception, $exitCode));
132
        }
133
134
        if ($exception !== null) {
135
            throw $exception;
136
        }
137
138
        return $exitCode;
139
    }
140
141
    /**
142
     * @param $callable
143
     */
144
    public function addCallback($callable)
145
    {
146
        $this->callback = $callable;
147
    }
148
}
149