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
12:41
created

HtmlEncodingConverter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 66
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

3 Methods

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