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 (#12)
by
unknown
03:42
created

EncodingConverter::convertTo()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7.3329

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 10
cts 15
cp 0.6667
rs 8.8571
cc 6
eloc 12
nc 18
nop 3
crap 7.3329
1
<?php
2
3
namespace Xparse\Parser\Helper;
4
5
use GuzzleHttp\Psr7\Response;
6
7
/**
8
 * Class EncodingConverter
9
 * @package Xparse\Parser\Helper
10
 */
11
class EncodingConverter
12
{
13
14
  /**
15
   * @var array|null
16
   */
17
  private static $supportedEncodings = null;
18
19
  /**
20
   * @param Response $response
21
   * @param $html
22
   * @param $convertToEncoding
23
   * @return mixed
24
   */
25 3
  public static function convertTo(Response $response, $html, $convertToEncoding) {
26 3
    $contentType = $response->getHeaderLine('content-type');
27
28 3
    if (!empty($contentType)) {
29
      preg_match("!^.*charset=([A-Za-z0-9-]{4,})$!", $contentType, $contentTypeData);
30
      $encoding = !empty($contentTypeData[1]) ? strtoupper(trim($contentTypeData[1])) : '';
31
    }
32
33 3
    if (empty($encoding)){
34 3
      preg_match("!.*<meta.*charset=[\"']?[ \t]*([A-Za-z0-9-]{4,})[ \t]*[\"']!mi", $html, $metaContentType);
35 3
      $encoding = !empty($metaContentType[1]) ? strtoupper(trim($metaContentType[1])) : '';
36 3
    }
37
38 3
    $supportedEncodings = self::getSupportedEncodings();
39 3
    if (in_array($encoding, $supportedEncodings)) {
40
      $html = mb_convert_encoding($html, $convertToEncoding, $encoding);
41
    }
42
43 3
    return $html;
44
  }
45
46
47
  /**
48
   * @return array
49
   */
50 3
  private static function getSupportedEncodings() {
51 3
    if (self::$supportedEncodings !== null) {
52 2
      return self::$supportedEncodings;
53
    }
54
55 1
    self::$supportedEncodings = [];
56 1
    foreach (mb_list_encodings() as $encoding) {
57 1
      if ($encoding == 'UTF-8' or $encoding == 'UTF8') {
58 1
        continue;
59
      }
60
61 1
      self::$supportedEncodings[] = $encoding;
62 1
      self::$supportedEncodings = array_merge(self::$supportedEncodings, mb_encoding_aliases($encoding));
63 1
    }
64
65 1
    return self::$supportedEncodings;
66
  }
67
}