Completed
Push — master ( 8b8afe...5f2bbe )
by Greg
02:21
created

src/Common/TaskIO.php (3 issues)

Labels
Severity

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\Common;
3
4
use Robo\Robo;
5
use Robo\TaskInfo;
6
use Consolidation\Log\ConsoleLogLevel;
7
use Psr\Log\LoggerAwareTrait;
8
use Psr\Log\LogLevel;
9
use Robo\Contract\ProgressIndicatorAwareInterface;
10
11
/**
12
 * Task input/output methods.  TaskIO is 'used' in BaseTask, so any
13
 * task that extends this class has access to all of the methods here.
14
 * printTaskInfo, printTaskSuccess, and printTaskError are the three
15
 * primary output methods that tasks are encouraged to use.  Tasks should
16
 * avoid using the IO trait output methods.
17
 */
18
trait TaskIO
19
{
20
    use LoggerAwareTrait;
21
    use ConfigAwareTrait;
22
    use VerbosityThresholdTrait;
23
24
    /**
25
     * @return mixed|null|\Psr\Log\LoggerInterface
26
     */
27
    public function logger()
28
    {
29
        // $this->logger should always be set in Robo core tasks.
30
        if ($this->logger) {
31
            return $this->logger;
32
        }
33
34
        // TODO: Remove call to Robo::logger() once maintaining backwards
35
        // compatibility with legacy external Robo tasks is no longer desired.
36
        if (!Robo::logger()) {
37
            return null;
38
        }
39
40
        static $gaveDeprecationWarning = false;
41
        if (!$gaveDeprecationWarning) {
42
            trigger_error('No logger set for ' . get_class($this) . '. Use $this->task(Foo::class) rather than new Foo() in loadTasks to ensure the builder can initialize task the task, or use $this->collectionBuilder()->taskFoo() if creating one task from within another.', E_USER_DEPRECATED);
43
            $gaveDeprecationWarning = true;
44
        }
45
        return Robo::logger();
46
    }
47
48
    /**
49
     * Print information about a task in progress.
50
     *
51
     * With the Symfony Console logger, NOTICE is displayed at VERBOSITY_VERBOSE
52
     * and INFO is displayed at VERBOSITY_VERY_VERBOSE.
53
     *
54
     * Robo overrides the default such that NOTICE is displayed at
55
     * VERBOSITY_NORMAL and INFO is displayed at VERBOSITY_VERBOSE.
56
     *
57
     * n.b. We should probably have printTaskNotice for our ordinary
58
     * output, and use printTaskInfo for less interesting messages.
59
     *
60
     * @param string $text
61
     * @param null|array $context
62
     */
63
    protected function printTaskInfo($text, $context = null)
64
    {
65
        // The 'note' style is used for both 'notice' and 'info' log levels;
66
        // However, 'notice' is printed at VERBOSITY_NORMAL, whereas 'info'
67
        // is only printed at VERBOSITY_VERBOSE.
68
        $this->printTaskOutput(LogLevel::NOTICE, $text, $this->getTaskContext($context));
69
    }
70
71
    /**
72
     * Provide notification that some part of the task succeeded.
73
     *
74
     * With the Symfony Console logger, success messages are remapped to NOTICE,
75
     * and displayed in VERBOSITY_VERBOSE. When used with the Robo logger,
76
     * success messages are displayed at VERBOSITY_NORMAL.
77
     *
78
     * @param string $text
79
     * @param null|array $context
80
     */
81
    protected function printTaskSuccess($text, $context = null)
82
    {
83
        // Not all loggers will recognize ConsoleLogLevel::SUCCESS.
84
        // We therefore log as LogLevel::NOTICE, and apply a '_level'
85
        // override in the context so that this message will be
86
        // logged as SUCCESS if that log level is recognized.
87
        $context['_level'] = ConsoleLogLevel::SUCCESS;
88
        $this->printTaskOutput(LogLevel::NOTICE, $text, $this->getTaskContext($context));
89
    }
90
91
    /**
92
     * Provide notification that there is something wrong, but
93
     * execution can continue.
94
     *
95
     * Warning messages are displayed at VERBOSITY_NORMAL.
96
     *
97
     * @param string $text
98
     * @param null|array $context
99
     */
100
    protected function printTaskWarning($text, $context = null)
101
    {
102
        $this->printTaskOutput(LogLevel::WARNING, $text, $this->getTaskContext($context));
103
    }
104
105
    /**
106
     * Provide notification that some operation in the task failed,
107
     * and the task cannot continue.
108
     *
109
     * Error messages are displayed at VERBOSITY_NORMAL.
110
     *
111
     * @param string $text
112
     * @param null|array $context
113
     */
114
    protected function printTaskError($text, $context = null)
115
    {
116
        $this->printTaskOutput(LogLevel::ERROR, $text, $this->getTaskContext($context));
117
    }
118
119
    /**
120
     * Provide debugging notification.  These messages are only
121
     * displayed if the log level is VERBOSITY_DEBUG.
122
     *
123
     * @param string$text
124
     * @param null|array $context
125
     */
126
    protected function printTaskDebug($text, $context = null)
127
    {
128
        $this->printTaskOutput(LogLevel::DEBUG, $text, $this->getTaskContext($context));
129
    }
130
131
    /**
132
     * @param string $level
133
     *   One of the \Psr\Log\LogLevel constant
134
     * @param string $text
135
     * @param null|array $context
136
     */
137
    protected function printTaskOutput($level, $text, $context)
138
    {
139
        if (!$this->verbosityMeetsThreshold()) {
140
            return;
141
        }
142
        $logger = $this->logger();
143
        if (!$logger) {
144
            return;
145
        }
146
        // Hide the progress indicator, if it is visible.
147
        $inProgress = $this->hideTaskProgress();
148
        $logger->log($level, $text, $this->getTaskContext($context));
149
        // After we have printed our log message, redraw the progress indicator.
150
        $this->showTaskProgress($inProgress);
151
    }
152
153
    /**
154
     * @return bool
155
     */
156
    protected function hideTaskProgress()
157
    {
158
        $inProgress = false;
159
        if ($this instanceof ProgressIndicatorAwareInterface) {
160
            $inProgress = $this->inProgress();
161
        }
162
163
        // If a progress indicator is running on this task, then we mush
164
        // hide it before we print anything, or its display will be overwritten.
165
        if ($inProgress) {
166
            $inProgress = $this->hideProgressIndicator();
0 ignored issues
show
It seems like hideProgressIndicator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
167
        }
168
        return $inProgress;
169
    }
170
171
    /**
172
     * @param $inProgress
173
     */
174
    protected function showTaskProgress($inProgress)
175
    {
176
        if ($inProgress) {
177
            $this->restoreProgressIndicator($inProgress);
0 ignored issues
show
It seems like restoreProgressIndicator() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
178
        }
179
    }
180
181
    /**
182
     * Format a quantity of bytes.
183
     *
184
     * @param int $size
185
     * @param int $precision
186
     *
187
     * @return string
188
     */
189
    protected function formatBytes($size, $precision = 2)
190
    {
191
        if ($size === 0) {
192
            return 0;
193
        }
194
        $base = log($size, 1024);
195
        $suffixes = array('', 'k', 'M', 'G', 'T');
196
        return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
197
    }
198
199
    /**
200
     * Get the formatted task name for use in task output.
201
     * This is placed in the task context under 'name', and
202
     * used as the log label by Robo\Common\RoboLogStyle,
203
     * which is inserted at the head of log messages by
204
     * Robo\Common\CustomLogStyle::formatMessage().
205
     *
206
     * @param null|object $task
207
     *
208
     * @return string
209
     */
210
    protected function getPrintedTaskName($task = null)
211
    {
212
        if (!$task) {
213
            $task = $this;
214
        }
215
        return TaskInfo::formatTaskName($task);
0 ignored issues
show
It seems like $task can also be of type this<Robo\Common\TaskIO>; however, Robo\TaskInfo::formatTaskName() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
216
    }
217
218
    /**
219
     * @param null|array $context
220
     *
221
     * @return array with context information
222
     */
223
    protected function getTaskContext($context = null)
224
    {
225
        if (!$context) {
226
            $context = [];
227
        }
228
        if (!is_array($context)) {
229
            $context = ['task' => $context];
230
        }
231
        if (!array_key_exists('task', $context)) {
232
            $context['task'] = $this;
233
        }
234
235
        return $context + TaskInfo::getTaskContext($context['task']);
236
    }
237
}
238