PHPackages                             yohang/finite - 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. yohang/finite

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

yohang/finite
=============

A simple PHP Finite State Machine

2.0.0(6mo ago)1.3k3.6M↓40%18510MITPHPPHP &gt;=8.1CI passing

Since Mar 4Pushed 6mo ago51 watchersCompare

[ Source](https://github.com/yohang/Finite)[ Packagist](https://packagist.org/packages/yohang/finite)[ Docs](https://github.com/yohang/Finite)[ RSS](/packages/yohang-finite/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (15)Versions (29)Used By (10)

Finite, A Simple PHP Finite State Machine
=========================================

[](#finite-a-simple-php-finite-state-machine)

Finite is a Simple State Machine, written in PHP. It can manage any Stateful object by defining states and transitions between these states.

As of version 2, Finite is a low-deps and lightweight state machine library, thanks to the use of PHP Enums.

[![CI Status](https://github.com/yohang/finite/actions/workflows/ci.yml/badge.svg)](https://github.com/yohang/finite/actions/workflows/ci.yml/badge.svg)[![Mutation testing badge](https://camo.githubusercontent.com/5d7605deb27515d1cf4e4b96d25483c028560fa1aa193ec6230285add6d42af0/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246796f68616e6725324646696e6974652532466d61696e)](https://dashboard.stryker-mutator.io/reports/github.com/yohang/Finite/main)

Features
--------

[](#features)

- Manage State/Transition graph for an object
- Attach business logic to states
- Listen to transitions between state to trigger your domain code
- Symfony integration
- Twig Extension

Getting started
---------------

[](#getting-started)

### Installation (via composer)

[](#installation-via-composer)

```
 $ composer req yohang/finite
```

### Define your state enum

[](#define-your-state-enum)

```
enum DocumentState: string implements State
{
    case DRAFT = 'draft';
    case PUBLISHED = 'published';
    case REPORTED = 'reported';
    case DISABLED = 'disabled';

    public static function getTransitions(): array
    {
        return [
            new Transition('publish', [self::DRAFT], self::PUBLISHED),
            new Transition('clear', [self::REPORTED, self::DISABLED], self::PUBLISHED),
            new Transition('report', [self::PUBLISHED], self::REPORTED),
            new Transition('disable', [self::REPORTED, self::PUBLISHED], self::DISABLED),
        ];
    }
}
```

### Define your Stateful Object

[](#define-your-stateful-object)

Your stateful object just need to have a state property

```
class Document
{
    private DocumentState $state = DocumentState::DRAFT;

    public function getState(): DocumentState
    {
        return $this->state;
    }

    public function setState(DocumentState $state): void
    {
        $this->state = $state;
    }
}
```

### Initializing a simple StateMachine

[](#initializing-a-simple-statemachine)

```
use Finite\StateMachine;

$document = new Document;

$sm = new StateMachine;

// Can we process a transition ?
$sm->can($document, 'publish');

// Apply a transition
$sm->apply($document, 'publish');
```

### Add business logic to states

[](#add-business-logic-to-states)

Finite &lt; 2.0 had properties on states. A metadata mechanism that allowed to add business properties on states to define the behavior of on object, depending on its state.

The idea behind this was to avoid to test the state in your domain code (A controller must not throw a 404 if the state is draft. But it can throw a 404 if the current state does not have a "visible" property. That was the idea).

Finite 2 does not needs this. PHP Enums can have methods. So, replace your properties with simple methods on your state.

```
enum DocumentState: string implements State
{
    // ...

    public function isDeletable(): bool
    {
        return in_array($this, [self::DRAFT, self::DISABLED]);
    }

    public function isPrintable(): bool
    {
        return in_array($this, [self::PUBLISHED, self::REPORTED]);
    }
}
```

After that, you can use theses methods on your object, even without instantiating the state machine.

```
var_dump($document->getState()->isDeletable());
var_dump($document->getState()->isPrintable());
```

### Events &amp; Guards

[](#events--guards)

Finite dispatches PSR-14 events. You can listen to them to add custom logic or block transitions.

```
use Finite\Event\CanTransitionEvent;
use Finite\Event\PostTransitionEvent;

$dispatcher = $stateMachine->getDispatcher();

// 1. Guard: Prevent a transition dynamically
$dispatcher->addEventListener(CanTransitionEvent::class, function (CanTransitionEvent $event) {
    if ('publish' === $event->getTransition()->getName() && !$event->getObject()->title) {
        // Block the transition if the title is empty
        $event->blockTransition();
    }
});

// 2. Post-Action: Do something after a transition
$dispatcher->addEventListener(PostTransitionEvent::class, function (PostTransitionEvent $event) {
    // e.g. Send an email, log activity...
    echo 'Transition ' . $event->getTransition()->getName() . ' completed!';
});
```

### Symfony Integration

[](#symfony-integration)

Register the bundle in `config/bundles.php`:

```
return [
    // ...
    Finite\Extension\Symfony\Bundle\FiniteBundle::class => ['all' => true],
];
```

This automatically registers the StateMachine service and the Twig extension.

#### Twig Helper

[](#twig-helper)

```
{# Check if a transition is available #}
{% if finite_can(document, 'publish') %}
    Publish
{% endif %}

{# List all reachable transitions #}

    {% for transition in finite_reachable_transitions(document) %}
        {{ transition.name }}
    {% endfor %}

```

#### Console Command

[](#console-command)

Dump your graph to visualize it:

```
php bin/console finite:state-machine:dump "App\State\DocumentState" mermaid
```

License
-------

[](#license)

This library is licensed under the MIT License.

###  Health Score

68

—

FairBetter than 99% of packages

Maintenance67

Regular maintenance activity

Popularity67

Solid adoption and visibility

Community41

Growing community involvement

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 82% 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 ~202 days

Recently: every ~333 days

Total

24

Last Release

190d ago

Major Versions

1.3.x-dev → 2.0.02025-12-01

PHP version history (7 changes)1.0.0-BETA1PHP &gt;=5.3.0

1.1.0PHP &gt;=5.3.9

1.1.1PHP &gt;=5.5

1.2.0PHP &gt;=5.6

1.3.0PHP &gt;7.4

1.3.1PHP &gt;=7.4

2.0.0PHP &gt;=8.1

### Community

Maintainers

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

---

Top Contributors

[![yohang](https://avatars.githubusercontent.com/u/608984?v=4)](https://github.com/yohang "yohang (196 commits)")[![winzou](https://avatars.githubusercontent.com/u/702928?v=4)](https://github.com/winzou "winzou (10 commits)")[![navitronic](https://avatars.githubusercontent.com/u/180519?v=4)](https://github.com/navitronic "navitronic (6 commits)")[![Padam87](https://avatars.githubusercontent.com/u/776488?v=4)](https://github.com/Padam87 "Padam87 (4 commits)")[![RonRademaker](https://avatars.githubusercontent.com/u/2697738?v=4)](https://github.com/RonRademaker "RonRademaker (4 commits)")[![tortuetorche](https://avatars.githubusercontent.com/u/5038872?v=4)](https://github.com/tortuetorche "tortuetorche (2 commits)")[![liuggio](https://avatars.githubusercontent.com/u/530406?v=4)](https://github.com/liuggio "liuggio (2 commits)")[![defrag](https://avatars.githubusercontent.com/u/15900?v=4)](https://github.com/defrag "defrag (1 commits)")[![nick-potts](https://avatars.githubusercontent.com/u/1109914?v=4)](https://github.com/nick-potts "nick-potts (1 commits)")[![nidup](https://avatars.githubusercontent.com/u/2104359?v=4)](https://github.com/nidup "nidup (1 commits)")[![acorncom](https://avatars.githubusercontent.com/u/802505?v=4)](https://github.com/acorncom "acorncom (1 commits)")[![pborreli](https://avatars.githubusercontent.com/u/77759?v=4)](https://github.com/pborreli "pborreli (1 commits)")[![pierreboissinot](https://avatars.githubusercontent.com/u/6716686?v=4)](https://github.com/pierreboissinot "pierreboissinot (1 commits)")[![realshadow](https://avatars.githubusercontent.com/u/6673222?v=4)](https://github.com/realshadow "realshadow (1 commits)")[![reiz](https://avatars.githubusercontent.com/u/652130?v=4)](https://github.com/reiz "reiz (1 commits)")[![arikal](https://avatars.githubusercontent.com/u/3074809?v=4)](https://github.com/arikal "arikal (1 commits)")[![tompedals](https://avatars.githubusercontent.com/u/6132043?v=4)](https://github.com/tompedals "tompedals (1 commits)")[![vitsadecky](https://avatars.githubusercontent.com/u/1543615?v=4)](https://github.com/vitsadecky "vitsadecky (1 commits)")[![dannykopping](https://avatars.githubusercontent.com/u/373762?v=4)](https://github.com/dannykopping "dannykopping (1 commits)")[![K-Phoen](https://avatars.githubusercontent.com/u/66958?v=4)](https://github.com/K-Phoen "K-Phoen (1 commits)")

---

Tags

symfonybundlestateworkflowstatemachinetransition

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/yohang-finite/health.svg)

```
[![Health](https://phpackages.com/badges/yohang-finite/health.svg)](https://phpackages.com/packages/yohang-finite)
```

###  Alternatives

[symfony/workflow

Provides tools for managing a workflow or finite state machine

62944.6M238](/packages/symfony-workflow)[gomachan46/state-machine

simple state machine with annotations for PHP, inspired by AASM known as a Ruby state machine.

1896.3k](/packages/gomachan46-state-machine)[winzou/state-machine-bundle

Bundle for the very lightweight yet powerful PHP state machine

33910.6M17](/packages/winzou-state-machine-bundle)[web-auth/webauthn-framework

FIDO2/Webauthn library for PHP and Symfony Bundle.

51090.8k2](/packages/web-auth-webauthn-framework)[web-auth/webauthn-symfony-bundle

FIDO2/Webauthn Security Bundle For Symfony

66474.5k8](/packages/web-auth-webauthn-symfony-bundle)[madmind/state-machine-visualization-bundle

Visualization bundle for winzou/state-machine-bundle

1258.8k](/packages/madmind-state-machine-visualization-bundle)

PHPackages © 2026

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