Padding   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 4

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 95.12%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 132
ccs 39
cts 41
cp 0.9512
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A char() 0 6 1
A length() 0 6 1
A getLength() 0 8 2
A padContent() 0 12 2
A result() 0 4 1
A label() 0 18 2
1
<?php
2
3
namespace League\CLImate\TerminalObject\Dynamic;
4
5
class Padding extends DynamicTerminalObject
6
{
7
    /**
8
     * The length that lines should be padded to
9
     *
10
     * @var integer $length
11
     */
12
    protected $length = 0;
13
14
    /**
15
     * The character(s) that should be used to pad
16
     *
17
     * @var string $char
18
     */
19
    protected $char = '.';
20
21
22
    /**
23
     * If they pass in a padding character, set the char
24
     *
25
     * @param int $length
26
     * @param string $char
27
     */
28 16
    public function __construct($length = null, $char = null)
29
    {
30 16
        if ($length !== null) {
31 12
            $this->length($length);
32 12
        }
33
34 16
        if (is_string($char)) {
35
            $this->char($char);
36
        }
37 16
    }
38
39
    /**
40
     * Set the character(s) that should be used to pad
41
     *
42
     * @param string $char
43
     *
44
     * @return \League\CLImate\TerminalObject\Dynamic\Padding
45
     */
46 8
    public function char($char)
47
    {
48 8
        $this->char = $char;
49
50 8
        return $this;
51
    }
52
53
    /**
54
     * Set the length of the line that should be generated
55
     *
56
     * @param integer $length
57
     *
58
     * @return \League\CLImate\TerminalObject\Dynamic\Padding
59
     */
60 12
    public function length($length)
61
    {
62 12
        $this->length = $length;
63
64 12
        return $this;
65
    }
66
67
    /**
68
     * Get the length of the line based on the width of the terminal window
69
     *
70
     * @return integer
71
     */
72 16
    protected function getLength()
73
    {
74 16
        if (!$this->length) {
75 4
            $this->length = $this->util->width();
76 4
        }
77
78 16
        return $this->length;
79
    }
80
81
    /**
82
     * Pad the content with the characters
83
     *
84
     * @param string $content
85
     *
86
     * @return string
87
     */
88 16
    protected function padContent($content)
89
    {
90 16
        if (strlen($this->char) > 0) {
91 16
            $length = $this->getLength();
92 16
            $padding_length = ceil($length / strlen($this->char));
93
94 16
            $padding = str_repeat($this->char, $padding_length);
95 16
            $content .= substr($padding, 0, $length - strlen($content));
96 16
        }
97
98 16
        return $content;
99
    }
100
101
    /**
102
     * Output the content and pad to the previously defined length
103
     *
104
     * @param string $content
105
     *
106
     * @return \League\CLImate\TerminalObject\Dynamic\Padding
107
     */
108 16
    public function label($content)
109
    {
110
        // Handle long labels by splitting them across several lines
111 16
        $lines   = str_split($content, $this->util->width());
112 16
        $content = array_pop($lines);
113
114 16
        foreach ($lines as $line) {
115 4
            $this->output->write($this->parser->apply($line));
116 16
        }
117
118 16
        $content = $this->padContent($content);
119 16
        $content = $this->parser->apply($content);
120
121 16
        $this->output->sameLine();
122 16
        $this->output->write($content);
123
124 16
        return $this;
125
    }
126
127
    /**
128
     * Output result
129
     *
130
     * @param string $content
131
     */
132 16
    public function result($content)
133
    {
134 16
        $this->output->write($this->parser->apply(' ' . $content));
135 16
    }
136
}
137