Parser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 41.88 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 80.36%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 9
c 4
b 1
f 1
lcom 1
cbo 3
dl 49
loc 117
ccs 45
cts 56
cp 0.8036
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getConnection() 0 4 1
A parseLine() 0 21 1
A getNetworkId() 15 15 2
A getChannelId() 17 17 2
A getNickId() 17 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace tomzx\IRCStats;
4
5
use Illuminate\Database\Connection;
6
7
class Parser {
8
	/**
9
	 * @var \tomzx\IRCStats\DatabaseProxy
10
	 */
11
	protected $databaseProxy;
12
13
	/**
14
	 * @param \tomzx\IRCStats\DatabaseProxy $databaseProxy
15
	 */
16 1
	public function __construct(DatabaseProxy $databaseProxy)
17
	{
18 1
		$this->databaseProxy = $databaseProxy;
19 1
	}
20
21
	/**
22
	 * @return \Illuminate\Database\Connection
23
	 */
24 1
	protected function getConnection()
25
	{
26 1
		return $this->databaseProxy->getConnection();
27
	}
28
29
	/**
30
	 * @param array $line
31
	 * @return void
32
	 */
33 1
	public function parseLine(array $line)
34
	{
35 1
		$db = $this->getConnection();
36
37 1
		$server = $line['server'];
38 1
		$channel = $line['channel'];
39 1
		$nick = $line['nick'];
40 1
		$timestamp = $line['timestamp'];
41 1
		$message = $line['message'];
42
43 1
		$networkId = $this->getNetworkId($db, $server);
44 1
		$channelId = $this->getChannelId($db, $networkId, $channel);
45 1
		$nickId = $this->getNickId($db, $channelId, $nick);
46
47 1
		$db->table('logs')->insert([
48 1
			'channel_id' => $channelId,
49 1
			'nick_id'    => $nickId,
50 1
			'timestamp'  => $timestamp,
51 1
			'message'    => $message,
52 1
		]);
53 1
	}
54
55
	/**
56
	 * @param \Illuminate\Database\Connection $db
57
	 * @param string                          $server
58
	 * @return int
59
	 */
60 1 View Code Duplication
	protected function getNetworkId(Connection $db, $server)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
	{
62 1
		$targetNetwork = $db->table('networks')
63 1
			->select('id')
64 1
			->where('server', '=', $server)
65 1
			->first();
66
67 1
		if ($targetNetwork) {
68 1
			return $targetNetwork['id'];
69
		} else {
70
			return $db->table('networks')->insertGetId([
71
				'server' => $server,
72
			]);
73
		}
74
	}
75
76
	/**
77
	 * @param \Illuminate\Database\Connection $db
78
	 * @param int                             $networkId
79
	 * @param string                          $channel
80
	 * @return int
81
	 */
82 1 View Code Duplication
	protected function getChannelId(Connection $db, $networkId, $channel)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
	{
84 1
		$targetChannel = $db->table('channels')
85 1
			->select('id')
86 1
			->where('network_id', '=', $networkId)
87 1
			->where('channel', '=', $channel)
88 1
			->first();
89
90 1
		if ($targetChannel) {
91 1
			return $targetChannel['id'];
92
		} else {
93
			return $db->table('channels')->insertGetId([
94
				'network_id' => $networkId,
95
				'channel'    => $channel,
96
			]);
97
		}
98
	}
99
100
	/**
101
	 * @param \Illuminate\Database\Connection $db
102
	 * @param int                             $channelId
103
	 * @param string                          $nick
104
	 * @return int
105
	 */
106 1 View Code Duplication
	protected function getNickId(Connection $db, $channelId, $nick)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
	{
108 1
		$targetNick = $db->table('nicks')
109 1
			->select('id')
110 1
			->where('channel_id', '=', $channelId)
111 1
			->where('nick', '=', $nick)
112 1
			->first();
113
114 1
		if ($targetNick) {
115 1
			return $targetNick['id'];
116
		} else {
117
			return $db->table('nicks')->insertGetId([
118
				'channel_id' => $channelId,
119
				'nick'       => $nick,
120
			]);
121
		}
122
	}
123
}
124