Completed
Push — ylebre-svn-fixes ( 2859de )
by
unknown
64:29
created

AriadneDiffFormatter   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 123
Duplicated Lines 4.88 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 6
loc 123
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A _start_diff() 0 3 1
A _end_diff() 0 3 1
A _block_header() 0 12 1
A _start_block() 0 5 2
A _end_block() 0 2 1
A _lines() 0 2 1
A addedLine() 0 9 1
A deletedLine() 0 9 1
A contextLine() 0 9 1
A emptyLine() 0 6 1
A _added() 3 5 2
A _deleted() 3 5 2
A _context() 0 5 2
A _changed() 0 16 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Theme function for a header line in the diff.
4
 */
5
function diff_header_line($vars) {
6
	return '<strong>Line ' . $vars['lineno']. '</strong>';
7
}
8
9
function check_plain($text) {
10
	return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
11
}
12
13
/**
14
 * Theme function for a content line in the diff.
15
 */
16
function diff_content_line($vars) {
17
  return '<div>'. $vars['line'] .'</div>';
18
}
19
20
/**
21
 * Theme function for an empty line in the diff.
22
 */
23
function diff_empty_line($vars) {
24
  return $vars['line'];
25
}
26
27
/**
28
 * Diff formatter for Ariadne
29
 * @private
30
 * @subpackage DifferenceEngine
31
 */
32
class AriadneDiffFormatter extends DiffFormatter {
33
34
  var $rows;
35
36
  function __construct() {
37
    $this->leading_context_lines = 2;
38
    $this->trailing_context_lines = 2;
39
  }
40
41
  function _start_diff() {
42
    $this->rows = array();
43
  }
44
45
  function _end_diff() {
46
    return $this->rows;
47
  }
48
49
  function _block_header($xbeg, $xlen, $ybeg, $ylen) {
50
    return array(
51
      array(
52
        'data' => diff_header_line(array('lineno' => $xbeg)),
53
        'colspan' => 2,
54
      ),
55
      array(
56
        'data' => diff_header_line(array('lineno' => $ybeg)),
57
        'colspan' => 2,
58
      )
59
    );
60
  }
61
62
  function _start_block($header) {
63
    if ($this->show_header) {
64
      $this->rows[] = $header;
65
    }
66
  }
67
68
  function _end_block() {
69
  }
70
71
  function _lines($lines, $prefix=' ', $color='white') {
0 ignored issues
show
Unused Code introduced by
The parameter $color 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...
72
  }
73
74
  /**
75
   * Note: you should HTML-escape parameter before calling this.
76
   */
77
  function addedLine($line) {
78
    return array(
79
      '+',
80
      array(
81
        'data' => diff_content_line(array('line' => $line)),
82
        'class' => 'diff-addedline',
83
      )
84
    );
85
  }
86
87
  /**
88
   * Note: you should HTML-escape parameter before calling this.
89
   */
90
  function deletedLine($line) {
91
    return array(
92
      '-',
93
      array(
94
        'data' => diff_content_line(array('line' => $line)),
95
        'class' => 'diff-deletedline',
96
      )
97
    );
98
  }
99
100
  /**
101
   * Note: you should HTML-escape parameter before calling this.
102
   */
103
  function contextLine($line) {
104
    return array(
105
      '&nbsp;',
106
      array(
107
        'data' => diff_content_line(array('line' => $line)),
108
        'class' => 'diff-context',
109
      )
110
    );
111
  }
112
113
  function emptyLine() {
114
    return array(
115
      '&nbsp;',
116
      diff_empty_line(array('line' => '&nbsp;')),
117
    );
118
  }
119
120
  function _added($lines) {
121 View Code Duplication
    foreach ($lines as $line) {
122
      $this->rows[] = array_merge($this->emptyLine(), $this->addedLine(check_plain($line)));
123
    }
124
  }
125
126
  function _deleted($lines) {
127 View Code Duplication
    foreach ($lines as $line) {
128
      $this->rows[] = array_merge($this->deletedLine(check_plain($line)), $this->emptyLine());
129
    }
130
  }
131
132
  function _context($lines) {
133
    foreach ($lines as $line) {
134
      $this->rows[] = array_merge($this->contextLine(check_plain($line)), $this->contextLine(check_plain($line)));
135
    }
136
  }
137
138
  function _changed($orig, $closing) {
139
    $diff = new WordLevelDiff($orig, $closing);
140
    $del = $diff->orig();
141
    $add = $diff->closing();
142
143
    // Notice that WordLevelDiff returns HTML-escaped output.
144
    // Hence, we will be calling addedLine/deletedLine without HTML-escaping.
145
146
    while ($line = array_shift($del)) {
147
      $aline = array_shift( $add );
148
      $this->rows[] = array_merge($this->deletedLine($line), $this->addedLine($aline));
149
    }
150
    foreach ($add as $line) {  // If any leftovers
151
      $this->rows[] = array_merge($this->emptyLine(), $this->addedLine($line));
152
    }
153
  }
154
}
155