Completed
Push — master ( 061d1e...31cfb3 )
by Greg
02:26
created

src/Log/ResultPrinter.php (1 issue)

Check for unnecessary variable assignments.

Unused Code Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Robo\Log;
3
4
use Robo\Result;
5
use Robo\Contract\PrintedInterface;
6
use Robo\Contract\ProgressIndicatorAwareInterface;
7
use Robo\Contract\VerbosityThresholdInterface;
8
use Robo\Common\ProgressIndicatorAwareTrait;
9
10
use Psr\Log\LogLevel;
11
use Psr\Log\LoggerAwareInterface;
12
use Psr\Log\LoggerAwareTrait;
13
use Consolidation\Log\ConsoleLogLevel;
14
15
/**
16
 * Log the creation of Result objects.
17
 */
18
class ResultPrinter implements LoggerAwareInterface, ProgressIndicatorAwareInterface
19
{
20
    use LoggerAwareTrait;
21
    use ProgressIndicatorAwareTrait;
22
23
    /**
24
     * Log the result of a Robo task.
25
     *
26
     * Returns 'true' if the message is printed, or false if it isn't.
27
     *
28
     * @param \Robo\Result $result
29
     *
30
     * @return bool
31
     */
32
    public function printResult(Result $result)
33
    {
34
        $task = $result->getTask();
35
        if ($task instanceof VerbosityThresholdInterface && !$task->verbosityMeetsThreshold()) {
36
            return;
37
        }
38
        if (!$result->wasSuccessful()) {
39
            return $this->printError($result);
40
        } else {
41
            return $this->printSuccess($result);
42
        }
43
    }
44
45
    /**
46
     * Log that we are about to abort due to an error being encountered
47
     * in 'stop on fail' mode.
48
     *
49
     * @param \Robo\Result $result
50
     */
51
    public function printStopOnFail($result)
52
    {
53
        $this->printMessage(LogLevel::NOTICE, 'Stopping on fail. Exiting....');
54
        $this->printMessage(LogLevel::ERROR, 'Exit Code: {code}', ['code' => $result->getExitCode()]);
55
    }
56
57
    /**
58
     * Log the result of a Robo task that returned an error.
59
     *
60
     * @param \Robo\Result $result
61
     *
62
     * @return bool
63
     */
64
    protected function printError(Result $result)
65
    {
66
        $task = $result->getTask();
67
        $context = $result->getContext() + ['timer-label' => 'Time', '_style' => []];
68
        $context['_style']['message'] = '';
69
70
        $printOutput = true;
71
        if ($task instanceof PrintedInterface) {
72
            $printOutput = !$task->getPrinted();
73
        }
74
        if ($printOutput) {
75
            $this->printMessage(LogLevel::ERROR, "{message}", $context);
76
        }
77
        $this->printMessage(LogLevel::ERROR, 'Exit code {code}', $context);
78
        return true;
79
    }
80
81
    /**
82
     * Log the result of a Robo task that was successful.
83
     *
84
     * @param \Robo\Result $result
85
     *
86
     * @return bool
87
     */
88
    protected function printSuccess(Result $result)
89
    {
90
        $task = $result->getTask();
0 ignored issues
show
$task is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
        $context = $result->getContext() + ['timer-label' => 'in'];
92
        $time = $result->getExecutionTime();
93
        if ($time) {
94
            $this->printMessage(ConsoleLogLevel::SUCCESS, 'Done', $context);
95
        }
96
        return false;
97
    }
98
99
    /**
100
     * @param string $level
101
     * @param string $message
102
     * @param array $context
103
     */
104
    protected function printMessage($level, $message, $context = [])
105
    {
106
        $inProgress = $this->hideProgressIndicator();
107
        $this->logger->log($level, $message, $context);
108
        if ($inProgress) {
109
            $this->restoreProgressIndicator($inProgress);
110
        }
111
    }
112
}
113