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 ( eb4109...fdccdc )
by Shcherbak
15:08
created

ConvertUrlElementFinderModifier   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B modify() 0 20 6
A attributeName() 0 9 4
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\ElementFinderModifierInterface;
10
11
  /**
12
   * @internal
13
   * @author Ivan Shcherbak <[email protected]>
14
   */
15
  class ConvertUrlElementFinderModifier implements ElementFinderModifierInterface {
16
17
18
    /**
19
     * @var string
20
     */
21
    private $affectedUrl;
22
23
    /**
24
     * @var string
25
     */
26
    private $baseUrl;
27
28
29
    public function __construct(string $affectedUrl, string $baseUrl) {
30
      $this->affectedUrl = $affectedUrl;
31
      $this->baseUrl = $baseUrl;
32
    }
33
34
35
    /**
36
     * @param \DOMNodeList $nodeList
37
     * @return void
38
     */
39
    public function modify(\DOMNodeList $nodeList) {
40
      $affected = new Uri($this->affectedUrl);
41
      foreach ($nodeList as $element) {
42
        /** @var \DOMElement $element */
43
        $attribute = $this->attributeName($element);
44
        $relative = $element->getAttribute($attribute);
45
        $isValid = parse_url($relative) !== false;
46
        if (
47
          !preg_match('!^\s*javascript\s*:\s*!', $relative)
48
          and
49
          $isValid
50
        ) {
51
          if ($this->baseUrl !== '' and !preg_match('!^(/|http)!i', $relative)) {
52
            $relative = UriResolver::resolve(new Uri($this->baseUrl), new Uri($relative));
53
          }
54
          $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 52 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...
55
          $element->setAttribute($attribute, (string) $url);
56
        }
57
      }
58
    }
59
60
61
    /**
62
     * @param \DOMElement $element
63
     * @return string
64
     */
65
    private function attributeName(\DOMElement $element): string {
66
      $name = 'href';
67
      if ($element->tagName === 'form' and $element->hasAttribute('action') === true) {
68
        $name = 'action';
69
      } else if ($element->hasAttribute('src') === true) {
70
        $name = 'src';
71
      }
72
      return $name;
73
    }
74
75
  }