| Conditions | 26 |
| Paths | 1386 |
| Total Lines | 101 |
| Code Lines | 68 |
| Lines | 10 |
| Ratio | 9.9 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 61 | public static function compareHTML($from, $to, $escape = false) |
||
| 62 | { |
||
| 63 | // First split up the content into words and tags |
||
| 64 | $set1 = self::getHTMLChunks($from); |
||
| 65 | $set2 = self::getHTMLChunks($to); |
||
| 66 | |||
| 67 | // Diff that |
||
| 68 | $diff = new Diff($set1, $set2); |
||
| 69 | |||
| 70 | $tagStack[1] = $tagStack[2] = 0; |
||
| 71 | $rechunked[1] = $rechunked[2] = array(); |
||
| 72 | |||
| 73 | // Go through everything, converting edited tags (and their content) into single chunks. Otherwise |
||
| 74 | // the generated HTML gets crusty |
||
| 75 | foreach ($diff->edits as $edit) { |
||
| 76 | $lookForTag = false; |
||
| 77 | $stuffFor = []; |
||
| 78 | switch ($edit->type) { |
||
| 79 | case 'copy': |
||
| 80 | $lookForTag = false; |
||
| 81 | $stuffFor[1] = $edit->orig; |
||
| 82 | $stuffFor[2] = $edit->orig; |
||
| 83 | break; |
||
| 84 | |||
| 85 | View Code Duplication | case 'change': |
|
| 86 | $lookForTag = true; |
||
| 87 | $stuffFor[1] = $edit->orig; |
||
| 88 | $stuffFor[2] = $edit->final; |
||
| 89 | break; |
||
| 90 | |||
| 91 | View Code Duplication | case 'add': |
|
| 92 | $lookForTag = true; |
||
| 93 | $stuffFor[1] = null; |
||
| 94 | $stuffFor[2] = $edit->final; |
||
| 95 | break; |
||
| 96 | |||
| 97 | case 'delete': |
||
| 98 | $lookForTag = true; |
||
| 99 | $stuffFor[1] = $edit->orig; |
||
| 100 | $stuffFor[2] = null; |
||
| 101 | break; |
||
| 102 | } |
||
| 103 | |||
| 104 | foreach ($stuffFor as $listName => $chunks) { |
||
| 105 | if ($chunks) { |
||
| 106 | foreach ($chunks as $item) { |
||
| 107 | // $tagStack > 0 indicates that we should be tag-building |
||
| 108 | if ($tagStack[$listName]) { |
||
| 109 | $rechunked[$listName][sizeof($rechunked[$listName])-1] .= ' ' . $item; |
||
| 110 | } else { |
||
| 111 | $rechunked[$listName][] = $item; |
||
| 112 | } |
||
| 113 | |||
| 114 | if ($lookForTag |
||
| 115 | && !$tagStack[$listName] |
||
| 116 | && isset($item[0]) |
||
| 117 | && $item[0] == "<" |
||
| 118 | && substr($item, 0, 2) != "</" |
||
| 119 | ) { |
||
| 120 | $tagStack[$listName] = 1; |
||
| 121 | } elseif ($tagStack[$listName]) { |
||
| 122 | if (substr($item, 0, 2) == "</") { |
||
| 123 | $tagStack[$listName]--; |
||
| 124 | } elseif (isset($item[0]) && $item[0] == "<") { |
||
| 125 | $tagStack[$listName]++; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | // Diff the re-chunked data, turning it into maked up HTML |
||
| 134 | $diff = new Diff($rechunked[1], $rechunked[2]); |
||
| 135 | $content = ''; |
||
| 136 | foreach ($diff->edits as $edit) { |
||
| 137 | $orig = ($escape) ? Convert::raw2xml($edit->orig) : $edit->orig; |
||
| 138 | $final = ($escape) ? Convert::raw2xml($edit->final) : $edit->final; |
||
| 139 | |||
| 140 | switch ($edit->type) { |
||
| 141 | case 'copy': |
||
| 142 | $content .= " " . implode(" ", $orig) . " "; |
||
| 143 | break; |
||
| 144 | |||
| 145 | case 'change': |
||
| 146 | $content .= " <ins>" . implode(" ", $final) . "</ins> "; |
||
| 147 | $content .= " <del>" . implode(" ", $orig) . "</del> "; |
||
| 148 | break; |
||
| 149 | |||
| 150 | case 'add': |
||
| 151 | $content .= " <ins>" . implode(" ", $final) . "</ins> "; |
||
| 152 | break; |
||
| 153 | |||
| 154 | case 'delete': |
||
| 155 | $content .= " <del>" . implode(" ", $orig) . "</del> "; |
||
| 156 | break; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | return self::cleanHTML($content); |
||
| 161 | } |
||
| 162 | |||
| 204 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: