Conditions | 9 |
Paths | 37 |
Total Lines | 57 |
Lines | 0 |
Ratio | 0 % |
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 |
||
71 | public function onReadable (): bool { |
||
72 | // read into the buffer |
||
73 | $this->buffer .= $bytes = $this->client->recvAll(); |
||
74 | |||
75 | // check for peer disconnection |
||
76 | if (!strlen($bytes)) { |
||
77 | $this->client->close(); |
||
78 | return false; |
||
79 | } |
||
80 | |||
81 | // read frames from the buffer and yield |
||
82 | try { |
||
83 | // length check |
||
84 | if (strlen($this->buffer) > $this->maxLength) { |
||
85 | throw new WebSocketError(413, "{$this->client} exceeded the maximum handshake size."); |
||
86 | } |
||
87 | // still reading? |
||
88 | if (false === $end = strpos($this->buffer, "\r\n\r\n")) { |
||
89 | return false; |
||
90 | } |
||
91 | // parse the headers |
||
92 | $head = explode("\r\n", substr($this->buffer, 0, $end)); |
||
93 | $this->method = array_shift($head); |
||
94 | foreach ($head as $header) { |
||
95 | $header = explode(':', $header, 2); |
||
96 | if (count($header) !== 2) { |
||
97 | throw new WebSocketError(400, "{$this->client} sent a malformed header."); |
||
98 | } |
||
99 | [$key, $value] = $header; |
||
100 | $key = strtolower(trim($key)); |
||
101 | $value = trim($value); |
||
102 | if (isset($this->headers[$key])) { |
||
103 | $this->headers[$key] .= ', ' . $value; |
||
104 | } |
||
105 | else { |
||
106 | $this->headers[$key] = $value; |
||
107 | } |
||
108 | } |
||
109 | $this->buffer = ''; // wipe the buffer |
||
110 | $this->validate(); |
||
111 | } |
||
112 | catch (WebSocketError $e) { // catch and respond with HTTP error and rethrow |
||
113 | $this->client->write("HTTP/1.1 {$e->getCode()} WebSocket Handshake Failure\r\n\r\n"); |
||
114 | throw $e; |
||
115 | } |
||
116 | catch (Throwable $e) { // catch everything else and respond with HTTP 500 and rethrow |
||
117 | $this->client->write("HTTP/1.1 500 WebSocket Internal Error\r\n\r\n"); |
||
118 | throw $e; |
||
119 | } |
||
120 | |||
121 | // send upgrade headers |
||
122 | $this->upgrade(); |
||
123 | $this->client->write("\r\n\r\n"); |
||
124 | |||
125 | // success |
||
126 | return true; |
||
127 | } |
||
128 | |||
163 | } |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: