Completed
Push — develop ( fa94ad...c6900b )
by Michał
16:07
created

AdditionalStyles::setTimeCharLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BlueConsole;
6
7
use Symfony\Component\Console\Helper\FormatterHelper;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
trait AdditionalStyles
12
{
13
    /**
14
     * @var int
15
     */
16
    protected $align = 12;
17
18
    /**
19
     * @var int
20
     */
21
    protected $timeCharLength = 14;
22
23
    /**
24
     * @var int|float
25
     */
26
    protected $time;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $showTimer = false;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $timerTypeDateTime = false;
37
38
    /**
39
     * @var FormatterHelper
40
     */
41
    protected $formatter;
42
43
    /**
44
     * @var string
45
     */
46
    protected $dateTimeFormat = 'c';
47
48
    /**
49
     * Style constructor.
50
     *
51
     * @param InputInterface $input
52
     * @param OutputInterface $output
53
     * @param FormatterHelper $formatter
54
     */
55 140
    public function __construct(InputInterface $input, OutputInterface $output, FormatterHelper $formatter)
56
    {
57 140
        $this->formatter = $formatter;
58
59 140
        parent::__construct($input, $output);
60
61 140
        $this->startTime();
62 140
    }
63
64
    /**
65
     * @param string $message
66
     * @param int $length
67
     * @param string $suffix
68
     * @return Style
69
     */
70 8
    public function truncate(string $message, int $length, string $suffix = '...'): self
71
    {
72 8
        $this->write($this->truncateMessage($message, $length, $suffix));
0 ignored issues
show
Bug introduced by
It seems like write() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        $this->/** @scrutinizer ignore-call */ 
73
               write($this->truncateMessage($message, $length, $suffix));
Loading history...
73
74 8
        return $this;
75
    }
76
77
    /**
78
     * @param string $message
79
     * @param int $length
80
     * @param string $suffix
81
     * @return Style
82
     */
83 4
    public function truncateLn(string $message, int $length, string $suffix = '...'): self
84
    {
85 4
        $this->writeln($this->truncateMessage($message, $length, $suffix));
0 ignored issues
show
Bug introduced by
It seems like writeln() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        $this->/** @scrutinizer ignore-call */ 
86
               writeln($this->truncateMessage($message, $length, $suffix));
Loading history...
86
87 4
        return $this;
88
    }
89
90
    /**
91
     * @param string $message
92
     * @param int $length
93
     * @param string $suffix
94
     * @return string
95
     */
96 16
    public function truncateMessage(string $message, int $length, string $suffix = '...'): string
97
    {
98 16
        return $this->formatter->truncate($message, $length, $suffix);
99
    }
100
101
    /**
102
     * Turn o/off show timer after info block
103
     */
104 36
    public function toggleShowTimer(): void
105
    {
106 36
        $this->showTimer = !$this->showTimer;
107 36
    }
108
109
    /**
110
     * Turn o/off show timer after info block
111
     */
112 16
    public function toggleTimerType(): void
113
    {
114 16
        $this->timerTypeDateTime = !$this->timerTypeDateTime;
115 16
    }
116
117
    /**
118
     * @return bool
119
     */
120 4
    public function isTimerOn(): bool
121
    {
122 4
        return $this->showTimer;
123
    }
124
125
    /**
126
     * @param int|null $time
127
     */
128 140
    public function startTime(?int $time = null): void
129
    {
130 140
        if (!$time) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $time of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
131 140
            $time = \microtime(true);
132
        }
133
134 140
        $this->time = $time;
135 140
    }
136
137
    /**
138
     * @param bool $checkEnableForBlock
139
     * @return string|null
140
     * @throws \Exception
141
     */
142 92
    protected function getTimer(bool $checkEnableForBlock = false): ?string
143
    {
144 92
        if ($checkEnableForBlock && !$this->showTimer) {
145 56
            return null;
146
        }
147
148 36
        if ($this->timerTypeDateTime) {
149 16
            return $this->getTimerDateTime();
150
        }
151
152 20
        return $this->getTimerDefault();
153
    }
154
155
    /**
156
     * @return string
157
     * @throws \Exception
158
     */
159 16
    protected function getTimerDateTime(): string
160
    {
161 16
        $dateTime = (new \DateTime())->format($this->dateTimeFormat);
162
163 16
        return "[ <options=bold>$dateTime</> ]";
164
    }
165
166
    /**
167
     * @return string
168
     */
169 4
    public function getDateTimeFormat(): string
170
    {
171 4
        return $this->dateTimeFormat;
172
    }
173
174
    /**
175
     * @param string $format
176
     * @return void
177
     */
178 4
    public function setDateTimeFormat(string $format): void
179
    {
180 4
        $this->dateTimeFormat = $format;
181 4
    }
182
183
    /**
184
     * @return string
185
     */
186 20
    protected function getTimerDefault(): string
187
    {
188 20
        $days = null;
189 20
        $current = \microtime(true);
190 20
        $calc = $current - $this->time;
191
192 20
        if ($calc > self::DAY_SECONDS) {
0 ignored issues
show
Bug introduced by
The constant BlueConsole\AdditionalStyles::DAY_SECONDS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
193
            $days = \round($calc / self::DAY_SECONDS);
194
            $calc -= self::DAY_SECONDS * $days;
195
            $days .= 'd ';
196
197
            $this->timeCharLength -= \strlen($days);
198
        }
199
200 20
        $formatted = \sprintf("%0{$this->timeCharLength}.4f", $calc);
201
202 20
        return "[ <options=bold>$days$formatted</> ]";
203
    }
204
205
    /**
206
     * @param int $align
207
     * @return $this
208
     */
209 4
    public function setAlign(int $align): self
210
    {
211 4
        $this->align = $align;
212
213 4
        return $this;
214
    }
215
216
    /**
217
     * @return int
218
     */
219 4
    public function getAlign(): int
220
    {
221 4
        return $this->align;
222
    }
223
224
    /**
225
     * @param int $align
226
     * @return $this
227
     */
228 4
    public function setTimeCharLength(int $align): self
229
    {
230 4
        $this->timeCharLength = $align;
231
232 4
        return $this;
233
    }
234
235
    /**
236
     * @return int
237
     */
238 4
    public function getTimeCharLength(): int
239
    {
240 4
        return $this->timeCharLength;
241
    }
242
243
    /**
244
     * add some spaces after string depends of it length
245
     * $string can be numeric/string/array, depends of type it work different
246
     * if its int inform about string length
247
     * if its string or object (__toString) it calculate string length for alignment
248
     * if its array, sum all strings in array length nad add +1for delimiter
249
     *
250
     * @param int|string|array|object $string
251
     * @param int $align
252
     * @return string
253
     */
254 88
    public function align($string, int $align): string
255
    {
256 88
        $strLength = 0;
257
258
        switch (true) {
259 88
            case \is_int($string):
260 76
                $strLength = $string;
261 76
                break;
262
263 36
            case \is_string($string) || \is_object($string):
264 28
                $strLength = \mb_strlen((string)$string);
265 28
                break;
266
267 8
            case \is_array($string):
268 4
                foreach ($string as $message) {
269 4
                    $strLength += \mb_strlen($message) + 1;
270
                }
271 4
                break;
272
273
            default:
274 4
                break;
275
        }
276
277 88
        $newAlign = ' ';
278 88
        $spaces = $align - $strLength;
279
280 88
        for ($i = 1; $i <= $spaces; $i++) {
281 88
            $newAlign .= ' ';
282
        }
283
284 88
        return $newAlign;
285
    }
286
}
287