Conditions | 11 |
Paths | 27 |
Total Lines | 48 |
Lines | 9 |
Ratio | 18.75 % |
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 |
||
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) { |
|
|
|||
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 |
||
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)) { |
|
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 | |||
141 |
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.