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
Pull Request — master (#32)
by Anatolii
24:58 queued 09:56
created

ToUtfConverter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B convert() 0 19 5
C getSupportedEncodings() 0 25 7
1
<?php
2
3
  declare(strict_types=1);
4
5
  namespace Xparse\Parser\Helper;
6
7
  /**
8
   * Try to convert input encoding
9
   */
10
  class ToUtfConverter implements EncodingConverterInterface {
11
12
    /**
13
     * @var array|null
14
     */
15
    private static $supportedEncodings;
16
17
18
    /**
19
     * @inheritdoc
20
     */
21
    public function convert(string $html, string $contentType = '') : string {
22
      $encoding = null;
23
      if (preg_match('!^.*charset=([A-Za-z0-9-]{4,})$!', $contentType, $contentTypeData) === 1) {
24
        $encoding = $contentTypeData[1];
25
      } elseif (preg_match("!.*<meta.*charset=[\"']?[ \t]*([A-Za-z0-9-]{4,})[ \t]*[\"']!mi", $html, $metaContentType) === 1) {
26
        $encoding = $metaContentType[1];
27
      }
28
29
      if ($encoding === null) {
30
        return $html;
31
      }
32
33
      $encoding = strtolower($encoding);
34
      if (in_array($encoding, self::getSupportedEncodings(), true)) {
35
        $html = mb_convert_encoding($html, 'utf-8', $encoding);
36
      }
37
38
      return $html;
39
    }
40
41
42
    /**
43
     * @return array
44
     */
45
    private static function getSupportedEncodings() : array {
46
47
      if (self::$supportedEncodings !== null) {
48
        return self::$supportedEncodings;
49
      }
50
51
      $hasAliasesFunction = function_exists('mb_encoding_aliases');
52
      self::$supportedEncodings = [];
53
      foreach (mb_list_encodings() as $encoding) {
54
        $encoding = strtolower($encoding);
55
        if ($encoding === 'utf-8' or $encoding === 'utf8') {
56
          continue;
57
        }
58
59
        self::$supportedEncodings[] = $encoding;
60
        if ($hasAliasesFunction) {
61
          foreach (mb_encoding_aliases($encoding) as $encodingAlias) {
62
            $encodingAlias = strtolower($encodingAlias);
63
            self::$supportedEncodings[] = $encodingAlias;
64
          }
65
        }
66
      }
67
68
      return self::$supportedEncodings;
69
    }
70
71
  }