Conditions | 33 |
Paths | 0 |
Total Lines | 96 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 1 | Features | 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 |
||
88 | public function onRead() |
||
89 | { |
||
90 | start: |
||
91 | if ($this->freed) { |
||
92 | return; |
||
93 | } |
||
94 | if ($this->state === self::STATE_ROOT) { |
||
95 | if (false === ($hdr = $this->readExact(16))) { |
||
96 | return; // we do not have a header |
||
97 | } |
||
98 | $this->hdr = unpack('Vlen/VreqId/VresponseTo/VopCode', $hdr); |
||
99 | $this->hdr['plen'] = $this->hdr['len'] - 16; |
||
100 | $this->setWatermark($this->hdr['plen'], $this->hdr['plen']); |
||
101 | $this->state = self::STATE_PACKET; |
||
102 | } |
||
103 | if ($this->state === self::STATE_PACKET) { |
||
104 | if (false === ($pct = $this->readExact($this->hdr['plen']))) { |
||
105 | return; //we do not have a whole packet |
||
106 | } |
||
107 | $this->state = self::STATE_ROOT; |
||
108 | $this->setWatermark(16, 0xFFFFFF); |
||
109 | if ($this->hdr['opCode'] === Pool::OP_REPLY) { |
||
110 | $r = unpack('Vflag/VcursorID1/VcursorID2/Voffset/Vlength', mb_orig_substr($pct, 0, 20)); |
||
111 | $r['cursorId'] = mb_orig_substr($pct, 4, 8); |
||
112 | $id = (int)$this->hdr['responseTo']; |
||
113 | if (isset($this->requests[$id])) { |
||
114 | $req = $this->requests[$id]; |
||
115 | if (sizeof($req) === 1) { // get more |
||
116 | $r['cursorId'] = $req[0]; |
||
117 | } |
||
118 | } else { |
||
119 | $req = false; |
||
120 | } |
||
121 | $flagBits = str_pad(strrev(decbin($r['flag'])), 8, '0', STR_PAD_LEFT); |
||
122 | $curId = ($r['cursorId'] !== "\x00\x00\x00\x00\x00\x00\x00\x00" ? 'c' . $r['cursorId'] : 'r' . $this->hdr['responseTo']); |
||
123 | |||
124 | if ($req && isset($req[2]) && ($req[2] === false) && !isset($this->cursors[$curId])) { |
||
125 | $cur = new Cursor($curId, $req[0], $this); |
||
126 | $this->cursors[$curId] = $cur; |
||
127 | $cur->failure = $flagBits[1] === '1'; |
||
128 | $cur->await = $flagBits[3] === '1'; |
||
129 | $cur->callback = $req[1]; |
||
130 | $cur->parseOplog = isset($req[3]) && $req[3]; |
||
131 | $cur->tailable = isset($req[4]) && $req[4]; |
||
132 | } else { |
||
133 | $cur = isset($this->cursors[$curId]) ? $this->cursors[$curId] : false; |
||
134 | } |
||
135 | if ($cur && (($r['length'] === 0) || (mb_orig_substr($curId, 0, 1) === 'r'))) { |
||
136 | if ($cur->tailable) { |
||
137 | if ($cur->finished = ($flagBits[0] === '1')) { |
||
138 | $cur->destroy(); |
||
139 | } |
||
140 | } else { |
||
141 | $cur->finished = true; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | $p = 20; |
||
146 | $items = []; |
||
147 | while ($p < $this->hdr['plen']) { |
||
148 | $dl = unpack('Vlen', mb_orig_substr($pct, $p, 4)); |
||
149 | $doc = bson_decode(mb_orig_substr($pct, $p, $dl['len'])); |
||
150 | |||
151 | if ($cur) { |
||
152 | if ($cur->parseOplog && isset($doc['ts'])) { |
||
153 | $tsdata = unpack('Vsec/Vinc', mb_orig_substr($pct, $p + 8, 8)); |
||
154 | $doc['ts'] = $tsdata['sec'] . ' ' . $tsdata['inc']; |
||
155 | } |
||
156 | $cur->items[] = $doc; |
||
157 | ++$cur->counter; |
||
158 | } else { |
||
159 | $items[] = $doc; |
||
160 | } |
||
161 | $p += $dl['len']; |
||
162 | } |
||
163 | $this->setFree(true); |
||
164 | if (isset($req[2]) && $req[2] && $req[1]) { |
||
165 | $req[1](sizeof($items) ? $items[0] : false); |
||
166 | |||
167 | if ($cur) { |
||
168 | if ($cur instanceof Cursor) { |
||
169 | $cur->destroy(); |
||
170 | } else { |
||
171 | unset($this->cursors[$curId]); |
||
172 | } |
||
173 | } |
||
174 | } elseif ($cur) { |
||
175 | $func = $cur->callback; |
||
176 | $func($cur); |
||
177 | } |
||
178 | unset($this->requests[$id]); |
||
179 | $req = null; |
||
180 | } |
||
181 | } |
||
182 | goto start; |
||
183 | } |
||
184 | |||
201 |
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.