Skip to content

Commit 4cf4635

Browse files
authored
Increase psalm strictness (#925)
* Fix PossiblyUndefinedArrayOffset ERROR: PossiblyUndefinedArrayOffset - ../src/Curl/Curl.php:1890:28 - Possibly undefined array key (see https://psalm.dev/167) list($key, $value) = explode(':', $raw_headers[$i], 2); * Fix PossiblyUndefinedArrayOffset ERROR: PossiblyUndefinedArrayOffset - ../src/Curl/Curl.php:1034:28 - Possibly undefined array key (see https://psalm.dev/167) list($key, $value) = explode(':', $header, 2); * Fix PossiblyUndefinedArrayOffset ERROR: PossiblyUndefinedArrayOffset - ../src/Curl/MultiCurl.php:465:28 - Possibly undefined array key (see https://psalm.dev/167) list($key, $value) = explode(':', $header, 2); * Fix PossiblyInvalidOperand ERROR: PossiblyInvalidOperand - ../src/Curl/Curl.php:1407:69 - Cannot concatenate with a int<0, max>|string (see https://psalm.dev/163) echo 'Response content length (calculated): ' . $response_calculated_length . "\n"; * Fix PossiblyInvalidArgument ERROR: PossiblyInvalidArgument - ../src/Curl/BaseCurl.php:408:34 - Argument 2 of define expects array<array-key, mixed>|null|scalar, but possibly different type false|resource provided (see https://psalm.dev/092) define('STDERR', fopen('php://stderr', 'wb')); * Fix ArgumentTypeCoercion ERROR: ArgumentTypeCoercion - ../src/Curl/MultiCurl.php:956:16 - Argument 1 of usleep expects int<0, max>, but parent type int provided (see https://psalm.dev/193) usleep((int) $sleep_seconds * 1000000); * Fix PossiblyFalseArgument ERROR: PossiblyFalseArgument - ../src/Curl/MultiCurl.php:131:50 - Argument 2 of file_put_contents cannot be false, possibly array<array-key, string>|resource|string value expected (see https://psalm.dev/104) file_put_contents($filename, stream_get_contents($fh)); * Fix PossiblyFalseArgument ERROR: PossiblyFalseArgument - ../src/Curl/Curl.php:437:49 - Argument 2 of stream_copy_to_stream cannot be false, possibly resource value expected (see https://psalm.dev/104) stream_copy_to_stream($tmpfile, $fh); ERROR: PossiblyFalseArgument - ../src/Curl/Curl.php:438:24 - Argument 1 of fclose cannot be false, possibly resource value expected (see https://psalm.dev/104) fclose($fh); * Fix PossiblyFalseArgument ERROR: PossiblyFalseArgument - ../src/Curl/Curl.php:463:49 - Argument 2 of stream_copy_to_stream cannot be false, possibly resource value expected (see https://psalm.dev/104) stream_copy_to_stream($file_handle, $main_file_handle); ERROR: PossiblyFalseArgument - ../src/Curl/Curl.php:468:16 - Argument 1 of fclose cannot be false, possibly resource value expected (see https://psalm.dev/104) fclose($main_file_handle); * Fix PossiblyFalseArgument ERROR: PossiblyFalseArgument - ../src/Curl/Curl.php:1891:36 - Argument 1 of count cannot be false, possibly Countable|array<array-key, mixed> value expected (see https://psalm.dev/104) $raw_headers_count = count($raw_headers); ERROR: PossiblyInvalidArrayAccess - ../src/Curl/Curl.php:1893:24 - Cannot access array value on non-array variable $raw_headers of type false (see https://psalm.dev/109) if (strpos($raw_headers[$i], ':') !== false) { * Fix PossiblyInvalidArgument ERROR: PossiblyInvalidArgument - ../src/Curl/Curl.php:1868:30 - Argument 2 of define expects array<array-key, mixed>|null|scalar, but possibly different type false|resource provided (see https://psalm.dev/092) define('STDOUT', fopen('php://stdout', 'w')); * Update psalm baseline
1 parent 4d126ef commit 4cf4635

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

src/Curl/BaseCurl.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,11 @@ public function unsetProxy()
404404
public function verbose($on = true, $output = 'STDERR')
405405
{
406406
if ($output === 'STDERR') {
407-
if (!defined('STDERR')) {
408-
define('STDERR', fopen('php://stderr', 'wb'));
407+
if (defined('STDERR')) {
408+
$output = STDERR;
409+
} else {
410+
$output = fopen('php://stderr', 'wb');
409411
}
410-
$output = STDERR;
411412
}
412413

413414
// Turn off CURLINFO_HEADER_OUT for verbose to work. This has the side

src/Curl/Curl.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,10 @@ public function fastDownload($url, $filename, $connections = 4)
430430

431431
$curl->downloadCompleteCallback = function ($instance, $tmpfile) use ($part_file_name) {
432432
$fh = fopen($part_file_name, 'wb');
433-
stream_copy_to_stream($tmpfile, $fh);
434-
fclose($fh);
433+
if ($fh !== false) {
434+
stream_copy_to_stream($tmpfile, $fh);
435+
fclose($fh);
436+
}
435437
};
436438

437439
$multi_curl->addCurl($curl);
@@ -447,6 +449,9 @@ public function fastDownload($url, $filename, $connections = 4)
447449

448450
// Combine downloaded chunks into a single file.
449451
$main_file_handle = fopen($filename, 'w');
452+
if ($main_file_handle === false) {
453+
return false;
454+
}
450455

451456
foreach ($part_file_names as $part_file_name) {
452457
if (!is_file($part_file_name)) {
@@ -1031,7 +1036,7 @@ public function setHeaders($headers)
10311036
}
10321037
} else {
10331038
foreach ($headers as $header) {
1034-
list($key, $value) = explode(':', $header, 2);
1039+
list($key, $value) = array_pad(explode(':', $header, 2), 2, '');
10351040
$key = trim($key);
10361041
$value = trim($value);
10371042
$this->headers[$key] = $value;
@@ -1404,7 +1409,7 @@ public function diagnose($return = false)
14041409
if (isset($this->responseHeaders['Content-Length'])) {
14051410
echo 'Response content length (from content-length header): ' . $response_header_length . "\n";
14061411
} else {
1407-
echo 'Response content length (calculated): ' . $response_calculated_length . "\n";
1412+
echo 'Response content length (calculated): ' . (string)$response_calculated_length . "\n";
14081413
}
14091414

14101415
if (
@@ -1859,13 +1864,15 @@ private function downloadComplete($fh)
18591864
// Fix "PHP Notice: Use of undefined constant STDOUT" when reading the
18601865
// PHP script from stdin. Using null causes "Warning: curl_setopt():
18611866
// supplied argument is not a valid File-Handle resource".
1862-
if (!defined('STDOUT')) {
1863-
define('STDOUT', fopen('php://stdout', 'w'));
1867+
if (defined('STDOUT')) {
1868+
$output = STDOUT;
1869+
} else {
1870+
$output = fopen('php://stdout', 'w');
18641871
}
18651872

18661873
// Reset CURLOPT_FILE with STDOUT to avoid: "curl_exec(): CURLOPT_FILE
18671874
// resource has gone away, resetting to default".
1868-
$this->setFile(STDOUT);
1875+
$this->setFile($output);
18691876

18701877
// Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent
18711878
// responses as the return value of curl_exec(). Without this,
@@ -1881,13 +1888,16 @@ private function downloadComplete($fh)
18811888
*/
18821889
private function parseHeaders($raw_headers)
18831890
{
1884-
$raw_headers = preg_split('/\r\n/', (string) $raw_headers, -1, PREG_SPLIT_NO_EMPTY);
18851891
$http_headers = new CaseInsensitiveArray();
1892+
$raw_headers = preg_split('/\r\n/', (string) $raw_headers, -1, PREG_SPLIT_NO_EMPTY);
1893+
if ($raw_headers === false) {
1894+
return ['', $http_headers];
1895+
}
18861896

18871897
$raw_headers_count = count($raw_headers);
18881898
for ($i = 1; $i < $raw_headers_count; $i++) {
18891899
if (strpos($raw_headers[$i], ':') !== false) {
1890-
list($key, $value) = explode(':', $raw_headers[$i], 2);
1900+
list($key, $value) = array_pad(explode(':', $raw_headers[$i], 2), 2, '');
18911901
$key = trim($key);
18921902
$value = trim($value);
18931903
// Use isset() as array_key_exists() and ArrayAccess are not compatible.

src/Curl/MultiCurl.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ public function addDownload($url, $mixed_filename)
127127
} else {
128128
$curl->fileHandle = fopen('php://temp', 'wb');
129129
$curl->downloadCompleteCallback = function ($instance, $fh) use ($filename) {
130-
file_put_contents($filename, stream_get_contents($fh));
130+
$contents = stream_get_contents($fh);
131+
if ($contents !== false) {
132+
file_put_contents($filename, $contents);
133+
}
131134
};
132135
}
133136
}
@@ -462,7 +465,7 @@ public function setHeaders($headers)
462465
}
463466
} else {
464467
foreach ($headers as $header) {
465-
list($key, $value) = explode(':', $header, 2);
468+
list($key, $value) = array_pad(explode(':', $header, 2), 2, '');
466469
$key = trim($key);
467470
$value = trim($value);
468471
$this->headers[$key] = $value;
@@ -946,7 +949,13 @@ private function waitUntilRequestQuotaAvailable()
946949
$sleep_seconds = $sleep_until - microtime(true);
947950

948951
// Avoid using time_sleep_until() as it appears to be less precise and not sleep long enough.
949-
usleep((int) $sleep_seconds * 1000000);
952+
// Avoid using usleep(): "Values larger than 1000000 (i.e. sleeping for
953+
// more than a second) may not be supported by the operating system.
954+
// Use sleep() instead."
955+
$sleep_seconds_int = (int)$sleep_seconds;
956+
if ($sleep_seconds_int >= 1) {
957+
sleep($sleep_seconds_int);
958+
}
950959

951960
// Ensure that enough time has passed as usleep() may not have waited long enough.
952961
$this->currentStartTime = microtime(true);

tests/psalm-baseline.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,22 @@
55
<code><![CDATA[var_dump($value)]]></code>
66
<code><![CDATA[var_dump($value)]]></code>
77
</ForbiddenCode>
8+
<PossiblyInvalidArgument>
9+
<code><![CDATA[$this->curl]]></code>
10+
<code><![CDATA[$this->curl]]></code>
11+
</PossiblyInvalidArgument>
12+
<PossiblyInvalidOperand>
13+
<code><![CDATA[$const_value]]></code>
14+
<code><![CDATA[$value]]></code>
15+
</PossiblyInvalidOperand>
16+
</file>
17+
<file src="../src/Curl/MultiCurl.php">
18+
<PossiblyInvalidArgument>
19+
<code><![CDATA[$this->multiCurl]]></code>
20+
<code><![CDATA[$this->multiCurl]]></code>
21+
<code><![CDATA[$this->multiCurl]]></code>
22+
<code><![CDATA[$this->multiCurl]]></code>
23+
<code><![CDATA[$this->multiCurl]]></code>
24+
</PossiblyInvalidArgument>
825
</file>
926
</files>

0 commit comments

Comments
 (0)