| Conditions | 10 |
| Paths | 4 |
| Total Lines | 46 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 86 | static public function rewriteUrls($str) |
||
| 87 | { |
||
| 88 | $res = trim($str); |
||
| 89 | if (!$res) { |
||
| 90 | return ''; |
||
| 91 | } |
||
| 92 | |||
| 93 | $doc = new DOMDocument(); |
||
| 94 | if ($doc->loadHTML('<?xml encoding="UTF-8">'.$res)) { |
||
| 95 | $xpath = new DOMXPath($doc); |
||
| 96 | $cache = new DiskCache("images"); |
||
| 97 | |||
| 98 | $entries = $xpath->query('(//img[@src]|//picture/source[@src]|//video[@poster]|//video/source[@src]|//audio/source[@src])'); |
||
| 99 | |||
| 100 | $need_saving = false; |
||
| 101 | |||
| 102 | foreach ($entries as $entry) { |
||
| 103 | |||
| 104 | if ($entry->hasAttribute('src') || $entry->hasAttribute('poster')) { |
||
| 105 | |||
| 106 | // should be already absolutized because this is called after sanitize() |
||
| 107 | $src = $entry->hasAttribute('poster') ? $entry->getAttribute('poster') : $entry->getAttribute('src'); |
||
| 108 | $cached_filename = sha1($src); |
||
| 109 | |||
| 110 | if ($cache->exists($cached_filename)) { |
||
| 111 | |||
| 112 | $src = $cache->getUrl(sha1($src)); |
||
| 113 | |||
| 114 | if ($entry->hasAttribute('poster')) { |
||
| 115 | $entry->setAttribute('poster', $src); |
||
| 116 | } else { |
||
| 117 | $entry->setAttribute('src', $src); |
||
| 118 | $entry->removeAttribute("srcset"); |
||
| 119 | } |
||
| 120 | |||
| 121 | $need_saving = true; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($need_saving) { |
||
| 127 | $doc->removeChild($doc->firstChild); //remove doctype |
||
| 128 | $res = $doc->saveHTML(); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | return $res; |
||
| 132 | } |
||
| 158 |