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 ( c017f3...3478bb )
by Shcherbak
7s
created

ElementFinderFactory::create()   C

Complexity

Conditions 8
Paths 36

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 8.0327

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 37
ccs 23
cts 25
cp 0.92
rs 5.3846
cc 8
eloc 22
nc 36
nop 2
crap 8.0327
1
<?php
2
3
  namespace Xparse\Parser\Helper;
4
5
  use Xparse\ElementFinder\Helper;
6
  use Xparse\ElementFinder\ElementFinder;
7
8
  class ElementFinderFactory
9
  {
10
    /**
11
     * @param $response
12
     * @param string $url
13
     * @return ElementFinder
14
     */
15 7
    public static function create($response, $url = '')
16
    {
17 7
      $html = $response->getBody();
18 7
      $html = Helper::safeEncodeStr((string)$html);
19
20 7
      $supportedEncodings = [];
21 7
      foreach (mb_list_encodings() as $encoding) {
22 7
        if ($encoding == 'UTF-8' || $encoding == 'UTF8') {
23 7
          continue;
24
        }
25
26 7
        $supportedEncodings[] = $encoding;
27 7
        $supportedEncodings = array_merge($supportedEncodings, mb_encoding_aliases($encoding));
28 7
      }
29
30 7
      $contentType = $response->getHeaderLine('content-type');
31
32 7
      if ($contentType) {
33 1
        preg_match("!^.*charset=([A-Za-z0-9-]{4,})$!", $contentType, $contentTypeData);
34 1
        $encoding = strtoupper(trim($contentTypeData[1]));
35 1
      } else {
36 7
        preg_match("!.*<meta.*charset=\"?([A-Za-z0-9-]{4,})\"!mi", $html, $metaContentType);
37 7
        $encoding = !empty($metaContentType[1]) ? strtoupper(trim($metaContentType[1])) : '';
38
      }
39
40 7
      if (in_array($encoding, $supportedEncodings)) {
41 2
        $html = mb_convert_encoding($html, 'UTF-8', $encoding);
42 2
      }
43
44 7
      $page = new ElementFinder((string)$html);
45
46 7
      if ($url) {
47
        LinkConverter::convertUrlsToAbsolute($page, $url);
48
      }
49
50 7
      return $page;
51
    }
52
  }