Completed
Push — develop ( f810d8...9feba0 )
by Lars
06:45
created

PunycodeWrapper::__construct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 11
ccs 4
cts 8
cp 0.5
crap 6
rs 9.2
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 14
    if (function_exists('idn_to_ascii') && function_exists('idn_to_utf8')) {
22 14
      $this->idnSupport = true;
23 14
      return;
24
    }
25
26
    if (self::$punycode === null) {
27
      self::$punycode = new Punycode();
28
    }
29
  }
30
31
  /**
32
   * Encode a domain to its Punycode version
33
   *
34
   * @param string $input Domain name in Unicode to be encoded
35
   *
36
   * @return string Punycode representation in ASCII
37
   */
38 14 View Code Duplication
  public function encode($input)
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...
39
  {
40 14
    if ($this->idnSupport === true) {
41
      // https://git.ispconfig.org/ispconfig/ispconfig3/blob/master/interface/lib/classes/functions.inc.php#L305
42 14
      if(defined('IDNA_NONTRANSITIONAL_TO_ASCII') && defined('INTL_IDNA_VARIANT_UTS46') && constant('IDNA_NONTRANSITIONAL_TO_ASCII')) {
43
        return idn_to_ascii($input, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
44
      } else {
45 14
        return idn_to_ascii($input);
46
      }
47
    }
48
49
    return self::$punycode->encode($input);
50
  }
51
52
  /**
53
   * Decode a Punycode domain name to its Unicode counterpart
54
   *
55
   * @param string $input Domain name in Punycode
56
   *
57
   * @return string Unicode domain name
58
   */
59 11 View Code Duplication
  public function decode($input)
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...
60
  {
61 11
    if ($this->idnSupport === true) {
62 11
      if(defined('IDNA_NONTRANSITIONAL_TO_UNICODE') && defined('INTL_IDNA_VARIANT_UTS46') && constant('IDNA_NONTRANSITIONAL_TO_UNICODE')) {
63
        return idn_to_utf8($input, IDNA_NONTRANSITIONAL_TO_UNICODE, INTL_IDNA_VARIANT_UTS46);
64
      } else {
65 11
        return idn_to_utf8($input);
66
      }
67
    }
68
69
    return self::$punycode->decode($input);
70
  }
71
}
72