Skip to content

Commit 712f355

Browse files
authored
Merge pull request php-curl-class#678 from zachborboa/master
Use strict types
2 parents 6ceabf7 + c51b175 commit 712f355

17 files changed

+93
-80
lines changed

src/Curl/ArrayUtil.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Curl;
44

src/Curl/CaseInsensitiveArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Curl;
44

src/Curl/Curl.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Curl;
44

@@ -1845,7 +1845,7 @@ private function buildCookies()
18451845
*/
18461846
private function downloadComplete($fh)
18471847
{
1848-
if ($this->error && is_file($this->downloadFileName)) {
1848+
if ($this->error && is_file((string) $this->downloadFileName)) {
18491849
@unlink($this->downloadFileName);
18501850
} elseif (!$this->error && $this->downloadCompleteCallback) {
18511851
rewind($fh);
@@ -1884,7 +1884,7 @@ private function downloadComplete($fh)
18841884
*/
18851885
private function parseHeaders($raw_headers)
18861886
{
1887-
$raw_headers = preg_split('/\r\n/', $raw_headers, null, PREG_SPLIT_NO_EMPTY);
1887+
$raw_headers = preg_split('/\r\n/', (string) $raw_headers, -1, PREG_SPLIT_NO_EMPTY);
18881888
$http_headers = new CaseInsensitiveArray();
18891889

18901890
$raw_headers_count = count($raw_headers);

src/Curl/Decoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Curl;
44

src/Curl/Encoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Curl;
44

src/Curl/MultiCurl.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Curl;
44

@@ -117,7 +117,7 @@ public function addDownload($url, $mixed_filename)
117117
// path. The download request will include header "Range: bytes=$filesize-" which is syntactically valid,
118118
// but unsatisfiable.
119119
$download_filename = $filename . '.pccdownload';
120-
$this->downloadFileName = $download_filename;
120+
$curl->downloadFileName = $download_filename;
121121

122122
// Attempt to resume download only when a temporary download file exists and is not empty.
123123
if (is_file($download_filename) && $filesize = filesize($download_filename)) {
@@ -1270,7 +1270,7 @@ private function waitUntilRequestQuotaAvailable()
12701270
$sleep_seconds = $sleep_until - microtime(true);
12711271

12721272
// Avoid using time_sleep_until() as it appears to be less precise and not sleep long enough.
1273-
usleep($sleep_seconds * 1000000);
1273+
usleep((int) $sleep_seconds * 1000000);
12741274

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

src/Curl/StringUtil.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Curl;
44

src/Curl/Url.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Curl;
44

@@ -194,7 +194,7 @@ private function parseUrl($url)
194194
// $7 = <undefined> (query)
195195
// $8 = #Related (ignore)
196196
// $9 = Related (fragment)
197-
preg_match('/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/', $url, $output_array);
197+
preg_match('/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/', (string) $url, $output_array);
198198

199199
$parts = [];
200200
if (isset($output_array['1']) && $output_array['1'] !== '') {

tests/PHPCurlClass/ArrayUtilTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace CurlTest;
44

tests/PHPCurlClass/ContentRangeServer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace ContentRangeServer;
44

tests/PHPCurlClass/Helper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Helper;
44

@@ -103,7 +103,7 @@ function get_tmp_file_path()
103103
// Return temporary file path without creating file.
104104
$tmp_file_path =
105105
rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) .
106-
DIRECTORY_SEPARATOR . 'php-curl-class.' . uniqid(rand(), true);
106+
DIRECTORY_SEPARATOR . 'php-curl-class.' . uniqid((string) rand(), true);
107107
return $tmp_file_path;
108108
}
109109

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace CurlTest;
44

@@ -643,7 +643,7 @@ public function testOptionsRequestMethod()
643643
$this->assertEquals('OPTIONS', $test->curl->responseHeaders['X-REQUEST-METHOD']);
644644
}
645645

646-
public function testDownload()
646+
public function testDownloadToFile()
647647
{
648648
// Create and upload a file.
649649
$upload_file_path = \Helper\get_png();
@@ -661,6 +661,7 @@ public function testDownload()
661661
$this->assertEquals(filesize($upload_file_path), filesize($downloaded_file_path));
662662
$this->assertEquals(md5_file($upload_file_path), md5_file($downloaded_file_path));
663663
$this->assertEquals(md5_file($upload_file_path), $download_test->curl->responseHeaders['ETag']);
664+
$this->assertEquals($download_test->curl->downloadFileName, $downloaded_file_path . '.pccdownload');
664665

665666
// Ensure successive requests set the appropriate values.
666667
$this->assertEquals('GET', $download_test->server('request_method', 'GET'));
@@ -683,22 +684,22 @@ public function testDownloadCallback()
683684
$uploaded_file_path = \Helper\upload_file_to_server($upload_file_path);
684685

685686
// Download the file.
686-
$callback_called = false;
687+
$download_callback_called = false;
687688
$curl = new Curl();
688689
$curl->setHeader('X-DEBUG-TEST', 'download_response');
689690
$curl->download(Test::TEST_URL . '?' . http_build_query([
690691
'file_path' => $uploaded_file_path,
691-
]), function ($instance, $fh) use (&$callback_called) {
692-
\PHPUnit\Framework\Assert::assertFalse($callback_called);
692+
]), function ($instance, $fh) use (&$download_callback_called) {
693+
\PHPUnit\Framework\Assert::assertFalse($download_callback_called);
693694
\PHPUnit\Framework\Assert::assertInstanceOf('Curl\Curl', $instance);
694695
\PHPUnit\Framework\Assert::assertTrue(is_resource($fh));
695696
\PHPUnit\Framework\Assert::assertEquals('stream', get_resource_type($fh));
696697
\PHPUnit\Framework\Assert::assertGreaterThan(0, strlen(stream_get_contents($fh)));
697698
\PHPUnit\Framework\Assert::assertEquals(0, strlen(stream_get_contents($fh)));
698699
\PHPUnit\Framework\Assert::assertTrue(fclose($fh));
699-
$callback_called = true;
700+
$download_callback_called = true;
700701
});
701-
$this->assertTrue($callback_called);
702+
$this->assertTrue($download_callback_called);
702703

703704
// Remove server file.
704705
\Helper\remove_file_from_server($uploaded_file_path);
@@ -824,10 +825,26 @@ public function testDownloadErrorDeleteTemporaryFile()
824825
$test->curl->setHeader('X-DEBUG-TEST', '404');
825826
$test->curl->download(Test::TEST_URL, $destination);
826827

827-
$this->assertFalse(file_exists($test->curl->getDownloadFileName()));
828+
$this->assertFalse(file_exists($test->curl->downloadFileName));
828829
$this->assertFalse(file_exists($destination));
829830
}
830831

832+
public function testDownloadCallbackError()
833+
{
834+
$download_before_send_called = false;
835+
$download_callback_called = false;
836+
$curl = new Curl();
837+
$curl->beforeSend(function ($instance) use (&$download_before_send_called) {
838+
\PHPUnit\Framework\Assert::assertFalse($download_before_send_called);
839+
$download_before_send_called = true;
840+
});
841+
$curl->download(Test::ERROR_URL, function ($instance, $fh) use (&$download_callback_called) {
842+
$download_callback_called = true;
843+
});
844+
$this->assertTrue($download_before_send_called);
845+
$this->assertFalse($download_callback_called);
846+
}
847+
831848
public function testMaxFilesize()
832849
{
833850
$tests = [

tests/PHPCurlClass/PHPMultiCurlClassTest.php

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace CurlTest;
44

@@ -2561,7 +2561,7 @@ public function testXMLDecoder()
25612561
$multi_curl->start();
25622562
}
25632563

2564-
public function testDownload()
2564+
public function testDownloadToFile()
25652565
{
25662566
// Create and upload a file.
25672567
$upload_file_path = \Helper\get_png();
@@ -2574,9 +2574,12 @@ public function testDownload()
25742574
$multi_curl->addDownload(Test::TEST_URL . '?' . http_build_query([
25752575
'file_path' => $uploaded_file_path,
25762576
]), $downloaded_file_path);
2577-
$multi_curl->complete(function ($instance) use ($upload_file_path) {
2578-
\PHPUnit\Framework\Assert::assertFalse($instance->error);
2577+
$multi_curl->complete(function ($instance) use ($upload_file_path, $downloaded_file_path) {
25792578
\PHPUnit\Framework\Assert::assertEquals(md5_file($upload_file_path), $instance->responseHeaders['ETag']);
2579+
\PHPUnit\Framework\Assert::assertEquals(
2580+
$instance->downloadFileName,
2581+
$downloaded_file_path . '.pccdownload'
2582+
);
25802583
});
25812584
$multi_curl->start();
25822585
$this->assertNotEquals($uploaded_file_path, $downloaded_file_path);
@@ -2593,6 +2596,38 @@ public function testDownload()
25932596
$this->assertFalse(file_exists($downloaded_file_path));
25942597
}
25952598

2599+
public function testDownloadCallback()
2600+
{
2601+
// Create and upload a file.
2602+
$upload_file_path = \Helper\get_png();
2603+
$uploaded_file_path = \Helper\upload_file_to_server($upload_file_path);
2604+
2605+
// Download the file.
2606+
$download_callback_called = false;
2607+
$multi_curl = new MultiCurl();
2608+
$multi_curl->setHeader('X-DEBUG-TEST', 'download_response');
2609+
$multi_curl->addDownload(Test::TEST_URL . '?' . http_build_query([
2610+
'file_path' => $uploaded_file_path,
2611+
]), function ($instance, $fh) use (&$download_callback_called) {
2612+
\PHPUnit\Framework\Assert::assertFalse($download_callback_called);
2613+
\PHPUnit\Framework\Assert::assertInstanceOf('Curl\Curl', $instance);
2614+
\PHPUnit\Framework\Assert::assertTrue(is_resource($fh));
2615+
\PHPUnit\Framework\Assert::assertEquals('stream', get_resource_type($fh));
2616+
\PHPUnit\Framework\Assert::assertGreaterThan(0, strlen(stream_get_contents($fh)));
2617+
\PHPUnit\Framework\Assert::assertEquals(0, strlen(stream_get_contents($fh)));
2618+
\PHPUnit\Framework\Assert::assertTrue(fclose($fh));
2619+
$download_callback_called = true;
2620+
});
2621+
$multi_curl->start();
2622+
$this->assertTrue($download_callback_called);
2623+
2624+
// Remove server file.
2625+
\Helper\remove_file_from_server($uploaded_file_path);
2626+
2627+
unlink($upload_file_path);
2628+
$this->assertFalse(file_exists($upload_file_path));
2629+
}
2630+
25962631
public function testDownloadRange()
25972632
{
25982633
// Create and upload a file.
@@ -2715,51 +2750,12 @@ public function testDownloadErrorDeleteTemporaryFile()
27152750
$multi_curl->setHeader('X-DEBUG-TEST', '404');
27162751
$multi_curl->addDownload(Test::TEST_URL, $destination);
27172752
$multi_curl->complete(function ($instance) use ($destination) {
2718-
\PHPUnit\Framework\Assert::assertFalse(file_exists($instance->getDownloadFileName()));
2753+
\PHPUnit\Framework\Assert::assertFalse(file_exists($instance->downloadFileName));
27192754
\PHPUnit\Framework\Assert::assertFalse(file_exists($destination));
27202755
});
27212756
$multi_curl->start();
27222757
}
27232758

2724-
public function testDownloadCallback()
2725-
{
2726-
// Upload a file.
2727-
$upload_file_path = \Helper\get_png();
2728-
$upload_test = new Test();
2729-
$upload_test->server('upload_response', 'POST', [
2730-
'image' => '@' . $upload_file_path,
2731-
]);
2732-
$uploaded_file_path = $upload_test->curl->response->file_path;
2733-
2734-
// Download the file.
2735-
$download_callback_called = false;
2736-
$multi_curl = new MultiCurl();
2737-
$multi_curl->setHeader('X-DEBUG-TEST', 'download_response');
2738-
$multi_curl->addDownload(Test::TEST_URL . '?' . http_build_query([
2739-
'file_path' => $uploaded_file_path,
2740-
]), function ($instance, $fh) use (&$download_callback_called) {
2741-
\PHPUnit\Framework\Assert::assertFalse($download_callback_called);
2742-
\PHPUnit\Framework\Assert::assertInstanceOf('Curl\Curl', $instance);
2743-
\PHPUnit\Framework\Assert::assertTrue(is_resource($fh));
2744-
\PHPUnit\Framework\Assert::assertEquals('stream', get_resource_type($fh));
2745-
\PHPUnit\Framework\Assert::assertGreaterThan(0, strlen(stream_get_contents($fh)));
2746-
\PHPUnit\Framework\Assert::assertEquals(0, strlen(stream_get_contents($fh)));
2747-
\PHPUnit\Framework\Assert::assertTrue(fclose($fh));
2748-
$download_callback_called = true;
2749-
});
2750-
$multi_curl->start();
2751-
$this->assertTrue($download_callback_called);
2752-
2753-
// Remove server file.
2754-
$this->assertEquals('true', $upload_test->server('upload_cleanup', 'POST', [
2755-
'file_path' => $uploaded_file_path,
2756-
]));
2757-
2758-
unlink($upload_file_path);
2759-
$this->assertFalse(file_exists($upload_file_path));
2760-
$this->assertFalse(file_exists($uploaded_file_path));
2761-
}
2762-
27632759
public function testDownloadCallbackError()
27642760
{
27652761
$download_before_send_called = false;
@@ -2946,7 +2942,7 @@ public function testAddRequestAfterStart()
29462942
$urls = [];
29472943
$copy_of_urls = [];
29482944
for ($i = 0; $i < 10; $i++) {
2949-
$url = Test::TEST_URL . '?' . md5(mt_rand());
2945+
$url = Test::TEST_URL . '?' . md5((string) mt_rand());
29502946
$urls[] = $url;
29512947
$copy_of_urls[] = $url;
29522948
}
@@ -3778,7 +3774,7 @@ public function testSetRateLimitPerSecond1()
37783774
$this->assertLessThanOrEqual(10.5, $request_stats['4']['relative_start']);
37793775
// Assert R4 ends around 11.
37803776
$this->assertGreaterThanOrEqual(10.8, $request_stats['4']['relative_stop']);
3781-
$this->assertLessThanOrEqual(11.5, $request_stats['4']['relative_stop']);
3777+
$this->assertLessThanOrEqual(11.5 + 1, $request_stats['4']['relative_stop']);
37823778
}
37833779

37843780
public function testSetRateLimitPerSecond2()
@@ -3847,7 +3843,7 @@ public function testSetRateLimitPerSecond2()
38473843
$this->assertLessThanOrEqual(10.5, $request_stats['4']['relative_start']);
38483844
// Assert R4 ends around 11.
38493845
$this->assertGreaterThanOrEqual(10.8, $request_stats['4']['relative_stop']);
3850-
$this->assertLessThanOrEqual(11.5, $request_stats['4']['relative_stop']);
3846+
$this->assertLessThanOrEqual(11.5 + 1, $request_stats['4']['relative_stop']);
38513847
}
38523848

38533849
public function testSetRateLimitPerSecond3()
@@ -4049,7 +4045,7 @@ public function testSetRateLimitPerSecond5()
40494045
$this->assertLessThanOrEqual(10.5, $request_stats['4']['relative_start']);
40504046
// Assert R4 ends around 12.
40514047
$this->assertGreaterThanOrEqual(11.8, $request_stats['4']['relative_stop']);
4052-
$this->assertLessThanOrEqual(12.5, $request_stats['4']['relative_stop']);
4048+
$this->assertLessThanOrEqual(12.5 + 1, $request_stats['4']['relative_stop']);
40534049
}
40544050

40554051
public function testSetRateLimitPerSecond6()
@@ -4117,7 +4113,7 @@ public function testSetRateLimitPerSecond6()
41174113
$this->assertLessThanOrEqual(10.5, $request_stats['4']['relative_start']);
41184114
// Assert R4 ends around 12.
41194115
$this->assertGreaterThanOrEqual(11.8, $request_stats['4']['relative_stop']);
4120-
$this->assertLessThanOrEqual(12.5, $request_stats['4']['relative_stop']);
4116+
$this->assertLessThanOrEqual(12.5 + 1, $request_stats['4']['relative_stop']);
41214117
}
41224118

41234119
public function testSetRateLimitPerSecond7()

tests/PHPCurlClass/RangeHeader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace RangeHeader;
44

tests/PHPCurlClass/UrlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace CurlTest;
44

tests/PHPCurlClass/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php declare(strict_types=1);
22

33
namespace Helper;
44

0 commit comments

Comments
 (0)