Test Setup Failed
Branch gcconnex (718fe4)
by Ilia
21:13
created

start.php ➔ change_user_email()   F

Complexity

Conditions 23
Paths 2898

Size

Total Lines 105
Code Lines 66

Duplication

Lines 55
Ratio 52.38 %

Importance

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