Passed
Pull Request — master (#9)
by Michael
03:24
created

Text_Diff_Renderer_inline   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 16
eloc 48
dl 0
loc 178
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A _blockHeader() 0 3 1
A _changed() 0 33 6
A _startBlock() 0 3 1
A _deleted() 0 7 1
A _encode() 0 3 1
A _lines() 0 11 3
A _added() 0 7 1
A _splitOnWords() 0 15 2
1
<?php
2
3
namespace XoopsModules\Mylinks;
4
5
/**
6
 * "Inline" diff renderer.
7
 *
8
 * This class renders diffs in the Wiki-style "inline" format.
9
 *
10
 * $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.14 2005/07/22 19:45:15 chuck Exp $
11
 *
12
 * @author  Ciprian Popovici
13
 * @package Text_Diff
14
 */
15
class Text_Diff_Renderer_inline extends Text_Diff_Renderer
16
{
17
    /**
18
     * Number of leading context "lines" to preserve.
19
     */
20
    public $_leading_context_lines = 10000;
21
22
    /**
23
     * Number of trailing context "lines" to preserve.
24
     */
25
    public $_trailing_context_lines = 10000;
26
27
    /**
28
     * Prefix for inserted text.
29
     */
30
    public $_ins_prefix = '<ins>';
31
32
    /**
33
     * Suffix for inserted text.
34
     */
35
    public $_ins_suffix = '</ins>';
36
37
    /**
38
     * Prefix for deleted text.
39
     */
40
    public $_del_prefix = '<del>';
41
42
    /**
43
     * Suffix for deleted text.
44
     */
45
    public $_del_suffix = '</del>';
46
47
    /**
48
     * Header for each change block.
49
     */
50
    public $_block_header = '';
51
52
    /**
53
     * What are we currently splitting on? Used to recurse to show word-level
54
     * changes.
55
     */
56
    public $_split_level = 'lines';
57
58
    /**
59
     * @param $xbeg
60
     * @param $xlen
61
     * @param $ybeg
62
     * @param $ylen
63
     * @return string
64
     */
65
    public function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
66
    {
67
        return $this->_block_header;
68
    }
69
70
    /**
71
     * @param $header
72
     * @return mixed
73
     */
74
    public function _startBlock($header)
75
    {
76
        return $header;
77
    }
78
79
    /**
80
     * @param         $lines
81
     * @param string  $prefix
82
     * @param bool    $encode
83
     * @return string
84
     */
85
    public function _lines($lines, $prefix = ' ', $encode = true)
86
    {
87
        if ($encode) {
88
            array_walk($lines, [&$this, '_encode']);
89
        }
90
91
        if ('words' === $this->_split_level) {
92
            return implode('', $lines);
93
        }
94
95
        return implode("\n", $lines) . "\n";
96
    }
97
98
    /**
99
     * @param $lines
100
     * @return string
101
     */
102
    public function _added($lines)
103
    {
104
        array_walk($lines, [&$this, '_encode']);
105
        $lines[0]                 = $this->_ins_prefix . $lines[0];
106
        $lines[count($lines) - 1] .= $this->_ins_suffix;
107
108
        return $this->_lines($lines, ' ', false);
109
    }
110
111
    /**
112
     * @param         $lines
113
     * @param bool    $words
114
     * @return string
115
     */
116
    public function _deleted($lines, $words = false)
0 ignored issues
show
Unused Code introduced by
The parameter $words is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

116
    public function _deleted($lines, /** @scrutinizer ignore-unused */ $words = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
117
    {
118
        array_walk($lines, [&$this, '_encode']);
119
        $lines[0]                 = $this->_del_prefix . $lines[0];
120
        $lines[count($lines) - 1] .= $this->_del_suffix;
121
122
        return $this->_lines($lines, ' ', false);
123
    }
124
125
    /**
126
     * @param $orig
127
     * @param $final
128
     * @return string
129
     */
130
    public function _changed($orig, $final)
131
    {
132
        /* If we've already split on words, don't try to do so again - just
133
         * display. */
134
        if ('words' === $this->_split_level) {
135
            $prefix = '';
136
            while (false !== $orig[0] && false !== $final[0] && ' ' === mb_substr($orig[0], 0, 1)
137
                   && ' ' === mb_substr($final[0], 0, 1)) {
138
                $prefix   .= mb_substr($orig[0], 0, 1);
139
                $orig[0]  = mb_substr($orig[0], 1);
140
                $final[0] = mb_substr($final[0], 1);
141
            }
142
143
            return $prefix . $this->_deleted($orig) . $this->_added($final);
144
        }
145
146
        $text1 = implode("\n", $orig);
147
        $text2 = implode("\n", $final);
148
149
        /* Non-printing newline marker. */
150
        $nl = "\0";
151
152
        /* We want to split on word boundaries, but we need to
153
         * preserve whitespace as well. Therefore we split on words,
154
         * but include all blocks of whitespace in the wordlist. */
155
        $diff = new Text_Diff($this->_splitOnWords($text1, $nl), $this->_splitOnWords($text2, $nl));
156
157
        /* Get the diff in inline format. */
158
        $renderer = new self(array_merge($this->getParams(), ['split_level' => 'words']));
159
160
        /* Run the diff and get the output. */
161
162
        return str_replace($nl, "\n", $renderer->render($diff)) . "\n";
163
    }
164
165
    /**
166
     * @param         $string
167
     * @param string  $newlineEscape
168
     * @return array
169
     */
170
    public function _splitOnWords($string, $newlineEscape = "\n")
171
    {
172
        $words  = [];
173
        $length = mb_strlen($string);
174
        $pos    = 0;
175
176
        while ($pos < $length) {
177
            // Eat a word with any preceding whitespace.
178
            $spaces  = strspn(mb_substr($string, $pos), " \n");
179
            $nextpos = strcspn(mb_substr($string, $pos + $spaces), " \n");
180
            $words[] = str_replace("\n", $newlineEscape, mb_substr($string, $pos, $spaces + $nextpos));
181
            $pos     += $spaces + $nextpos;
182
        }
183
184
        return $words;
185
    }
186
187
    /**
188
     * @param $string
189
     */
190
    public function _encode(&$string)
191
    {
192
        $string = htmlspecialchars($string, ENT_QUOTES | ENT_HTML5);
193
    }
194
}
195