Passed
Branch master (0da9c6)
by Smoren
12:00
created
Category
src/MultiCurlRunner.php 1 patch
Spacing   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
 
49 49
         $mh = curl_multi_init();
50 50
 
51
-        if(!$mh) {
51
+        if (!$mh) {
52 52
             throw new RuntimeException("failed creating curl multi handle");
53 53
         }
54 54
 
@@ -65,18 +65,18 @@  discard block
 block discarded – undo
65 65
      */
66 66
     public function run(): self
67 67
     {
68
-        for($i=0; $i<$this->maxConnections; ++$i) {
68
+        for ($i = 0; $i < $this->maxConnections; ++$i) {
69 69
             /** @var resource|false $unemployedWorker */
70 70
             $unemployedWorker = curl_init();
71
-            if(!$unemployedWorker) {
71
+            if (!$unemployedWorker) {
72 72
                 throw new RuntimeException("failed creating unemployed worker #{$i}");
73 73
             }
74 74
             $this->unemployedWorkers[] = $unemployedWorker;
75 75
         }
76 76
         unset($i, $this->unemployedWorker);
77 77
 
78
-        foreach($this->requestsConfigMap as $id => $options) {
79
-            while(!count($this->unemployedWorkers)) {
78
+        foreach ($this->requestsConfigMap as $id => $options) {
79
+            while (!count($this->unemployedWorkers)) {
80 80
                 $this->doWork();
81 81
             }
82 82
 
@@ -84,23 +84,23 @@  discard block
 block discarded – undo
84 84
 
85 85
             $newWorker = array_pop($this->unemployedWorkers);
86 86
 
87
-            if(!curl_setopt_array($newWorker, $options)) {
87
+            if (!curl_setopt_array($newWorker, $options)) {
88 88
                 $errNo = curl_errno($newWorker);
89 89
                 $errMess = curl_error($newWorker);
90 90
                 $errData = var_export($options, true);
91 91
                 throw new RuntimeException("curl_setopt_array failed: {$errNo} {$errMess} {$errData}");
92 92
             }
93 93
 
94
-            $this->workersMap[(int)$newWorker] = $id;
94
+            $this->workersMap[(int) $newWorker] = $id;
95 95
             curl_multi_add_handle($this->mh, $newWorker);
96 96
         }
97 97
         unset($options);
98 98
 
99
-        while(count($this->workersMap) > 0) {
99
+        while (count($this->workersMap) > 0) {
100 100
             $this->doWork();
101 101
         }
102 102
 
103
-        foreach($this->unemployedWorkers as $unemployedWorker) {
103
+        foreach ($this->unemployedWorkers as $unemployedWorker) {
104 104
             curl_close($unemployedWorker);
105 105
         }
106 106
 
@@ -119,8 +119,8 @@  discard block
 block discarded – undo
119 119
     {
120 120
         $result = [];
121 121
 
122
-        foreach($this->result as $key => $value) {
123
-            if(!$okOnly || $value['code'] === 200) {
122
+        foreach ($this->result as $key => $value) {
123
+            if (!$okOnly || $value['code'] === 200) {
124 124
                 $result[$key] = $value;
125 125
             }
126 126
         }
@@ -138,8 +138,8 @@  discard block
 block discarded – undo
138 138
     {
139 139
         $result = [];
140 140
 
141
-        foreach($this->result as $key => $value) {
142
-            if(!$okOnly || $value['code'] >= 200 && $value['code'] < 300) {
141
+        foreach ($this->result as $key => $value) {
142
+            if (!$okOnly || $value['code'] >= 200 && $value['code'] < 300) {
143 143
                 $result[$key] = $value['body'];
144 144
             }
145 145
         }
@@ -156,12 +156,12 @@  discard block
 block discarded – undo
156 156
         assert(count($this->workersMap) > 0, "work() called with 0 workers!!");
157 157
         $stillRunning = null;
158 158
 
159
-        while(true) {
159
+        while (true) {
160 160
             do {
161 161
                 $err = curl_multi_exec($this->mh, $stillRunning);
162
-            } while($err === CURLM_CALL_MULTI_PERFORM);
162
+            } while ($err === CURLM_CALL_MULTI_PERFORM);
163 163
 
164
-            if($err !== CURLM_OK) {
164
+            if ($err !== CURLM_OK) {
165 165
                 $errInfo = [
166 166
                     "multi_exec_return" => $err,
167 167
                     "curl_multi_errno" => curl_multi_errno($this->mh),
@@ -171,7 +171,7 @@  discard block
 block discarded – undo
171 171
                 $errData = str_replace(["\r", "\n"], "", var_export($errInfo, true));
172 172
                 throw new RuntimeException("curl_multi_exec error: {$errData}");
173 173
             }
174
-            if($stillRunning < count($this->workersMap)) {
174
+            if ($stillRunning < count($this->workersMap)) {
175 175
                 // some workers has finished downloading, process them
176 176
                 // echo "processing!";
177 177
                 break;
@@ -181,13 +181,13 @@  discard block
 block discarded – undo
181 181
                 // sleep(1);
182 182
             }
183 183
         }
184
-        while(($info = curl_multi_info_read($this->mh)) !== false) {
185
-            if($info['msg'] !== CURLMSG_DONE) {
184
+        while (($info = curl_multi_info_read($this->mh)) !== false) {
185
+            if ($info['msg'] !== CURLMSG_DONE) {
186 186
                 // no idea what this is, it's not the message we're looking for though, ignore it.
187 187
                 continue;
188 188
             }
189 189
 
190
-            if($info['result'] !== CURLM_OK) {
190
+            if ($info['result'] !== CURLM_OK) {
191 191
                 $errInfo = [
192 192
                     "effective_url" => curl_getinfo($info['handle'], CURLINFO_EFFECTIVE_URL),
193 193
                     "curl_errno" => curl_errno($info['handle']),
@@ -201,7 +201,7 @@  discard block
 block discarded – undo
201 201
             }
202 202
 
203 203
             $ch = $info['handle'];
204
-            $chIndex = (int)$ch;
204
+            $chIndex = (int) $ch;
205 205
 
206 206
             $this->result[$this->workersMap[$chIndex]] = $this->parseResponse(curl_multi_getcontent($ch));
207 207
 
@@ -224,29 +224,29 @@  discard block
 block discarded – undo
224 224
         $statusCode = null;
225 225
         $body = null;
226 226
 
227
-        while(count($arResponse)) {
227
+        while (count($arResponse)) {
228 228
             $respItem = array_shift($arResponse);
229 229
 
230
-            $line = (string)strtok($respItem, "\r\n");
230
+            $line = (string) strtok($respItem, "\r\n");
231 231
             $statusCodeLine = trim($line);
232
-            if(preg_match('|HTTP/[\d.]+\s+(\d+)|', $statusCodeLine, $matches)) {
232
+            if (preg_match('|HTTP/[\d.]+\s+(\d+)|', $statusCodeLine, $matches)) {
233 233
                 $arHeaders = [];
234 234
 
235
-                if(isset($matches[1])) {
236
-                    $statusCode = (int)$matches[1];
235
+                if (isset($matches[1])) {
236
+                    $statusCode = (int) $matches[1];
237 237
                 } else {
238 238
                     $statusCode = null;
239 239
                 }
240 240
 
241 241
                 // Parse the string, saving it into an array instead
242
-                while(($line = strtok("\r\n")) !== false) {
243
-                    if(($matches = explode(':', $line, 2)) !== false) {
242
+                while (($line = strtok("\r\n")) !== false) {
243
+                    if (($matches = explode(':', $line, 2)) !== false) {
244 244
                         $arHeaders[trim(mb_strtolower($matches[0]))] = trim(mb_strtolower($matches[1]));
245 245
                     }
246 246
                 }
247 247
             } else {
248 248
                 $contentType = $arHeaders['content-type'] ?? null;
249
-                if($contentType === 'application/json') {
249
+                if ($contentType === 'application/json') {
250 250
                     $body = json_decode($respItem, true);
251 251
                 } else {
252 252
                     $body = $respItem;
Please login to merge, or discard this patch.
src/MultiCurl.php 1 patch
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
             CURLOPT_ENCODING => ''
46 46
         ];
47 47
 
48
-        foreach($commonOptions as $key => $value) {
48
+        foreach ($commonOptions as $key => $value) {
49 49
             $this->commonOptions[$key] = $value;
50 50
         }
51 51
 
@@ -84,23 +84,23 @@  discard block
 block discarded – undo
84 84
         array $options = [],
85 85
         array $headers = []
86 86
     ): self {
87
-        foreach($this->commonOptions as $key => $value) {
88
-            if(!array_key_exists($key, $options)) {
87
+        foreach ($this->commonOptions as $key => $value) {
88
+            if (!array_key_exists($key, $options)) {
89 89
                 $options[$key] = $value;
90 90
             }
91 91
         }
92 92
 
93
-        foreach($this->commonHeaders as $key => $value) {
94
-            if(!array_key_exists($key, $headers)) {
93
+        foreach ($this->commonHeaders as $key => $value) {
94
+            if (!array_key_exists($key, $headers)) {
95 95
                 $headers[$key] = $value;
96 96
             }
97 97
         }
98 98
 
99
-        if(is_array($body)) {
99
+        if (is_array($body)) {
100 100
             $body = json_encode($body);
101 101
         }
102 102
 
103
-        if($body !== null) {
103
+        if ($body !== null) {
104 104
             $options[CURLOPT_POSTFIELDS] = strval($body);
105 105
         }
106 106
 
@@ -121,7 +121,7 @@  discard block
 block discarded – undo
121 121
     {
122 122
         $result = [];
123 123
 
124
-        foreach($headers as $key => $value) {
124
+        foreach ($headers as $key => $value) {
125 125
             $result[] = "{$key}: {$value}";
126 126
         }
127 127
 
Please login to merge, or discard this patch.