Conditions | 14 |
Paths | 26 |
Total Lines | 52 |
Lines | 33 |
Ratio | 63.46 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
47 | public function bindSocket() |
||
48 | { |
||
49 | if ($this->erroneous) { |
||
50 | return false; |
||
51 | } |
||
52 | $port = $this->getPort(); |
||
53 | View Code Duplication | if (!is_int($port)) { |
|
|
|||
54 | Daemon::log(get_class($this) . ' (' . get_class($this->pool) . '): no port defined for \'' . $this->uri['uri'] . '\''); |
||
55 | return; |
||
56 | } |
||
57 | if (($port < 1024) && Daemon::$config->user->value !== 'root') { |
||
58 | $this->listenerMode = false; |
||
59 | } |
||
60 | if ($this->listenerMode) { |
||
61 | $this->setFd($this->host . ':' . $port); |
||
62 | return true; |
||
63 | } |
||
64 | $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
||
65 | View Code Duplication | if (!$sock) { |
|
66 | $errno = socket_last_error(); |
||
67 | Daemon::$process->log(get_class($this->pool) . ': Couldn\'t create TCP-socket (' . $errno . ' - ' . socket_strerror($errno) . ').'); |
||
68 | return false; |
||
69 | } |
||
70 | View Code Duplication | if ($this->reuse) { |
|
71 | if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) { |
||
72 | $errno = socket_last_error(); |
||
73 | Daemon::$process->log(get_class($this->pool) . ': Couldn\'t set option REUSEADDR to socket (' . $errno . ' - ' . socket_strerror($errno) . ').'); |
||
74 | return false; |
||
75 | } |
||
76 | if (defined('SO_REUSEPORT') && !@socket_set_option($sock, SOL_SOCKET, SO_REUSEPORT, 1)) { |
||
77 | $errno = socket_last_error(); |
||
78 | Daemon::$process->log(get_class($this->pool) . ': Couldn\'t set option REUSEPORT to socket (' . $errno . ' - ' . socket_strerror($errno) . ').'); |
||
79 | return false; |
||
80 | } |
||
81 | } |
||
82 | View Code Duplication | if (!@socket_bind($sock, $this->host, $port)) { |
|
83 | $errno = socket_last_error(); |
||
84 | Daemon::$process->log(get_class($this->pool) . ': Couldn\'t bind TCP-socket \'' . $this->host . ':' . $port . '\' (' . $errno . ' - ' . socket_strerror($errno) . ').'); |
||
85 | return false; |
||
86 | } |
||
87 | socket_getsockname($sock, $this->host, $this->port); |
||
88 | socket_set_nonblock($sock); |
||
89 | View Code Duplication | if (!$this->listenerMode) { |
|
90 | if (!socket_listen($sock, SOMAXCONN)) { |
||
91 | $errno = socket_last_error(); |
||
92 | Daemon::$process->log(get_class($this->pool) . ': Couldn\'t listen TCP-socket \'' . $this->host . ':' . $port . '\' (' . $errno . ' - ' . socket_strerror($errno) . ')'); |
||
93 | return false; |
||
94 | } |
||
95 | } |
||
96 | $this->setFd($sock); |
||
97 | return true; |
||
98 | } |
||
99 | |||
121 |
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.