start.php ➔ change_user_email()   F
last analyzed

Complexity

Conditions 23
Paths 2898

Size

Total Lines 105

Duplication

Lines 55
Ratio 52.38 %

Importance

Changes 0
Metric Value
cc 23
nc 2898
nop 0
dl 55
loc 105
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * WET 4 Collab Theme plugin
4
 *
5
 * @package wet4Theme
6
 */
7
8
elgg_register_event_handler('init', 'system', 'wet4_collab_theme_init');
9
10
function wet4_collab_theme_init() {
11
12
	//message preview
13
    elgg_register_ajax_view("messages/message_preview");
14
	
15
	//elgg_register_plugin_hook_handler('register', 'menu:user_menu', 'remove_custom_colleagues_menu_item', 1);
16
	elgg_register_event_handler('pagesetup', 'system', 'add_custom_colleagues_menu_item', 1000);
17
18
    elgg_unregister_plugin_hook_handler('usersettings:save', 'user', '_elgg_set_user_email');
19
    elgg_register_plugin_hook_handler('usersettings:save', 'user', 'change_user_email');
20
21
    elgg_define_js('moment', [
22
        'src' => '/mod/wet4_collab/views/default/js/moment/moment.js',
23
        'deps' => array('jquery')
24
    ]);
25
26
    elgg_unregister_js('elgg.full_calendar');
27
    $calendar_js = elgg_get_simplecache_url('js', 'event_calendar/fullcalendar.min.js');
28
    elgg_register_simplecache_view('js/event_calendar/fullcalendar.min.js');
29
    elgg_register_js('elgg.full_calendar', $calendar_js);
30
31
    $calendar_css = elgg_get_simplecache_url('css', 'event_calendar/fullcalendar.min.css');
32
    elgg_register_css('elgg.full_calendar', $calendar_css);
33
    
34
    elgg_register_plugin_hook_handler('register', 'menu:site', 'remove_menu_item_handler_collab');
35
36
}
37
38
function remove_menu_item_handler_collab($hook, $type, $menu, $params){
39
    foreach ($menu as $key => $item){
40
        if ($item->getName() == 'mission_main'){
41
			unset($menu[$key]);
42
		}
43
    }
44
    return $menu;
45
}
46
47
function remove_custom_colleagues_menu_item($hook, $type, $return, $params) {
48
    // Remove Colleagues menu item
49
    foreach($return as $key => $item) {
50
        if ($item->getName() == 'Colleagues') {
51
            unset($return[$key]);
52
        }
53
    }
54
    return $return;
55
}
56
/*
57
function add_custom_colleagues_menu_item() {
58
	$user = elgg_get_logged_in_user_entity();
59
60
    if( !empty($user) ){
61
		$options = array(
62
			"type" => "user",
63
			"count" => true,
64
			"relationship" => "friendrequest",
65
			"relationship_guid" => $user->getGUID(),
66
			"inverse_relationship" => true
67
		);
68
69
		$count = elgg_get_entities_from_relationship($options);
70
71
		$countTitle = " - ";
72
		$countBadge = "";
73
		if( $count > 0 ){
74
            //display 9+ instead of huge numbers in notif badge
75
            if( $count >= 10 ){
76
                $countTitle .= '9+';
77
            } else {
78
				$countTitle .= $count;
79
            }
80
81
           $countBadge = "<span class='notif-badge'>" . $count . "</span>";
82
        }
83
84
	    $params = array(
85
			"name" => "Colleaguess",
86
			"href" => "friends/" . $user->username,
87
			"text" => '<i class="fa fa-users mrgn-rght-sm mrgn-tp-sm fa-lg"></i>' . $countBadge,
88
			"title" => elgg_echo('userMenu:colleagues') . $countTitle . elgg_echo('friend_request') .'(s)',
89
	        "class" => '',
90
	        "item_class" => '',
91
			"priority" => '1'
92
		);
93
94
		elgg_register_menu_item("user_menu", $params);
95
	}
96
}*/
97
98
function change_user_email() {
99
	$email = get_input('email');
100
	$user_guid = get_input('guid');
101
102
	if ($user_guid) {
103
		$user = get_user($user_guid);
104
	} else {
105
		$user = elgg_get_logged_in_user_entity();
106
	}
107
108
	if (!is_email_address($email)) {
109
		register_error(elgg_echo('email:save:fail'));
110
		return false;
111
	}
112
113
    elgg_load_library('c_ext_lib');
114
    $isValid = false;
115
116 View Code Duplication
    if ($email) {
117
        // cyu - check if the email is in the list of exceptions
118
        $user_email = explode('@',$email);
119
        $list_of_domains = getExtension();
120
121
        if( elgg_is_active_plugin('c_email_extensions') ){
122
            // Checks against the domain manager list...
123
            $wildcard_query = "SELECT ext FROM email_extensions WHERE ext LIKE '%*%'";
124
            $wildcard_emails = get_data($wildcard_query);
125
            
126
            if( $wildcard_emails ){
0 ignored issues
show
Bug Best Practice introduced by
The expression $wildcard_emails 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...
127
                foreach($wildcard_emails as $wildcard){
128
                    $regex = str_replace(".", "\.", $wildcard->ext);
129
                    $regex = str_replace("*", "[\w-.]+", $regex);
130
                    $regex = "/^@" . $regex . "$/";
131
                    if(preg_match($regex, "@".$user_email[1]) || strtolower(str_replace("*.", "", $wildcard->ext)) == strtolower($user_email[1])){
132
                        $isValid = true;
133
                        break;
134
                    }
135
                }
136
            }
137
        }
138
139
        if( elgg_is_active_plugin('gcRegistration_invitation') ){
140
            // Checks against the email invitation list...
141
            $invitation_query = "SELECT email FROM email_invitations WHERE email = '{$email}'";
142
            $result = get_data($invitation_query);
143
144
            if( count($result) > 0 ) 
145
                $isValid = true;
146
        }
147
148
        if (count($list_of_domains) > 0) {
149
            while ($row = mysqli_fetch_array($list_of_domains)) {
150
                if (strtolower($row['ext']) === strtolower($user_email[1])) {
151
                    $isValid = true;
152
                    break;
153
                }
154
            }
155
            $error_message = elgg_echo('gcc_profile:error').elgg_echo('gcc_profile:notaccepted');
156
        }
157
158
        // cyu - check if domain is gc.ca
159
        if (!$isValid) {
160
            $govt_domain = explode('.',$user_email[1]);
161
            $govt_domain_len = count($govt_domain) - 1;
162
163
            if ($govt_domain[$govt_domain_len - 1].'.'.$govt_domain[$govt_domain_len] === 'gc.ca') {
164
                $isValid = true;
165
            } else {
166
                $isValid = false;
167
                $error_message = elgg_echo('gcc_profile:error').elgg_echo('gcc_profile:notaccepted');
168
            }
169
        }
170
    }
171
172
    if( !$isValid ){
173
    	if( elgg_is_active_plugin('b_extended_profile_collab') ){
174
        	prepare_email_change($user_guid, $email);
175
    	} else {
176
    		register_error($error_message);
0 ignored issues
show
Bug introduced by
The variable $error_message 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...
177
    	}
178
    } else {
179
        if ($user) {
180
			if (strcmp($email, $user->email) != 0) {
181
				if (!get_user_by_email($email)) {
182
					if ($user->email != $email) {
183
184
						$user->email = $email;
185
						if ($user->save()) {
186
							system_message(elgg_echo('email:save:success'));
187
							return true;
188
						} else {
189
							register_error(elgg_echo('email:save:fail'));
190
						}
191
					}
192
				} else {
193
					register_error(elgg_echo('registration:dupeemail'));
194
				}
195
			}
196
		} else {
197
			register_error(elgg_echo('email:save:fail'));
198
		}
199
    }
200
201
    return false;
202
}