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

PunycodeWrapper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 70
Duplicated Lines 35.71 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 65%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 25
loc 70
ccs 13
cts 20
cp 0.65
rs 10
c 1
b 0
f 0
wmc 14
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 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
    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