SecureClientSocket   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 11.49 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 2
dl 10
loc 87
ccs 23
cts 25
cp 0.92
rs 10

6 Methods

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