Skip to content

Commit 0596b87

Browse files
committed
Use isset over array_key_exists
1 parent 5995c3d commit 0596b87

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/Client.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
2+
declare(strict_types=1);
33
namespace Http\Client\Curl;
44

55
use Http\Client\Exception;
@@ -330,10 +330,10 @@ private function createHeaders(RequestInterface $request, array $options)
330330
continue;
331331
}
332332
if ('content-length' === $header) {
333-
if (array_key_exists(CURLOPT_POSTFIELDS, $options)) {
333+
if (isset($options[CURLOPT_POSTFIELDS])) {
334334
// Small body content length can be calculated here.
335335
$values = [strlen($options[CURLOPT_POSTFIELDS])];
336-
} elseif (!array_key_exists(CURLOPT_READFUNCTION, $options)) {
336+
} elseif (!isset($options[CURLOPT_READFUNCTION])) {
337337
// Else if there is no body, forcing "Content-length" to 0
338338
$values = [0];
339339
}

tests/ClientTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ public function testExpectHeader()
3333
static::assertContains('Expect:', $headers);
3434
}
3535

36+
/**
37+
* "Expect" header should be empty.
38+
*
39+
* @link https://github.com/php-http/curl-client/issues/18
40+
*/
41+
public function testWithNullPostFields()
42+
{
43+
$client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()
44+
->setMethods(['__none__'])->getMock();
45+
46+
$createHeaders = new \ReflectionMethod(Client::class, 'createHeaders');
47+
$createHeaders->setAccessible(true);
48+
49+
$request = new Request();
50+
$request = $request->withHeader('content-length', 0);
51+
52+
$headers = $createHeaders->invoke($client, $request, [CURLOPT_POSTFIELDS => null]);
53+
54+
static::assertContains('Expect:', $headers);
55+
}
56+
3657
public function testRewindStream()
3758
{
3859
$client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()

0 commit comments

Comments
 (0)