Skip to content

Commit 70cdfb7

Browse files
committed
Fixing nullable parameters with non null default value
1 parent 222a915 commit 70cdfb7

File tree

8 files changed

+33
-7
lines changed

8 files changed

+33
-7
lines changed

generated/fileinfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function finfo_close($finfo): void
4343
* @throws FileinfoException
4444
*
4545
*/
46-
function finfo_open(int $options = FILEINFO_NONE, string $magic_file = null)
46+
function finfo_open(int $options = FILEINFO_NONE, ?string $magic_file = null)
4747
{
4848
error_clear_last();
4949
$result = \finfo_open($options, $magic_file);

generated/image.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @throws ImageException
1919
*
2020
*/
21-
function image2wbmp($image, string $filename = null, int $foreground = null): void
21+
function image2wbmp($image, ?string $filename = null, int $foreground = null): void
2222
{
2323
error_clear_last();
2424
if ($foreground !== null) {
@@ -2396,7 +2396,7 @@ function imagewebp($image, $to = null, int $quality = 80): void
23962396
* @throws ImageException
23972397
*
23982398
*/
2399-
function imagexbm($image, string $filename, int $foreground = null): void
2399+
function imagexbm($image, ?string $filename, int $foreground = null): void
24002400
{
24012401
error_clear_last();
24022402
if ($foreground !== null) {

generated/imap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ function imap_gc($imap_stream, int $caches): void
419419
* @throws ImapException
420420
*
421421
*/
422-
function imap_headerinfo($imap_stream, int $msg_number, int $fromlength = 0, int $subjectlength = 0, string $defaulthost = null): object
422+
function imap_headerinfo($imap_stream, int $msg_number, int $fromlength = 0, int $subjectlength = 0, ?string $defaulthost = null): object
423423
{
424424
error_clear_last();
425425
$result = \imap_headerinfo($imap_stream, $msg_number, $fromlength, $subjectlength, $defaulthost);

generated/ldap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function ldap_add($link_identifier, string $dn, array $entry, array $serverctrls
4646
* @throws LdapException
4747
*
4848
*/
49-
function ldap_bind($link_identifier, string $bind_rdn = null, string $bind_password = null): void
49+
function ldap_bind($link_identifier, ?string $bind_rdn = null, ?string $bind_password = null): void
5050
{
5151
error_clear_last();
5252
$result = \ldap_bind($link_identifier, $bind_rdn, $bind_password);

generated/pcre.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ function preg_match(string $pattern, string $subject, array &$matches = null, in
651651
* @throws PcreException
652652
*
653653
*/
654-
function preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
654+
function preg_split(string $pattern, string $subject, ?int $limit = -1, int $flags = 0): array
655655
{
656656
error_clear_last();
657657
$result = \preg_split($pattern, $subject, $limit, $flags);

generator/src/Parameter.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public function isVariadic(): bool
8080
return $this->parameter->parameter->__toString() === '...';
8181
}
8282

83+
public function isNullable(): bool
84+
{
85+
if ($this->phpStanFunction !== null) {
86+
$phpStanParameter = $this->phpStanFunction->getParameter($this->getParameter());
87+
if ($phpStanParameter) {
88+
return $phpStanParameter->isNullable();
89+
}
90+
}
91+
return $this->hasDefaultValue() && $this->getDefaultValue() === 'null';
92+
}
8393

8494
/*
8595
* @return string

generator/src/PhpStanFunctions/PhpStanParameter.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ class PhpStanParameter
2828
* @var bool
2929
*/
3030
private $byReference = false;
31+
/**
32+
* @var bool
33+
*/
34+
private $nullable = false;
3135

3236
public function __construct(string $name, string $type)
3337
{
@@ -47,6 +51,7 @@ public function __construct(string $name, string $type)
4751

4852
if (\strpos($type, '?') !== false) {
4953
$type = \str_replace('?', '', $type).'|null';
54+
$this->nullable = true;
5055
}
5156

5257
$this->type = $type;
@@ -91,4 +96,12 @@ public function isByReference(): bool
9196
{
9297
return $this->byReference;
9398
}
99+
100+
/**
101+
* @return bool
102+
*/
103+
public function isNullable(): bool
104+
{
105+
return $this->nullable;
106+
}
94107
}

generator/src/WritePhpFunction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ private function displayParamsWithType(array $params): string
138138
foreach ($params as $param) {
139139
$paramAsString = '';
140140
if ($param->getType() !== 'mixed' && $param->getType() !== 'resource') {
141-
$paramAsString = $param->getType().' ';
141+
if ($param->isNullable()) {
142+
$paramAsString .= '?';
143+
}
144+
$paramAsString .= $param->getType().' ';
142145
}
143146

144147
$paramName = $param->getParameter();

0 commit comments

Comments
 (0)