1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/console-diff-renderer. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/console-diff-renderer/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/console-diff-renderer |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DiffRenderer\Wrap; |
15
|
|
|
|
16
|
|
|
use Graze\DiffRenderer\Terminal\DimensionsInterface; |
17
|
|
|
|
18
|
|
|
class Wrapper |
19
|
|
|
{ |
20
|
|
|
/** @var int */ |
21
|
|
|
private $width = DimensionsInterface::DEFAULT_WIDTH; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Wrapper constructor. |
25
|
|
|
* |
26
|
|
|
* @param int $width |
27
|
|
|
*/ |
28
|
|
|
public function __construct($width) |
29
|
|
|
{ |
30
|
|
|
$this->width = $width; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return int |
35
|
|
|
*/ |
36
|
|
|
public function getWidth() |
37
|
|
|
{ |
38
|
|
|
return $this->width; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param int $width |
43
|
|
|
* |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
|
public function setWidth($width) |
47
|
|
|
{ |
48
|
|
|
$this->width = $width; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string[] $input |
54
|
|
|
* |
55
|
|
|
* @return string[] |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function wrap(array $input) |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$out = []; |
60
|
|
|
foreach ($input as $line) { |
61
|
|
|
foreach ($this->chunk($line) as $new) { |
62
|
|
|
$out[] = $new; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return $out; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string[] $input |
70
|
|
|
* |
71
|
|
|
* @return string[] |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function trim(array $input) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$out = []; |
76
|
|
|
foreach ($input as $line) { |
77
|
|
|
$chunk = $this->chunk($line); |
78
|
|
|
$out[] = reset($chunk); |
79
|
|
|
} |
80
|
|
|
return $out; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $line |
85
|
|
|
* |
86
|
|
|
* @return string[] |
87
|
|
|
*/ |
88
|
|
|
private function chunk($line) |
89
|
|
|
{ |
90
|
|
|
if (mb_strlen($line) <= $this->width) { |
91
|
|
|
return [$line]; |
92
|
|
|
} |
93
|
|
|
$stripped = strip_tags($line); |
94
|
|
|
$offset = 0; |
95
|
|
|
$out = []; |
96
|
|
|
|
97
|
|
|
// create a stripped tags version of the string |
98
|
|
|
// loop through both and only move the counter if both characters are equal |
99
|
|
|
// yield when we get to <width> for the stripped tags version |
100
|
|
|
for ($i = 0, $j = 0; $i <= mb_strlen($stripped); $j++) { |
101
|
|
|
if (mb_substr($stripped, $i, 1) === mb_substr($line, $j, 1)) { |
102
|
|
|
if ($i >= $this->width && $i % $this->width == 0) { |
103
|
|
|
$out[] = mb_substr($line, $offset, $j - $offset); |
104
|
|
|
$offset = $j; |
105
|
|
|
} |
106
|
|
|
$i++; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// return any remaining entries |
111
|
|
|
if ($offset != $j - 1) { |
112
|
|
|
$out[] = mb_substr($line, $offset); |
113
|
|
|
} |
114
|
|
|
return $out; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.