Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — dependabot/composer/phpunit/ph... ( 988165...1414b9 )
by
unknown
18:51 queued 12:52
created

check_for_registration()   B

Complexity

Conditions 11
Paths 15

Size

Total Lines 64
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 35
nc 15
nop 5
dl 0
loc 64
rs 7.3166
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 declare(strict_types=1);
2
3
/**
4
 * @param resource $fp
5
 */
6
function check_for_registration($fp, string $nick, string $channel, callable $callback, bool $validationMessages = true) : AbstractSmrPlayer|false {
7
	//Force $validationMessages to always be boolean.
8
	$validationMessages = $validationMessages === true;
9
10
	$db = Smr\Database::getInstance();
11
12
	// only registered users are allowed to use this command
13
	$dbResult = $db->read('SELECT * FROM irc_seen WHERE nick = ' . $db->escapeString($nick) . ' AND registered = 1 AND channel = ' . $db->escapeString($channel));
14
	if (!$dbResult->hasRecord()) {
15
16
		global $actions;
17
18
		// execute a whois and continue here on whois
19
		fputs($fp, 'WHOIS ' . $nick . EOL);
20
		array_push($actions, array('MSG_318', $channel, $nick, $callback, time(), $validationMessages));
21
22
		return false;
23
	}
24
25
	$registeredNick = $dbResult->record()->getString('registered_nick');
26
27
	// get alliance_id and game_id for this channel
28
	try {
29
		$alliance = SmrAlliance::getAllianceByIrcChannel($channel, true);
30
	} catch (Smr\Exceptions\AllianceNotFound) {
31
		if ($validationMessages === true) {
32
			fputs($fp, 'PRIVMSG ' . $channel . ' :' . $nick . ', the channel ' . $channel . ' has not been registered with me.' . EOL);
33
		}
34
		return false;
35
	}
36
37
	// get smr account
38
	try {
39
		$account = SmrAccount::getAccountByIrcNick($nick, true);
40
	} catch (Smr\Exceptions\AccountNotFound) {
41
		try {
42
			$account = SmrAccount::getAccountByIrcNick($registeredNick, true);
43
		} catch (Smr\Exceptions\AccountNotFound) {
44
			if ($validationMessages === true) {
45
				fputs($fp, 'PRIVMSG ' . $channel . ' :' . $nick . ', please set your \'irc nick\' in SMR preferences to your registered nick so i can recognize you.' . EOL);
46
			}
47
			return false;
48
		}
49
	}
50
51
	// get smr player
52
	try {
53
		$player = SmrPlayer::getPlayer($account->getAccountID(), $alliance->getGameID(), true);
54
	} catch (Smr\Exceptions\PlayerNotFound) {
55
		if ($validationMessages === true) {
56
			fputs($fp, 'PRIVMSG ' . $channel . ' :' . $nick . ', you have not joined the game that this channel belongs to.' . EOL);
57
		}
58
		return false;
59
	}
60
61
	// is the user part of this alliance? (no need to check for 0, cannot happen at this point in code)
62
	if ($player->getAllianceID() != $alliance->getAllianceID()) {
63
		if ($validationMessages === true) {
64
			fputs($fp, 'KICK ' . $channel . ' ' . $nick . ' :You are not a member of this alliance!' . EOL);
65
		}
66
		return false;
67
	}
68
69
	return $player;
70
}
71
72
/**
73
 * @param resource $fp
74
 */
75
function channel_msg_with_registration($fp, string $rdata) : bool
76
{
77
	if (preg_match('/^:(.*)!(.*)@(.*)\sPRIVMSG\s(.*)\s:!(money|forces|seed|seedlist|op|sd)\s/i', $rdata, $msg)) {
78
79
		$nick = $msg[1];
80
		$user = $msg[2];
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
81
		$host = $msg[3];
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
82
		$channel = $msg[4];
83
84
		// check if the query is in public channel
85
		if ($channel == '#smr' || $channel == '#smr-bar') {
86
			fputs($fp, 'PRIVMSG ' . $channel . ' :' . $nick . ', that command can only be used in an alliance controlled channel.' . EOL);
87
			return true;
88
		}
89
90
		$callback = function() use($fp, $rdata) : bool {
91
			return channel_msg_with_registration($fp, $rdata);
92
		};
93
		if (($player = check_for_registration($fp, $nick, $channel, $callback)) === false) {
94
			return true;
95
		}
96
97
		if (channel_msg_money($fp, $rdata, $player)) {
98
			return true;
99
		}
100
		if (channel_msg_forces($fp, $rdata, $player)) {
101
			return true;
102
		}
103
104
		if (channel_msg_seed($fp, $rdata, $player)) {
105
			return true;
106
		}
107
		if (channel_msg_seedlist_add($fp, $rdata, $player)) {
108
			return true;
109
		}
110
		if (channel_msg_seedlist_del($fp, $rdata, $player)) {
111
			return true;
112
		}
113
114
		if (channel_msg_op_info($fp, $rdata, $player)) {
115
			return true;
116
		}
117
		if (channel_msg_op_cancel($fp, $rdata, $player)) {
118
			return true;
119
		}
120
		if (channel_msg_op_set($fp, $rdata, $player)) {
121
			return true;
122
		}
123
		if (channel_msg_op_turns($fp, $rdata, $player)) {
124
			return true;
125
		}
126
		if (channel_msg_op_response($fp, $rdata, $player)) {
127
			return true;
128
		}
129
		if (channel_msg_op_list($fp, $rdata, $player)) {
130
			return true;
131
		}
132
		if (channel_msg_sd_set($fp, $rdata)) {
133
			return true;
134
		}
135
		if (channel_msg_sd_del($fp, $rdata)) {
136
			return true;
137
		}
138
		if (channel_msg_sd_list($fp, $rdata, $player)) {
139
			return true;
140
		}
141
142
	}
143
144
	return false;
145
}
146
147
148
/**
149
 * @param resource $fp
150
 */
151
function channel_msg_seen($fp, string $rdata) : bool
152
{
153
154
	// <Caretaker> MrSpock, Azool ([email protected]) was last seen quitting #smr
155
	// 2 days 10 hours 43 minutes ago (05.10. 05:04) stating 'Some people follow their dreams,
156
	// others hunt them down and mercessly beat them into submission' after spending 1 hour 54 minutes there.
157
158
	// MrSpock, do I look like a mirror? ^_^
159
160
	// MrSpock, please look a bit closer at the memberlist of this channel.
161
162
	if (preg_match('/^:(.*)!(.*)@(.*)\sPRIVMSG\s(.*)\s:!seen\s(.*)\s$/i', $rdata, $msg)) {
163
164
		$nick = $msg[1];
165
		$user = $msg[2];
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
166
		$host = $msg[3];
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
167
		$channel = $msg[4];
168
		$seennick = $msg[5];
169
170
		echo_r('[SEEN] by ' . $nick . ' in ' . $channel . ' for ' . $seennick);
171
172
		// if the user asks for himself
173
		if ($nick == $seennick) {
174
			fputs($fp, 'PRIVMSG ' . $channel . ' :' . $nick . ', do I look like a mirror?' . EOL);
175
			return true;
176
		}
177
178
		$db = Smr\Database::getInstance();
179
180
		// if user provided more than 3 letters we do a wildcard search
181
		if (strlen($seennick) > 3) {
182
			$dbResult = $db->read('SELECT * FROM irc_seen WHERE nick LIKE ' . $db->escapeString('%' . $seennick . '%') . ' AND channel = ' . $db->escapeString($channel) . ' ORDER BY signed_on DESC');
183
		} else {
184
			$dbResult = $db->read('SELECT * FROM irc_seen WHERE nick = ' . $db->escapeString($seennick) . ' AND channel = ' . $db->escapeString($channel));
185
		}
186
187
		// get only one result. shouldn't match more than one
188
		if ($dbResult->hasRecord()) {
189
			$dbRecord = $dbResult->record();
190
191
			$seennick = $dbRecord->getField('nick');
192
			$seenuser = $dbRecord->getField('user');
193
			$seenhost = $dbRecord->getField('host');
194
			$signed_on = $dbRecord->getInt('signed_on');
195
			$signed_off = $dbRecord->getInt('signed_off');
196
197
			if ($signed_off > 0) {
198
199
				$seen_id = $dbRecord->getInt('seen_id');
200
201
				// remember who did the !seen command
202
				$db->write('UPDATE irc_seen
203
							SET seen_count = seen_count + 1,
204
								seen_by = ' . $db->escapeString($nick) . '
205
							WHERE seen_id = ' . $seen_id);
206
207
				fputs($fp, 'PRIVMSG ' . $channel . ' :' . $nick . ', ' . $seennick . ' (' . $seenuser . '@' . $seenhost . ') was last seen quitting ' . $channel . ' ' . format_time(time() - $signed_off) . ' ago after spending ' . format_time($signed_off - $signed_on) . ' there.' . EOL);
208
			} else {
209
				fputs($fp, 'PRIVMSG ' . $channel . ' :' . $nick . ', please look a bit closer at the memberlist of this channel.' . EOL);
210
			}
211
212
			return true;
213
		}
214
215
		fputs($fp, 'PRIVMSG ' . $channel . ' :' . $nick . ', I don\'t remember seeing ' . $seennick . '.' . EOL);
216
		return true;
217
	}
218
219
	return false;
220
}
221
222
/**
223
 * @param resource $fp
224
 */
225
function channel_msg_money($fp, string $rdata, AbstractSmrPlayer $player) : bool
226
{
227
228
	if (preg_match('/^:(.*)!(.*)@(.*)\sPRIVMSG\s(.*)\s:!money\s$/i', $rdata, $msg)) {
229
230
		$nick = $msg[1];
231
		$user = $msg[2];
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
232
		$host = $msg[3];
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
233
		$channel = $msg[4];
234
235
		echo_r('[MONEY] by ' . $nick . ' in ' . $channel);
236
237
		$result = shared_channel_msg_money($player);
238
239
		foreach ($result as $line) {
240
			fputs($fp, 'PRIVMSG ' . $channel . ' :' . $line . EOL);
241
		}
242
243
		return true;
244
	}
245
246
	return false;
247
}
248
249
/**
250
 * @param resource $fp
251
 */
252
function channel_msg_timer($fp, string $rdata) : bool
253
{
254
255
	if (preg_match('/^:(.*)!(.*)@(.*) PRIVMSG (.*) :!timer(\s\d+)?(\s.+)?\s$/i', $rdata, $msg)) {
256
257
		global $events;
258
259
		$nick = $msg[1];
260
		$user = $msg[2];
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
261
		$host = $msg[3];
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
262
		$channel = $msg[4];
263
264
		// no countdown means we give a list of active timers
265
		if (!isset($msg[5])) {
266
267
			fputs($fp, 'PRIVMSG ' . $channel . ' :The following timers have been defined for this channel:' . EOL);
268
			foreach ($events as $event) {
269
				if ($event[2] == $channel) {
270
					fputs($fp, 'PRIVMSG ' . $channel . ' :' . $event[1] . ' in ' . format_time($event[0] - time()) . EOL);
271
				}
272
			}
273
274
			return true;
275
		}
276
277
		if (!is_numeric($msg[5])) {
278
			fputs($fp, 'PRIVMSG ' . $channel . ' :I need to know in how many minutes the timer needs to go off. Example: !timer 25 message to channel' . EOL);
279
		}
280
281
		$countdown = intval($msg[5]);
282
		$message = 'ALERT! ALERT! ALERT!';
283
284
		if (isset($msg[6])) {
285
			$message .= ' ' . $msg[6];
286
		}
287
288
		echo_r('[TIMER] ' . $nick . ' started a timer with ' . $countdown . ' minute(s) (' . $message . ') in ' . $channel);
289
290
		array_push($events, array(time() + $countdown * 60, $message, $channel));
291
292
		fputs($fp, 'PRIVMSG ' . $channel . ' :The timer has been started and will go off in ' . $countdown . ' minute(s).' . EOL);
293
294
		return true;
295
	}
296
297
	return false;
298
}
299
300
/**
301
 * @param resource $fp
302
 */
303
function channel_msg_8ball($fp, string $rdata) : bool
304
{
305
	if (preg_match('/^:(.*)!(.*)@(.*)\sPRIVMSG\s(.*)\s:!8ball (.*)\s$/i', $rdata, $msg)) {
306
307
		$nick = $msg[1];
308
		$user = $msg[2];
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
309
		$host = $msg[3];
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
310
		$channel = $msg[4];
311
		$question = $msg[4];
312
313
		echo_r('[8BALL] by ' . $nick . ' in ' . $channel . '. Question: ' . $question);
314
315
		fputs($fp, 'PRIVMSG ' . $channel . ' :' . shared_channel_msg_8ball() . EOL);
316
317
		return true;
318
	}
319
320
	return false;
321
}
322
323
/**
324
 * @param resource $fp
325
 */
326
function channel_msg_forces($fp, string $rdata, AbstractSmrPlayer $player) : bool
327
{
328
	if (preg_match('/^:(.*)!(.*)@(.*)\sPRIVMSG\s(.*)\s:!forces(.*)\s$/i', $rdata, $msg)) {
329
330
		$nick = $msg[1];
331
		$user = $msg[2];
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
332
		$host = $msg[3];
0 ignored issues
show
Unused Code introduced by
The assignment to $host is dead and can be removed.
Loading history...
333
		$channel = $msg[4];
334
		$galaxy = trim($msg[5]);
335
336
		echo_r('[FORCE_EXPIRE] by ' . $nick . ' in ' . $channel . ' Galaxy: ' . $galaxy);
337
338
		$result = shared_channel_msg_forces($player, $galaxy);
339
		foreach ($result as $line) {
340
			fputs($fp, 'PRIVMSG ' . $channel . ' :' . $line . EOL);
341
		}
342
343
		return true;
344
	}
345
346
	return false;
347
}
348
349
/**
350
 * @param resource $fp
351
 */
352
function channel_msg_help($fp, string $rdata) : bool
353
{
354
355
	// global help?
356
	if (preg_match('/^:(.*)!(.*)@(.*)\sPRIVMSG\s(.*)\s:!help\s$/i', $rdata, $msg)) {
357
358
		$nick = $msg[1];
359
		$user = $msg[2];
360
		$host = $msg[3];
361
		$channel = $msg[4];
362
363
		echo_r('[HELP] ' . $nick . '!' . $user . '@' . $host . ' ' . $channel);
364
365
		fputs($fp, 'NOTICE ' . $nick . ' :--- HELP ---' . EOL);
366
		fputs($fp, 'NOTICE ' . $nick . ' :' . IRC_BOT_NICK . ' is the official SMR bot' . EOL);
367
		fputs($fp, 'NOTICE ' . $nick . ' :If you want his services in your channel please invite him using \'/invite ' . IRC_BOT_NICK . ' #channel\'' . EOL);
368
		fputs($fp, 'NOTICE ' . $nick . ' : ' . EOL);
369
		fputs($fp, 'NOTICE ' . $nick . ' :Available public commands commands:' . EOL);
370
		fputs($fp, 'NOTICE ' . $nick . ' :  !seen <nickname>         Displays the last time <nickname> was seen' . EOL);
371
		fputs($fp, 'NOTICE ' . $nick . ' :  !timer <mins> <msg>      Starts a countdown which will send a notice to the channel with the <msg> in <mins> minutes' . EOL);
372
		fputs($fp, 'NOTICE ' . $nick . ' :  !8ball <question>        Display one of the famous 8ball answers to your <question>' . EOL);
373
		fputs($fp, 'NOTICE ' . $nick . ' :Available alliance commands commands:' . EOL);
374
		fputs($fp, 'NOTICE ' . $nick . ' :  !seedlist                Manages the seedlist' . EOL);
375
		fputs($fp, 'NOTICE ' . $nick . ' :  !seed                    Displays a list of sectors you have not yet seeded' . EOL);
376
		fputs($fp, 'NOTICE ' . $nick . ' :  !op                      Command to manage OPs' . EOL);
377
		fputs($fp, 'NOTICE ' . $nick . ' :  !sd                      Command to manage supply/demands for ports' . EOL);
378
		fputs($fp, 'NOTICE ' . $nick . ' :  !money                   Displays the funds the alliance owns' . EOL);
379
		fputs($fp, 'NOTICE ' . $nick . ' :  !forces [Galaxy]         Will tell you when forces will expire. Can be used without parameters.' . EOL);
380
381
		return true;
382
383
		// help on a spec command?
384
	} elseif (preg_match('/^:(.*)!(.*)@(.*)\sPRIVMSG\s(.*)\s:!help\s(.*)\s$/i', $rdata, $msg)) {
385
386
		$nick = $msg[1];
387
		$user = $msg[2];
388
		$host = $msg[3];
389
		$channel = $msg[4];
390
		$topic = $msg[5];
391
392
		echo_r('[HELP' . $topic . '] ' . $nick . '!' . $user . '@' . $host . ' ' . $channel);
393
394
		if ($topic == 'seen') {
395
			fputs($fp, 'NOTICE ' . $nick . ' :Syntax !seen <nickname>' . EOL);
396
			fputs($fp, 'NOTICE ' . $nick . ' :   Displays the last time <nickname> was seen' . EOL);
397
		} else {
398
			fputs($fp, 'NOTICE ' . $nick . ' :There is no help available for this command! Try !help' . EOL);
399
		}
400
401
		return true;
402
	}
403
404
	return false;
405
}
406