Completed
Pull Request — master (#10)
by Tomáš
04:48 queued 01:50
created

ConsoleIo   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 62
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A write() 0 5 2
B overwrite() 0 29 5
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Io;
13
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
final class ConsoleIo
17
{
18
    /**
19
     * @var OutputInterface
20
     */
21
    private $output;
22
23
    /**
24
     * @var string
25
     */
26
    private $lastMessage;
27
28
    public function __construct(OutputInterface $output)
29
    {
30
        $this->output = $output;
31
    }
32
33
    /**
34
     * @param string|array $messages
35
     * @param bool         $newline
36
     */
37
    public function write($messages, bool $newline = true)
38
    {
39
        $this->output->write($messages, $newline);
40
        $this->lastMessage = implode($newline ? "\n" : '', (array) $messages);
41
    }
42
43
    /**
44
     * @param string|array $messages
45
     * @param bool         $newline
46
     * @param int          $size
47
     */
48
    public function overwrite($messages, bool $newline = true, int $size = null)
49
    {
50
        // messages can be an array, let's convert it to string anyway
51
        $messages = implode($newline ? "\n" : '', (array) $messages);
52
53
        // since overwrite is supposed to overwrite last message...
54
        if (!isset($size)) {
55
            // removing possible formatting of lastMessage with strip_tags
56
            $size = strlen(strip_tags($this->lastMessage));
57
        }
58
        // ...let's fill its length with backspaces
59
        $this->write(str_repeat("\x08", $size), false);
60
61
        // write the new message
62
        $this->write($messages, false);
63
64
        $fill = $size - strlen(strip_tags($messages));
65
        if ($fill > 0) {
66
            // whitespace whatever has left
67
            $this->write(str_repeat(' ', $fill), false);
68
            // move the cursor back
69
            $this->write(str_repeat("\x08", $fill), false);
70
        }
71
72
        if ($newline) {
73
            $this->write('');
74
        }
75
        $this->lastMessage = $messages;
76
    }
77
}
78