From 70cdfb7732ab22b1bd82c6c4a3f8650a45759c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 8 Nov 2018 15:42:06 +0100 Subject: [PATCH] Fixing nullable parameters with non null default value --- generated/fileinfo.php | 2 +- generated/image.php | 4 ++-- generated/imap.php | 2 +- generated/ldap.php | 2 +- generated/pcre.php | 2 +- generator/src/Parameter.php | 10 ++++++++++ generator/src/PhpStanFunctions/PhpStanParameter.php | 13 +++++++++++++ generator/src/WritePhpFunction.php | 5 ++++- 8 files changed, 33 insertions(+), 7 deletions(-) diff --git a/generated/fileinfo.php b/generated/fileinfo.php index 1ed4b6a4..40f2eb13 100644 --- a/generated/fileinfo.php +++ b/generated/fileinfo.php @@ -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); diff --git a/generated/image.php b/generated/image.php index 7b1eed92..ecf80026 100644 --- a/generated/image.php +++ b/generated/image.php @@ -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) { @@ -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) { diff --git a/generated/imap.php b/generated/imap.php index 10bb0517..fb4e5d56 100644 --- a/generated/imap.php +++ b/generated/imap.php @@ -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); diff --git a/generated/ldap.php b/generated/ldap.php index 264b66dd..5ece469d 100644 --- a/generated/ldap.php +++ b/generated/ldap.php @@ -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); diff --git a/generated/pcre.php b/generated/pcre.php index f6ba9ae0..34b91fbb 100644 --- a/generated/pcre.php +++ b/generated/pcre.php @@ -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); diff --git a/generator/src/Parameter.php b/generator/src/Parameter.php index a8842487..80a2cc08 100644 --- a/generator/src/Parameter.php +++ b/generator/src/Parameter.php @@ -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 diff --git a/generator/src/PhpStanFunctions/PhpStanParameter.php b/generator/src/PhpStanFunctions/PhpStanParameter.php index 2a9d5268..8d159b91 100644 --- a/generator/src/PhpStanFunctions/PhpStanParameter.php +++ b/generator/src/PhpStanFunctions/PhpStanParameter.php @@ -28,6 +28,10 @@ class PhpStanParameter * @var bool */ private $byReference = false; + /** + * @var bool + */ + private $nullable = false; public function __construct(string $name, string $type) { @@ -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; @@ -91,4 +96,12 @@ public function isByReference(): bool { return $this->byReference; } + + /** + * @return bool + */ + public function isNullable(): bool + { + return $this->nullable; + } } diff --git a/generator/src/WritePhpFunction.php b/generator/src/WritePhpFunction.php index 52b035b2..19eab1ba 100644 --- a/generator/src/WritePhpFunction.php +++ b/generator/src/WritePhpFunction.php @@ -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();