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.
Passed
Pull Request — master (#2037)
by Elan
02:09
created

Messenger::renderException()   C

Complexity

Conditions 14
Paths 12

Size

Total Lines 57

Duplication

Lines 24
Ratio 42.11 %

Code Coverage

Tests 9
CRAP Score 103.2018

Importance

Changes 0
Metric Value
cc 14
nc 12
nop 2
dl 24
loc 57
ccs 9
cts 39
cp 0.2308
crap 103.2018
rs 6.2666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Executor;
9
10
use Deployer\Exception\Exception;
11
use Deployer\Exception\RunException;
12
use Deployer\Host\Host;
13
use Deployer\Task\Task;
14
use Symfony\Component\Console\Input\Input;
15
use Symfony\Component\Console\Output\Output;
16
use Throwable;
17
18
class Messenger
19
{
20
    private $input;
21
    private $output;
22
23
    /**
24
     * @var int|double
25
     */
26
    private $startTime;
27
28 9
    public function __construct(Input $input, Output $output)
29
    {
30 9
        $this->input = $input;
31 9
        $this->output = $output;
32 9
    }
33
34 8
    public function startTask(Task $task)
35
    {
36 8
        $this->startTime = round(microtime(true) * 1000);
37 8
        if (!$task->isShallow()) {
38 8
            $this->output->writeln("<fg=cyan;options=bold>task</> {$task->getName()}");
39
        }
40 8
    }
41
42
    /*
43
     * Print task was ok.
44
     */
45 8
    public function endTask(Task $task)
46
    {
47 8
        if ($task->isShallow()) {
48 5
            return;
49
        }
50
51 8
        $endTime = round(microtime(true) * 1000);
52 8
        $millis = $endTime - $this->startTime;
53 8
        $seconds = floor($millis / 1000);
54 8
        $millis = $millis - $seconds * 1000;
55 8
        $taskTime = ($seconds > 0 ? "{$seconds}s " : "") . "{$millis}ms";
56
57 8
        if ($this->output->isVeryVerbose()) {
58
            $this->output->writeln("<fg=yellow;options=bold>done</> {$task->getName()} $taskTime");
59
        }
60
61 8
        if (!empty($this->input->getOption('profile'))) {
62
            $line = sprintf("%s\t%s\n", $task->getName(), $taskTime);
63
            file_put_contents($this->input->getOption('profile'), $line, FILE_APPEND);
64
65
        }
66 8
    }
67
68 9
    public function endOnHost(Host $host)
69
    {
70 9
        if ($this->output->isVeryVerbose()) {
71
            $this->output->writeln("[{$host->getTag()}] <info>ok</info>");
72
        }
73 9
    }
74
75 1
    public function renderException(Throwable $exception, Host $host)
76
    {
77 1
        if ($exception instanceof RunException) {
78
79 1
            $message = "";
80 1
            $message .= "[{$host->getTag()}] <fg=white;bg=red> error </> <comment>in {$exception->getTaskFilename()} on line {$exception->getTaskLineNumber()}:</>\n";
81 1
            if ($this->output->getVerbosity() === Output::VERBOSITY_NORMAL) {
82
                $message .= "[{$host->getTag()}] <fg=green;options=bold>run</> {$exception->getCommand()}\n";
83 View Code Duplication
                foreach (explode("\n", $exception->getErrorOutput()) as $line) {
84
                    $line = trim($line);
85
                    if ($line !== "") {
86
                        $message .= "[{$host->getTag()}] <fg=red>err</> $line\n";
87
                    }
88
                }
89 View Code Duplication
                foreach (explode("\n", $exception->getOutput()) as $line) {
90
                    $line = trim($line);
91
                    if ($line !== "") {
92
                        $message .= "[{$host->getTag()}] $line\n";
93
                    }
94
                }
95
            }
96 1
            $message .= "[{$host->getTag()}] <fg=red>exit code</> {$exception->getExitCode()} ({$exception->getExitCodeText()})\n";
97 1
            $this->output->write($message);
98
99
        } else {
100
            $message = "";
101
            $class = get_class($exception);
102
            $file = basename($exception->getFile());
103
            $line = $exception->getLine();
104
            if ($exception instanceof Exception) {
105
                $file = $exception->getTaskFilename();
106
                $line = $exception->getTaskLineNumber();
107
            }
108
            $message .= "[{$host->getTag()}] <fg=white;bg=red> $class </> <comment>in $file on line $line:</>\n";
109
            $message .= "[{$host->getTag()}]\n";
110 View Code Duplication
            foreach (explode("\n", $exception->getMessage()) as $line) {
111
                $line = trim($line);
112
                if ($line !== "") {
113
                    $message .= "[{$host->getTag()}]   <comment>$line</comment>\n";
114
                }
115
            }
116
            $message .= "[{$host->getTag()}]\n";
117
            if ($this->output->isDebug()) {
118 View Code Duplication
                foreach (explode("\n", $exception->getTraceAsString()) as $line) {
119
                    $line = trim($line);
120
                    if ($line !== "") {
121
                        $message .= "[{$host->getTag()}] $line\n";
122
                    }
123
                }
124
            }
125
            $this->output->write($message);
126
        }
127
128 1
        if ($exception->getPrevious()) {
129
            $this->renderException($exception->getPrevious(), $host);
130
        }
131 1
    }
132
}
133