Completed
Push — master ( d1d02b...e3e5bf )
by Maik
02:45
created

ClientSocket   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 8
loc 84
ccs 28
cts 28
cp 1
rs 10
c 2
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A connect() 7 7 2
A disconnect() 0 3 1
A isConnected() 0 3 1
A close() 0 8 2
A isWriteable() 0 7 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
/**
4
 * This file is part of the PHP Generics package.
5
 *
6
 * @package Generics
7
 */
8
namespace Generics\Socket;
9
10
/**
11
 * This class provides a basic client socket implementation
12
 *
13
 * @author Maik Greubel <[email protected]>
14
 */
15
class ClientSocket extends Socket {
16
	
17
	/**
18
	 * Whether the socket is connected
19
	 *
20
	 * @var boolean
21
	 */
22
	private $conntected;
23
	
24
	/**
25
	 * Create a new client socket
26
	 *
27
	 * @param Endpoint $endpoint
28
	 *        	The endpoint to use
29
	 * @param resource $clientHandle
30
	 *        	optional existing client handle
31
	 */
32 6
	public function __construct(Endpoint $endpoint, $clientHandle = null) {
33 6
		$this->endpoint = $endpoint;
34 6
		$this->handle = $clientHandle;
35 6
		$this->conntected = false;
36
		
37 6
		if (! is_resource ( $clientHandle )) {
38 6
			parent::__construct ( $endpoint );
39
		}
40 6
	}
41
	
42
	/**
43
	 * Connect to remote endpoint
44
	 *
45
	 * @throws SocketException
46
	 */
47 5 View Code Duplication
	public function connect() {
48 5
		if (! @socket_connect ( $this->handle, $this->endpoint->getAddress (), $this->endpoint->getPort () )) {
49 1
			$code = socket_last_error ( $this->handle );
50 1
			throw new SocketException ( socket_strerror ( $code ), array (), $code );
51
		}
52 4
		$this->conntected = true;
53 4
	}
54
	
55
	/**
56
	 * Disconnects the socket
57
	 *
58
	 * @throws SocketException
59
	 */
60 3
	public function disconnect() {
61 3
		$this->close ();
62 3
	}
63
	
64
	/**
65
	 * Whether the client is connected
66
	 *
67
	 * @return boolean
68
	 */
69 4
	public function isConnected() {
70 4
		return $this->conntected;
71
	}
72
	
73
	/**
74
	 *
75
	 * @see \Generics\Socket\ClientSocket::disconnect()
76
	 */
77 6
	public function close() {
78 6
		if (! $this->conntected) {
79 6
			throw new SocketException ( "Socket is not connected" );
80
		}
81
		
82 4
		parent::close ();
83 4
		$this->conntected = false;
84 4
	}
85
	
86
	/**
87
	 *
88
	 * {@inheritdoc}
89
	 * @see \Generics\Socket\Socket::isWriteable()
90
	 */
91 1
	public function isWriteable() {
92 1
		if (! $this->isConnected ()) {
93 1
			return false;
94
		}
95
		
96 1
		return parent::isWriteable ();
97
	}
98
}
99