Skip to content

Commit 58ebfd6

Browse files
committed
[generator] ignore benevolence properly
functions which phpstan marked as `__benevolent<foo|bar>` were being type-hinted as `foo|bar` verbatim, when what we actually want to do is look inside the `benevolent` marker and check each type individually (which allows us to do things like stripping "false" from the return type when returning false becomes an exception and we never actually return false)
1 parent 863df4a commit 58ebfd6

21 files changed

+50
-46
lines changed

generated/8.1/datetime.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ function date_parse(string $datetime): ?array
5151
* @param int $timestamp Unix timestamp.
5252
* @param float $latitude Latitude in degrees.
5353
* @param float $longitude Longitude in degrees.
54-
* @return array{sunrise: int|bool,sunset: int|bool,transit: int|bool,civil_twilight_begin: int|bool,civil_twilight_end: int|bool,nautical_twilight_begin: int|bool,nautical_twilight_end: int|bool,astronomical_twilight_begin: int|bool,astronomical_twilight_end: int|bool}|false Returns array on success.
54+
* @return array{sunrise: int|bool,sunset: int|bool,transit: int|bool,civil_twilight_begin: int|bool,civil_twilight_end: int|bool,nautical_twilight_begin: int|bool,nautical_twilight_end: int|bool,astronomical_twilight_begin: int|bool,astronomical_twilight_end: int|bool} Returns array on success.
5555
* The structure of the array is detailed in the following list:
5656
*
5757
*
@@ -603,13 +603,13 @@ function idate(string $format, ?int $timestamp = null): int
603603
* 1970-2000. On systems where time_t is a 32bit signed integer, as
604604
* most common today, the valid range for year
605605
* is somewhere between 1901 and 2038.
606-
* @return int|false mktime returns the Unix timestamp of the arguments
606+
* @return int mktime returns the Unix timestamp of the arguments
607607
* given.
608608
* If the arguments are invalid, the function returns FALSE.
609609
* @throws DatetimeException
610610
*
611611
*/
612-
function mktime(int $hour, ?int $minute = null, ?int $second = null, ?int $month = null, ?int $day = null, ?int $year = null): int|false
612+
function mktime(int $hour, ?int $minute = null, ?int $second = null, ?int $month = null, ?int $day = null, ?int $year = null): int
613613
{
614614
error_clear_last();
615615
if ($year !== null) {

generated/8.1/filesystem.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1520,7 +1520,7 @@ function tempnam(string $directory, string $prefix): string
15201520
* the file handle returned by tmpfile), or when the
15211521
* script ends.
15221522
*
1523-
* @return resource|false Returns a file handle, similar to the one returned by
1523+
* @return resource Returns a file handle, similar to the one returned by
15241524
* fopen, for the new file.
15251525
* @throws FilesystemException
15261526
*

generated/8.1/image.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ function imagecopyresized(\GdImage $dst_image, \GdImage $src_image, int $dst_x,
727727
*
728728
* @param int $width The image width.
729729
* @param int $height The image height.
730-
* @return resource|false Returns an image object on success.
730+
* @return resource Returns an image object on success.
731731
* @throws ImageException
732732
*
733733
*/
@@ -1033,7 +1033,7 @@ function imagecreatefromxpm(string $filename)
10331033
*
10341034
* @param int $width Image width.
10351035
* @param int $height Image height.
1036-
* @return resource|false Returns an image object on success.
1036+
* @return resource Returns an image object on success.
10371037
* @throws ImageException
10381038
*
10391039
*/

generated/8.1/info.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ function dl(string $extension_filename): void
207207
/**
208208
*
209209
*
210-
* @return string|false Returns the path, as a string.
210+
* @return string Returns the path, as a string.
211211
* @throws InfoException
212212
*
213213
*/
214-
function get_include_path(): string|false
214+
function get_include_path(): string
215215
{
216216
error_clear_last();
217217
$safeResult = \get_include_path();
@@ -323,7 +323,7 @@ function getmyuid(): int
323323
* @param string $short_options
324324
* @param array $long_options
325325
* @param int|null $rest_index
326-
* @return array|array|array|false This function will return an array of option / argument pairs.
326+
* @return array|array|array This function will return an array of option / argument pairs.
327327
* @throws InfoException
328328
*
329329
*/

generated/8.2/datetime.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function date_parse(string $datetime): ?array
135135
* @param int $timestamp Unix timestamp.
136136
* @param float $latitude Latitude in degrees.
137137
* @param float $longitude Longitude in degrees.
138-
* @return array{sunrise: int|bool,sunset: int|bool,transit: int|bool,civil_twilight_begin: int|bool,civil_twilight_end: int|bool,nautical_twilight_begin: int|bool,nautical_twilight_end: int|bool,astronomical_twilight_begin: int|bool,astronomical_twilight_end: int|bool}|false Returns array on success.
138+
* @return array{sunrise: int|bool,sunset: int|bool,transit: int|bool,civil_twilight_begin: int|bool,civil_twilight_end: int|bool,nautical_twilight_begin: int|bool,nautical_twilight_end: int|bool,astronomical_twilight_begin: int|bool,astronomical_twilight_end: int|bool} Returns array on success.
139139
* The structure of the array is detailed in the following list:
140140
*
141141
*

generated/8.2/filesystem.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ function tempnam(string $directory, string $prefix): string
15261526
* the file handle returned by tmpfile), or when the
15271527
* script ends.
15281528
*
1529-
* @return resource|false Returns a file handle, similar to the one returned by
1529+
* @return resource Returns a file handle, similar to the one returned by
15301530
* fopen, for the new file.
15311531
* @throws FilesystemException
15321532
*

generated/8.2/image.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ function imagecopyresized(\GdImage $dst_image, \GdImage $src_image, int $dst_x,
705705
*
706706
* @param int $width The image width.
707707
* @param int $height The image height.
708-
* @return resource|false Returns an image object on success.
708+
* @return resource Returns an image object on success.
709709
* @throws ImageException
710710
*
711711
*/
@@ -1012,7 +1012,7 @@ function imagecreatefromxpm(string $filename)
10121012
*
10131013
* @param int $width Image width.
10141014
* @param int $height Image height.
1015-
* @return resource|false Returns an image object on success.
1015+
* @return resource Returns an image object on success.
10161016
* @throws ImageException
10171017
*
10181018
*/

generated/8.2/info.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ function dl(string $extension_filename): void
207207
/**
208208
*
209209
*
210-
* @return string|false Returns the path, as a string.
210+
* @return string Returns the path, as a string.
211211
* @throws InfoException
212212
*
213213
*/
214-
function get_include_path(): string|false
214+
function get_include_path(): string
215215
{
216216
error_clear_last();
217217
$safeResult = \get_include_path();
@@ -323,7 +323,7 @@ function getmyuid(): int
323323
* @param string $short_options
324324
* @param array $long_options
325325
* @param int|null $rest_index
326-
* @return array|array|array|false This function will return an array of option / argument pairs.
326+
* @return array|array|array This function will return an array of option / argument pairs.
327327
* @throws InfoException
328328
*
329329
*/

generated/8.3/datetime.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function date_parse(string $datetime): ?array
135135
* @param int $timestamp Unix timestamp.
136136
* @param float $latitude Latitude in degrees.
137137
* @param float $longitude Longitude in degrees.
138-
* @return array{sunrise: int|bool,sunset: int|bool,transit: int|bool,civil_twilight_begin: int|bool,civil_twilight_end: int|bool,nautical_twilight_begin: int|bool,nautical_twilight_end: int|bool,astronomical_twilight_begin: int|bool,astronomical_twilight_end: int|bool}|false Returns array on success.
138+
* @return array{sunrise: int|bool,sunset: int|bool,transit: int|bool,civil_twilight_begin: int|bool,civil_twilight_end: int|bool,nautical_twilight_begin: int|bool,nautical_twilight_end: int|bool,astronomical_twilight_begin: int|bool,astronomical_twilight_end: int|bool} Returns array on success.
139139
* The structure of the array is detailed in the following list:
140140
*
141141
*

generated/8.3/filesystem.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ function tempnam(string $directory, string $prefix): string
15261526
* the file handle returned by tmpfile), or when the
15271527
* script ends.
15281528
*
1529-
* @return resource|false Returns a file handle, similar to the one returned by
1529+
* @return resource Returns a file handle, similar to the one returned by
15301530
* fopen, for the new file.
15311531
* @throws FilesystemException
15321532
*

generated/8.3/image.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ function imagecopyresized(\GdImage $dst_image, \GdImage $src_image, int $dst_x,
705705
*
706706
* @param int $width The image width.
707707
* @param int $height The image height.
708-
* @return resource|false Returns an image object on success.
708+
* @return resource Returns an image object on success.
709709
* @throws ImageException
710710
*
711711
*/
@@ -1012,7 +1012,7 @@ function imagecreatefromxpm(string $filename)
10121012
*
10131013
* @param int $width Image width.
10141014
* @param int $height Image height.
1015-
* @return resource|false Returns an image object on success.
1015+
* @return resource Returns an image object on success.
10161016
* @throws ImageException
10171017
*
10181018
*/

generated/8.3/info.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@ function dl(string $extension_filename): void
207207
/**
208208
*
209209
*
210-
* @return string|false Returns the path, as a string.
210+
* @return string Returns the path, as a string.
211211
* @throws InfoException
212212
*
213213
*/
214-
function get_include_path(): string|false
214+
function get_include_path(): string
215215
{
216216
error_clear_last();
217217
$safeResult = \get_include_path();
@@ -323,7 +323,7 @@ function getmyuid(): int
323323
* @param string $short_options
324324
* @param array $long_options
325325
* @param int|null $rest_index
326-
* @return array|array|array|false This function will return an array of option / argument pairs.
326+
* @return array|array|array This function will return an array of option / argument pairs.
327327
* @throws InfoException
328328
*
329329
*/

generated/8.4/filesystem.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ function tempnam(string $directory, string $prefix): string
14881488
* the file handle returned by tmpfile), or when the
14891489
* script ends.
14901490
*
1491-
* @return resource|false Returns a file handle, similar to the one returned by
1491+
* @return resource Returns a file handle, similar to the one returned by
14921492
* fopen, for the new file.
14931493
* @throws FilesystemException
14941494
*

generated/8.4/image.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ function imagecopyresized(\GdImage $dst_image, \GdImage $src_image, int $dst_x,
705705
*
706706
* @param int $width The image width.
707707
* @param int $height The image height.
708-
* @return resource|false Returns an image object on success.
708+
* @return resource Returns an image object on success.
709709
* @throws ImageException
710710
*
711711
*/
@@ -1012,7 +1012,7 @@ function imagecreatefromxpm(string $filename)
10121012
*
10131013
* @param int $width Image width.
10141014
* @param int $height Image height.
1015-
* @return resource|false Returns an image object on success.
1015+
* @return resource Returns an image object on success.
10161016
* @throws ImageException
10171017
*
10181018
*/

generated/8.4/info.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ function dl(string $extension_filename): void
9090
/**
9191
*
9292
*
93-
* @return string|false Returns the path, as a string.
93+
* @return string Returns the path, as a string.
9494
* @throws InfoException
9595
*
9696
*/
97-
function get_include_path(): string|false
97+
function get_include_path(): string
9898
{
9999
error_clear_last();
100100
$safeResult = \get_include_path();
@@ -206,7 +206,7 @@ function getmyuid(): int
206206
* @param string $short_options
207207
* @param array $long_options
208208
* @param int|null $rest_index
209-
* @return array|array|array|false This function will return an array of option / argument pairs.
209+
* @return array|array|array This function will return an array of option / argument pairs.
210210
* @throws InfoException
211211
*
212212
*/

generated/8.5/filesystem.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ function tempnam(string $directory, string $prefix): string
14881488
* the file handle returned by tmpfile), or when the
14891489
* script ends.
14901490
*
1491-
* @return resource|false Returns a file handle, similar to the one returned by
1491+
* @return resource Returns a file handle, similar to the one returned by
14921492
* fopen, for the new file.
14931493
* @throws FilesystemException
14941494
*

generated/8.5/image.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ function imagecopyresized(\GdImage $dst_image, \GdImage $src_image, int $dst_x,
705705
*
706706
* @param int $width The image width.
707707
* @param int $height The image height.
708-
* @return resource|false Returns an image object on success.
708+
* @return resource Returns an image object on success.
709709
* @throws ImageException
710710
*
711711
*/
@@ -1012,7 +1012,7 @@ function imagecreatefromxpm(string $filename)
10121012
*
10131013
* @param int $width Image width.
10141014
* @param int $height Image height.
1015-
* @return resource|false Returns an image object on success.
1015+
* @return resource Returns an image object on success.
10161016
* @throws ImageException
10171017
*
10181018
*/

generated/8.5/info.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ function dl(string $extension_filename): void
9090
/**
9191
*
9292
*
93-
* @return string|false Returns the path, as a string.
93+
* @return string Returns the path, as a string.
9494
* @throws InfoException
9595
*
9696
*/
97-
function get_include_path(): string|false
97+
function get_include_path(): string
9898
{
9999
error_clear_last();
100100
$safeResult = \get_include_path();
@@ -206,7 +206,7 @@ function getmyuid(): int
206206
* @param string $short_options
207207
* @param array $long_options
208208
* @param int|null $rest_index
209-
* @return array|array|array|false This function will return an array of option / argument pairs.
209+
* @return array|array|array This function will return an array of option / argument pairs.
210210
* @throws InfoException
211211
*
212212
*/

generator/src/PhpStanFunctions/PhpStanParameter.php

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Safe\PhpStanFunctions;
66

7-
use Safe\XmlDocParser\Type;
8-
97
class PhpStanParameter
108
{
119
private readonly string $name;

generator/src/PhpStanFunctions/PhpStanType.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class PhpStanType
3030

3131
public function __construct(string $data, bool $writeOnly = false)
3232
{
33+
if (\preg_match('/__benevolent\<(.*)\>/', $data, $regs)) {
34+
$data = $regs[1];
35+
}
36+
3337
//weird case: null|false => null
3438
if ($data === 'null|false') {
3539
$this->nullable = false;
@@ -97,10 +101,6 @@ public function __construct(string $data, bool $writeOnly = false)
97101
$returnType = 'int';
98102
}
99103

100-
if (\preg_match('/__benevolent\<(.*)\>/', $returnType, $regs)) {
101-
$returnType = $regs[1];
102-
}
103-
104104
$returnType = Type::toRootNamespace($returnType);
105105
}
106106
$this->types = array_unique($returnTypes);

generator/tests/PhpStanFunctions/PhpStanTypeTest.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ public function testVoid(): void
111111
{
112112
$param = new PhpStanType('');
113113
$this->assertEquals('', $param->getDocBlockType());
114-
if (PHP_VERSION_ID >= 80200) {
115-
$this->assertEquals('void', $param->getSignatureType());
116-
} else {
117-
$this->assertEquals('', $param->getSignatureType());
118-
}
114+
if (PHP_VERSION_ID >= 80200) {
115+
$this->assertEquals('void', $param->getSignatureType());
116+
} else {
117+
$this->assertEquals('', $param->getSignatureType());
118+
}
119119

120120
$param = new PhpStanType('void');
121121
$this->assertEquals('void', $param->getDocBlockType());
@@ -197,4 +197,10 @@ public function testNumbersAreRemoved(): void
197197
$this->assertEquals('int', $param->getSignatureType());
198198
}
199199

200+
public function testIgnoreBenevolence(): void
201+
{
202+
$param = new PhpStanType('__benevolent<string|false>');
203+
$this->assertEquals('string', $param->getDocBlockType(Method::FALSY_TYPE));
204+
$this->assertEquals('string', $param->getSignatureType(Method::FALSY_TYPE));
205+
}
200206
}

0 commit comments

Comments
 (0)