Completed
Push — master ( 68b6aa...23a9a3 )
by Patrick
03:33
created

LDAPGroup::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
namespace Auth;
3
4
class LDAPGroup extends Group
5
{
6
    use LDAPCachableObject;
7
8
    private $ldapObj;
9
    private $server;
10
11
    public function __construct($data)
12
    {
13
        $this->server = \LDAP\LDAPServer::getInstance();
14
        $this->initialize($data);
15
    }
16
17
    public function getGroupName()
18
    {
19
        return $this->getFieldSingleValue('cn');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFieldSingleValue('cn'); (string) is incompatible with the return type of the parent method Auth\Group::getGroupName of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
20
    }
21
22
    public function getDescription()
23
    {
24
        return $this->getFieldSingleValue('description');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFieldSingleValue('description'); (string) is incompatible with the return type of the parent method Auth\Group::getDescription of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
25
    }
26
27
    public function setDescription($name)
28
    {
29
        return $this->setField('description', $name);
30
    }
31
32
    private function getMembersField(&$fieldName = false)
33
    {
34
        $rawMembers = $this->getField('member');
35
        $fieldName  = 'member';
36
        if($rawMembers === false)
37
        {
38
            $rawMembers = $this->getField('uniqueMember');
39
            $fieldName  = 'uniqueMember';
40
        }
41
        if($rawMembers === false)
42
        {
43
            $rawMembers = $this->getField('memberUid');
44
            $fieldName  = 'memberUid';
45
        }
46
        if(!isset($rawMembers['count']))
47
        {
48
            $rawMembers['count'] = count($rawMembers);
49
        }
50
        return $rawMembers;
51
    }
52
53
    private function getIDFromDN($dn)
54
    {
55
        $split = explode(',', $dn);
56
        if(strncmp('cn=', $split[0], 3) === 0)
57
        {
58
            return substr($split[0], 3);
59
        }
60
        return substr($split[0], 4);
61
    }
62
63
    public function getMemberUids($recursive = true)
64
    {
65
        $members = array();
66
        $rawMembers = $this->getMembersField();
67
        for($i = 0; $i < $rawMembers['count']; $i++)
68
        {
69
            if($recursive && strncmp($rawMembers[$i], 'cn=', 3) === 0)
70
            {
71
                $child = new LDAPGroup($rawMembers[$i]);
72
                if($child !== false)
73
                {
74
                    $members = array_merge($members, $child->members());
75
                }
76
            }
77
            else
78
            {
79
                array_push($members, $rawMembers[$i]);
80
            }
81
        }
82
        $count = count($members);
83
        for($i = 0; $i < $count; $i++)
84
        {
85
            $members[$i] = $this->getIDFromDN($members[$i]);
86
        }
87
        return $members;
88
    }
89
90
    private function getObjectFromDN($dn)
91
    {
92
        $split = explode(',', $dn);
93
        if(strncmp('cn=', $dn, 3) === 0)
94
        {
95
            if(count($split) === 1)
96
            {
97
                return LDAPGroup::from_name($dn, $this->server);
98
            }
99
            return LDAPGroup::from_name(substr($split[0], 3), $this->server);
100
        }
101
        if(count($split) === 1)
102
        {
103
            return LDAPUser::from_name($dn, $this->server);
104
        }
105
        return LDAPUser::from_name(substr($split[0], 4), $this->server);
106
    }
107
108
    private function getMemberDetail($members)
109
    {
110
        $details = array();
111
        $count = count($members);
112
        for($i = 0; $i < $count; $i++)
113
        {
114
            $details[$i] = $this->getObjectFromDN($members[$i]);
115
        }
116
        return $details;
117
    }
118
119
    public function members($details = false, $recursive = true, $includeGroups = true)
120
    {
121
        $members = array();
122
        $rawMembers = $this->getMembersField();
123
        for($i = 0; $i < $rawMembers['count']; $i++)
124
        {
125
            if($recursive && strncmp($rawMembers[$i], 'cn=', 3) === 0)
126
            {
127
                $child = new LDAPGroup($rawMembers[$i]);
128
                if($child !== false)
129
                {
130
                    $members = array_merge($members, $child->members());
131
                }
132
            }
133
            else if($includeGroups !== false || strncmp($rawMembers[$i], 'cn=', 3) !== 0)
134
            {
135
                array_push($members, $rawMembers[$i]);
136
            }
137
        }
138
        if($details === true)
139
        {
140
            $members = $this->getMemberDetail($members);
141
        }
142
        return $members;
143
    }
144
145
    public function getNonMemebers($select = false)
146
    {
147
        $data = array();
148
        $groupFilter = '(&(cn=*)(!(cn='.$this->getGroupName().'))';
149
        $userFilter = '(&(cn=*)';
150
        $members = $this->members();
151
        $count = count($members);
152
        for($i = 0; $i < $count; $i++)
153
        {
154
            $dnComps = explode(',', $members[$i]);
155
            if(strncmp($members[$i], "uid=", 4) == 0)
156
            {
157
                $userFilter .= '(!('.$dnComps[0].'))';
158
            }
159
            else
160
            {
161
                $groupFilter .= '(!('.$dnComps[0].'))';
162
            }
163
        }
164
        $userFilter .= ')';
165
        $groupFilter .= ')';
166
        $groups = $this->server->read($this->server->group_base, $groupFilter);
167
        $count = count($groups);
168
        for($i = 0; $i < $count; $i++)
169
        {
170
            if($groups[$i] === false || $groups[$i] === null)
171
            {
172
                continue;
173
            }
174
            array_push($data, new LDAPGroup($groups[$i]));
175
        }
176
        $users = $this->server->read($this->server->user_base, $userFilter, false, $select);
177
        $count = count($users);
178
        for($i = 0; $i < $count; $i++)
179
        {
180
            array_push($data, new LDAPUser($users[$i]));
181
        } 
182
        return $data;
183
    }
184
185
    public function clearMembers()
186
    {
187
        if(isset($this->ldapObj['member']))
188
        {
189
            $this->ldapObj['member'] = array();
190
        }
191
        else if(isset($this->ldapObj['uniquemember']))
192
        {
193
            $this->ldapObj['uniquemember'] = array();
194
        }
195
        else if(isset($this->ldapObj['memberuid']))
196
        {
197
            $this->ldapObj['memberuid'] = array();
198
        }
199
    }
200
201
    public function addMember($name, $isGroup = false, $flush = true)
202
    {
203
        $dn = false;
0 ignored issues
show
Unused Code introduced by
$dn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
204
        if($isGroup)
205
        {
206
            $dn = 'cn='.$name.','.$this->server->group_base;
207
        }
208
        else
209
        {
210
            $dn = 'uid='.$name.','.$this->server->user_base;
211
        }
212
        $propName   = false;
213
        $rawMembers = $this->getMembersField($propName);
214
        if(isset($rawMembers['count']))
215
        {
216
            unset($rawMembers['count']);
217
        }
218
        if(in_array($dn, $rawMembers) || in_array($name, $rawMembers))
219
        {
220
            return true;
221
        }
222
        if($propName === 'memberUid')
223
        {
224
            if($isGroup)
225
            {
226
                throw new \Exception('Unable to add a group as a child of this group type');
227
            }
228
            array_push($rawMembers, $name);
229
        }
230
        else
231
        {
232
            array_push($rawMembers, $dn);
233
        }
234
        $tmp = strtolower($propName);
235
        $this->ldapObj->$tmp = $rawMembers;
236
        if($flush === true)
237
        {
238
            $obj = array('dn'=>$this->ldapObj->dn);
239
            $obj[$propName] = $rawMembers;
240
            return $this->server->update($obj);
241
        }
242
        else
243
        {
244
            return true;
245
        }
246
    }
247
248 View Code Duplication
    public static function from_name($name, $data = false)
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...
249
    {
250
        if($data === false)
251
        {
252
            throw new \Exception('data must be set for LDAPGroup');
253
        }
254
        $filter = new \Data\Filter("cn eq $name");
255
        $group = $data->read($data->group_base, $filter);
0 ignored issues
show
Bug introduced by
The method read cannot be called on $data (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
256
        if($group === false || !isset($group[0]))
257
        {
258
            return false;
259
        }
260
        return new static($group[0]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new static($group[0]); (Auth\LDAPGroup) is incompatible with the return type of the parent method Auth\Group::from_name of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
261
    }
262
}
263
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
264