Socket   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 138
Duplicated Lines 18.12 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 18
c 3
b 0
f 1
lcom 1
cbo 1
dl 25
loc 138
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B _connect() 9 23 5
A getOptions() 0 3 1
B read() 8 24 4
A readOnce() 4 10 2
A write() 4 12 3
A close() 0 3 1

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
namespace Cassandra\Connection;
3
4
class Socket {
5
6
	/**
7
	 * @var resource
8
	 */
9
	protected $_socket;
10
11
	/**
12
	 * @var array
13
	 */
14
	protected $_options = [
15
		'host'		=> null,
16
		'port'		=> 9042,
17
		'username'	=> null,
18
		'password'	=> null,
19
		'socket'	=> [
20
			SO_RCVTIMEO => ["sec" => 30, "usec" => 0],
21
			SO_SNDTIMEO => ["sec" => 5, "usec" => 0],
22
		],
23
	];
24
25
	/**
26
	 * @param array $options
27
	 */
28
	public function __construct(array $options) {
29
		if (isset($options['socket'])) {
30
			$options['socket'] += $this->_options['socket'];
31
		}
32
		$this->_options = array_merge($this->_options, $options);
33
		
34
		$this->_connect();
35
	}
36
37
	/**
38
	 * 
39
	 * @throws SocketException
40
	 * @return resource
41
	 */
42
	protected function _connect() {
43
		if (!empty($this->_socket)) return $this->_socket;
44
45
		$this->_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
46
47 View Code Duplication
		if ($this->_socket === false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
48
			$errorCode = socket_last_error($this->_socket);
49
			throw new SocketException(socket_strerror($errorCode), $errorCode);
50
		}
51
52
		socket_set_option($this->_socket, SOL_TCP, TCP_NODELAY, 1);
53
		
54
		foreach($this->_options['socket'] as $optname => $optval)
55
			socket_set_option($this->_socket, SOL_SOCKET, $optname, $optval);
56
		
57
		$result = socket_connect($this->_socket, $this->_options['host'], $this->_options['port']);
58
		
59 View Code Duplication
		if ($result === false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
60
			$errorCode = socket_last_error($this->_socket);
61
			//Unable to connect to Cassandra node: {$this->_options['host']}:{$this->_options['port']}
62
			throw new SocketException(socket_strerror($errorCode), $errorCode);
63
		}
64
	}
65
66
	/**
67
	 * @return array
68
	 */
69
	public function getOptions() {
70
		return $this->_options;
71
	}
72
	
73
74
	/**
75
	 * @param $length
76
	 * @throws SocketException
77
	 * @return string
78
	 */
79
	public function read($length) {
80
		$data = socket_read($this->_socket, $length);
81
		
82 View Code Duplication
		if ($data === false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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
			$errorCode = socket_last_error($this->_socket);
84
			throw new SocketException(socket_strerror($errorCode), $errorCode);
85
		}
86
		
87
		$remainder = $length - strlen($data);
88
		
89
		while($remainder > 0) {
90
			$readData = socket_read($this->_socket, $remainder);
91
	
92 View Code Duplication
			if ($readData === false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
93
				$errorCode = socket_last_error($this->_socket);
94
				throw new SocketException(socket_strerror($errorCode), $errorCode);
95
			}
96
			
97
			$data .= $readData;
98
			$remainder -= strlen($readData);
99
		}
100
		
101
		return $data;
102
	}
103
104
	/**
105
	 * @param $length
106
	 * @throws SocketException
107
	 * @return string
108
	 */
109
	public function readOnce($length){
110
		$data = socket_read($this->_socket, $length);
111
		
112 View Code Duplication
		if ($data === false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
113
			$errorCode = socket_last_error($this->_socket);
114
			throw new SocketException(socket_strerror($errorCode), $errorCode);
115
		}
116
		
117
		return $data;
118
	}
119
	
120
	/**
121
	 * 
122
	 * @param string $binary
123
	 * @throws SocketException
124
	 */
125
	public function write($binary){
126
		do{
127
			$sentBytes = socket_write($this->_socket, $binary);
128
			
129 View Code Duplication
			if ($sentBytes === false){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
130
				$errorCode = socket_last_error($this->_socket);
131
				throw new SocketException(socket_strerror($errorCode), $errorCode);
132
			}
133
			$binary = substr($binary, $sentBytes);
134
		}
135
		while(!empty($binary));
136
	}
137
	
138
	public function close(){
139
		socket_shutdown($this->_socket);
140
	}
141
}
142