Skip to content

Commit 3e709ff

Browse files
committed
Coding style
1 parent d17a566 commit 3e709ff

File tree

1 file changed

+37
-66
lines changed

1 file changed

+37
-66
lines changed

PHPDaemon/Clients/Mongo/Connection.php

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,18 @@ class Connection extends ClientConnection
2121
public $requests = []; // Pending requests
2222
public $lastReqId = 0; // ID of the last request
2323

24-
public function onReady()
25-
{
26-
if ($this->user===null)
27-
{
24+
public function onReady() {
25+
if ($this->user === null) {
2826
$this->connected = true;
2927
}
30-
if ($this->connected)
31-
{
28+
if ($this->connected) {
3229
parent::onReady();
3330
return;
3431
}
3532
$this->dbname = $this->path;
3633
$this->pool->getNonce(['dbname' => $this->dbname],
37-
function ($result)
38-
{
39-
if (isset($result['$err']))
40-
{
34+
function ($result) {
35+
if (isset($result['$err'])) {
4136
Daemon::log('MongoClient: getNonce() error with '.$this->url.': '.$result['$err']);
4237
$this->finish();
4338
}
@@ -47,11 +42,9 @@ function ($result)
4742
'nonce' => $result['nonce'],
4843
'dbname' => $this->dbname,
4944
],
50-
function ($result)
51-
{
52-
if (!isset($result['ok']) || !$result['ok'])
53-
{
54-
Daemon::log('MongoClient: authentication error with '.$this->url.': '.$result['errmsg']);
45+
function ($result) {
46+
if (!isset($result['ok']) || !$result['ok']) {
47+
Daemon::log('MongoClient: authentication error with ' . $this->url . ': ' . $result['errmsg']);
5548
$this->finish();
5649
return;
5750
}
@@ -66,121 +59,99 @@ function ($result)
6659
* Called when new data received
6760
* @return void
6861
*/
69-
public function onRead()
70-
{
62+
public function onRead() {
7163
start:
72-
if ($this->freed)
73-
{
64+
if ($this->freed) {
7465
return;
7566
}
76-
if ($this->state===self::STATE_ROOT)
77-
{
78-
if (false===($hdr = $this->readExact(16)))
79-
{
67+
if ($this->state === self::STATE_ROOT) {
68+
if (false === ($hdr = $this->readExact(16))) {
8069
return; // we do not have a header
8170
}
8271
$this->hdr = unpack('Vlen/VreqId/VresponseTo/VopCode', $hdr);
83-
$this->hdr['plen'] = $this->hdr['len']-16;
72+
$this->hdr['plen'] = $this->hdr['len'] - 16;
8473
$this->setWatermark($this->hdr['plen'], $this->hdr['plen']);
8574
$this->state = self::STATE_PACKET;
8675
}
87-
if ($this->state===self::STATE_PACKET)
76+
if ($this->state === self::STATE_PACKET)
8877
{
89-
if (false===($pct = $this->readExact($this->hdr['plen'])))
78+
if (false === ($pct = $this->readExact($this->hdr['plen'])))
9079
{
9180
return; //we do not have a whole packet
9281
}
9382
$this->state = self::STATE_ROOT;
9483
$this->setWatermark(16, 0xFFFFFF);
95-
if ($this->hdr['opCode']===Pool::OP_REPLY)
96-
{
84+
if ($this->hdr['opCode'] === Pool::OP_REPLY) {
9785
$r = unpack('Vflag/VcursorID1/VcursorID2/Voffset/Vlength', binarySubstr($pct, 0, 20));
9886
$r['cursorId'] = binarySubstr($pct, 4, 8);
9987
$id = (int)$this->hdr['responseTo'];
100-
if (isset($this->requests[$id]))
101-
{
88+
if (isset($this->requests[$id])) {
10289
$req = $this->requests[$id];
10390
}
104-
else
105-
{
91+
else {
10692
$req = false;
10793
}
10894
$flagBits = str_pad(strrev(decbin($r['flag'])), 8, '0', STR_PAD_LEFT);
109-
$curId = ($r['cursorId']!=="\x00\x00\x00\x00\x00\x00\x00\x00" ? 'c'.$r['cursorId'] : 'r'.$this->hdr['responseTo']);
95+
$curId = ($r['cursorId'] !== "\x00\x00\x00\x00\x00\x00\x00\x00" ? 'c'.$r['cursorId'] : 'r'.$this->hdr['responseTo']);
11096

111-
if ($req && isset($req[2]) && ($req[2]===false) && !isset($this->cursors[$curId]))
112-
{
97+
if ($req && isset($req[2]) && ($req[2]===false) && !isset($this->cursors[$curId])) {
11398
$cur = new Cursor($curId, $req[0], $this);
11499
$this->cursors[$curId] = $cur;
115-
$cur->failure = $flagBits[1]==='1';
116-
$cur->await = $flagBits[3]==='1';
100+
$cur->failure = $flagBits[1] === '1';
101+
$cur->await = $flagBits[3] === '1';
117102
$cur->callback = $req[1];
118103
$cur->parseOplog = isset($req[3]) && $req[3];
119104
$cur->tailable = isset($req[4]) && $req[4];
120105
}
121-
else
122-
{
106+
else {
123107
$cur = isset($this->cursors[$curId]) ? $this->cursors[$curId] : false;
124108
}
125-
if ($cur && (($r['length']===0) || (binarySubstr($curId, 0, 1)==='r')))
126-
{
127-
if ($cur->tailable)
128-
{
129-
if ($cur->finished = ($flagBits[0]=='1'))
130-
{
109+
if ($cur && (($r['length']===0) || (binarySubstr($curId, 0, 1) === 'r'))) {
110+
if ($cur->tailable) {
111+
if ($cur->finished = ($flagBits[0] == '1')) {
131112
$cur->destroy();
132113
}
133114
}
134-
else
135-
{
115+
else {
136116
$cur->finished = true;
137117
}
138118
}
139119

140120
$p = 20;
141121
$items = [];
142-
while ($p < $this->hdr['plen'])
143-
{
122+
while ($p < $this->hdr['plen']) {
144123
$dl = unpack('Vlen', binarySubstr($pct, $p, 4));
145124
$doc = bson_decode(binarySubstr($pct, $p, $dl['len']));
146125

147-
if ($cur)
148-
{
149-
if ($cur->parseOplog && isset($doc['ts']))
150-
{
151-
$tsdata = unpack('Vsec/Vinc', binarySubstr($pct, $p+1+4+3, 8));
126+
if ($cur) {
127+
if ($cur->parseOplog && isset($doc['ts'])) {
128+
$tsdata = unpack('Vsec/Vinc', binarySubstr($pct, $p + 8, 8));
152129
$doc['ts'] = $tsdata['sec'].' '.$tsdata['inc'];
153130
}
154131
$cur->items[] = $doc;
155132
}
156-
else
157-
{
133+
else {
158134
$items[] = $doc;
159135
}
160136
$p += $dl['len'];
161137
}
162138
$this->setFree(true);
163-
if (isset($req[2]) && $req[2] && $req[1])
164-
{
139+
if (isset($req[2]) && $req[2] && $req[1]) {
165140
call_user_func(
166141
$req[1],
167142
sizeof($items) ? $items[0] : false
168143
);
169144

170-
if ($cur)
171-
{
172-
if ($cur instanceof Cursor)
173-
{
145+
if ($cur) {
146+
if ($cur instanceof Cursor) {
174147
$cur->destroy();
175148
}
176-
else
177-
{
149+
else {
178150
unset($this->cursors[$curId]);
179151
}
180152
}
181153
}
182-
elseif ($cur)
183-
{
154+
elseif ($cur) {
184155
call_user_func($cur->callback, $cur);
185156
}
186157
unset($this->requests[$id], $req);

0 commit comments

Comments
 (0)