awjudd/guard-clauses
最新稳定版本:0.5.0
Composer 安装命令:
composer require awjudd/guard-clauses
包简介
A simple package with guard clause helpers.
README 文档
README
A guard clause is a software pattern that simplifies complex functions by "failing fast", checking for invalid inputs up front and immediately failing if any are found.
Sample Usage
Avoiding Primative Obsession:
use JuddDev\GuardClauses\Guards\Numeric\IntegerGuard; class PositiveInteger { public function __construct(int $value) { IntegerGuard::isPositiveOrZero($value); } }
By doing this, you are then able to quickly make sure your objects are valid upon creation.
You can also use it in methods:
use JuddDev\GuardClauses\Guards\Numeric\IntegerGuard; class BankAccount { public function __construct(private int $balance) { } public function withdraw(int $amount): bool { IntegerGuard::isPositiveOrZero($amount); // Logic to remove } }
Why is this better? It reduces the overall nesting required in your code. While the below is a simple problem, you can see how it can propagate.
use JuddDev\GuardClauses\Guards\Numeric\IntegerGuard; class BankAccount { public function __construct(private int $balance) { } public function withdraw(int $amount): bool { if($amount <= 0) { return false; } // Logic to remove } }
统计信息
- 总下载量: 316
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-06-20