PHPackages                             webhappens/conditional-methods - 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. webhappens/conditional-methods

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

webhappens/conditional-methods
==============================

Make method calls conditional by simply appending If or Unless when you call them.

v0.3.0(5y ago)05.4k1MITPHPPHP ^7.3|^8.0CI failing

Since Feb 24Pushed 5y ago1 watchersCompare

[ Source](https://github.com/webhappens/conditional-methods)[ Packagist](https://packagist.org/packages/webhappens/conditional-methods)[ Docs](https://github.com/webhappens/conditional-methods)[ RSS](/packages/webhappens-conditional-methods/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (5)Used By (1)

[![tests](https://github.com/webhappens/conditional-methods/workflows/tests/badge.svg)](https://github.com/webhappens/conditional-methods/workflows/tests/badge.svg)

Conditional methods
===================

[](#conditional-methods)

Make method calls conditional by simply appending If or Unless when you call them. Conditional methods allow you to pass a condition to determine whether they should be run or not, removing the need to wrap them inside logic. This is especially useful when you are chaining.

- [Installation](#installation)
- [If](#if)
- [Unless](#unless)

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

[](#installation)

Install via composer:

```
composer require webhappens/conditional-methods
```

Insert the `ConditionalMethods` trait into your class:

```
use \WebHappens\ConditionalMethods\ConditionalMethods;
```

If your class is already using the `__call` method, add the following to it:

```
public function __call($method, $arguments)
{
    if ($type = static::matchConditionalMethod($method)) {
        return $this->callConditionalMethod($type, $method, $arguments);
    }

    // ...

    // throw new \BadMethodCallException();
}
```

If
--

[](#if)

Append `If` to the end of any method call and pass your condition as the first argument.

```
$insurer->renewIf($car->insuranceIsDue(), $car);
```

This replaces the need to do something like:

```
if ($car->insuranceIsDue()) {
    $insurer->renew($car);
}
```

Unless
------

[](#unless)

`Unless` works in the same way as `If` except the condition is inverted.

Append `Unless` to the end of any method call and pass your condition as the first argument.

```
$insurer->renewUnless($car->insuranceIsValid(), $car);
```

Credits
-------

[](#credits)

- Sam Leicester:
- Ben Gurney:
- Big thanks to the guys over at [Spatie](https://spatie.be) for the inspiration on this through their [laravel-html](https://github.com/spatie/laravel-html) package ❤️
- [All Contributors](../../contributors)

Our `Str` class is just a copy of some functions from Laravel's `Str` helper.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 52.9% 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 ~167 days

Total

3

Last Release

1932d ago

PHP version history (3 changes)v0.1.0PHP ^7.3

v0.2.0PHP ^7.1.3

v0.3.0PHP ^7.3|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/988de2ed821f544a3dcf054d14167b3921df7a87e07d98038d52bf4caf6347fc?d=identicon)[sleicester](/maintainers/sleicester)

![](https://www.gravatar.com/avatar/9b62c6a5d92ad11d42ffbb44772833c48e5ec3b21af4f9869628a0d0daa1800c?d=identicon)[bgurney](/maintainers/bgurney)

---

Top Contributors

[![sleicester](https://avatars.githubusercontent.com/u/319829?v=4)](https://github.com/sleicester "sleicester (9 commits)")[![bgurney](https://avatars.githubusercontent.com/u/6359858?v=4)](https://github.com/bgurney "bgurney (8 commits)")

---

Tags

webhappensconditional-methods

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/webhappens-conditional-methods/health.svg)

```
[![Health](https://phpackages.com/badges/webhappens-conditional-methods/health.svg)](https://phpackages.com/packages/webhappens-conditional-methods)
```

###  Alternatives

[fortawesome/wordpress-fontawesome

Official Font Awesome WordPress plugin composer package.

662.1k](/packages/fortawesome-wordpress-fontawesome)

PHPackages © 2026

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