marick/laravel-strict

dev-main 2022-04-27 13:06 UTC

This package is auto-updated.

Last update: 2024-05-23 14:46:58 UTC


README

Laravel offers some handy helpers out-of-the-box. But their return types can make it difficult to combine it with static code analysis like PHPStan:

echo strlen(config('app.name'));

Parameter #1 $string of function strlen expects string, mixed given

This happens because the helper can theoretically return any type:

/**
 * Get / set the specified configuration value.
 *
 * 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  mixed  $default
 * @return mixed|\Illuminate\Config\Repository
 */
function config($key = null, $default = null)

This package wraps Laravel's helpers and adds nice methods that always expect a clear return type. If the method has a different return type, an exception will automatically be thrown, making it strict.

use function Marick\LaravelStrict\config;

echo strlen(config('app.name')->string());

Or use the global helper:

echo strlen(strictConfig('app.name'));

# Or in class form...
Helper::config('app.name')->string();