Skip to content

Commit ed20126

Browse files
committed
Make curl_init() always return a benevolent union
When we decide to not ignore curl memory allocation & initialization issues entirely, this leaves us with the edge case of a zero byte input or a huge URL which might return false or never. Both seem similarly unlikely, making the return type extension not quite worthwhile.
1 parent a58ae14 commit ed20126

File tree

8 files changed

+20
-128
lines changed

8 files changed

+20
-128
lines changed

conf/config.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,11 +1360,6 @@ services:
13601360
tags:
13611361
- phpstan.broker.dynamicFunctionReturnTypeExtension
13621362

1363-
-
1364-
class: PHPStan\Type\Php\CurlInitReturnTypeExtension
1365-
tags:
1366-
- phpstan.broker.dynamicFunctionReturnTypeExtension
1367-
13681363
-
13691364
class: PHPStan\Type\Php\DateFunctionReturnTypeHelper
13701365

resources/functionMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@
14941494
'curl_exec' => ['bool|string', 'ch'=>'resource'],
14951495
'curl_file_create' => ['CURLFile', 'filename'=>'string', 'mimetype='=>'string', 'postfilename='=>'string'],
14961496
'curl_getinfo' => ['mixed', 'ch'=>'resource', 'option='=>'int'],
1497-
'curl_init' => ['resource|false', 'url='=>'string'],
1497+
'curl_init' => ['__benevolent<resource|false>', 'url='=>'string'],
14981498
'curl_multi_add_handle' => ['int', 'mh'=>'resource', 'ch'=>'resource'],
14991499
'curl_multi_close' => ['void', 'mh'=>'resource'],
15001500
'curl_multi_errno' => ['int', 'mh'=>'resource'],

resources/functionMap_php80delta.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
'ceil' => ['float', 'number'=>'float'],
3232
'com_load_typelib' => ['bool', 'typelib_name'=>'string', 'case_insensitive='=>'true'],
3333
'count_chars' => ['array<int,int>|string', 'input'=>'string', 'mode='=>'int'],
34+
'curl_init' => ['__benevolent<CurlHandle|false>', 'url='=>'string'],
3435
'date_add' => ['DateTime', 'object'=>'DateTime', 'interval'=>'DateInterval'],
3536
'date_date_set' => ['DateTime', 'object'=>'DateTime', 'year'=>'int', 'month'=>'int', 'day'=>'int'],
3637
'date_diff' => ['DateInterval', 'obj1'=>'DateTimeInterface', 'obj2'=>'DateTimeInterface', 'absolute='=>'bool'],
@@ -169,6 +170,7 @@
169170
'convert_cyr_string' => ['string', 'str'=>'string', 'from'=>'string', 'to'=>'string'],
170171
'com_load_typelib' => ['bool', 'typelib_name'=>'string', 'case_insensitive='=>'bool'],
171172
'count_chars' => ['array<int,int>|false|string', 'input'=>'string', 'mode='=>'int'],
173+
'curl_init' => ['__benevolent<resource|false>', 'url='=>'string'],
172174
'date_add' => ['DateTime|false', 'object'=>'DateTime', 'interval'=>'DateInterval'],
173175
'date_date_set' => ['DateTime|false', 'object'=>'DateTime', 'year'=>'int', 'month'=>'int', 'day'=>'int'],
174176
'date_diff' => ['DateInterval|false', 'obj1'=>'DateTimeInterface', 'obj2'=>'DateTimeInterface', 'absolute='=>'bool'],

src/Type/Php/CurlInitReturnTypeExtension.php

Lines changed: 0 additions & 106 deletions
This file was deleted.

tests/PHPStan/Reflection/SignatureMap/Php8SignatureMapProviderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\Reflection\PassedByReference;
1313
use PHPStan\Testing\PHPStanTestCase;
1414
use PHPStan\Type\ArrayType;
15+
use PHPStan\Type\BenevolentUnionType;
1516
use PHPStan\Type\BooleanType;
1617
use PHPStan\Type\CallableType;
1718
use PHPStan\Type\ClassStringType;
@@ -59,7 +60,7 @@ public function dataFunctions(): array
5960
'variadic' => false,
6061
],
6162
],
62-
new UnionType([
63+
new BenevolentUnionType([
6364
new ObjectType('CurlHandle'),
6465
new ConstantBooleanType(false),
6566
]),

tests/PHPStan/Type/Php/CurlInitReturnTypeExtensionTest.php renamed to tests/PHPStan/Type/Php/CurlInitReturnTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use PHPStan\Testing\TypeInferenceTestCase;
66
use const PHP_VERSION_ID;
77

8-
class CurlInitReturnTypeExtensionTest extends TypeInferenceTestCase
8+
class CurlInitReturnTypeTest extends TypeInferenceTestCase
99
{
1010

1111
public static function dataFileAsserts(): iterable

tests/PHPStan/Type/Php/data/curl-init-php-7.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
function (string $unknownString) {
88
assertType('(resource|false)', curl_init());
99
assertType('(resource|false)', curl_init('https://phpstan.org'));
10-
assertType('resource|false', curl_init($unknownString));
10+
assertType('(resource|false)', curl_init($unknownString));
1111
assertType('(resource|false)', curl_init(null));
1212
assertType('(resource|false)', curl_init(''));
13-
assertType('resource|false', curl_init(':'));
14-
assertType('resource|false', curl_init('file://host/text.txt'));
15-
assertType('resource|false', curl_init('FIle://host/text.txt'));
13+
assertType('(resource|false)', curl_init(':'));
14+
assertType('(resource|false)', curl_init('file://host/text.txt'));
15+
assertType('(resource|false)', curl_init('FIle://host/text.txt'));
1616
assertType('(resource|false)', curl_init('host/text.txt'));
17-
assertType('false', curl_init("\0"));
18-
assertType('false', curl_init("https://phpstan.org\0"));
17+
assertType('(resource|false)', curl_init("\0"));
18+
assertType('(resource|false)', curl_init("https://phpstan.org\0"));
1919

2020
$url = 'https://phpstan.org';
2121
if (rand(0,1)) $url = null;
@@ -35,7 +35,7 @@ function (string $unknownString) {
3535

3636
$url = 'https://phpstan.org';
3737
if (rand(0,1)) $url = $unknownString;
38-
assertType('resource|false', curl_init($url));
38+
assertType('(resource|false)', curl_init($url));
3939

4040
$url = 'https://phpstan.org';
4141
if (rand(0,1)) $url = ':';
@@ -61,5 +61,5 @@ function (string $unknownString) {
6161
$url = 'https://phpstan.org';
6262
if (rand(0,1)) $url = null;
6363
if (rand(0,1)) $url = $unknownString;
64-
assertType('resource|false', curl_init($url));
64+
assertType('(resource|false)', curl_init($url));
6565
};

tests/PHPStan/Type/Php/data/curl-init-php-8.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
function (string $unknownString) {
88
assertType('(CurlHandle|false)', curl_init());
99
assertType('(CurlHandle|false)', curl_init('https://phpstan.org'));
10-
assertType('CurlHandle|false', curl_init($unknownString));
10+
assertType('(CurlHandle|false)', curl_init($unknownString));
1111
assertType('(CurlHandle|false)', curl_init(null));
1212
assertType('(CurlHandle|false)', curl_init(''));
1313
assertType('(CurlHandle|false)', curl_init(':'));
1414
assertType('(CurlHandle|false)', curl_init('file://host/text.txt'));
1515
assertType('(CurlHandle|false)', curl_init('FIle://host/text.txt'));
1616
assertType('(CurlHandle|false)', curl_init('host/text.txt'));
17-
assertType('*NEVER*', curl_init("\0"));
18-
assertType('*NEVER*', curl_init("https://phpstan.org\0"));
17+
assertType('(CurlHandle|false)', curl_init("\0"));
18+
assertType('(CurlHandle|false)', curl_init("https://phpstan.org\0"));
1919

2020
$url = 'https://phpstan.org';
2121
if (rand(0,1)) $url = null;
@@ -35,11 +35,11 @@ function (string $unknownString) {
3535

3636
$url = 'https://phpstan.org';
3737
if (rand(0,1)) $url = $unknownString;
38-
assertType('CurlHandle|false', curl_init($url));
38+
assertType('(CurlHandle|false)', curl_init($url));
3939

4040
$url = 'https://phpstan.org';
4141
if (rand(0,1)) $url .= $unknownString;
42-
assertType('CurlHandle|false', curl_init($url));
42+
assertType('(CurlHandle|false)', curl_init($url));
4343

4444
$url = 'https://phpstan.org';
4545
if (rand(0,1)) $url = ':';
@@ -65,5 +65,5 @@ function (string $unknownString) {
6565
$url = 'https://phpstan.org';
6666
if (rand(0,1)) $url = null;
6767
if (rand(0,1)) $url = $unknownString;
68-
assertType('CurlHandle|false', curl_init($url));
68+
assertType('(CurlHandle|false)', curl_init($url));
6969
};

0 commit comments

Comments
 (0)