Skip to content

Commit 96f5bd4

Browse files
paragonie-scottamouhzi
authored andcommitted
A handful of small improvements (#38)
* Public methods that didn't return a value can now be chained together * Simplified `isFoo()` methods' logic to always return a boolean value.
1 parent 676562d commit 96f5bd4

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

src/Curl/Curl.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ public function __construct()
157157
* Initializer for the curl resource.
158158
*
159159
* Is called by the __construct() of the class or when the curl request is reseted.
160+
* @return self
160161
*/
161162
private function init()
162163
{
@@ -165,6 +166,7 @@ private function init()
165166
$this->setOpt(CURLINFO_HEADER_OUT, true);
166167
$this->setOpt(CURLOPT_HEADER, true);
167168
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
169+
return $this;
168170
}
169171

170172
// protected methods
@@ -253,6 +255,7 @@ public function _exec()
253255
*
254256
* @param string $url The url to make the get request for
255257
* @param array $data Optional arguments who are part of the url
258+
* @return self
256259
*/
257260
public function get($url, $data = array())
258261
{
@@ -263,19 +266,22 @@ public function get($url, $data = array())
263266
}
264267
$this->setOpt(CURLOPT_HTTPGET, true);
265268
$this->exec();
269+
return $this;
266270
}
267271

268272
/**
269273
* Make a post request with optional post data.
270274
*
271275
* @param string $url The url to make the get request
272276
* @param array $data Post data to pass to the url
277+
* @return self
273278
*/
274279
public function post($url, $data = array())
275280
{
276281
$this->setOpt(CURLOPT_URL, $url);
277282
$this->preparePayload($data);
278283
$this->exec();
284+
return $this;
279285
}
280286

281287
/**
@@ -286,6 +292,7 @@ public function post($url, $data = array())
286292
* @param string $url The url to make the get request
287293
* @param array $data Optional data to pass to the $url
288294
* @param bool $payload Whether the data should be transmitted trough payload or as get parameters of the string
295+
* @return self
289296
*/
290297
public function put($url, $data = array(), $payload = false)
291298
{
@@ -298,6 +305,7 @@ public function put($url, $data = array(), $payload = false)
298305
$this->setOpt(CURLOPT_URL, $url);
299306
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PUT');
300307
$this->exec();
308+
return $this;
301309
}
302310

303311
/**
@@ -308,6 +316,7 @@ public function put($url, $data = array(), $payload = false)
308316
* @param string $url The url to make the get request
309317
* @param array $data Optional data to pass to the $url
310318
* @param bool $payload Whether the data should be transmitted trough payload or as get parameters of the string
319+
* @return self
311320
*/
312321
public function patch($url, $data = array(), $payload = false)
313322
{
@@ -320,6 +329,7 @@ public function patch($url, $data = array(), $payload = false)
320329
$this->setOpt(CURLOPT_URL, $url);
321330
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH');
322331
$this->exec();
332+
return $this;
323333
}
324334

325335
/**
@@ -328,6 +338,7 @@ public function patch($url, $data = array(), $payload = false)
328338
* @param string $url The url to make the delete request
329339
* @param array $data Optional data to pass to the $url
330340
* @param bool $payload Whether the data should be transmitted trough payload or as get parameters of the string
341+
* @return self
331342
*/
332343
public function delete($url, $data = array(), $payload = false)
333344
{
@@ -339,6 +350,7 @@ public function delete($url, $data = array(), $payload = false)
339350
$this->setOpt(CURLOPT_URL, $url);
340351
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE');
341352
$this->exec();
353+
return $this;
342354
}
343355

344356
// setters
@@ -356,11 +368,13 @@ public function delete($url, $data = array(), $payload = false)
356368
*
357369
* @param string $username The username for the authentification
358370
* @param string $password The password for the given username for the authentification
371+
* @return self
359372
*/
360373
public function setBasicAuthentication($username, $password)
361374
{
362375
$this->setHttpAuth(self::AUTH_BASIC);
363376
$this->setOpt(CURLOPT_USERPWD, $username.':'.$password);
377+
return $this;
364378
}
365379

366380
/**
@@ -376,11 +390,13 @@ public function setBasicAuthentication($username, $password)
376390
*
377391
* @param string $key The header key
378392
* @param string $value The value for the given header key
393+
* @return self
379394
*/
380395
public function setHeader($key, $value)
381396
{
382397
$this->_headers[$key] = $key.': '.$value;
383398
$this->setOpt(CURLOPT_HTTPHEADER, array_values($this->_headers));
399+
return $this;
384400
}
385401

386402
/**
@@ -395,10 +411,12 @@ public function setHeader($key, $value)
395411
* ```
396412
*
397413
* @param string $useragent The name of the user agent to set for the current request
414+
* @return self
398415
*/
399416
public function setUserAgent($useragent)
400417
{
401418
$this->setOpt(CURLOPT_USERAGENT, $useragent);
419+
return $this;
402420
}
403421

404422
/**
@@ -407,6 +425,7 @@ public function setUserAgent($useragent)
407425
public function setReferrer($referrer)
408426
{
409427
$this->setReferer($referrer);
428+
return $this;
410429
}
411430

412431
/**
@@ -415,22 +434,26 @@ public function setReferrer($referrer)
415434
* The $referer informations can help identify the requested client where the requested was made.
416435
*
417436
* @param string $referer An url to pass and will be set as referer header
437+
* @return self
418438
*/
419439
public function setReferer($referer)
420440
{
421441
$this->setOpt(CURLOPT_REFERER, $referer);
442+
return $this;
422443
}
423444

424445
/**
425446
* Set contents of HTTP Cookie header.
426447
*
427448
* @param string $key The name of the cookie
428449
* @param string $value The value for the provided cookie name
450+
* @return self
429451
*/
430452
public function setCookie($key, $value)
431453
{
432454
$this->_cookies[$key] = $value;
433455
$this->setOpt(CURLOPT_COOKIE, http_build_query($this->_cookies, '', '; '));
456+
return $this;
434457
}
435458

436459
/**
@@ -454,16 +477,19 @@ public function setOpt($option, $value)
454477
* @todo As to keep naming convention it should be renamed to `setVerbose()`
455478
*
456479
* @param string $on
480+
* @return self
457481
*/
458482
public function verbose($on = true)
459483
{
460484
$this->setOpt(CURLOPT_VERBOSE, $on);
485+
return $this;
461486
}
462487

463488
/**
464489
* Reset all curl options.
465490
*
466491
* In order to make multiple requests with the same curl object all settings requires to be reset.
492+
* @return self
467493
*/
468494
public function reset()
469495
{
@@ -483,16 +509,19 @@ public function reset()
483509
$this->response_headers = null;
484510
$this->response = null;
485511
$this->init();
512+
return $this;
486513
}
487514

488515
/**
489516
* Closing the current open curl resource.
517+
* @return self
490518
*/
491519
public function close()
492520
{
493521
if (is_resource($this->curl)) {
494522
curl_close($this->curl);
495523
}
524+
return $this;
496525
}
497526

498527
/**
@@ -505,61 +534,55 @@ public function __destruct()
505534

506535
/**
507536
* Was an 'info' header returned.
537+
* @return bool
508538
*/
509539
public function isInfo()
510540
{
511-
if ($this->http_status_code >= 100 && $this->http_status_code < 200) {
512-
return true;
513-
}
541+
return $this->http_status_code >= 100 && $this->http_status_code < 200;
514542
}
515543

516544
/**
517545
* Was an 'OK' response returned.
546+
* @return bool
518547
*/
519548
public function isSuccess()
520549
{
521-
if ($this->http_status_code >= 200 && $this->http_status_code < 300) {
522-
return true;
523-
}
550+
return $this->http_status_code >= 200 && $this->http_status_code < 300;
524551
}
525552

526553
/**
527554
* Was a 'redirect' returned.
555+
* @return bool
528556
*/
529557
public function isRedirect()
530558
{
531-
if ($this->http_status_code >= 300 && $this->http_status_code < 400) {
532-
return true;
533-
}
559+
return $this->http_status_code >= 300 && $this->http_status_code < 400;
534560
}
535561

536562
/**
537563
* Was an 'error' returned (client error or server error).
564+
* @return bool
538565
*/
539566
public function isError()
540567
{
541-
if ($this->http_status_code >= 400 && $this->http_status_code < 600) {
542-
return true;
543-
}
568+
return $this->http_status_code >= 400 && $this->http_status_code < 600;
544569
}
545570

546571
/**
547572
* Was a 'client error' returned.
573+
* @return bool
548574
*/
549575
public function isClientError()
550576
{
551-
if ($this->http_status_code >= 400 && $this->http_status_code < 500) {
552-
return true;
553-
}
577+
return $this->http_status_code >= 400 && $this->http_status_code < 500;
554578
}
555579

556580
/**
557581
* Was a 'server error' returned.
582+
* @return bool
558583
*/
559584
public function isServerError()
560585
{
561-
if ($this->http_status_code >= 500 && $this->http_status_code < 600) {
562-
return true;
563-
}
586+
return $this->http_status_code >= 500 && $this->http_status_code < 600;
564587
}
565588
}

0 commit comments

Comments
 (0)