PHPackages                             lukasss93/smatch - 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. lukasss93/smatch

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

lukasss93/smatch
================

Match for PHP 7.3 and PHP 7.4

v1.0(5y ago)32MITPHPPHP ^7.3|^7.4

Since Apr 30Pushed 5y ago1 watchersCompare

[ Source](https://github.com/Lukasss93/smatch)[ Packagist](https://packagist.org/packages/lukasss93/smatch)[ GitHub Sponsors](https://github.com/Lukasss93)[ RSS](/packages/lukasss93-smatch/feed)WikiDiscussions master Synced 6d ago

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

 [![](https://camo.githubusercontent.com/00fe832616191a56d03044fce56554e7b2625b1e7cd5295792752fa9a88cb3e6/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f536d617463682e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6c756b617373733933253246736d61746368267061747465726e3d636861726c696542726f776e267374796c653d7374796c655f31266465736372697074696f6e3d4d617463682b666f722b5048502b372e332b616e642b5048502b372e342b266d643d302673686f7757617465726d61726b3d3026666f6e7453697a653d313535707826696d616765733d6d656e752d616c742d32)](https://camo.githubusercontent.com/00fe832616191a56d03044fce56554e7b2625b1e7cd5295792752fa9a88cb3e6/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f536d617463682e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6c756b617373733933253246736d61746368267061747465726e3d636861726c696542726f776e267374796c653d7374796c655f31266465736372697074696f6e3d4d617463682b666f722b5048502b372e332b616e642b5048502b372e342b266d643d302673686f7757617465726d61726b3d3026666f6e7453697a653d313535707826696d616765733d6d656e752d616c742d32)

Smatch
======

[](#smatch)

[![Version](https://camo.githubusercontent.com/9ecb055fb38271e96fc984e49e259fc0843851bbafca783a31c47f64445b563e/68747470733a2f2f706f7365722e707567782e6f72672f6c756b6173737339332f736d617463682f762f737461626c65)](https://packagist.org/packages/lukasss93/smatch)[![Packagist Downloads](https://camo.githubusercontent.com/d8bc0218cce1839ef048fe986f0e5b2b4be77a2bc5e62d5d06112eb4666e0a6b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c756b6173737339332f736d61746368)](https://camo.githubusercontent.com/d8bc0218cce1839ef048fe986f0e5b2b4be77a2bc5e62d5d06112eb4666e0a6b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c756b6173737339332f736d61746368)[![PHP](https://camo.githubusercontent.com/86af8a500155215034de54ef323c09be7c0b1c98ab5e371a8d6822239b8bbae0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e33253230253743253230372e342d626c7565)](https://camo.githubusercontent.com/86af8a500155215034de54ef323c09be7c0b1c98ab5e371a8d6822239b8bbae0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e33253230253743253230372e342d626c7565)[![GitHub](https://camo.githubusercontent.com/30b24c632f7f62c8a60b92cfa36425df9ed79a2c248abcaa0e9db720bc82d7f8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c756b6173737339332f736d61746368)](https://camo.githubusercontent.com/30b24c632f7f62c8a60b92cfa36425df9ed79a2c248abcaa0e9db720bc82d7f8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6c756b6173737339332f736d61746368)[![Build](https://camo.githubusercontent.com/72f05cd66f3f2aa74766da6fc59669f43dec5611515604e9298a9f3a2cf7a023/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f4c756b6173737339332f736d617463682f72756e2d7465737473)](https://github.com/Lukasss93/smatch/actions/workflows/tests.yml)[![Codecov](https://camo.githubusercontent.com/6d52f4b549777a60e7eca849756d56d299f464d6bd62696e9f43c7edb473707f/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6c756b6173737339332f736d617463683f746f6b656e3d5532594e44544c384758)](https://codecov.io/gh/Lukasss93/smatch)

> Match for PHP 7.3 and PHP 7.4

🚀 Installation
--------------

[](#-installation)

You can install the package using composer:

```
composer require lukasss93/smatch
```

👓 Usage
-------

[](#-usage)

**Structure of a smatch function**

```
$result = smatch('apple')
    ->case('pear', 'tasty')
    ->case('apple', 'delicious')
    ->case('banana', 'yellow')
    ->get();

// $result = 'delicious'
```

**Case methods value can accept a closure too.**

```
$result = smatch('apple')
    ->case('pear', fn () => 'tasty')
    ->case('apple', fn () => 'delicious')
    ->case('banana', fn () => 'yellow')
    ->get();

// $result = 'delicious'
```

**Case method may contain an array of values**

```
$result = smatch('chair')
    ->case(['apple', 'pear', 'banana'], 'fruit')
    ->case(['table', 'chair'], 'furniture')
    ->get();

// $result = 'furniture'
```

**A special case is the fallback pattern. This pattern matches anything that wasn't previously matched.**

```
$result = smatch('strawberry')
    ->case('pear', 'tasty')
    ->case('apple', 'delicious')
    ->case('banana', 'yellow')
    ->fallback('invalid')
    ->get();

// $result = 'invalid'
```

**Fallback method can accept a closure too.**

```
$result = smatch('strawberry')
    ->case('pear', 'tasty')
    ->case('apple', 'delicious')
    ->case('banana', 'yellow')
    ->fallback(fn () => 'invalid')
    ->get();

// $result = 'invalid'
```

> Note: Multiple fallback methods will override the last one.

A smatch function must be exhaustive. If the subject function is not handled by any case method an UnhandledSmatchException is thrown.

**Example of an unhandled smatch function**

```
try {
    $result = smatch('strawberry')
        ->case('pear', 'tasty')
        ->case('apple', 'delicious')
        ->case('banana', 'yellow')
        ->get();
} catch (UnhandledSmatchException $e){
    echo $e->getMessage();
}

// $e->getMessage() = Unhandled smatch value of type string
```

**Using getOr method to handle missing fallback method**

```
$result = smatch('car')
    ->case('pear', 'tasty')
    ->case('apple', 'delicious')
    ->case('banana', 'yellow')
    ->getOr(fn () => 'complex logic');

// $result = 'complex logic'
```

#### Using smatch function to handle non identity checks

[](#using-smatch-function-to-handle-non-identity-checks)

It is possible to use a smatch function to handle non-identity conditional cases by using true as the subject function.

**Using a generalized smatch function to branch on integer ranges**

```
$age = 23;

$result = smatch(true)
    ->case($age >= 65, 'senior')
    ->case($age >= 25, 'adult')
    ->case($age >= 18, 'young adult')
    ->fallback('kid')
    ->get();

// $result = 'young adult'
```

**Using a generalized smatch function to branch on string content**

```
$text = 'Bienvenue chez nous';

$result = smatch(true)
    ->case(str_contains($text, 'Welcome') || str_contains($text, 'Hello'), 'en')
    ->case(str_contains($text, 'Bienvenue') || str_contains($text, 'Bonjour'), 'fr')
    ->get();

// $result = 'fr'
```

⚗️ Testing
----------

[](#️-testing)

```
composer test
```

📃 Changelog
-----------

[](#-changelog)

Please see the [CHANGELOG.md](CHANGELOG.md) for more information on what has changed recently.

🏅 Credits
---------

[](#-credits)

- [Luca Patera](https://github.com/Lukasss93)
- [All Contributors](https://github.com/Lukasss93/smatch/contributors)

📖 License
---------

[](#-license)

Please see the [LICENSE.md](LICENSE.md) file for more information.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

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

1843d ago

### Community

Maintainers

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

---

Top Contributors

[![Lukasss93](https://avatars.githubusercontent.com/u/4071613?v=4)](https://github.com/Lukasss93 "Lukasss93 (12 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lukasss93-smatch/health.svg)

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

###  Alternatives

[olifanton/ton

PHP library for The Open Network blockchain

8849.8k2](/packages/olifanton-ton)[bigwhoop/sentence-breaker

Sentence boundary disambiguation (SBD) - or sentence breaking - library written in PHP.

42132.3k](/packages/bigwhoop-sentence-breaker)[mwhite/random-uagent

A library for generating random User Agents

2353.4k2](/packages/mwhite-random-uagent)

PHPackages © 2026

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