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 ( 7054ef...4a2eaa )
by Shcherbak
11s
created

ElementFinderFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.75%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 12
c 4
b 0
f 1
lcom 1
cbo 4
dl 0
loc 67
ccs 30
cts 32
cp 0.9375
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C create() 0 30 7
B getSupportedEncodings() 0 17 5
1
<?php
2
3
  namespace Xparse\Parser\Helper;
4
5
  use GuzzleHttp\Psr7\Response;
6
  use Xparse\ElementFinder\ElementFinder;
7
  use Xparse\ElementFinder\Helper;
8
9
  /**
10
   * Create ElementFinder.
11
   * Convert charset to UTF-8
12
   * Convert relative links to absolute
13
   */
14
  class ElementFinderFactory {
15
16
    /**
17
     * @var array|null
18
     */
19
    private static $supportedEncodings = null;
20
21
22
    /**
23
     * @param Response $response
24
     * @param string $affectedUrl
25
     * @return ElementFinder
26
     */
27 16
    public static function create(Response $response, $affectedUrl = '') {
28 16
      $html = $response->getBody();
29 16
      $html = Helper::safeEncodeStr((string) $html);
30
31 16
      $supportedEncodings = self::getSupportedEncodings();
32
33 16
      $contentType = $response->getHeaderLine('content-type');
34
35 16
      if (!empty($contentType)) {
36 3
        preg_match("!^.*charset=([A-Za-z0-9-]{4,})$!", $contentType, $contentTypeData);
37 3
        $encoding = !empty($contentTypeData[1]) ? strtoupper(trim($contentTypeData[1])) : '';
38 3
      }
39
40 16
      if (empty($encoding)){
41 14
        preg_match("!.*<meta.*charset=[\"']?[ \t]*([A-Za-z0-9-]{4,})[ \t]*[\"']!mi", $html, $metaContentType);
42 14
        $encoding = !empty($metaContentType[1]) ? strtoupper(trim($metaContentType[1])) : '';
43 14
      }
44
      
45 16
      if (in_array($encoding, $supportedEncodings)) {
46 7
        $html = mb_convert_encoding($html, 'UTF-8', $encoding);
47 7
      }
48
49 16
      $page = new ElementFinder((string) $html);
50
51 16
      if ($affectedUrl) {
52
        LinkConverter::convertUrlsToAbsolute($page, $affectedUrl);
53
      }
54
55 16
      return $page;
56
    }
57
58
59
    /**
60
     * @return array
61
     */
62 16
    private static function getSupportedEncodings() {
63 16
      if (self::$supportedEncodings !== null) {
64 15
        return self::$supportedEncodings;
65
      }
66
67 1
      self::$supportedEncodings = [];
68 1
      foreach (mb_list_encodings() as $encoding) {
69 1
        if ($encoding == 'UTF-8' or $encoding == 'UTF8') {
70 1
          continue;
71
        }
72
73 1
        self::$supportedEncodings[] = $encoding;
74 1
        self::$supportedEncodings = array_merge(self::$supportedEncodings, mb_encoding_aliases($encoding));
75 1
      }
76
77 1
      return self::$supportedEncodings;
78
    }
79
80
  }