Conditions | 9 |
Paths | 13 |
Total Lines | 51 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
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 |
||
21 | public function convert(ElementFinder $finder, string $affectedUrl = ''): ElementFinder { |
||
22 | $affected = new Uri($affectedUrl); |
||
23 | $dom = new \DomDocument(); |
||
24 | $internalErrors = libxml_use_internal_errors(true); |
||
25 | $disableEntities = libxml_disable_entity_loader(); |
||
26 | $data = mb_convert_encoding(StringHelper::safeEncodeStr((string) $finder->content('.')->first()), 'HTML-ENTITIES', 'UTF-8'); |
||
27 | $dom->loadHTML($data, LIBXML_NOCDATA & LIBXML_NOERROR); |
||
28 | libxml_clear_errors(); |
||
29 | libxml_use_internal_errors($internalErrors); |
||
30 | libxml_disable_entity_loader($disableEntities); |
||
31 | $xpath = new \DomXPath($dom); |
||
32 | |||
33 | $srcElements = $xpath->query('//*[@src] | //*[@href] | //form[@action]'); |
||
34 | $baseUrlAttr = $xpath->query('//base/@href')->item(0) ?? new \DOMAttr('href', ''); |
||
35 | $baseUrl = $baseUrlAttr->value; |
||
36 | |||
37 | foreach ($srcElements as $element) { |
||
38 | /** @var \DOMElement $element */ |
||
39 | $attributeName = 'href'; |
||
40 | |||
41 | if ($element->hasAttribute('action') === true and $element->tagName === 'form') { |
||
42 | $attributeName = 'action'; |
||
43 | } else if ($element->hasAttribute('src') === true) { |
||
44 | $attributeName = 'src'; |
||
45 | } |
||
46 | |||
47 | $relative = $element->getAttribute($attributeName); |
||
48 | |||
49 | # don`t change javascript in href |
||
50 | if (preg_match('!^\s*javascript\s*:\s*!', $relative)) { |
||
51 | continue; |
||
52 | } |
||
53 | |||
54 | if (parse_url($relative) === false) { |
||
55 | continue; |
||
56 | } |
||
57 | |||
58 | if ($baseUrl !== '' and !preg_match('!^(/|http)!i', $relative)) { |
||
59 | $relative = UriResolver::resolve(new Uri($baseUrl), new Uri($relative)); |
||
60 | } |
||
61 | |||
62 | $url = UriResolver::resolve($affected, new Uri($relative)); |
||
|
|||
63 | $element->setAttribute($attributeName, (string) $url); |
||
64 | } |
||
65 | $output = NodeHelper::getInnerContent( |
||
66 | $xpath->query('.')->item(0) ?? new \DOMNode() |
||
67 | ); |
||
68 | return new ElementFinder( |
||
69 | $output |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.