PHPackages                             jdecool/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. jdecool/guard-clauses

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jdecool/guard-clauses
=====================

Guard clauses pattern implementation library

01PHP

Since Feb 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/jdecool/guard-clauses)[ Packagist](https://packagist.org/packages/jdecool/guard-clauses)[ RSS](/packages/jdecool-guard-clauses/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

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

[](#guard-clauses)

[![Latest Stable Version](https://camo.githubusercontent.com/5cb4a6dd9ffc261502525416c52a19a992cebe97bd413215730dd9ac71702ea2/68747470733a2f2f706f7365722e707567782e6f72672f6a6465636f6f6c2f67756172642d636c61757365732f762f737461626c65)](https://packagist.org/packages/jdecool/guard-clauses)[![Build Status](https://github.com/jdecool/guard-clauses/workflows/CI/badge.svg?ref=main)](https://github.com/jdecool/guard-clauses/actions?ref=main)[![Total Downloads](https://camo.githubusercontent.com/8e455a7cfa8f9eaeda94244ac95fa313319d5a92f9c01d9df6d0c981851ae4f4/68747470733a2f2f706f7365722e707567782e6f72672f6a6465636f6f6c2f67756172642d636c61757365732f646f776e6c6f616473)](https://packagist.org/packages/jdecool/guard-clauses)[![License](https://camo.githubusercontent.com/e50c8aec42bd61505dcd25cbac334161dcf294f0feb7319267a7f8509a036f48/68747470733a2f2f706f7365722e707567782e6f72672f6a6465636f6f6c2f67756172642d636c61757365732f6c6963656e7365)](https://packagist.org/packages/jdecool/guard-clauses)[![Latest Unstable Version](https://camo.githubusercontent.com/9260c09c66890461be4c9f4b67003b072a7fb14d62bc374e1b4ca070f29eba68/68747470733a2f2f706f7365722e707567782e6f72672f6a6465636f6f6c2f67756172642d636c61757365732f762f756e737461626c65)](https://packagist.org/packages/jdecool/guard-clauses)

A PHP library providing a clean and expressive way to implement guard clauses in your code. Built on top of [webmozart/assert](https://github.com/webmozart/assert), this library offers a fluent interface for input validation and defensive programming.

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

[](#installation)

You can install the package via Composer:

```
composer require jdecool/guard-clauses
```

Requirements
------------

[](#requirements)

- PHP 8.1 or higher

Usage
-----

[](#usage)

The library provides a `Guard` class with static methods for various assertions:

```
use JDecool\GuardClauses\Guard;

// Basic type checks
$validatedValue = Guard::string($value);
$validatedValue = Guard::integer($value);
$validatedValue = Guard::float($value);
$validatedValue = Guard::boolean($value);
$validatedValue = Guard::object($value);

// String validations
$validatedValue = Guard::stringNotEmpty($value);
$validatedValue = Guard::contains($string, $substring);
$validatedValue = Guard::startsWith($string, $prefix);
$validatedValue = Guard::endsWith($string, $suffix);

// Numeric comparisons
$validatedValue = Guard::greaterThan($number, $limit);
$validatedValue = Guard::lessThan($number, $limit);
$validatedValue = Guard::range($number, $min, $max);

// Array operations
$validatedValue = Guard::isArray($value);
$validatedValue = Guard::count($array, $expectedCount);
$validatedValue = Guard::keyExists($array, $key);

// And many more...
```

Each assertion method accepts an optional message parameter that will be used in the exception if the assertion fails:

```
$validatedValue = Guard::string($value, 'The value must be a string');
```

Error Handling
--------------

[](#error-handling)

When an assertion fails, a `GuardClausesException` is thrown, which extends from `InvalidArgumentException`.

Development
-----------

[](#development)

To contribute to the development of this library:

1. Clone the repository
2. Install dependencies: `composer install`
3. Run tests: `composer test`
4. Run static analysis: `composer lint`

License
-------

[](#license)

This package is open-sourced software licensed under the [MIT license](LICENSE).

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance33

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity16

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/a648f4457ad94c2fc48782bded875f3bf051b480fd2fe01eb7654b9fab180e5e?d=identicon)[jdecool](/maintainers/jdecool)

---

Top Contributors

[![jdecool](https://avatars.githubusercontent.com/u/433926?v=4)](https://github.com/jdecool "jdecool (6 commits)")

### Embed Badge

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

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

###  Alternatives

[pocketmine/math

PHP library containing math related code used in PocketMine-MP

45573.2k14](/packages/pocketmine-math)[nathancox/minify

Minifies CSS requirements using Minify (http://code.google.com/p/minify/)

28143.4k1](/packages/nathancox-minify)

PHPackages © 2026

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