Completed
Push — master ( 1bb428...fe8713 )
by Tobias
8s
created

Group   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 113
Duplicated Lines 21.24 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 24
loc 113
rs 10
c 3
b 0
f 1
wmc 17
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B add() 7 31 6
C addMember() 17 48 11

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
FreeIPA library for PHP
4
Copyright (C) 2015  Tobias Sette <[email protected]>
5
6
This program is free software: you can redistribute it and/or modify
7
it under the terms of the GNU Lesser General Public License as published by
8
the Free Software Foundation, either version 3 of the License, or
9
(at your option) any later version.
10
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU Lesser General Public License for more details.
15
16
You should have received a copy of the GNU Lesser General Public License
17
along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
*/
19
20
// Dependencies:
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
21
//require_once('Base.php');
22
23
/**
24
 * Classes for access to FreeIPA API
25
 * @since GIT: 0.1.0
26
 */
27
namespace FreeIPA\APIAccess;
28
29
/**
30
 * Class to access group resources
31
 *
32
 * @author Tobias Sette <[email protected]>
33
 * @copyright Copyright (c) 2015 Tobias Sette <[email protected]>
34
 * @license LGPLv3
35
 * @package php-freeipa
36
 * @since GIT: 0.1.0
37
 * @version GIT: 0.2.0
38
 */
39
class Group extends \FreeIPA\APIAccess\Base
40
{
41
    /**
42
     * Adds a group
43
     * The main fields in $data:
44
     *  'description' => group description
45
     * If $data is string will be a group description
46
     * 
47
     * @param string $name group name
48
     * @param array|string $data see above
49
     * @return object|bool Object with new group data or false if the group was not found
50
     * @since GIT: 0.1.0
51
     * @version GIT: 0.1.0
52
     * @see ../../docs/return_samples/group_add.txt
53
     * @see \FreeIPA\APIAccess\Connection\buildRequest()
54
     */
55
    public function add($name = null, $data = array())
56
    {
57
        if (!$name || !$data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $name of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
58
            return false;
59
        }
60
61
        // Obtained with the command: ipa -vv group-add group_one --desc="Group one" --all 
62
        $args = array($name);
63
        $default_options = array(
64
            'all' => false,
65
            'external' => false,
66
            'no_members' => false,
67
            'nonposix' => false,
68
            'raw' => false,
69
        );
70 View Code Duplication
        if (is_array($data)) {
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...
71
            $final_options = array_merge($default_options, $data);
72
        } else if (is_string($data)) {
73
            $final_options = array_merge($default_options, array('description' => $data));
74
        } else {
75
            return false;
76
        }
77
78
        // The buildRequest() method already checks the field 'error', which is the only relevant to this API method
79
        $response = $this->getConnection()->buildRequest('group_add', $args, $final_options); //returns json and http code of response
80
        if (!$response) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $response of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
81
            return false;
82
        }
83
84
        return $response[0]->result->result;
85
    }
86
87
    /**
88
     * Adds members (users or other groups) to group
89
     * The main fields in $data:
90
     *  'user' => array that contain users that will be added
91
     *  'group' => array that contain groups that will be added
92
     * If $data is a string, will be user uid
93
     * 
94
     * @param string $group_name group name
95
     * @param array|string $data See explanation above
96
     * @return mixed Array containing information about processing and group data OR false on error
97
     * @since GIT: 0.1.0
98
     * @version GIT: 0.1.0
99
     * @see ../../docs/return_samples/group_add_member.txt
100
     * @see \FreeIPA\APIAccess\Connection\buildRequest()
101
     * @throws \Exception if the request was not completed successfully
102
     */
103
    public function addMember($group_name = null, $data = array())
104
    {
105
        if (!$group_name || !$data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $group_name of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
106
            return false;
107
        }
108
109
        // Obtained with the command: ipa -vv group_add_member group_one --users="stallman" 
110
        $args = array($group_name);
111
        $default_options = array(
112
            'all' => true,
113
            'no_members' => false,
114
            'raw' => false,
115
        );
116 View Code Duplication
        if (is_array($data)) {
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...
117
            $final_options = array_merge($default_options, $data);
118
        } else if (is_string($data)) {
119
            $final_options = array_merge($default_options, array('user' => array($data)));
120
        } else {
121
            return false;
122
        }
123
124
        $response = $this->getConnection()->buildRequest('group_add_member', $args, $final_options); //returns json and http code of response
125
        if (!$response) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $response of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
126
            return false;
127
        }
128
        $returned_json = $response[0];
129
        if (!$returned_json->result->completed) {
130
            $message = "Error while inserting members in group \"$group_name\".";
131 View Code Duplication
            if (!empty($returned_json->result->failed->member->group) ||
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...
132
            !empty($returned_json->result->failed->member->user)) {
133
                $message .= 'Details: ';
134
            }
135
136 View Code Duplication
            if (!empty($returned_json->result->failed->member->group)) {
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...
137
                $message .= implode(' ', $returned_json->result->failed->member->group[0]);
138
            }
139
140 View Code Duplication
            if (!empty($returned_json->result->failed->member->user)) {
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...
141
                $message .= implode(' ', $returned_json->result->failed->member->user[0]);
142
            }
143
144
            throw new \Exception($message);
145
        }
146
147
        // Unlike other methods, where $returned_json->result->result is returned,
148
        // here the $returned_json->result contain usefull information
149
        return $returned_json->result;
150
    }
151
}
152