From 979fbd967b4dd4e182a38ed29d75a1b6d9aaf4e6 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Fri, 22 Nov 2024 15:15:47 +0100 Subject: [PATCH 1/5] Improve getopt() function stub --- stubs/core.stub | 8 ++++++++ tests/PHPStan/Analyser/nsrt/getopt.php | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/stubs/core.stub b/stubs/core.stub index f2b72c235b..784449a16a 100644 --- a/stubs/core.stub +++ b/stubs/core.stub @@ -318,3 +318,11 @@ function abs($num) {} * @return ($categorize is true ? array> : array) */ function get_defined_constants(bool $categorize = false): array {} + +/** + * @param array $long_options + * @param-out int $rest_index + * @return __benevolent|array|array>|false> + */ +function getopt(string $short_options, array $long_options = [], int &$rest_index = null): array|false {} + diff --git a/tests/PHPStan/Analyser/nsrt/getopt.php b/tests/PHPStan/Analyser/nsrt/getopt.php index 453667cd51..52647b6d4b 100644 --- a/tests/PHPStan/Analyser/nsrt/getopt.php +++ b/tests/PHPStan/Analyser/nsrt/getopt.php @@ -5,5 +5,6 @@ use function getopt; use function PHPStan\Testing\assertType; -$opts = getopt("ab:c::", ["longopt1", "longopt2:", "longopt3::"]); +$opts = getopt("ab:c::", ["longopt1", "longopt2:", "longopt3::"], $restIndex); assertType('(array|string|false>|false)', $opts); +assertType('int', $restIndex); From d7b93cb477c7ed8699dd2b71f032bd4ebd1ea7c7 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Fri, 22 Nov 2024 15:22:49 +0100 Subject: [PATCH 2/5] Adjust stub --- stubs/core.stub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/core.stub b/stubs/core.stub index 784449a16a..f60486b3c6 100644 --- a/stubs/core.stub +++ b/stubs/core.stub @@ -324,5 +324,5 @@ function get_defined_constants(bool $categorize = false): array {} * @param-out int $rest_index * @return __benevolent|array|array>|false> */ -function getopt(string $short_options, array $long_options = [], int &$rest_index = null): array|false {} +function getopt(string $short_options, array $long_options = [], &$rest_index = null) {} From 5efdbd95884176cb063cdce54d1bb7c1799c1bcb Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Fri, 22 Nov 2024 15:31:01 +0100 Subject: [PATCH 3/5] rest_index accepts anything --- stubs/core.stub | 1 + 1 file changed, 1 insertion(+) diff --git a/stubs/core.stub b/stubs/core.stub index f60486b3c6..759d41064e 100644 --- a/stubs/core.stub +++ b/stubs/core.stub @@ -321,6 +321,7 @@ function get_defined_constants(bool $categorize = false): array {} /** * @param array $long_options + * @param mixed $rest_index * @param-out int $rest_index * @return __benevolent|array|array>|false> */ From 5ea5679e36cbba3d128609b7024c1b20d9e17c07 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Tue, 4 Mar 2025 13:48:51 +0100 Subject: [PATCH 4/5] positive-int --- stubs/core.stub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/core.stub b/stubs/core.stub index 759d41064e..de4c904423 100644 --- a/stubs/core.stub +++ b/stubs/core.stub @@ -322,7 +322,7 @@ function get_defined_constants(bool $categorize = false): array {} /** * @param array $long_options * @param mixed $rest_index - * @param-out int $rest_index + * @param-out positive-int $rest_index * @return __benevolent|array|array>|false> */ function getopt(string $short_options, array $long_options = [], &$rest_index = null) {} From 98d59901b09109fd5e04e4b2d2985b1f61a5442a Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Tue, 4 Mar 2025 14:02:32 +0100 Subject: [PATCH 5/5] Fix test --- tests/PHPStan/Analyser/nsrt/getopt.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/PHPStan/Analyser/nsrt/getopt.php b/tests/PHPStan/Analyser/nsrt/getopt.php index 52647b6d4b..aae0e128e5 100644 --- a/tests/PHPStan/Analyser/nsrt/getopt.php +++ b/tests/PHPStan/Analyser/nsrt/getopt.php @@ -7,4 +7,4 @@ $opts = getopt("ab:c::", ["longopt1", "longopt2:", "longopt3::"], $restIndex); assertType('(array|string|false>|false)', $opts); -assertType('int', $restIndex); +assertType('int<1, max>', $restIndex);