Completed
Pull Request — profile-edit-layout (#2304)
by
unknown
17:03 queued 08:27
created

events.php ➔ pagesetup()   F

Complexity

Conditions 21
Paths 579

Size

Total Lines 129

Duplication

Lines 40
Ratio 31.01 %

Code Coverage

Tests 0
CRAP Score 462

Importance

Changes 0
Metric Value
cc 21
nc 579
nop 0
dl 40
loc 129
ccs 0
cts 107
cp 0
crap 462
rs 0.4677
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
namespace AU\ActivityTabs;
4
5
/**
6
 * Pagesetup
7
 * 
8
 * @return type
9
 */
10
function pagesetup() {
11
	if (!elgg_is_logged_in()) {
12
		return;
13
	}
14
15
	if (!elgg_in_context('activity') && !elgg_in_context('activity_tabs')) {
16
		return;
17
	}
18
19
	$dbprefix = elgg_get_config('dbprefix');
20
	$priority = 500;
21
22
	$user = elgg_get_logged_in_user_entity();
23
	$filter_context = get_input('filter_context', false);
24
    
25
	$plugin = elgg_get_plugin_from_id(PLUGIN_ID);
26
	if (!$plugin) {
27
		// dunno how this could be possible
28
		return true;
29
	}
30
	$all_settings = $plugin->getAllUserSettings($user->guid);
31
32
	$tabs = array(
33
		'group' => array(),
34
		'collection' => array(),
35
	);
36
37
	if (!empty($all_settings)) {
38
		foreach ($all_settings as $name => $value) {
39
			list($type, $id, $opt) = explode('_', $name);
40
			if ($type !== 'group' && $type !== 'collection') {
41
				continue;
42
			}
43
			if (!$opt) {
44
				$opt = 'enabled';
45
			}
46
			$tabs[$type][$id][$opt] = $value;
47
		}
48
	}
49
50
	$collection_ids = array();
51
	foreach ($tabs['collection'] as $id => $opts) {
52
		$enabled = elgg_extract('enabled', $opts);
53
		if ($enabled == 'yes') {
54
			$collection_ids[] = (int) $id;
55
		}
56
	}
57
58
	$group_ids = array();
59
	foreach ($tabs['group'] as $id => $opts) {
60
		$enabled = elgg_extract('enabled', $opts);
61
		if ($enabled == 'yes') {
62
			$group_ids[] = (int) $id;
63
		}
64
	}
65
66 View Code Duplication
	if (!empty($collection_ids)) {
67
		$collection_ids_in = implode(',', $collection_ids);
68
		$query = "SELECT * FROM {$dbprefix}access_collections
69
			WHERE owner_guid = {$user->guid} AND id IN ($collection_ids_in) AND name NOT LIKE 'Group:%'";
70
		$collections = get_data($query);
71
	}
72
73 View Code Duplication
	if (!empty($collections)) {
74
75
		// iterate through collections and add tabs as necessary
76
		foreach ($collections as $collection) {
77
			// we need to create a tab
78
			$tab = array(
79
				'name' => "collection:$collection->id",
80
				'text' => $collection->name,
81
				'href' => "activity_tabs/collection/{$collection->id}/" . elgg_get_friendly_title($collection->name),
82
				'selected' => $filter_context == 'collection_' . $collection->id,
83
				'priority' => $priority + (int) $tabs['collection']["$collection->id"]['priority'],
84
			);
85
			elgg_register_menu_item('filter', $tab);
86
		}
87
	}
88
89 View Code Duplication
	if (!empty($group_ids)) {
90
		$group_ids_in = implode(',', $group_ids);
91
		$query = "SELECT * FROM {$dbprefix}groups_entity ge
92
			JOIN {$dbprefix}entity_relationships er ON er.guid_two = ge.guid
93
			WHERE er.guid_one = {$user->guid} AND ge.guid IN ($group_ids_in)";
94
		$groups = get_data($query);
95
	}
96
97 View Code Duplication
	if (!empty($groups)) {
98
		foreach ($groups as $group) {
99
			$tab = array(
100
				'name' => "group:$group->guid",
101
				'text' => $group->name,
102
				'href' => "activity_tabs/group/{$group->guid}/" . elgg_get_friendly_title($group->name),
103
				'selected' => $filter_context == 'group_' . $group->guid,
104
				'priority' => $priority + (int) $tabs['group']["$group->guid"]['priority'],
105
			);
106
		}
107
		elgg_register_menu_item('filter', $tab);
0 ignored issues
show
Bug introduced by
The variable $tab 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...
108
	}
109
110
	if( strpos(elgg_get_site_entity()->name, 'collab') == false ){
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos(elgg_get_site_entity()->name, 'collab') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
111
		$tab = array(
112
			'name' => "mydept:$user->department",
113
			'text' => elgg_echo('activity_tabs:mydepartment'),
114
			'href' => "activity_tabs/mydept/",
115
			'selected' => $filter_context == 'mydept_',
116
			'priority' => $priority + (int) $tabs['dept']["$user->department"]['priority'],
117
		);
118
		elgg_register_menu_item('filter', $tab);
119
120
		$tab = array(
121
			'name' => "otherdept:$user->department",
122
			'text' => elgg_echo('activity_tabs:otherdepartments'),
123
			'href' => "activity_tabs/otherdept/",
124
			'selected' => $filter_context == 'otherdept_',
125
			'priority' => $priority + (int) $tabs['otherdept']["$user->department"]['priority'],
126
		);
127
		elgg_register_menu_item('filter', $tab);
128
	}
129
130
	// register menu item for configuring tabs
131
	$link = array(
132
		'name' => 'configure_activity_tabs',
133
		'text' => elgg_echo('activity_tabs:configure'),
134
		'href' => 'settings/plugins/' . $user->username,
135
	);
136
137
	elgg_register_menu_item('page', $link);
138
}
139
140
/**
141
 * Run-once upgrades
142
 * @return boolean
143
 */
144
function upgrades() {
145
	if (!elgg_is_admin_logged_in()) {
146
		return true;
147
	}
148
149
	require_once __DIR__ . '/upgrades.php';
150
	run_function_once(__NAMESPACE__ . '\\upgrade20151017');
151
}
152