From 0369da4a30cf2058012927403de4a0dceeda59cc Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Mon, 5 Jul 2021 11:22:17 +0200 Subject: [PATCH 1/2] test: ensure head/last work correctly --- tests/acceptance/Helpers.feature | 34 ++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/tests/acceptance/Helpers.feature b/tests/acceptance/Helpers.feature index 734cfe1c..e7f73a29 100644 --- a/tests/acceptance/Helpers.feature +++ b/tests/acceptance/Helpers.feature @@ -31,6 +31,38 @@ Feature: helpers When I run Psalm Then I see no errors + Scenario: head and last support + Given I have the following code + """ + /** + * @return false + */ + function empty_head() + { + return head([]); + } + + /** + * @return false + */ + function empty_last() + { + return last([]); + } + + function non_empty_head(): int + { + return last([1, 2, 3]); + } + + function non_empty_last(): int + { + return last([1, 2, 3]); + } + """ + When I run Psalm + Then I see no errors + Scenario: optional support Given I have the following code """ @@ -38,7 +70,6 @@ Feature: helpers { return optional($user)->getMessage(); } - """ When I run Psalm Then I see no errors @@ -58,7 +89,6 @@ Feature: helpers { return logger(); } - """ When I run Psalm Then I see no errors From 1e7bbf8ee1b7c3024ca6a74f5f2f55b4223ba04e Mon Sep 17 00:00:00 2001 From: Claas Augner Date: Mon, 5 Jul 2021 11:35:03 +0200 Subject: [PATCH 2/2] feature: support head() and last() --- stubs/helpers.stubphp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/stubs/helpers.stubphp b/stubs/helpers.stubphp index 5862f5b8..f40c377a 100644 --- a/stubs/helpers.stubphp +++ b/stubs/helpers.stubphp @@ -81,6 +81,32 @@ class NullObject { } } +/** + * Get the first element of an array. Useful for method chaining. + * + * @template TValue + * @template TParam of TValue[] + * @param TParam $array + * @return (TParam is non-empty-array ? TValue : false) + */ +function head($array) +{ + return reset($array); +} + +/** + * Get the last element from an array. + * + * @template TValue + * @template TParam of TValue[] + * @param TParam $array + * @return (TParam is non-empty-array ? TValue : false) + */ +function last($array) +{ + +} + /** * Provide access to optional objects. *