Skip to content

Commit

Permalink
Support data_fill(), data_set(), precognitive(), object_get()
Browse files Browse the repository at this point in the history
  • Loading branch information
alies-dev committed Feb 24, 2023
1 parent 2a8c68d commit 2840fd9
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 7 deletions.
29 changes: 27 additions & 2 deletions stubs/Collections/helpers.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,34 @@
*/

// collect: nothing to stub
// data_fill: nothing to stub

/**
* Fill in data where it's missing.
*
* @template TTarget
*
* @param TTarget $target
* @param string|array $key
* @param mixed $value
* @return mixed
* @psalm-return TTarget is array ? array : TTarget
*/
function data_fill(&$target, $key, $value) {}

// data_get: nothing to stub
// data_set: nothing to stub

/**
* Set an item on an array or object using dot notation.
*
* @template TTarget
*
* @param TTarget $target
* @param string|array $key
* @param mixed $value
* @param bool $overwrite
* @psalm-return TTarget is array ? array : TTarget
*/
function data_set(&$target, $key, $value, $overwrite = true) {}

/**
* Get the first element of an array. Useful for method chaining.
Expand Down
30 changes: 26 additions & 4 deletions stubs/Foundation/helpers.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ function action($name, $parameters = [], $absolute = true) {}
// auth: processed by Psalm handlers

/**
* Get the available auth instance.
*
* @param string|null $guard
* @return (
* $guard is null
Expand All @@ -73,6 +75,8 @@ function auth($guard = null) {}
*/
function back($status = 302, $headers = [], $fallback = false) {}

// base_path: processed by Psalm handlers

/**
* Hash the given value against the bcrypt algorithm.
*
Expand All @@ -90,6 +94,8 @@ function bcrypt($value, $options = []) {}
*/
function broadcast($event = null) {}

// cache: processed by Psalm handlers

/**
* Get / set the specified configuration value.
*
Expand Down Expand Up @@ -157,23 +163,24 @@ function event(...$args) {}

// fake: nothing to stub
// info: nothing to stub
// lang_path: processed by Psalm handlers

/**
* Log a debug message to the logs.
*
* @param string|null $message
* @param array $context
* @return \Illuminate\Log\LogManager|null
* @psalm-return (func_num_args() is 0 ? \Illuminate\Log\LogManager : void)
* @psalm-return ($message is null ? \Illuminate\Log\LogManager : null)
*/
function logger($message = null, array $context = []) {}

// lang_path: processed by Psalm handlers

/**
* Get a log driver instance.
*
* @param string|null $driver
* @return ($driver is null ? \Illuminate\Log\LogManager : \Psr\Log\LoggerInterface)
* @return ($driver is null ? \Illuminate\Log\LogManager : \Psr\Log\LoggerInterface&\Illuminate\Log\Logger)
*/
function logs($driver = null) {}

Expand All @@ -182,7 +189,17 @@ function logs($driver = null) {}
// now: nothing to stub
// old: nothing to stub
// policy: nothing to stub
// precognitive: nothing to stub

/**
* Handle a Precognition controller hook.
*
* @template TCallableReturn
*
* @param null|callable(callable(\Illuminate\Http\Response=, mixed=): void): TCallableReturn $callable
* @return TCallableReturn
*/
function precognitive($callable = null) {}

// public_path: processed by Psalm handlers

/**
Expand All @@ -208,6 +225,8 @@ function redirect($to = null, $status = 302, $headers = [], $secure = null) {}
function request($key = null, $default = null) {}

/**
* Catch a potential exception and return a default value.
*
* @template TValue
* @template TDefault
* @template TDefaultCallableReturn
Expand Down Expand Up @@ -286,6 +305,7 @@ function __($key = null, $replace = [], $locale = null) {}

/**
* Generate a url for the application.
*
* @param string|null $path
* @param mixed $parameters
* @param bool|null $secure
Expand All @@ -305,6 +325,8 @@ function url($path = null, $parameters = [], $secure = null) {}
function validator(array $data = [], array $rules = [], array $messages = [], array $customAttributes = []) {}

/**
* Get the evaluated view contents for the given view.
*
* @param string|null $view
* @param \Illuminate\Contracts\Support\Arrayable<string, mixed>|array<string, mixed> $data
* @param array<string, mixed> $mergeData
Expand Down
18 changes: 17 additions & 1 deletion stubs/Support/helpers.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,17 @@ function class_uses_recursive($class) {}
*/
function filled($value) {}

// object_get: nothing to stub
/**
* Get an item from an object using "dot" notation.
*
* @template TObject
*
* @param object $object
* @param string|null $key
* @param mixed $default
* @return ($key is (null|'') ? TObject : mixed)
*/
function object_get($object, $key, $default = null) {}

/**
* Provide access to optional objects.
Expand All @@ -89,6 +99,8 @@ function optional($value = null, callable $callback = null) {}
// preg_replace_array: nothing to stub

/**
* Retry an operation a given number of times.
*
* @template TValue
* @param positive-int|list<int> $times
* @param callable(int): TValue $callback
Expand All @@ -112,7 +124,10 @@ function retry($times, callable $callback, $sleep = 0, $when = null) {}
function str($string = null) {}

/**
* Call the given Closure with the given value then return the value.
*
* @template TValue
*
* @param TValue $value
* @param null|callable(TValue): void $callback
* @return TValue
Expand Down Expand Up @@ -151,6 +166,7 @@ function throw_unless($condition, $exception = 'RuntimeException', ...$parameter

/**
* Returns all traits used by a trait and its traits.
*
* @param class-string $trait
* @return array<trait-string, trait-string>
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/Acceptance/acceptance/CollectionHelpers.feature
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ Feature: Collection helpers
<?php declare(strict_types=1);
"""

Scenario: data_fill() support
Given I have the following code
"""
function data_fill_supports_array(array $input): array
{
return data_fill($input, 'key', 'value');
}
function data_fill_supports_object(object $input): object
{
return data_fill($input, 'property', 'value');
}
"""
When I run Psalm
Then I see no errors

Scenario: data_set() support
Given I have the following code
"""
function data_set_supports_array(array $input): array
{
return data_set($input, 'key', 'value');
}
function data_set_supports_object(object $input): object
{
return data_set($input, 'property', 'value');
}
"""
When I run Psalm
Then I see no errors

Scenario: head() and last() support
Given I have the following code
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/Acceptance/acceptance/FoundationHelpers.feature
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ Feature: Foundation helpers
When I run Psalm
Then I see no errors

Scenario: precognitive() support
Given I have the following code
"""
$payload = precognitive(function () {
return ['foo' => 'bar'];
});
/** @psalm-check-type $payload = array{'foo': 'bar'} */
"""
When I run Psalm
Then I see no errors

Scenario: redirect() support
Given I have the following code
"""
Expand Down
16 changes: 16 additions & 0 deletions tests/Acceptance/acceptance/SupportHelpers.feature
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ Feature: Support helpers
When I run Psalm
Then I see no errors

Scenario: object_get() support
Given I have the following code
"""
function object_get_returns_first_arg_when_second_is_null(\stdClass $object): \stdClass
{
return object_get($object, null);
}
function object_get_returns_first_arg_when_second_is_empty_string(\stdClass $object): \stdClass
{
return object_get($object, '');
}
"""
When I run Psalm
Then I see no errors

Scenario: retry() support
Given I have the following code
"""
Expand Down

0 comments on commit 2840fd9

Please sign in to comment.