Conditions | 39 |
Paths | 49 |
Total Lines | 280 |
Code Lines | 112 |
Lines | 61 |
Ratio | 21.79 % |
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 |
||
115 | public function convert($outputXHTML = false) |
||
116 | { |
||
117 | // redefine |
||
118 | $outputXHTML = (bool) $outputXHTML; |
||
119 | |||
120 | // validate |
||
121 | if ($this->html == null) { |
||
122 | throw new Exception('No HTML provided.'); |
||
123 | } |
||
124 | |||
125 | // should we use inline style-block |
||
126 | if ($this->useInlineStylesBlock) { |
||
127 | // init var |
||
128 | $matches = array(); |
||
129 | |||
130 | // match the style blocks |
||
131 | preg_match_all('|<style(.*)>(.*)</style>|isU', $this->html, $matches); |
||
132 | |||
133 | // any style-blocks found? |
||
134 | if (!empty($matches[2])) { |
||
135 | // add |
||
136 | foreach ($matches[2] as $match) { |
||
137 | $this->css .= trim($match) . "\n"; |
||
138 | } |
||
139 | } |
||
140 | } |
||
141 | |||
142 | // process css |
||
143 | $this->processCSS(); |
||
144 | |||
145 | // create new DOMDocument |
||
146 | $document = new \DOMDocument('1.0', $this->getEncoding()); |
||
147 | |||
148 | // set error level |
||
149 | $internalErrors = libxml_use_internal_errors(true); |
||
150 | |||
151 | // load HTML |
||
152 | $document->loadHTML($this->html); |
||
153 | |||
154 | // Restore error level |
||
155 | libxml_use_internal_errors($internalErrors); |
||
156 | |||
157 | // create new XPath |
||
158 | $xPath = new \DOMXPath($document); |
||
159 | |||
160 | // any rules? |
||
161 | if (!empty($this->cssRules)) { |
||
162 | // loop rules |
||
163 | foreach ($this->cssRules as $rule) { |
||
164 | try { |
||
165 | $query = CssSelector::toXPath($rule['selector']); |
||
166 | } catch (ExceptionInterface $e) { |
||
167 | continue; |
||
168 | } |
||
169 | |||
170 | // search elements |
||
171 | $elements = $xPath->query($query); |
||
172 | |||
173 | // validate elements |
||
174 | if ($elements === false) { |
||
175 | continue; |
||
176 | } |
||
177 | |||
178 | // loop found elements |
||
179 | foreach ($elements as $element) { |
||
180 | // no styles stored? |
||
181 | if ($element->attributes->getNamedItem( |
||
182 | 'data-css-to-inline-styles-original-styles' |
||
183 | ) == null |
||
184 | ) { |
||
185 | // init var |
||
186 | $originalStyle = ''; |
||
187 | if ($element->attributes->getNamedItem('style') !== null) { |
||
188 | $originalStyle = $element->attributes->getNamedItem('style')->value; |
||
189 | } |
||
190 | |||
191 | // store original styles |
||
192 | $element->setAttribute( |
||
193 | 'data-css-to-inline-styles-original-styles', |
||
194 | $originalStyle |
||
195 | ); |
||
196 | |||
197 | // clear the styles |
||
198 | $element->setAttribute('style', ''); |
||
199 | } |
||
200 | |||
201 | // init var |
||
202 | $properties = array(); |
||
203 | |||
204 | // get current styles |
||
205 | $stylesAttribute = $element->attributes->getNamedItem('style'); |
||
206 | |||
207 | // any styles defined before? |
||
208 | if ($stylesAttribute !== null) { |
||
209 | // get value for the styles attribute |
||
210 | $definedStyles = (string) $stylesAttribute->value; |
||
211 | |||
212 | // split into properties |
||
213 | $definedProperties = $this->splitIntoProperties($definedStyles); |
||
214 | // loop properties |
||
215 | foreach ($definedProperties as $property) { |
||
216 | // validate property |
||
217 | if ($property == '') { |
||
218 | continue; |
||
219 | } |
||
220 | |||
221 | // split into chunks |
||
222 | $chunks = (array) explode(':', trim($property), 2); |
||
223 | |||
224 | // validate |
||
225 | if (!isset($chunks[1])) { |
||
226 | continue; |
||
227 | } |
||
228 | |||
229 | // loop chunks |
||
230 | $properties[$chunks[0]] = trim($chunks[1]); |
||
231 | } |
||
232 | } |
||
233 | |||
234 | // add new properties into the list |
||
235 | foreach ($rule['properties'] as $key => $value) { |
||
236 | // If one of the rules is already set and is !important, don't apply it, |
||
237 | // except if the new rule is also important. |
||
238 | if ( |
||
239 | !isset($properties[$key]) |
||
240 | || stristr($properties[$key], '!important') === false |
||
241 | || (stristr(implode('', $value), '!important') !== false) |
||
242 | ) { |
||
243 | $properties[$key] = $value; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | // build string |
||
248 | $propertyChunks = array(); |
||
249 | |||
250 | // build chunks |
||
251 | foreach ($properties as $key => $values) { |
||
252 | foreach ((array) $values as $value) { |
||
253 | $propertyChunks[] = $key . ': ' . $value . ';'; |
||
254 | } |
||
255 | } |
||
256 | |||
257 | // build properties string |
||
258 | $propertiesString = implode(' ', $propertyChunks); |
||
259 | |||
260 | // set attribute |
||
261 | if ($propertiesString != '') { |
||
262 | $element->setAttribute('style', $propertiesString); |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | |||
267 | // reapply original styles |
||
268 | // search elements |
||
269 | $elements = $xPath->query('//*[@data-css-to-inline-styles-original-styles]'); |
||
270 | |||
271 | // loop found elements |
||
272 | foreach ($elements as $element) { |
||
273 | // get the original styles |
||
274 | $originalStyle = $element->attributes->getNamedItem( |
||
275 | 'data-css-to-inline-styles-original-styles' |
||
276 | )->value; |
||
277 | |||
278 | if ($originalStyle != '') { |
||
279 | $originalProperties = array(); |
||
280 | $originalStyles = $this->splitIntoProperties($originalStyle); |
||
281 | |||
282 | foreach ($originalStyles as $property) { |
||
283 | // validate property |
||
284 | if ($property == '') { |
||
285 | continue; |
||
286 | } |
||
287 | |||
288 | // split into chunks |
||
289 | $chunks = (array) explode(':', trim($property), 2); |
||
290 | |||
291 | // validate |
||
292 | if (!isset($chunks[1])) { |
||
293 | continue; |
||
294 | } |
||
295 | |||
296 | // loop chunks |
||
297 | $originalProperties[$chunks[0]] = trim($chunks[1]); |
||
298 | } |
||
299 | |||
300 | // get current styles |
||
301 | $stylesAttribute = $element->attributes->getNamedItem('style'); |
||
302 | $properties = array(); |
||
303 | |||
304 | // any styles defined before? |
||
305 | if ($stylesAttribute !== null) { |
||
306 | // get value for the styles attribute |
||
307 | $definedStyles = (string) $stylesAttribute->value; |
||
308 | |||
309 | // split into properties |
||
310 | $definedProperties = $this->splitIntoProperties($definedStyles); |
||
311 | |||
312 | // loop properties |
||
313 | foreach ($definedProperties as $property) { |
||
314 | // validate property |
||
315 | if ($property == '') { |
||
316 | continue; |
||
317 | } |
||
318 | |||
319 | // split into chunks |
||
320 | $chunks = (array) explode(':', trim($property), 2); |
||
321 | |||
322 | // validate |
||
323 | if (!isset($chunks[1])) { |
||
324 | continue; |
||
325 | } |
||
326 | |||
327 | // loop chunks |
||
328 | $properties[$chunks[0]] = trim($chunks[1]); |
||
329 | } |
||
330 | } |
||
331 | |||
332 | // add new properties into the list |
||
333 | foreach ($originalProperties as $key => $value) { |
||
334 | $properties[$key] = $value; |
||
335 | } |
||
336 | |||
337 | // build string |
||
338 | $propertyChunks = array(); |
||
339 | |||
340 | // build chunks |
||
341 | foreach ($properties as $key => $values) { |
||
342 | foreach ((array) $values as $value) { |
||
343 | $propertyChunks[] = $key . ': ' . $value . ';'; |
||
344 | } |
||
345 | } |
||
346 | |||
347 | // build properties string |
||
348 | $propertiesString = implode(' ', $propertyChunks); |
||
349 | |||
350 | // set attribute |
||
351 | if ($propertiesString != '') { |
||
352 | $element->setAttribute( |
||
353 | 'style', |
||
354 | $propertiesString |
||
355 | ); |
||
356 | } |
||
357 | } |
||
358 | |||
359 | // remove placeholder |
||
360 | $element->removeAttribute( |
||
361 | 'data-css-to-inline-styles-original-styles' |
||
362 | ); |
||
363 | } |
||
364 | } |
||
365 | |||
366 | // strip original style tags if we need to |
||
367 | if ($this->stripOriginalStyleTags) { |
||
368 | $this->stripOriginalStyleTags($xPath); |
||
369 | } |
||
370 | |||
371 | // should we output XHTML? |
||
372 | if ($outputXHTML) { |
||
373 | // set formating |
||
374 | $document->formatOutput = true; |
||
375 | |||
376 | // get the HTML as XML |
||
377 | $html = $document->saveXML(null, LIBXML_NOEMPTYTAG); |
||
378 | |||
379 | // remove the XML-header |
||
380 | $html = ltrim(preg_replace('/<?xml (.*)?>/', '', $html)); |
||
381 | } // just regular HTML 4.01 as it should be used in newsletters |
||
382 | else { |
||
383 | // get the HTML |
||
384 | $html = $document->saveHTML(); |
||
385 | } |
||
386 | |||
387 | // cleanup the HTML if we need to |
||
388 | if ($this->cleanup) { |
||
389 | $html = $this->cleanupHTML($html); |
||
390 | } |
||
391 | |||
392 | // return |
||
393 | return $html; |
||
394 | } |
||
395 | |||
686 |