Failed Conditions
Branch release-2.1 (4e22cf)
by Rick
07:22
created

EventNew_Notify_Background::execute()   C

Complexity

Conditions 11
Paths 96

Size

Total Lines 79
Code Lines 46

Duplication

Lines 6
Ratio 7.59 %

Importance

Changes 0
Metric Value
cc 11
eloc 46
nc 96
nop 0
dl 6
loc 79
rs 5.3086
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
/**
4
 * This taks handles notifying someone that a new event has been
5
 * added to the calendar - but only when no topic has been created.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines http://www.simplemachines.org
11
 * @copyright 2017 Simple Machines and individual contributors
12
 * @license http://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 Beta 4
15
 */
16
17
/**
18
 * Class EventNew_Notify_Background
19
 */
20
class EventNew_Notify_Background extends SMF_BackgroundTask
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
21
{
22
	/**
23
     * This executes the task - loads up the information, puts the email in the queue and inserts alerts as needed.
24
	 * @return bool Always returns true
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
25
	 */
26
	public function execute()
27
 	{
28
 		global $sourcedir, $smcFunc, $user_profile;
29
30
		// Get everyone who could be notified - those are the people who can see the calendar.
31
		require_once($sourcedir . '/Subs-Members.php');
32
		$members = membersAllowedTo('calendar_view');
33
34
		// Don't alert the event creator
35
		if (!empty($this->_details['sender_id']))
36
			$members = array_diff($members, array($this->_details['sender_id']));
37
38
		// Having successfully figured this out, now let's get the preferences of everyone.
39
		require_once($sourcedir . '/Subs-Notify.php');
40
		$prefs = getNotifyPrefs($members, 'event_new', true);
41
42
		// Just before we go any further, we may not have the sender's name. Let's just quickly fix that.
43
		// If a guest creates the event, we wouldn't be capturing a username or anything.
44
		if (!empty($this->_details['sender_id']) && empty($this->_details['sender_name']))
45
		{
46
			loadMemberData($this->_details['sender_id'], 'minimal');
0 ignored issues
show
Documentation introduced by
'minimal' is of type string, but the function expects a boolean.

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...
47
			if (!empty($user_profile[$this->_details['sender_id']]))
48
				$this->_details['sender_name'] = $user_profile[$this->_details['sender_id']]['real_name'];
49
			else
50
				$this->_details['sender_id'] = 0;
51
		}
52
53
		// So now we find out who wants what.
54
		$alert_bits = array(
55
			'alert' => 0x01,
56
			'email' => 0x02,
57
		);
58
		$notifies = array();
59
60 View Code Duplication
		foreach ($prefs as $member => $pref_option)
61
		{
62
			foreach ($alert_bits as $type => $bitvalue)
63
				if ($pref_option['event_new'] & $bitvalue)
64
					$notifies[$type][] = $member;
65
		}
66
67
		// Firstly, anyone who wants alerts.
68
		if (!empty($notifies['alert']))
69
		{
70
			// Alerts are relatively easy.
71
			$insert_rows = array();
72
			foreach ($notifies['alert'] as $member)
73
			{
74
				$insert_rows[] = array(
75
					'alert_time' => $this->_details['time'],
76
					'id_member' => $member,
77
					'id_member_started' => $this->_details['sender_id'],
78
					'member_name' => $this->_details['sender_name'],
79
					'content_type' => 'event',
80
					'content_id' => $this->_details['event_id'],
81
					'content_action' => empty($this->_details['sender_id']) ? 'new_guest' : 'new',
82
					'is_read' => 0,
83
					'extra' => $smcFunc['json_encode'](
84
					    array(
85
					        "event_id" => $this->_details['event_id'],
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal event_id does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
86
							"event_title" => $this->_details['event_title']
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal event_title does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
87
						)
88
					),
89
				);
90
			}
91
92
			$smcFunc['db_insert']('insert',
93
				'{db_prefix}user_alerts',
94
				array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
95
					'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
96
					'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
97
				$insert_rows,
98
				array('id_alert')
99
			);
100
101
			// And update the count of alerts for those people.
102
			updateMemberData($notifies['alert'], array('alerts' => '+'));
103
		}
104
	}
105
}
106
107
?>