Skip to content

Commit

Permalink
Fixing nullable parameters with non null default value
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed Nov 8, 2018
1 parent 222a915 commit 70cdfb7
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion generated/fileinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function finfo_close($finfo): void
* @throws FileinfoException
*
*/
function finfo_open(int $options = FILEINFO_NONE, string $magic_file = null)
function finfo_open(int $options = FILEINFO_NONE, ?string $magic_file = null)
{
error_clear_last();
$result = \finfo_open($options, $magic_file);
Expand Down
4 changes: 2 additions & 2 deletions generated/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @throws ImageException
*
*/
function image2wbmp($image, string $filename = null, int $foreground = null): void
function image2wbmp($image, ?string $filename = null, int $foreground = null): void
{
error_clear_last();
if ($foreground !== null) {
Expand Down Expand Up @@ -2396,7 +2396,7 @@ function imagewebp($image, $to = null, int $quality = 80): void
* @throws ImageException
*
*/
function imagexbm($image, string $filename, int $foreground = null): void
function imagexbm($image, ?string $filename, int $foreground = null): void
{
error_clear_last();
if ($foreground !== null) {
Expand Down
2 changes: 1 addition & 1 deletion generated/imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ function imap_gc($imap_stream, int $caches): void
* @throws ImapException
*
*/
function imap_headerinfo($imap_stream, int $msg_number, int $fromlength = 0, int $subjectlength = 0, string $defaulthost = null): object
function imap_headerinfo($imap_stream, int $msg_number, int $fromlength = 0, int $subjectlength = 0, ?string $defaulthost = null): object
{
error_clear_last();
$result = \imap_headerinfo($imap_stream, $msg_number, $fromlength, $subjectlength, $defaulthost);
Expand Down
2 changes: 1 addition & 1 deletion generated/ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function ldap_add($link_identifier, string $dn, array $entry, array $serverctrls
* @throws LdapException
*
*/
function ldap_bind($link_identifier, string $bind_rdn = null, string $bind_password = null): void
function ldap_bind($link_identifier, ?string $bind_rdn = null, ?string $bind_password = null): void
{
error_clear_last();
$result = \ldap_bind($link_identifier, $bind_rdn, $bind_password);
Expand Down
2 changes: 1 addition & 1 deletion generated/pcre.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ function preg_match(string $pattern, string $subject, array &$matches = null, in
* @throws PcreException
*
*/
function preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array
function preg_split(string $pattern, string $subject, ?int $limit = -1, int $flags = 0): array
{
error_clear_last();
$result = \preg_split($pattern, $subject, $limit, $flags);
Expand Down
10 changes: 10 additions & 0 deletions generator/src/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ public function isVariadic(): bool
return $this->parameter->parameter->__toString() === '...';
}

public function isNullable(): bool
{
if ($this->phpStanFunction !== null) {
$phpStanParameter = $this->phpStanFunction->getParameter($this->getParameter());
if ($phpStanParameter) {
return $phpStanParameter->isNullable();
}
}
return $this->hasDefaultValue() && $this->getDefaultValue() === 'null';
}

/*
* @return string
Expand Down
13 changes: 13 additions & 0 deletions generator/src/PhpStanFunctions/PhpStanParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class PhpStanParameter
* @var bool
*/
private $byReference = false;
/**
* @var bool
*/
private $nullable = false;

public function __construct(string $name, string $type)
{
Expand All @@ -47,6 +51,7 @@ public function __construct(string $name, string $type)

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

$this->type = $type;
Expand Down Expand Up @@ -91,4 +96,12 @@ public function isByReference(): bool
{
return $this->byReference;
}

/**
* @return bool
*/
public function isNullable(): bool
{
return $this->nullable;
}
}
5 changes: 4 additions & 1 deletion generator/src/WritePhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ private function displayParamsWithType(array $params): string
foreach ($params as $param) {
$paramAsString = '';
if ($param->getType() !== 'mixed' && $param->getType() !== 'resource') {
$paramAsString = $param->getType().' ';
if ($param->isNullable()) {
$paramAsString .= '?';
}
$paramAsString .= $param->getType().' ';
}

$paramName = $param->getParameter();
Expand Down

0 comments on commit 70cdfb7

Please # to comment.