naugrim/laravel-strict-config-helper
最新稳定版本:1.2.0
Composer 安装命令:
composer require naugrim/laravel-strict-config-helper
包简介
Ensures your config returns the correct types
README 文档
README
Deprecation Notice
As Laravel 11+ supports using the Config-facade for the exact same purpose, this package will
not be maintained anymore.
Note though, that Laravel currently does not have an equivalent to the config_numeric function in this package.
Usage
This package is mainly intended to work together with PHPStan.
Laravel's helper config($key, $default) returns mixed. In higher PHPStan levels, you cannot perform
certain operations on mixed types.
Example:
https://phpstan.org/r/39022f87-cbae-4e7b-bb44-8a4a655d2f00
<?php $value = config('foo'); if (strpos($value, 'bar')) { // do something }
This produces the following error:
| Line | Message |
|---|---|
| 10 | Parameter #1 $haystack of function strpos expects string, mixed given |
With this package, you can do:
https://phpstan.org/r/0c0e3e10-2dbd-418b-966d-2930016d803d
$value = config_string('foo'); if (strpos($value, 'bar')) { // do something }
No error is returned, as $value is guaranteed to be a string.
A RuntimeException is thrown if the value in the config has the wrong type.
Note: The helper functions in this package cannot return NULL. So if the value is not set in your
config and you do not provide a $default value, an exception will be thrown.
Installation
composer require naugrim/laravel-strict-config-helper
Available Functions
config_string(string $key, ?string $default = null): string config_int(string $key, ?int $default = null): int config_float(string $key, ?float $default = null): float config_numeric(string $key, int | float | null $default = null): int | float config_bool(string $key, ?bool $default = null): bool
IDE config key autocompletion
If you are using the awesome Laravel IDEA plugin for PHPStorm, you can create a file named ide.json in
your project with the following content to have autocompletion of the config keys:
{
"$schema": "https://laravel-ide.com/schema/laravel-ide-v2.json",
"completions": [
{
"complete": "configKey",
"condition": [
{
"functionNames": ["config_bool", "config_float", "config_int", "config_numeric", "config_string"],
"parameters": [1]
}
]
}
]
}
naugrim/laravel-strict-config-helper 适用场景与选型建议
naugrim/laravel-strict-config-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.99k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 naugrim/laravel-strict-config-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 naugrim/laravel-strict-config-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-23