Text_Diff_Renderer_inline::_added()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 8
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * "Inline" diff renderer.
5
 *
6
 * This class renders diffs in the Wiki-style "inline" format.
7
 *
8
 * $Horde: framework/Text_Diff/Diff/Renderer/inline.php,v 1.14 2005/07/22 19:45:15 chuck Exp $
9
 *
10
 * @author  Ciprian Popovici
11
 * @package Text_Diff
12
 */
13
class Text_Diff_Renderer_inline extends Text_Diff_Renderer
14
{
15
    /**
16
     * Number of leading context "lines" to preserve.
17
     */
18
    public $_leading_context_lines = 10000;
19
20
    /**
21
     * Number of trailing context "lines" to preserve.
22
     */
23
    public $_trailing_context_lines = 10000;
24
25
    /**
26
     * Prefix for inserted text.
27
     */
28
    public $_ins_prefix = '<ins>';
29
30
    /**
31
     * Suffix for inserted text.
32
     */
33
    public $_ins_suffix = '</ins>';
34
35
    /**
36
     * Prefix for deleted text.
37
     */
38
    public $_del_prefix = '<del>';
39
40
    /**
41
     * Suffix for deleted text.
42
     */
43
    public $_del_suffix = '</del>';
44
45
    /**
46
     * Header for each change block.
47
     */
48
    public $_block_header = '';
49
50
    /**
51
     * What are we currently splitting on? Used to recurse to show word-level
52
     * changes.
53
     */
54
    public $_split_level = 'lines';
55
56
    /**
57
     * @param $xbeg
58
     * @param $xlen
59
     * @param $ybeg
60
     * @param $ylen
61
     * @return string
62
     */
63
    public function _blockHeader($xbeg, $xlen, $ybeg, $ylen)
64
    {
65
        return $this->_block_header;
66
    }
67
68
    /**
69
     * @param $header
70
     * @return mixed
71
     */
72
    public function _startBlock($header)
73
    {
74
        return $header;
75
    }
76
77
    /**
78
     * @param         $lines
79
     * @param  string $prefix
80
     * @param  bool   $encode
81
     * @return string
82
     */
83
    public function _lines($lines, $prefix = ' ', $encode = true)
84
    {
85
        if ($encode) {
86
            array_walk($lines, array(&$this, '_encode'));
87
        }
88
89
        if ($this->_split_level === 'words') {
90
            return implode('', $lines);
91
        } else {
92
            return implode("\n", $lines) . "\n";
93
        }
94
    }
95
96
    /**
97
     * @param $lines
98
     * @return string
99
     */
100 View Code Duplication
    public function _added($lines)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
101
    {
102
        array_walk($lines, array(&$this, '_encode'));
103
        $lines[0]                 = $this->_ins_prefix . $lines[0];
104
        $lines[count($lines) - 1] .= $this->_ins_suffix;
105
106
        return $this->_lines($lines, ' ', false);
107
    }
108
109
    /**
110
     * @param         $lines
111
     * @param  bool   $words
112
     * @return string
113
     */
114 View Code Duplication
    public function _deleted($lines, $words = false)
0 ignored issues
show
Unused Code introduced by
The parameter $words is not used and could be removed.

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

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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.

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