Completed
Push — develop ( 71033d...a88aab )
by Lars
03:02
created

PunycodeWrapper::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8437

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 3
nop 0
dl 0
loc 16
ccs 5
cts 8
cp 0.625
crap 4.8437
rs 9.2
c 1
b 0
f 0
1
<?php
2
3
namespace Pdp;
4
5
use TrueBV\Punycode;
6
7
class PunycodeWrapper
8
{
9
  /**
10
   * @var Punycode
11
   */
12
  private static $punycode;
13
14
  /**
15
   * @var bool
16
   */
17
  private $idnSupport = false;
18
19 14
  public function __construct()
20
  {
21
    if (
22 14
        \function_exists('idn_to_ascii')
23
        &&
24 14
        \function_exists('idn_to_utf8')
25
    ) {
26 14
      $this->idnSupport = true;
27
28 14
      return;
29
    }
30
31
    if (self::$punycode === null) {
32
      self::$punycode = new Punycode();
33
    }
34
  }
35
36
  /**
37
   * Encode a domain to its Punycode version
38
   *
39
   * @param string $input Domain name in Unicode to be encoded
40
   *
41
   * @return string Punycode representation in ASCII
42
   */
43 13 View Code Duplication
  public function encode($input): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
  {
45 13
    if ($this->idnSupport === true) {
46
      // https://git.ispconfig.org/ispconfig/ispconfig3/blob/master/interface/lib/classes/functions.inc.php#L305
47 13
      if (defined('IDNA_NONTRANSITIONAL_TO_ASCII') && defined('INTL_IDNA_VARIANT_UTS46') && constant('IDNA_NONTRANSITIONAL_TO_ASCII')) {
48 13
        return idn_to_ascii($input, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
49
      } else {
50
        return idn_to_ascii($input);
51
      }
52
    }
53
54
    return self::$punycode->encode($input);
55
  }
56
57
  /**
58
   * Decode a Punycode domain name to its Unicode counterpart
59
   *
60
   * @param string $input Domain name in Punycode
61
   *
62
   * @return string Unicode domain name
63
   */
64 11 View Code Duplication
  public function decode($input): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
  {
66 11
    if ($this->idnSupport === true) {
67 11
      if (defined('IDNA_NONTRANSITIONAL_TO_UNICODE') && defined('INTL_IDNA_VARIANT_UTS46') && constant('IDNA_NONTRANSITIONAL_TO_UNICODE')) {
68 11
        return idn_to_utf8($input, IDNA_NONTRANSITIONAL_TO_UNICODE, INTL_IDNA_VARIANT_UTS46);
69
      } else {
70
        return idn_to_utf8($input);
71
      }
72
    }
73
74
    return self::$punycode->decode($input);
75
  }
76
}
77