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

PunycodeWrapper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 65
Duplicated Lines 38.46 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 60%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 25
loc 65
ccs 12
cts 20
cp 0.6
rs 10
c 1
b 0
f 0
wmc 14
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 4
B encode() 13 13 5
B decode() 12 12 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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