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 ( e2875b...35ced9 )
by Anton
03:41
created

Messenger   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 118
Duplicated Lines 20.34 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 72.06%

Importance

Changes 0
Metric Value
dl 24
loc 118
ccs 49
cts 68
cp 0.7206
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A startTask() 0 7 2
A endOnHost() 0 6 2
C renderException() 24 57 14
B endTask() 0 25 6

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