From ae2dc75139df8480a9e165e92beb5068f0642684 Mon Sep 17 00:00:00 2001 From: kuma <4714658+kuma3@users.noreply.github.com> Date: Wed, 26 Jun 2024 23:54:19 +0900 Subject: [PATCH 1/3] improve mode parameter of count function --- resources/functionMap.php | 2 +- .../Functions/CallToFunctionParametersRuleTest.php | 14 ++++++++++++++ tests/PHPStan/Rules/Functions/data/count.php | 12 ++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Rules/Functions/data/count.php diff --git a/resources/functionMap.php b/resources/functionMap.php index eae5a837bf..71712a90a1 100644 --- a/resources/functionMap.php +++ b/resources/functionMap.php @@ -1365,7 +1365,7 @@ 'Couchbase\WildcardSearchQuery::jsonSerialize' => ['array'], 'Couchbase\zlibCompress' => ['string', 'data'=>'string'], 'Couchbase\zlibDecompress' => ['string', 'data'=>'string'], -'count' => ['0|positive-int', 'var'=>'Countable|array', 'mode='=>'int'], +'count' => ['0|positive-int', 'var'=>'Countable|array', 'mode='=>'0|1'], 'count_chars' => ['mixed', 'input'=>'string', 'mode='=>'0|1|2|3|4'], 'Countable::count' => ['0|positive-int'], 'crack_check' => ['bool', 'dictionary'=>'', 'password'=>'string'], diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 90859848d0..7971622222 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -1716,4 +1716,18 @@ public function testCountArrayShift(): void $this->analyse([__DIR__ . '/data/count-array-shift.php'], $errors); } + public function testCount(): void + { + $this->analyse([__DIR__ . '/data/count.php'], [ + [ + 'Parameter #2 $mode of function count expects 0|1, -1 given.', + 9, + ], + [ + 'Parameter #2 $mode of function count expects 0|1, int given.', + 10, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/count.php b/tests/PHPStan/Rules/Functions/data/count.php new file mode 100644 index 0000000000..6619345993 --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/count.php @@ -0,0 +1,12 @@ + Date: Fri, 28 Jun 2024 01:03:50 +0900 Subject: [PATCH 2/3] Revert "improve mode parameter of count function" This reverts commit ae2dc75139df8480a9e165e92beb5068f0642684. --- resources/functionMap.php | 2 +- .../Functions/CallToFunctionParametersRuleTest.php | 14 -------------- tests/PHPStan/Rules/Functions/data/count.php | 12 ------------ 3 files changed, 1 insertion(+), 27 deletions(-) delete mode 100644 tests/PHPStan/Rules/Functions/data/count.php diff --git a/resources/functionMap.php b/resources/functionMap.php index 71712a90a1..eae5a837bf 100644 --- a/resources/functionMap.php +++ b/resources/functionMap.php @@ -1365,7 +1365,7 @@ 'Couchbase\WildcardSearchQuery::jsonSerialize' => ['array'], 'Couchbase\zlibCompress' => ['string', 'data'=>'string'], 'Couchbase\zlibDecompress' => ['string', 'data'=>'string'], -'count' => ['0|positive-int', 'var'=>'Countable|array', 'mode='=>'0|1'], +'count' => ['0|positive-int', 'var'=>'Countable|array', 'mode='=>'int'], 'count_chars' => ['mixed', 'input'=>'string', 'mode='=>'0|1|2|3|4'], 'Countable::count' => ['0|positive-int'], 'crack_check' => ['bool', 'dictionary'=>'', 'password'=>'string'], diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 7971622222..90859848d0 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -1716,18 +1716,4 @@ public function testCountArrayShift(): void $this->analyse([__DIR__ . '/data/count-array-shift.php'], $errors); } - public function testCount(): void - { - $this->analyse([__DIR__ . '/data/count.php'], [ - [ - 'Parameter #2 $mode of function count expects 0|1, -1 given.', - 9, - ], - [ - 'Parameter #2 $mode of function count expects 0|1, int given.', - 10, - ], - ]); - } - } diff --git a/tests/PHPStan/Rules/Functions/data/count.php b/tests/PHPStan/Rules/Functions/data/count.php deleted file mode 100644 index 6619345993..0000000000 --- a/tests/PHPStan/Rules/Functions/data/count.php +++ /dev/null @@ -1,12 +0,0 @@ - Date: Fri, 28 Jun 2024 01:05:08 +0900 Subject: [PATCH 3/3] Bleeding edge - Implement narrowed type definition for mode in bleedingEdge. --- resources/functionMap_bleedingEdge.php | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/functionMap_bleedingEdge.php b/resources/functionMap_bleedingEdge.php index 01ff0be329..9f2230dd6f 100644 --- a/resources/functionMap_bleedingEdge.php +++ b/resources/functionMap_bleedingEdge.php @@ -141,6 +141,7 @@ 'RecursiveIteratorIterator::__construct' => ['void', 'iterator'=>'RecursiveIterator|IteratorAggregate', 'mode='=>'RecursiveIteratorIterator::LEAVES_ONLY|RecursiveIteratorIterator::SELF_FIRST|RecursiveIteratorIterator::CHILD_FIRST', 'flags='=>'0|RecursiveIteratorIterator::CATCH_GET_CHILD'], 'Locale::composeLocale' => ['string|false', 'subtags'=>'array{language:string, script?:string, region?:string, variant?:array, private?:array, extlang?:array, variant0?:string, variant1?:string, variant2?:string, variant3?:string, variant4?:string, variant5?:string, variant6?:string, variant7?:string, variant8?:string, variant9?:string, variant10?:string, variant11?:string, variant12?:string, variant13?:string, variant14?:string, private0?:string, private1?:string, private2?:string, private3?:string, private4?:string, private5?:string, private6?:string, private7?:string, private8?:string, private9?:string, private10?:string, private11?:string, private12?:string, private13?:string, private14?:string, extlang0?:string, extlang1?:string, extlang2?:string}'], 'locale_compose' => ['string|false', 'subtags'=>'array{language:string, script?:string, region?:string, variant?:array, private?:array, extlang?:array, variant0?:string, variant1?:string, variant2?:string, variant3?:string, variant4?:string, variant5?:string, variant6?:string, variant7?:string, variant8?:string, variant9?:string, variant10?:string, variant11?:string, variant12?:string, variant13?:string, variant14?:string, private0?:string, private1?:string, private2?:string, private3?:string, private4?:string, private5?:string, private6?:string, private7?:string, private8?:string, private9?:string, private10?:string, private11?:string, private12?:string, private13?:string, private14?:string, extlang0?:string, extlang1?:string, extlang2?:string}'], + 'count' => ['0|positive-int', 'var'=>'Countable|array', 'mode='=>'0|1'], ], 'old' => [