UNIX   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 128
Duplicated Lines 22.66 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 29
loc 128
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 4 1
B bindSocket() 9 48 11
C onBound() 20 32 13

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 PHPDaemon\BoundSocket;
3
4
use PHPDaemon\Core\Daemon;
5
6
/**
7
 * UNIX
8
 *
9
 * @package Core
10
 *
11
 * @author  Vasily Zorin <[email protected]>
12
 */
13
class UNIX extends Generic
14
{
15
    /**
16
     * @var \PHPDaemon\Config\Section
17
     */
18
    public $config;
19
    /**
20
     * Group
21
     * @var string
22
     */
23
    protected $group;
24
25
    /**
26
     * User
27
     * @var string
28
     */
29
    protected $user;
30
31
    /**
32
     * Path
33
     * @var string
34
     */
35
    protected $path;
36
    /**
37
     * Listener mode?
38
     * @var boolean
39
     */
40
    protected $listenerMode = false;
41
42
    /**
43
     * toString handler
44
     * @return string
45
     */
46
    public function __toString()
47
    {
48
        return $this->path;
49
    }
50
51
    /**
52
     * Bind socket
53
     * @return boolean Success.
54
     */
55
    public function bindSocket()
56
    {
57
        if ($this->erroneous) {
58
            return false;
59
        }
60
61
        if ($this->path === null && isset($this->uri['path'])) {
62
            $this->path = $this->uri['path'];
63
        }
64
65
        if (pathinfo($this->path, PATHINFO_EXTENSION) !== 'sock') {
66
            Daemon::$process->log('Unix-socket \'' . $this->path . '\' must has \'.sock\' extension.');
67
            return false;
68
        }
69
70
        if (file_exists($this->path)) {
71
            unlink($this->path);
72
        }
73
74
        if ($this->listenerMode) {
75
            $this->setFd('unix:' . $this->path);
76
            return true;
77
        }
78
        $sock = socket_create(AF_UNIX, SOCK_STREAM, 0);
79 View Code Duplication
        if (!$sock) {
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...
80
            $errno = socket_last_error();
81
            Daemon::$process->log(get_class($this) . ': Couldn\'t create UNIX-socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
82
            return false;
83
        }
84
85
        // SO_REUSEADDR is meaningless in AF_UNIX context
86
        if (!@socket_bind($sock, $this->path)) {
87
            if (isset($this->config->maxboundsockets->value)) { // no error-messages when maxboundsockets defined
0 ignored issues
show
Bug introduced by
The property maxboundsockets does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
88
                return false;
89
            }
90
            $errno = socket_last_error();
91
            Daemon::$process->log(get_class($this) . ': Couldn\'t bind Unix-socket \'' . $this->path . '\' (' . $errno . ' - ' . socket_strerror($errno) . ').');
92
            return false;
93
        }
94
        socket_set_nonblock($sock);
95
        $this->onBound();
96 View Code Duplication
        if (!socket_listen($sock, SOMAXCONN)) {
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...
97
            $errno = socket_last_error();
98
            Daemon::$process->log(get_class($this) . ': Couldn\'t listen UNIX-socket \'' . $this->path . '\' (' . $errno . ' - ' . socket_strerror($errno) . ')');
99
        }
100
        $this->setFd($sock);
101
        return true;
102
    }
103
104
    /**
105
     * Called when socket is bound
106
     * @return boolean Success
107
     */
108
    protected function onBound()
109
    {
110
        touch($this->path);
111
        chmod($this->path, 0770);
112
        if ($this->group === null && !empty($this->uri['pass'])) {
113
            $this->group = $this->uri['pass'];
114
        }
115 View Code Duplication
        if ($this->group === null && isset(Daemon::$config->group->value)) {
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...
116
            $this->group = Daemon::$config->group->value;
117
        }
118 View Code Duplication
        if ($this->group !== null) {
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...
119
            if (!@chgrp($this->path, $this->group)) {
120
                unlink($this->path);
121
                Daemon::log('Couldn\'t change group of the socket \'' . $this->path . '\' to \'' . $this->group . '\'.');
122
                return false;
123
            }
124
        }
125
        if ($this->user === null && !empty($this->uri['user'])) {
126
            $this->user = $this->uri['user'];
127
        }
128 View Code Duplication
        if ($this->user === null && isset(Daemon::$config->user->value)) {
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...
129
            $this->user = Daemon::$config->user->value;
130
        }
131 View Code Duplication
        if ($this->user !== null) {
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...
132
            if (!@chown($this->path, $this->user)) {
133
                unlink($this->path);
134
                Daemon::log('Couldn\'t change owner of the socket \'' . $this->path . '\' to \'' . $this->user . '\'.');
135
                return false;
136
            }
137
        }
138
        return true;
139
    }
140
}
141