Merge pull request #167 from caugner/head-support

This commit is contained in:
feek 2021-07-05 11:07:50 -04:00 committed by GitHub
commit 3392dc087d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 2 deletions

View File

@ -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.
*

View File

@ -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