awjudd/guard-clauses 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-20