GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( dbd94c...eb4109 )
by Shcherbak
10:39
created

RelativeToAbsoluteLinkConverter::convert()   C

Complexity

Conditions 9
Paths 13

Size

Total Lines 51
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 6.2727
c 0
b 0
f 0
cc 9
eloc 33
nc 13
nop 2

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
  declare(strict_types=1);
4
5
  namespace Xparse\Parser\Helper;
6
7
  use GuzzleHttp\Psr7\Uri;
8
  use GuzzleHttp\Psr7\UriResolver;
9
  use Xparse\ElementFinder\ElementFinder;
10
  use Xparse\ElementFinder\Helper\NodeHelper;
11
  use Xparse\ElementFinder\Helper\StringHelper;
12
13
  /**
14
   * @author Ivan Shcherbak <[email protected]> 12/28/14
15
   */
16
  class RelativeToAbsoluteLinkConverter implements LinkConverterInterface {
17
18
    /**
19
     * @inheritdoc
20
     */
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));
0 ignored issues
show
Bug introduced by
It seems like $relative defined by \GuzzleHttp\Psr7\UriReso...tp\Psr7\Uri($relative)) on line 59 can also be of type object<Psr\Http\Message\UriInterface>; however, GuzzleHttp\Psr7\Uri::__construct() does only seem to accept string, maybe add an additional type check?

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:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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
  }