discussion.php ➔ discussion_handle_edit_page()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 58

Duplication

Lines 4
Ratio 6.9 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 8
nop 2
dl 4
loc 58
ccs 0
cts 44
cp 0
crap 56
rs 7.983
c 0
b 0
f 0

How to fix   Long Method   

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
 * Discussion function library
4
 */
5
6
/**
7
 * List all discussion topics
8
 */
9
function discussion_handle_all_page() {
10
11
	elgg_pop_breadcrumb();
12
	elgg_push_breadcrumb(elgg_echo('discussion'));
13
14
	$content = elgg_list_entities(array(
15
		'type' => 'object',
16
		'subtype' => 'groupforumtopic',
17
		'order_by' => 'e.last_action desc',
18
		'limit' => max(20, elgg_get_config('default_limit')),
19
		'full_view' => false,
20
		'no_results' => elgg_echo('discussion:none'),
21
		'preload_owners' => true,
22
		'preload_containers' => true,
23
	));
24
25
	$title = elgg_echo('discussion:latest');
26
27
	$params = array(
28
		'content' => $content,
29
		'title' => $title,
30
		'sidebar' => elgg_view('discussion/sidebar'),
31
		'filter' => '',
32
	);
33
	$body = elgg_view_layout('content', $params);
34
35
	echo elgg_view_page($title, $body);
36
}
37
38
/**
39
 * List discussion topics in a group
40
 *
41
 * @param int $guid Group entity GUID
42
 */
43
function discussion_handle_list_page($guid) {
44
	$lang = get_current_language();
45
	elgg_set_page_owner_guid($guid);
46
47
	elgg_group_gatekeeper();
48
49
	$group = get_entity($guid);
50
	if (!elgg_instanceof($group, 'group')) {
51
		forward('', '404');
52
	}
53
	elgg_push_breadcrumb(gc_explode_translation($group->name,$lang), $group->getURL());
54
	elgg_push_breadcrumb(elgg_echo('item:object:groupforumtopic'));
55
56
	elgg_register_title_button();
57
58
	$title = elgg_echo('item:object:groupforumtopic');
59
60
	$options = array(
61
		'type' => 'object',
62
		'subtype' => 'groupforumtopic',
63
		'limit' => max(20, elgg_get_config('default_limit')),
64
		'order_by' => 'e.last_action desc',
65
		'container_guid' => $guid,
66
		'full_view' => false,
67
		'no_results' => elgg_echo('discussion:none'),
68
		'preload_owners' => true,
69
	);
70
	$content = elgg_list_entities($options);
71
72
	$params = array(
73
		'content' => $content,
74
		'title' => $title,
75
		'sidebar' => elgg_view('discussion/sidebar'),
76
		'filter' => '',
77
	);
78
79
	$body = elgg_view_layout('content', $params);
80
81
	echo elgg_view_page($title, $body);
82
}
83
84
/**
85
 * Edit or add a discussion topic
86
 *
87
 * @param string $type 'add' or 'edit'
88
 * @param int    $guid GUID of group or topic
89
 */
90
function discussion_handle_edit_page($type, $guid) {
91
	elgg_gatekeeper();
92
	$lang = get_current_language();
93
94
95
	if ($type == 'add') {
96
		$group = get_entity($guid);
97
		if (!elgg_instanceof($group, 'group')) {
98
			register_error(elgg_echo('group:notfound'));
99
			forward();
100
		}
101
102
		// make sure user has permissions to add a topic to container
103
		if (!$group->canWriteToContainer(0, 'object', 'groupforumtopic')) {
104
			register_error(elgg_echo('groups:permissions:error'));
105
			forward($group->getURL());
106
		}
107
108
		$title = elgg_echo('groups:addtopic');
109
110
		elgg_push_breadcrumb(gc_explode_translation($group->name,$lang), "discussion/owner/$group->guid");
111
		elgg_push_breadcrumb($title);
112
113
		$body_vars = discussion_prepare_form_vars();
114
		$content = elgg_view_form('discussion/save', array(), $body_vars);
115
	} else {
116
		$topic = get_entity($guid);
117 View Code Duplication
		if (!elgg_instanceof($topic, 'object', 'groupforumtopic') || !$topic->canEdit()) {
118
			register_error(elgg_echo('discussion:topic:notfound'));
119
			forward();
120
		}
121
		$group = $topic->getContainerEntity();
122
		if (!elgg_instanceof($group, 'group')) {
123
			register_error(elgg_echo('group:notfound'));
124
			forward();
125
		}
126
127
		$title = elgg_echo('groups:edittopic');
128
129
		elgg_push_breadcrumb(gc_explode_translation($group->title,$lang), "discussion/owner/$group->guid");
130
		elgg_push_breadcrumb(gc_explode_translation($topic->title,$lang), $topic->getURL());
131
		elgg_push_breadcrumb($title);
132
133
		$body_vars = discussion_prepare_form_vars($topic);
0 ignored issues
show
Documentation introduced by
$topic is of type object<ElggEntity>, but the function expects a object<ElggObject>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
		$content = elgg_view_form('discussion/save', array(), $body_vars);
135
	}
136
137
138
	$params = array(
139
		'content' => $content,
140
		'title' => $title,
141
		'sidebar' => elgg_view('discussion/sidebar/edit'),
142
		'filter' => '',
143
	);
144
	$body = elgg_view_layout('content', $params);
145
146
	echo elgg_view_page($title, $body);
147
}
148
149
/**
150
 * Edit discussion reply
151
 *
152
 * @param string $type 'edit'
153
 * @param int    $guid GUID of group or topic
154
 */
155
function discussion_handle_reply_edit_page($type, $guid) {
156
	elgg_gatekeeper();
157
158
	if ($type == 'edit') {
159
		$reply = get_entity($guid);
160
		if (!elgg_instanceof($reply, 'object', 'discussion_reply', 'ElggDiscussionReply') || !$reply->canEdit()) {
161
			register_error(elgg_echo('discussion:reply:error:notfound'));
162
			forward();
163
		}
164
		$topic = $reply->getContainerEntity();
165
		if (!elgg_instanceof($topic, 'object', 'groupforumtopic')) {
166
			register_error(elgg_echo('discussion:topic:notfound'));
167
			forward();
168
		}
169
		$group = $topic->getContainerEntity();
170
		if (!elgg_instanceof($group, 'group')) {
171
			register_error(elgg_echo('group:notfound'));
172
			forward();
173
		}
174
175
		$title = elgg_echo('discussion:reply:edit');
176
177
		elgg_push_breadcrumb($group->name, "discussion/owner/$group->guid");
178
		elgg_push_breadcrumb($topic->title, $topic->getURL());
179
		elgg_push_breadcrumb($title);
180
181
		$params = array(
182
			'guid' => $reply->guid,
183
			'hidden' => false,
184
		);
185
		$content = elgg_view('ajax/discussion/reply/edit', $params);
186
	}
187
188
	$params = array(
189
		'content' => $content,
0 ignored issues
show
Bug introduced by
The variable $content 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...
190
		'title' => $title,
0 ignored issues
show
Bug introduced by
The variable $title 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...
191
		'sidebar' => elgg_view('discussion/sidebar/edit'),
192
		'filter' => '',
193
	);
194
	$body = elgg_view_layout('content', $params);
195
196
	echo elgg_view_page($title, $body);
197
}
198
199
/**
200
 * View a discussion topic
201
 *
202
 * @param int $guid GUID of topic
203
 */
204
function discussion_handle_view_page($guid) {
205
	// We now have RSS on topics
206
	global $autofeed;
207
	$autofeed = true;
208
	$lang = get_current_language();
209
	elgg_entity_gatekeeper($guid, 'object', 'groupforumtopic');
210
211
	$topic = get_entity($guid);
212
//$topic->description =  gc_explode_translation($topic->description, $lang); //change content to translation description   
213
	$group = $topic->getContainerEntity();
214
	if (!elgg_instanceof($group, 'group')) {
215
		register_error(elgg_echo('group:notfound'));
216
		forward();
217
	}
218
219
	elgg_load_js('elgg.discussion');
220
221
	elgg_set_page_owner_guid($group->getGUID());
222
223
	elgg_group_gatekeeper();
224
225
	
226
		elgg_push_breadcrumb(gc_explode_translation($group->title,$lang), "discussion/owner/$group->guid");
227
228
	elgg_push_breadcrumb(gc_explode_translation($topic->title, $lang));
229
230
231
	$params = array(
232
		'topic' => $topic,
233
		'show_add_form' => false,
234
	);
235
236
237
	$content = elgg_view_entity($topic, array('full_view' => true, ));
238
	if ($topic->status == 'closed') {
239
		$content .= elgg_view('discussion/replies', $params);
240
		$content .= elgg_view('discussion/closed');
241
	} elseif ($group->canWriteToContainer(0, 'object', 'groupforumtopic') || elgg_is_admin_logged_in()) {
242
		$params['show_add_form'] = true;
243
		$content .= elgg_view('discussion/replies', $params);
244
	} else {
245
		$content .= elgg_view('discussion/replies', $params);
246
	}
247
248
   $title = gc_explode_translation($topic->title, $lang);
249
250
	$params = array(
251
		'content' => $content,
252
		'title' =>$title,
253
		'sidebar' => elgg_view('discussion/sidebar'),
254
		'filter' => '',
255
	);
256
	$body = elgg_view_layout('content', $params);
257
258
	echo elgg_view_page($topic->title, $body);
259
}
260
261
/**
262
 * Prepare discussion topic form variables
263
 *
264
 * @param ElggObject $topic Topic object if editing
265
 * @return array
266
 */
267 View Code Duplication
function discussion_prepare_form_vars($topic = NULL) {
268
	// input names => defaults
269
	$lang = get_current_language();
270
	$values = array(
271
		'title' => '',
272
		'title2' => '',
273
		'description' => '',
274
		'description2' => '',
275
		'status' => '',
276
		'access_id' => ACCESS_DEFAULT,
277
		'tags' => '',
278
		'container_guid' => elgg_get_page_owner_guid(),
279
		'guid' => null,
280
		'topic' => $topic,
281
	);
282
283
	if ($topic) {
284
		foreach (array_keys($values) as $field) {
285
			if (isset($topic->$field)) {
286
287
					$values[$field] = $topic->$field;
288
			}
289
		}
290
	}
291
292
	if (elgg_is_sticky_form('topic')) {
293
		$sticky_values = elgg_get_sticky_values('topic');
294
		foreach ($sticky_values as $key => $value) {
295
			$values[$key] = $value;
296
		}
297
	}
298
299
	elgg_clear_sticky_form('topic');
300
301
	return $values;
302
}
303