PHPackages                             cleatsquad/php-state-decay - 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. cleatsquad/php-state-decay

ActiveLibrary

cleatsquad/php-state-decay
==========================

Temporal exponential half-life decay for counters and scores, in dependency-free PHP

v1.0.0(today)01↑2900%[1 issues](https://github.com/CleatSquad/php-state-decay/issues)[2 PRs](https://github.com/CleatSquad/php-state-decay/pulls)MITPHPPHP &gt;=8.2CI passing

Since Aug 14Pushed todayCompare

[ Source](https://github.com/CleatSquad/php-state-decay)[ Packagist](https://packagist.org/packages/cleatsquad/php-state-decay)[ Docs](https://github.com/cleatSquad/php-state-decay)[ RSS](/packages/cleatsquad-php-state-decay/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (2)Used By (0)

PHP State Decay
===============

[](#php-state-decay)

[![License: MIT](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)[![PHP Version](https://camo.githubusercontent.com/a04c4ba987a1efdcd8ce8fe345636b2eef0a31fdddb79b9b3f654dd38e4b2fb9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e322d3737376262342e737667)](composer.json)

Exponential half-life decay for counters and scores, in dependency-free PHP.

Evidence gathered a year ago should not weigh as much as evidence gathered yesterday. This library computes that discount: after one half-life a value is worth half of what it was, after two it is worth a quarter, and so on.

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

[](#installation)

```
composer require cleatsquad/php-state-decay
```

Requires PHP 8.2 or later. No runtime dependencies.

Usage
-----

[](#usage)

### Decay a score

[](#decay-a-score)

```
use CleatSquad\StateDecay\DecayConfig;
use CleatSquad\StateDecay\HalfLifeDecay;

$decay = new HalfLifeDecay(new DecayConfig(halfLifeDays: 30));

$decay->applyToValue(100.0, 30 * 86400); // 50.0
$decay->applyToValue(100.0, 60 * 86400); // 25.0
```

### Decay integer counters

[](#decay-integer-counters)

```
$counters = ['successes' => 40, 'failures' => 10];

$decay->applyToCounters($counters, 30 * 86400);
// ['successes' => 20, 'failures' => 5]
```

Counters are rounded, not floored. A pass that runs every hour would otherwise floor barely-aged evidence down to nothing, one unit at a time.

### Read the multiplier directly

[](#read-the-multiplier-directly)

```
$decay->factor(0);             // 1.0
$decay->factor(30 * 86400);    // 0.5
$decay->factor(-3600);         // 1.0 — clock skew never resurrects state
```

### Short half-lives

[](#short-half-lives)

Days are the usual unit, but anything shorter is expressible:

```
DecayConfig::fromSeconds(1800);  // 30 minutes
DecayConfig::fromDays(0.25);     // 6 hours
DecayConfig::never();            // decay disabled; factor() stays 1.0
```

Design notes
------------

[](#design-notes)

**Elapsed time is the caller's business.** Nothing here reads a clock. You pass the number of seconds that went by, which keeps the library free of any time dependency and makes every test deterministic. If you use a PSR-20 clock, take the difference yourself and hand it over.

**Time never runs backwards.** A negative or zero elapsed time yields a factor of `1.0`. Clock skew between machines cannot make aged state young again.

**Counters cannot be owed.** A negative count is treated as `0` rather than propagated.

**Invalid configuration fails loudly.** A negative, `NAN` or `INF` half-life throws `InvalidArgumentException` instead of silently poisoning every later computation.

When to use it
--------------

[](#when-to-use-it)

Good fits: recency ranking, reputation and trust scores, abuse and rate heuristics, health scores, recommendation weights — anywhere accumulated evidence should fade rather than be dropped by a cutoff.

Poor fits: you need a hard expiry (use a TTL), or a fixed-size sliding window (count within the window instead).

Testing
-------

[](#testing)

```
composer install
composer test      # PHPUnit
composer analyse   # PHPStan, max level
```

License
-------

[](#license)

MIT. See [LICENSE](LICENSE).

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance100

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity45

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

0d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/75b8cb31786be9b4017a0c617eebe3a0cd3b8d039069ffad5bb5007b5510fd9d?d=identicon)[mimou78](/maintainers/mimou78)

---

Top Contributors

[![mohaelmrabet](https://avatars.githubusercontent.com/u/3817628?v=4)](https://github.com/mohaelmrabet "mohaelmrabet (1 commits)")

---

Tags

statisticscountershalf-lifedecayexponential-decaytime-decayrecency

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/cleatsquad-php-state-decay/health.svg)

```
[![Health](https://phpackages.com/badges/cleatsquad-php-state-decay/health.svg)](https://phpackages.com/packages/cleatsquad-php-state-decay)
```

###  Alternatives

[markrogoyski/math-php

Math Library for PHP. Features descriptive statistics and regressions; Continuous and discrete probability distributions; Linear algebra with matrices and vectors, Numerical analysis; special mathematical functions; Algebra

2.4k7.8M58](/packages/markrogoyski-math-php)[wnx/laravel-stats

Get insights about your Laravel Project

1.7k1.9M7](/packages/wnx-laravel-stats)[rubix/tensor

A library and extension that provides objects for scientific computing in PHP.

2801.6M5](/packages/rubix-tensor)[szymach/c-pchart

Port of "pChart" library into PHP 8+

1513.3M32](/packages/szymach-c-pchart)[hi-folks/statistics

PHP package that provides functions for calculating mathematical statistics of numeric data.

403126.0k](/packages/hi-folks-statistics)[bilfeldt/laravel-route-statistics

Log statistics about route usage per user/team

240154.2k2](/packages/bilfeldt-laravel-route-statistics)

PHPackages © 2026

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