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.

Messenger::renderException()   C
last analyzed

Complexity

Conditions 14
Paths 12

Size

Total Lines 57
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 22.6141

Importance

Changes 0
Metric Value
cc 14
eloc 39
c 0
b 0
f 0
nc 12
nop 2
dl 0
loc 57
rs 6.2666
ccs 11
cts 17
cp 0.6471
crap 22.6141

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
3
declare(strict_types=1);
4
5
/* (c) Anton Medvedev <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Deployer\Executor;
12
13
use Deployer\Exception\Exception;
14
use Deployer\Exception\RunException;
15
use Deployer\Host\Host;
16
use Deployer\Logger\Logger;
17
use Deployer\Task\Task;
18
use Symfony\Component\Console\Input\Input;
19
use Symfony\Component\Console\Output\Output;
20
use Throwable;
21
22
class Messenger
23
{
24
    /**
25
     * @var Input
26
     */
27
    private $input;
28 12
29
    /**
30 12
     * @var Output
31 12
     */
32 12
    private $output;
33
34 12
    /**
35
     * @var Logger
36 12
     */
37 12
    private $logger;
38 12
39
    /**
40 12
     * @var int|double
41
     */
42
    private $startTime;
43
44
    public function __construct(Input $input, Output $output, Logger $logger)
45 12
    {
46
        $this->input = $input;
47 12
        $this->output = $output;
48 4
        $this->logger = $logger;
49
    }
50 12
51
    public function startTask(Task $task): void
52
    {
53
        $this->startTime = round(microtime(true) * 1000);
54 12
        if (getenv('GITHUB_WORKFLOW')) {
55 12
            $this->output->writeln("::group::task {$task->getName()}");
56 12
        } elseif (getenv('GITLAB_CI')) {
57 12
            $sectionId = md5($task->getName());
58 12
            $start = round($this->startTime / 1000);
59
            $this->output->writeln("\e[0Ksection_start:{$start}:{$sectionId}\r\e[0K{$task->getName()}");
60 12
        } else {
61 1
            $this->output->writeln("<fg=cyan;options=bold>task</> {$task->getName()}");
62
        }
63
        $this->logger->log("task {$task->getName()}");
64 12
    }
65
66
    /*
67
     * Print task was ok.
68
     */
69 12
    public function endTask(Task $task, bool $error = false): void
70
    {
71 8
        if (empty($this->startTime)) {
72
            $this->startTime = round(microtime(true) * 1000);
73 8
        }
74
75
        $endTime = round(microtime(true) * 1000);
76 8
        $millis = $endTime - $this->startTime;
77
        $seconds = floor($millis / 1000);
78 2
        $millis = $millis - $seconds * 1000;
79
        $taskTime = ($seconds > 0 ? "{$seconds}s " : "") . "{$millis}ms";
80 2
81
        if (getenv('GITHUB_WORKFLOW')) {
82 1
            $this->output->writeln("::endgroup::");
83 1
        } elseif (getenv('GITLAB_CI')) {
84 1
            $sectionId = md5($task->getName());
85
            $endTime = round($endTime / 1000);
86
            $this->output->writeln("\e[0Ksection_end:{$endTime}:{$sectionId}\r\e[0K");
87
        } elseif ($this->output->isVeryVerbose()) {
88
            $this->output->writeln("<fg=yellow;options=bold>done</> {$task->getName()} $taskTime");
89
        }
90
        if ($error) {
91
            $this->output->writeln("\e[0K\e[31;1mERROR: Task {$task->getName()} failed!\e[0;m");
92
            return;
93
        }
94
        $this->logger->log("done {$task->getName()} $taskTime");
95
96
        if (!empty($this->input->getOption('profile'))) {
97
            $line = sprintf("%s\t%s\n", $task->getName(), $taskTime);
98
            file_put_contents($this->input->getOption('profile'), $line, FILE_APPEND);
99 1
        }
100 1
    }
101
102
    public function endOnHost(Host $host): void
103 1
    {
104 1
        if ($this->output->isVeryVerbose()) {
105 1
            $this->output->writeln("<fg=yellow;options=bold>done</> on $host");
106 1
        }
107 1
    }
108 1
109 1
    public function renderException(Throwable $exception, Host $host): void
110
    {
111 1
        if ($exception instanceof RunException) {
112 1
113 1
            $message = "";
114 1
            $message .= "[$host] <fg=white;bg=red> error </> <comment>in {$exception->getTaskFilename()} on line {$exception->getTaskLineNumber()}:</>\n";
115 1
            if ($this->output->getVerbosity() === Output::VERBOSITY_NORMAL) {
116
                $message .= "[$host] <fg=green;options=bold>run</> {$exception->getCommand()}\n";
117
                foreach (explode("\n", $exception->getErrorOutput()) as $line) {
118
                    $line = trim($line);
119 1
                    if ($line !== "") {
120 1
                        $message .= "[$host] <fg=red>err</> $line\n";
121
                    }
122
                }
123
                foreach (explode("\n", $exception->getOutput()) as $line) {
124
                    $line = trim($line);
125
                    if ($line !== "") {
126
                        $message .= "[$host] $line\n";
127
                    }
128 1
                }
129
            }
130
            $message .= "[$host] <fg=red>exit code</> {$exception->getExitCode()} ({$exception->getExitCodeText()})\n";
131 2
            $this->output->write($message);
132
133
        } else {
134 2
            $message = "";
135
            $class = get_class($exception);
136
            $file = basename($exception->getFile());
137
            $line = $exception->getLine();
138
            if ($exception instanceof Exception) {
139
                $file = $exception->getTaskFilename();
140
                $line = $exception->getTaskLineNumber();
141
            }
142
            $message .= "[$host] <fg=white;bg=red> $class </> <comment>in $file on line $line:</>\n";
143
            $message .= "[$host]\n";
144
            foreach (explode("\n", $exception->getMessage()) as $line) {
145
                $line = trim($line);
146
                if ($line !== "") {
147
                    $message .= "[$host]   <comment>$line</comment>\n";
148
                }
149
            }
150
            $message .= "[$host]\n";
151
            if ($this->output->isDebug()) {
152
                foreach (explode("\n", $exception->getTraceAsString()) as $line) {
153
                    $line = trim($line);
154
                    if ($line !== "") {
155
                        $message .= "[$host] $line\n";
156
                    }
157
                }
158
            }
159
            $this->output->write($message);
160
        }
161
162
        $this->logger->log($exception->__toString());
163
164
        if ($exception->getPrevious()) {
165
            $this->renderException($exception->getPrevious(), $host);
166
        }
167
    }
168
}
169