StringExcerpt   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
cbo 1
dl 0
loc 126
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B toTerminal() 0 24 4
A toText() 0 4 1
B toHtml() 0 25 4
A __toString() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the ILess
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace ILess\Util;
11
12
use ILess\CLI\ANSIColor;
13
14
/**
15
 * String excerpt.
16
 */
17
final class StringExcerpt
18
{
19
    /**
20
     * Array of lines.
21
     *
22
     * @var array
23
     */
24
    protected $lines = [];
25
26
    /**
27
     * Current line.
28
     *
29
     * @var int
30
     */
31
    protected $currentLine;
32
33
    /**
34
     * Current column.
35
     *
36
     * @var int
37
     */
38
    protected $currentColumn;
39
40
    /**
41
     * The line numbers width.
42
     *
43
     * @var string
44
     */
45
    protected $lineWidth;
46
47
    /**
48
     * Constructor.
49
     *
50
     * @param array $lines The array of lines
51
     * @param int $currentLine The current line
52
     * @param int $currentColumn The current column
53
     */
54
    public function __construct(array $lines, $currentLine, $currentColumn = null)
55
    {
56
        $this->lines = $lines;
57
        $this->currentLine = $currentLine;
58
        $this->currentColumn = $currentColumn;
59
        $this->lineWidth = strlen((string) key($this->lines));
0 ignored issues
show
Documentation Bug introduced by
The property $lineWidth was declared of type string, but strlen((string) key($this->lines)) is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
60
    }
61
62
    /**
63
     * Converts the exceprt to colorized string for terminal.
64
     *
65
     * @return string
66
     */
67
    public function toTerminal()
68
    {
69
        $output = '';
70
        foreach ($this->lines as $lineNumber => $lineContent) {
71
            if ($lineNumber + 1 == $this->currentLine) {
72
                // current column will be highlighted
73
                if ($this->currentColumn !== null) {
74
                    $output .= ANSIColor::colorize(sprintf("%{$this->lineWidth}s: ", $lineNumber + 1), 'grey+inverse');
75
                    $output .= ANSIColor::colorize(substr($lineContent, 0, $this->currentColumn - 1), 'grey');
76
                    $output .= ANSIColor::colorize(substr($lineContent, $this->currentColumn - 1, 1), 'red+bold');
77
                    $output .= ANSIColor::colorize(substr($lineContent, $this->currentColumn), 'red');
78
                    $output .= "\n";
79
                } else {
80
                    $output .= ANSIColor::colorize(sprintf("%{$this->lineWidth}s: %s\n",
81
                        $lineNumber + 1, $lineContent), 'red');
82
                }
83
            } else {
84
                $output .= ANSIColor::colorize(sprintf("%{$this->lineWidth}s: %s\n", $lineNumber + 1, $lineContent),
85
                    'grey');
86
            }
87
        }
88
89
        return $output;
90
    }
91
92
    /**
93
     * Converts the except to plain text.
94
     *
95
     * @return string
96
     */
97
    public function toText()
98
    {
99
        return strip_tags($this->toHtml());
100
    }
101
102
    /**
103
     * Coverts the lines to HTML.
104
     *
105
     * @return string The HTML
106
     */
107
    public function toHtml()
108
    {
109
        $html = '';
110
        foreach ($this->lines as $lineNumber => $lineContent) {
111
            if ($lineNumber + 1 == $this->currentLine) {
112
                // current column will be highlighted
113
                if ($this->currentColumn !== null) {
114
                    $html .= sprintf("<span class=\"iless-line iless-current-line\">%{$this->lineWidth}s: %s</span>\n",
115
                        $lineNumber + 1,
116
                        substr_replace($lineContent,
117
                            '<span class="iless-current-column">' . substr($lineContent, $this->currentColumn - 1,
118
                                1) . '</span>', $this->currentColumn - 1, 1));
119
                } else {
120
                    $html .= sprintf("<span class=\"iless-line iless-current-line\">%{$this->lineWidth}s: %s</span>\n",
121
                        $lineNumber + 1, $lineContent
122
                    );
123
                }
124
            } else {
125
                $html .= sprintf("<span class=\"iless-line\">%{$this->lineWidth}s: %s</span>\n",
126
                    $lineNumber + 1, $lineContent);
127
            }
128
        }
129
130
        return $html;
131
    }
132
133
    /**
134
     * Converts the object to string.
135
     *
136
     * @return string
137
     */
138
    public function __toString()
139
    {
140
        return $this->toText();
141
    }
142
}
143