@@ -71,38 +71,43 @@ class HtmlArray extends RendererAbstract
71
71
* into string at the given start offset.
72
72
* @return string|array The result string is returned. If string is an array then array is returned.
73
73
*/
74
- public function mb_substr_replace($string, $replacement, $start, $length=NULL) {
74
+ public function mbSubstrReplace($string, $replacement, $start, $length = null)
75
+ {
75
76
if (is_array($string)) {
76
77
$num = count($string);
77
78
// $replacement
78
- $replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array($replacement), $num, $replacement);
79
+ if (is_array($replacement)) {
80
+ $replacement = array_slice($replacement, 0, $num);
81
+ } else {
82
+ $replacement = array_pad(array($replacement), $num, $replacement);
83
+ }
84
+
79
85
// $start
80
86
if (is_array($start)) {
81
87
$start = array_slice($start, 0, $num);
82
88
foreach ($start as $key => $value)
83
89
$start[$key] = is_int($value) ? $value : 0;
84
- }
85
- else {
90
+ } else {
86
91
$start = array_pad(array($start), $num, $start);
87
92
}
88
93
// $length
89
94
if (!isset($length)) {
90
95
$length = array_fill(0, $num, 0);
91
- }
92
- elseif (is_array($length)) {
96
+ } elseif (is_array($length)) {
93
97
$length = array_slice($length, 0, $num);
94
98
foreach ($length as $key => $value)
95
99
$length[$key] = isset($value) ? (is_int($value) ? $value : $num) : 0;
96
- }
97
- else {
100
+ } else {
98
101
$length = array_pad(array($length), $num, $length);
99
102
}
100
103
// Recursive call
101
- return array_map(array($this, 'mb_substr_replace '), $string, $replacement, $start, $length);
104
+ return array_map(array($this, 'mbSubstrReplace '), $string, $replacement, $start, $length);
102
105
}
103
106
preg_match_all('/./us', (string)$string, $smatches);
104
107
preg_match_all('/./us', (string)$replacement, $rmatches);
105
- if ($length === NULL) $length = mb_strlen($string);
108
+ if ($length === null) {
109
+ $length = mb_strlen($string);
110
+ }
106
111
array_splice($smatches['0'], $start, $length, $rmatches[0]);
107
112
return join($smatches['0']);
108
113
}
@@ -124,20 +129,20 @@ public function render()
124
129
125
130
$changes = array();
126
131
$opCodes = $this->diff->getGroupedOpcodes();
127
- foreach($opCodes as $group) {
132
+ foreach ($opCodes as $group) {
128
133
$blocks = array();
129
134
$lastTag = null;
130
135
$lastBlock = 0;
131
- foreach($group as $code) {
136
+ foreach ($group as $code) {
132
137
list($tag, $i1, $i2, $j1, $j2) = $code;
133
138
134
- if($tag == 'replace' && $i2 - $i1 == $j2 - $j1) {
135
- for($i = 0; $i < ($i2 - $i1); ++$i) {
139
+ if ($tag == 'replace' && $i2 - $i1 == $j2 - $j1) {
140
+ for ($i = 0; $i < ($i2 - $i1); ++$i) {
136
141
$fromLine = $a[$i1 + $i];
137
142
$toLine = $b[$j1 + $i];
138
143
139
144
list($start, $end) = $this->getChangeExtent($fromLine, $toLine);
140
- if($start != 0 || $end != 0) {
145
+ if ($start != 0 || $end != 0) {
141
146
$realEnd = mb_strlen($fromLine) + $end;
142
147
143
148
$fromLine = mb_substr($fromLine, 0, $start) . "\0" .
@@ -155,28 +160,27 @@ public function render()
155
160
}
156
161
}
157
162
158
- if($tag != $lastTag) {
163
+ if ($tag != $lastTag) {
159
164
$blocks[] = $this->getDefaultArray($tag, $i1, $j1);
160
165
$lastBlock = count($blocks)-1;
161
166
}
162
167
163
168
$lastTag = $tag;
164
169
165
- if($tag == 'equal') {
170
+ if ($tag == 'equal') {
166
171
$lines = array_slice($a, $i1, ($i2 - $i1));
167
172
$blocks[$lastBlock]['base']['lines'] += $this->formatLines($lines);
168
173
$lines = array_slice($b, $j1, ($j2 - $j1));
169
174
$blocks[$lastBlock]['changed']['lines'] += $this->formatLines($lines);
170
- }
171
- else {
172
- if($tag == 'replace' || $tag == 'delete') {
175
+ } else {
176
+ if ($tag == 'replace' || $tag == 'delete') {
173
177
$lines = array_slice($a, $i1, ($i2 - $i1));
174
178
$lines = $this->formatLines($lines);
175
179
$lines = str_replace(array("\0", "\1"), array('<del>', '</del>'), $lines);
176
180
$blocks[$lastBlock]['base']['lines'] += $lines;
177
181
}
178
182
179
- if($tag == 'replace' || $tag == 'insert') {
183
+ if ($tag == 'replace' || $tag == 'insert') {
180
184
$lines = array_slice($b, $j1, ($j2 - $j1));
181
185
$lines = $this->formatLines($lines);
182
186
$lines = str_replace(array("\0", "\1"), array('<ins>', '</ins>'), $lines);
@@ -201,12 +205,12 @@ private function getChangeExtent($fromLine, $toLine)
201
205
{
202
206
$start = 0;
203
207
$limit = min(mb_strlen($fromLine), mb_strlen($toLine));
204
- while($start < $limit && mb_substr($fromLine, $start, 1) == mb_substr($toLine, $start, 1)) {
208
+ while ($start < $limit && mb_substr($fromLine, $start, 1) == mb_substr($toLine, $start, 1)) {
205
209
++$start;
206
210
}
207
211
$end = -1;
208
212
$limit = $limit - $start;
209
- while(-$end <= $limit && mb_substr($fromLine, $end, 1) == mb_substr($toLine, $end, 1)) {
213
+ while (-$end <= $limit && mb_substr($fromLine, $end, 1) == mb_substr($toLine, $end, 1)) {
210
214
--$end;
211
215
}
212
216
return array(
@@ -229,7 +233,7 @@ protected function formatLines($lines)
229
233
$lines = array_map(array($this, 'ExpandTabs'), $lines);
230
234
}
231
235
$lines = array_map(array($this, 'HtmlSafe'), $lines);
232
- foreach($lines as &$line) {
236
+ foreach ($lines as &$line) {
233
237
$line = preg_replace_callback('# ( +)|^ #', array($this, 'fixSpaces'), $line);
234
238
}
235
239
return $lines;
@@ -245,9 +249,9 @@ protected function fixSpaces($matches)
245
249
{
246
250
$buffer = '';
247
251
$count = 0;
248
- foreach($matches as $spaces){
252
+ foreach ($matches as $spaces) {
249
253
$count = strlen($spaces);
250
- if($count == 0) {
254
+ if ($count == 0) {
251
255
continue;
252
256
}
253
257
$div = (int) floor($count / 2);
@@ -269,12 +273,12 @@ protected function fixSpaces($matches)
269
273
private function expandTabs($line)
270
274
{
271
275
$tabSize = $this->options['tabSize'];
272
- while(($pos = strpos($line, "\t")) !== FALSE) {
276
+ while (($pos = strpos($line, "\t")) !== false) {
273
277
$left = substr($line, 0, $pos);
274
278
$right = substr($line, $pos + 1);
275
279
$length = $tabSize - ($pos % $tabSize);
276
280
$spaces = str_repeat(' ', $length);
277
- $line = $left. $spaces. $right;
281
+ $line = $left . $spaces . $right;
278
282
}
279
283
return $line;
280
284
}
@@ -296,8 +300,10 @@ private function htmlSafe($string)
296
300
* @param integer $j1
297
301
* @return array
298
302
*/
299
- private function getDefaultArray($tag, $i1, $j1){
300
- return array(
303
+ private function getDefaultArray($tag, $i1, $j1)
304
+ {
305
+ return array
306
+ (
301
307
'tag' => $tag,
302
308
'base' => array(
303
309
'offset' => $i1,
0 commit comments