PHPackages                             samlev/recursion-guard - 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. samlev/recursion-guard

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

samlev/recursion-guard
======================

A simple, zero-dependency mechanism for preventing infinite recursion in PHP

v1.0.0(1y ago)4736MITPHPPHP ^8.2

Since Sep 27Pushed 1y ago2 watchersCompare

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

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

Recursion Guard
===============

[](#recursion-guard)

A simple, zero dependency mechanism for preventing infinite recursion in PHP.

[![Build Status](https://github.com/samlev/recursion-guard/actions/workflows/tests.yml/badge.svg)](https://github.com/samlev/recursion-guard/actions)[![Total Downloads](https://camo.githubusercontent.com/a6dc1b6fc2475e5e8df415b19fa9bbf23484dbd5eea7163e044e3611c1e77e19/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73616d6c65762f726563757273696f6e2d6775617264)](https://packagist.org/packages/samlev/recursion-guard)[![Latest Stable Version](https://camo.githubusercontent.com/9271e1e84cdfa1f3330a63e014f3ca6742b240deac12a6fc24a4cc752a77348a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73616d6c65762f726563757273696f6e2d6775617264)](https://packagist.org/packages/samlev/recursion-guard)[![License](https://camo.githubusercontent.com/42339d7412ed57c1d19ffed8f6501ce5e539775508f56675bb3fa25a1e354a47/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73616d6c65762f726563757273696f6e2d6775617264)](https://packagist.org/packages/samlev/recursion-guard)

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

[](#installation)

```
composer require samlev/recursion-guard
```

Usage
-----

[](#usage)

You can prevent a function from being called recursively by using `RecursionGuard\Recurser::call()` to execute a callback and provide a default result if the function is called recursively within the callback.

```
function bozo_repeat(string $repeat = ''): string
{
    return RecursionGuard\Recurser::call(
        // The callback that we want to call
        fn () => bozo_repeat() . ' : ' . bozo_repeat(),
        // What to return if this function is called recursively
        $repeat ?: 'bozo(' . random_int(0, 100)) . ')';
    );
}

bozo_repeat(); // 'bozo(4) : bozo(4)'
bozo_repeat('foo'); // 'foo : foo'
bozo_repeat(); // 'bozo(88) : bozo(88)'
```

See [the documentation](docs/index.md) for more explanation, examples, and advanced usage.

Testing
-------

[](#testing)

You can run individual test suites using composer commands:

```
# Static Analysis with phpstan
composer test:types

# Architecture tests
composer test:architecture
# Documentation tests (tests that cover any code in the documentation)
composer test:docs
# Feature/integration tests
composer test:feature
# Unit tests
composer test:unit

# Code coverage
composer test:coverage

# Mutation tests
composer test:mutate
```

Or you can run grouped tests:

```
# All code style checks
composer lint

# Static analysis and main test suites
composer test

# All checks that should be performed before a PR (linting, static analysis, and mutation tests)
composer test:pr
```

### Code Style

[](#code-style)

The code style for this project adheres to PSR-12, and can be checked using the following composer commands:

```
# Code Style checks with phpcs
composer lint:phpcs
# Code Style checks with PHP-CS-Fixer
composer lint:phpcsfixer

# Run all code style checks
composer lint
```

You can attempt to automatically fix a number of code style issues with the command:

```
composer lint:fix
```

Contributing
------------

[](#contributing)

Contributions via PR are welcome, either as complete changes (including tests), or as failing test cases.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity55

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

Every ~17 days

Total

4

Last Release

547d ago

Major Versions

v0.2.0 → v1.0.02024-11-18

### Community

Maintainers

![](https://www.gravatar.com/avatar/9714fd07ca3e7a95e46a4fb725311c82b57099c5a54dcdc908766639fd185e1b?d=identicon)[samlev](/maintainers/samlev)

---

Top Contributors

[![samlev](https://avatars.githubusercontent.com/u/462466?v=4)](https://github.com/samlev "samlev (46 commits)")

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/samlev-recursion-guard/health.svg)

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

###  Alternatives

[dholmes/bga-workbench

BoardGameArena Workbench

375.3k](/packages/dholmes-bga-workbench)[limingxinleo/x-phalcon-enum

a phalcon enum component

104.4k1](/packages/limingxinleo-x-phalcon-enum)

PHPackages © 2026

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