Completed
Push — master ( 547b58...93d99e )
by Aurimas
02:33
created

SocketPair   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 30.14 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%
Metric Value
wmc 7
lcom 1
cbo 2
dl 22
loc 73
ccs 0
cts 35
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 8 2
A useLeft() 11 11 1
A useRight() 11 11 1
A shutdown() 0 4 1
A getConnection() 0 4 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
3
namespace Thruster\Component\Socket;
4
5
use Thruster\Component\EventEmitter\EventEmitterTrait;
6
use Thruster\Component\EventLoop\EventLoopInterface;
7
use Thruster\Component\Socket\Exception\ConnectionException;
8
9
/**
10
 * Class SocketPair
11
 *
12
 * @package Thruster\Component\Socket
13
 * @author  Aurimas Niekis <[email protected]>
14
 */
15
class SocketPair
16
{
17
    const LEFT_SIDE = true;
18
    const RIGHT_SIDE = false;
19
20
    /**
21
     * @var EventLoopInterface
22
     */
23
    protected $loop;
24
25
    /**
26
     * @var Connection
27
     */
28
    protected $connection;
29
30
    /**
31
     * @var array
32
     */
33
    protected $sockets;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $mode;
39
40
    public function __construct(EventLoopInterface $loop)
41
    {
42
        $this->loop = $loop;
43
    }
44
45
    public function create()
46
    {
47
        $this->sockets = @stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
48
49
        if (false === $this->sockets) {
50
            throw new ConnectionException('Could not create socket pair');
51
        }
52
    }
53
54 View Code Duplication
    public function useLeft() : Connection
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...
55
    {
56
        list($left, $right) = $this->sockets;
57
58
        fclose($right);
59
60
        $this->mode = self::LEFT_SIDE;
61
        $this->connection = new Connection($left, $this->loop);
62
63
        return $this->connection;
64
    }
65
66 View Code Duplication
    public function useRight() : Connection
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...
67
    {
68
        list($left, $right) = $this->sockets;
69
70
        fclose($left);
71
72
        $this->mode = self::RIGHT_SIDE;
73
        $this->connection = new Connection($right, $this->loop);
74
75
        return $this->connection;
76
    }
77
78
    public function shutdown()
79
    {
80
        $this->connection->close();
81
    }
82
83
    public function getConnection() : Connection
84
    {
85
        return $this->connection;
86
    }
87
}
88