PHPackages                             zsolt/guard-clauses - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. zsolt/guard-clauses

ActiveLibrary

zsolt/guard-clauses
===================

Guard clauses for php

1.0.0(3y ago)03PHPPHP ^8.0

Since Sep 28Pushed 3y ago1 watchersCompare

[ Source](https://github.com/zsolt0005/GuardClauses)[ Packagist](https://packagist.org/packages/zsolt/guard-clauses)[ RSS](/packages/zsolt-guard-clauses/feed)WikiDiscussions main Synced 4w ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

Guard Clauses
=============

[](#guard-clauses)

A simple package with guard clause.
A [guard clause](https://deviq.com/design-patterns/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.

Installation
------------

[](#installation)

`composer require zsolt/guard-clauses`

Give a Star! ⭐
--------------

[](#give-a-star-)

If you like or are using this project please give it a star. Thanks!

Usage
-----

[](#usage)

```
function sendEmailToCustomer(Customer $customer): void
{
    $email = GuardAgainst::null($customer->getEmail());

    // ...
}

final class Customer
{
    private string $firstName;
    private string $lastName;
    private int $age;
    private string $login;
    private ?string $email;

    function __construct(
        string $firstName,
        string $lastName,
        int $age,
        string $login,
        ?string $email = null
    )
    {
        $this->firstName = GuardAgainst::emptyOrWhiteSpace($firstName);
        $this->lastName = GuardAgainst::emptyOrWhiteSpace($lastName);
        $this->age = GuardAgainst::negative($age);
        $this->login = GuardAgainst::match($login, "admin", "login", "You can not be an admin");
        $this->email = $email;
    }
}
```

Supported Guard Clauses
-----------------------

[](#supported-guard-clauses)

```
GuardAgainst::null(); // Guard against null values
GuardAgainst::true(); // Guard against true values
GuardAgainst::nullOrTrue(); // Guard against null or true values
GuardAgainst::false(); // Guard against false values
GuardAgainst::nullOrFalse(); // Guard against null or false values
GuardAgainst::negative(); // Guard against numbers less than zero
GuardAgainst::positive(); // Guard against numbers more than zero
GuardAgainst::zero(); // Guard against the number zero
GuardAgainst::negativeOrZero(); // Guard against numbers less than or equal to zero
GuardAgainst::positiveOrZero(); // Guard against numbers more than or equal to zero
GuardAgainst::match(); // Guard against values that loosely match a specified value
GuardAgainst::strictMatch(); // Guard against values that strictly match a specified value
GuardAgainst::range(); // Guard against values that are in the specified range
GuardAgainst::notRange(); // Guard against values that are NOT in the specified range
GuardAgainst::empty(); // Guard against empty values
GuardAgainst::whiteSpace(); // Guard against string that contains only one white space
GuardAgainst::emptyOrWhiteSpace(); // Guard against string that are empty or contains only one white space
GuardAgainst::regex(); // Guard against string that matches the regex pattern
GuardAgainst::count(); // Guard against countable that matches a specific count
GuardAgainst::countOrMore(); // Guard against countable that is MORE or matches a specific count
GuardAgainst::countOrLess(); // Guard against countable that is LESS or matches a specific count
GuardAgainst::arrayHasValue(); // Guard against a value in the array
GuardAgainst::arrayHasNoValue(); // Guard against a value NOT in the array
GuardAgainst::type(); // Guard against the specified type type (gettype)
GuardAgainst::notType(); // Guard against not the specified type (gettype)
GuardAgainst::hasProperty(); // Guard against a property existing on an object
GuardAgainst::hasNoProperty(); // Guard against a property NOT existing on an object
GuardAgainst::expression(); // Guard against the expression returning true
```

###  Health Score

22

—

LowBetter than 23% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1318d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/24b79ac4dfce8acf5a9d8b8e8157a63b1e7d88e21dff39358d370e35910c7592?d=identicon)[zsolt0005](/maintainers/zsolt0005)

---

Top Contributors

[![zsolt0005](https://avatars.githubusercontent.com/u/60756804?v=4)](https://github.com/zsolt0005 "zsolt0005 (17 commits)")

---

Tags

guardclausephpphp-libraryphp8php81

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/zsolt-guard-clauses/health.svg)

```
[![Health](https://phpackages.com/badges/zsolt-guard-clauses/health.svg)](https://phpackages.com/packages/zsolt-guard-clauses)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
