Skip to content

Commit

Permalink
[11.x] feat: add more specific types and tests for helpers (#51938)
Browse files Browse the repository at this point in the history
* feat: add more specific types and tests for helpers

* feat: narrow types for `filled` and `blank` helpers
  • Loading branch information
calebdw authored Jun 28, 2024
1 parent c307abc commit 5d006a5
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 123 deletions.
1 change: 0 additions & 1 deletion phpstan.src.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ parameters:
- "#Caught class [a-zA-Z0-9\\\\_]+ not found.#"
- "#Class [a-zA-Z0-9\\\\_]+ not found.#"
- "#has invalid type#"
- "#should always throw an exception or terminate script execution#"
- "#Instantiated class [a-zA-Z0-9\\\\_]+ not found.#"
- "#Unsafe usage of new static#"
excludePaths:
Expand Down
9 changes: 6 additions & 3 deletions src/Illuminate/Collections/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,12 @@ function last($array)
/**
* Return the default value of the given value.
*
* @param mixed $value
* @param mixed ...$args
* @return mixed
* @template TValue
* @template TArgs
*
* @param TValue|\Closure(TArgs): TValue $value
* @param TArgs ...$args
* @return TValue
*/
function value($value, ...$args)
{
Expand Down
93 changes: 51 additions & 42 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ function action($name, $parameters = [], $absolute = true)
/**
* Get the available container instance.
*
* @param string|null $abstract
* @template TClass
*
* @param string|class-string<TClass>|null $abstract
* @param array $parameters
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application|mixed
* @return ($abstract is class-string<TClass> ? TClass : ($abstract is null ? \Illuminate\Foundation\Application : mixed))
*/
function app($abstract = null, array $parameters = [])
{
Expand Down Expand Up @@ -155,7 +157,7 @@ function asset($path, $secure = null)
* Get the available auth instance.
*
* @param string|null $guard
* @return \Illuminate\Contracts\Auth\Factory|\Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
* @return ($guard is null ? \Illuminate\Contracts\Auth\Factory : \Illuminate\Contracts\Auth\StatefulGuard)
*/
function auth($guard = null)
{
Expand Down Expand Up @@ -228,28 +230,29 @@ function broadcast($event = null)
*
* If an array is passed, we'll assume you want to put to the cache.
*
* @param mixed ...$arguments key|key,default|data,expiration|null
* @return mixed|\Illuminate\Cache\CacheManager
* @param string|array<string, mixed>|null $key key|data
* @param mixed $default default|expiration|null
* @return ($key is null ? \Illuminate\Cache\CacheManager : ($key is string ? mixed : bool))
*
* @throws \InvalidArgumentException
*/
function cache(...$arguments)
function cache($key = null, $default = null)
{
if (empty($arguments)) {
if (is_null($key)) {
return app('cache');
}

if (is_string($arguments[0])) {
return app('cache')->get(...$arguments);
if (is_string($key)) {
return app('cache')->get($key, $default);
}

if (! is_array($arguments[0])) {
if (! is_array($key)) {
throw new InvalidArgumentException(
'When setting a value in the cache, you must pass an array of key / value pairs.'
);
}

return app('cache')->put(key($arguments[0]), reset($arguments[0]), $arguments[1] ?? null);
return app('cache')->put(key($key), reset($key), ttl: $default);
}
}

Expand All @@ -259,9 +262,9 @@ function cache(...$arguments)
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* @param array|string|null $key
* @param array<string, mixed>|string|null $key
* @param mixed $default
* @return mixed|\Illuminate\Config\Repository
* @return ($key is null ? \Illuminate\Config\Repository : ($key is string ? mixed : null))
*/
function config($key = null, $default = null)
{
Expand Down Expand Up @@ -297,13 +300,16 @@ function config_path($path = '')
* @param array|string|null $key
* @param mixed $default
* @return mixed|\Illuminate\Log\Context\Repository
* @return ($key is string ? mixed : \Illuminate\Log\Context\Repository)
*/
function context($key = null, $default = null)
{
$context = app(ContextRepository::class);

return match (true) {
is_null($key) => app(ContextRepository::class),
is_array($key) => app(ContextRepository::class)->add($key),
default => app(ContextRepository::class)->get($key, $default),
is_null($key) => $context,
is_array($key) => $context->add($key),
default => $context->get($key, $default),
};
}
}
Expand All @@ -321,7 +327,7 @@ function context($key = null, $default = null)
* @param bool $httpOnly
* @param bool $raw
* @param string|null $sameSite
* @return \Illuminate\Cookie\CookieJar|\Symfony\Component\HttpFoundation\Cookie
* @return ($name is null ? \Illuminate\Cookie\CookieJar : \Symfony\Component\HttpFoundation\Cookie)
*/
function cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null)
{
Expand Down Expand Up @@ -400,6 +406,7 @@ function decrypt($value, $unserialize = true)
*
* @param mixed $job
* @return \Illuminate\Foundation\Bus\PendingDispatch
* @return ($job is \Closure ? \Illuminate\Foundation\Bus\PendingClosureDispatch : \Illuminate\Foundation\Bus\PendingDispatch)
*/
function dispatch($job)
{
Expand Down Expand Up @@ -499,7 +506,7 @@ function info($message, $context = [])
*
* @param string|null $message
* @param array $context
* @return \Illuminate\Log\LogManager|null
* @return ($message is null ? \Illuminate\Log\LogManager : null)
*/
function logger($message = null, array $context = [])
{
Expand Down Expand Up @@ -529,7 +536,7 @@ function lang_path($path = '')
* Get a log driver instance.
*
* @param string|null $driver
* @return \Illuminate\Log\LogManager|\Psr\Log\LoggerInterface
* @return ($driver is null ? \Illuminate\Log\LogManager : \Psr\Log\LoggerInterface)
*/
function logs($driver = null)
{
Expand Down Expand Up @@ -658,7 +665,7 @@ function public_path($path = '')
* @param int $status
* @param array $headers
* @param bool|null $secure
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
* @return ($to is null ? \Illuminate\Routing\Redirector : \Illuminate\Http\RedirectResponse)
*/
function redirect($to = null, $status = 302, $headers = [], $secure = null)
{
Expand Down Expand Up @@ -723,9 +730,9 @@ function report_unless($boolean, $exception)
/**
* Get an instance of the current request or an input item from the request.
*
* @param array|string|null $key
* @param list<string>|string|null $key
* @param mixed $default
* @return mixed|\Illuminate\Http\Request|string|array|null
* @return ($key is null ? \Illuminate\Http\Request : ($key is string ? mixed : array<string, mixed>))
*/
function request($key = null, $default = null)
{
Expand All @@ -747,13 +754,13 @@ function request($key = null, $default = null)
/**
* Catch a potential exception and return a default value.
*
* @template TRescueValue
* @template TRescueFallback
* @template TValue
* @template TFallback
*
* @param callable(): TRescueValue $callback
* @param (callable(\Throwable): TRescueFallback)|TRescueFallback $rescue
* @param bool|callable $report
* @return TRescueValue|TRescueFallback
* @param callable(): TValue $callback
* @param (callable(\Throwable): TFallback)|TFallback $rescue
* @param bool|callable(\Throwable): bool $report
* @return TValue|TFallback
*/
function rescue(callable $callback, $rescue = null, $report = true)
{
Expand All @@ -773,9 +780,11 @@ function rescue(callable $callback, $rescue = null, $report = true)
/**
* Resolve a service from the container.
*
* @param string $name
* @template TClass
*
* @param string|class-string<TClass> $name
* @param array $parameters
* @return mixed
* @return ($name is class-string<TClass> ? TClass : mixed)
*/
function resolve($name, array $parameters = [])
{
Expand Down Expand Up @@ -803,17 +812,17 @@ function resource_path($path = '')
* @param \Illuminate\Contracts\View\View|string|array|null $content
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
* @return ($content is null ? \Illuminate\Contracts\Routing\ResponseFactory : \Illuminate\Http\Response)
*/
function response($content = '', $status = 200, array $headers = [])
function response($content = null, $status = 200, array $headers = [])
{
$factory = app(ResponseFactory::class);

if (func_num_args() === 0) {
return $factory;
}

return $factory->make($content, $status, $headers);
return $factory->make($content ?? '', $status, $headers);
}
}

Expand Down Expand Up @@ -865,9 +874,9 @@ function secure_url($path, $parameters = [])
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* @param array|string|null $key
* @param array<string, mixed>|string|null $key
* @param mixed $default
* @return mixed|\Illuminate\Session\Store|\Illuminate\Session\SessionManager
* @return ($key is null ? \Illuminate\Session\SessionManager : ($key is string ? mixed : null))
*/
function session($key = null, $default = null)
{
Expand Down Expand Up @@ -932,7 +941,7 @@ function today($tz = null)
* @param string|null $key
* @param array $replace
* @param string|null $locale
* @return \Illuminate\Contracts\Translation\Translator|string|array|null
* @return ($key is null ? \Illuminate\Contracts\Translation\Translator : array|string)
*/
function trans($key = null, $replace = [], $locale = null)
{
Expand Down Expand Up @@ -986,7 +995,7 @@ function __($key = null, $replace = [], $locale = null)
* @param string|null $path
* @param mixed $parameters
* @param bool|null $secure
* @return \Illuminate\Contracts\Routing\UrlGenerator|string
* @return ($path is null ? \Illuminate\Contracts\Routing\UrlGenerator : string)
*/
function url($path = null, $parameters = [], $secure = null)
{
Expand All @@ -1002,21 +1011,21 @@ function url($path = null, $parameters = [], $secure = null)
/**
* Create a new Validator instance.
*
* @param array $data
* @param array|null $data
* @param array $rules
* @param array $messages
* @param array $attributes
* @return \Illuminate\Contracts\Validation\Validator|\Illuminate\Contracts\Validation\Factory
* @return ($data is null ? \Illuminate\Contracts\Validation\Factory : \Illuminate\Contracts\Validation\Validator)
*/
function validator(array $data = [], array $rules = [], array $messages = [], array $attributes = [])
function validator(?array $data = null, array $rules = [], array $messages = [], array $attributes = [])
{
$factory = app(ValidationFactory::class);

if (func_num_args() === 0) {
return $factory;
}

return $factory->make($data, $rules, $messages, $attributes);
return $factory->make($data ?? [], $rules, $messages, $attributes);
}
}

Expand All @@ -1027,7 +1036,7 @@ function validator(array $data = [], array $rules = [], array $messages = [], ar
* @param string|null $view
* @param \Illuminate\Contracts\Support\Arrayable|array $data
* @param array $mergeData
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
* @return ($view is null ? \Illuminate\Contracts\View\Factory : \Illuminate\Contracts\View\View)
*/
function view($view = null, $data = [], $mergeData = [])
{
Expand Down
Loading

0 comments on commit 5d006a5

Please sign in to comment.