Completed
Pull Request — master (#84)
by Robbie
08:08
created

LDAPUtil   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 8.26 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 0
dl 9
loc 109
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validGuid() 0 7 2
A bin_to_str_guid() 9 20 4
B str_to_hex_guid() 0 27 4
A bin_to_str_sid() 0 16 2
A little_endian() 0 8 2

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 SilverStripe\ActiveDirectory\Model;
4
5
/**
6
 * Class LDAPUtil
7
 *
8
 * Provides some commonly used functions for LDAP and SAML.
9
 *
10
 * @package activedirectory
11
 */
12
class LDAPUtil
13
{
14
    /**
15
     * Checks if the string is a valid guid in the format of A98C5A1E-A742-4808-96FA-6F409E799937
16
     *
17
     * @param  string $guid
18
     * @return bool
19
     */
20
    public static function validGuid($guid)
21
    {
22
        if (preg_match('/^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}?$/', $guid)) {
23
            return true;
24
        }
25
        return false;
26
    }
27
28
    /**
29
     * @param  string $object_guid
30
     * @return string
31
     */
32
    public static function bin_to_str_guid($object_guid)
33
    {
34
        $hex_guid = bin2hex($object_guid);
35
        $hex_guid_to_guid_str = '';
36 View Code Duplication
        for ($k = 1; $k <= 4; ++$k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
37
            $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2);
38
        }
39
        $hex_guid_to_guid_str .= '-';
40 View Code Duplication
        for ($k = 1; $k <= 2; ++$k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
41
            $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2);
42
        }
43
        $hex_guid_to_guid_str .= '-';
44 View Code Duplication
        for ($k = 1; $k <= 2; ++$k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
45
            $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2);
46
        }
47
        $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4);
48
        $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20);
49
50
        return strtoupper($hex_guid_to_guid_str);
51
    }
52
53
    /**
54
     * @param  string $str_guid
55
     * @param  bool   $escape
56
     * @return string
57
     */
58
    public static function str_to_hex_guid($str_guid, $escape = false)
59
    {
60
        $str_guid = str_replace('-', '', $str_guid);
61
62
        $octet_str = substr($str_guid, 6, 2);
63
        $octet_str .= substr($str_guid, 4, 2);
64
        $octet_str .= substr($str_guid, 2, 2);
65
        $octet_str .= substr($str_guid, 0, 2);
66
        $octet_str .= substr($str_guid, 10, 2);
67
        $octet_str .= substr($str_guid, 8, 2);
68
        $octet_str .= substr($str_guid, 14, 2);
69
        $octet_str .= substr($str_guid, 12, 2);
70
        $octet_str .= substr($str_guid, 16, strlen($str_guid));
71
72
        if ($escape) {
73
            $escaped = '\\';
74
            for ($i = 0; $i < strlen($octet_str); $i+=2) {
75
                $escaped .= substr($octet_str, $i, 2);
76
                if ($i != strlen($octet_str) - 2) {
77
                    $escaped .= '\\';
78
                }
79
            }
80
            return $escaped;
81
        }
82
83
        return $octet_str;
84
    }
85
86
    /**
87
     * @param  string $binsid
88
     * @return string
89
     */
90
    public static function bin_to_str_sid($binsid)
91
    {
92
        $hex_sid = bin2hex($binsid);
93
        $rev = hexdec(substr($hex_sid, 0, 2));
94
        $subcount = hexdec(substr($hex_sid, 2, 2));
95
        $auth = hexdec(substr($hex_sid, 4, 12));
96
        $result = "$rev-$auth";
97
98
        for ($x = 0; $x < $subcount; $x++) {
99
            $subauth[$x] = hexdec(self::little_endian(substr($hex_sid, 16 + ($x * 8), 8)));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$subauth was never initialized. Although not strictly required by PHP, it is generally a good practice to add $subauth = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
100
            $result .= '-' . $subauth[$x];
0 ignored issues
show
Bug introduced by
The variable $subauth does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
101
        }
102
103
        // Cheat by tacking on the S-
104
        return 'S-' . $result;
105
    }
106
107
    /**
108
     * Converts a little-endian hex-number to one, that 'hexdec' can convert
109
     * @param  string $hex
110
     * @return string
111
     */
112
    public static function little_endian($hex)
113
    {
114
        $result = '';
115
        for ($x = strlen($hex) - 2; $x >= 0; $x = $x - 2) {
116
            $result .= substr($hex, $x, 2);
117
        }
118
        return $result;
119
    }
120
}
121