PHPackages                             astrotomic/php-conditional-proxy - 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. astrotomic/php-conditional-proxy

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

astrotomic/php-conditional-proxy
================================

This package provides a trait and class to allow calling methods based on a condition without breaking the method chain.

0.3.0(2mo ago)31172.3k—1.8%1[1 issues](https://github.com/Astrotomic/php-conditional-proxy/issues)[1 PRs](https://github.com/Astrotomic/php-conditional-proxy/pulls)5MITPHPPHP ^7.1 || ^8.0

Since Jun 11Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/Astrotomic/php-conditional-proxy)[ Packagist](https://packagist.org/packages/astrotomic/php-conditional-proxy)[ Docs](https://github.com/Astrotomic/php-conditional-proxy)[ Fund](https://forest.astrotomic.info)[ GitHub Sponsors](https://github.com/Gummibeer)[ RSS](/packages/astrotomic-php-conditional-proxy/feed)WikiDiscussions main Synced yesterday

READMEChangelog (4)Dependencies (2)Versions (6)Used By (5)

PHP ConditionalProxy
====================

[](#php-conditionalproxy)

[![Latest Version](https://camo.githubusercontent.com/fb3bbb23b5814f0481e17ef1fbca4ec31b43ac7e8303e20b80557e3028ae2665/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f617374726f746f6d69632f7068702d636f6e646974696f6e616c2d70726f78792e7376673f6c6162656c3d52656c65617365267374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/astrotomic/php-conditional-proxy)[![MIT License](https://camo.githubusercontent.com/58181c8a71f6000bb4cfb76afa8e6f07bb530324e2f2fcf5697e28d73ed837ad/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f417374726f746f6d69632f7068702d636f6e646974696f6e616c2d70726f78792e7376673f6c6162656c3d4c6963656e736526636f6c6f723d626c7565267374796c653d666f722d7468652d6261646765)](https://github.com/Astrotomic/php-conditional-proxy/blob/master/LICENSE)[![Offset Earth](https://camo.githubusercontent.com/d204555ebe1fb0ae82d10c97b4f4ffc2dfdd2ba1489f98be7f7e8708333a0466/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54726565776172652d2546302539462538432542332d677265656e3f7374796c653d666f722d7468652d6261646765)](https://plant.treeware.earth/Astrotomic/php-conditional-proxy)[![Larabelles](https://camo.githubusercontent.com/a2c8d5126ddd8c5ddc627176d1d2e0568f8399b50038e71fd7f774c3e24dbe4b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726162656c6c65732d2546302539462541362538342d6c6967687470696e6b3f7374796c653d666f722d7468652d6261646765)](https://www.larabelles.com/)

[![GitHub Workflow Status](https://camo.githubusercontent.com/421c772dbfac37ef7a0a971928ba41d7d79b76d10cc9ef87584174c10442d09d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f417374726f746f6d69632f7068702d636f6e646974696f6e616c2d70726f78792f72756e2d74657374733f7374796c653d666c61742d737175617265266c6f676f436f6c6f723d7768697465266c6f676f3d676974687562266c6162656c3d5465737473)](https://github.com/Astrotomic/php-conditional-proxy/actions?query=workflow%3Arun-tests)[![StyleCI](https://camo.githubusercontent.com/bd755ec11b970daecaa2be79b582e4d9a29a29503922f396368a7a05b741c1e3/68747470733a2f2f7374796c6563692e696f2f7265706f732f3237313337353931322f736869656c64)](https://styleci.io/repos/271375912)[![Total Downloads](https://camo.githubusercontent.com/d46fa5a5f03b99d6e9d7d9476f594f3e48bade5505b06a6d1e6eb5974e937b6a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f617374726f746f6d69632f7068702d636f6e646974696f6e616c2d70726f78792e7376673f6c6162656c3d446f776e6c6f616473267374796c653d666c61742d737175617265)](https://packagist.org/packages/astrotomic/php-conditional-proxy)

This package provides a trait and class to allow calling methods based on a condition without breaking the method chain. This is useful if you want to call a method only if the variable you insert has a value.

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

[](#installation)

You can install the package via composer:

```
composer require astrotomic/php-conditional-proxy
```

Usage
-----

[](#usage)

The easiest will be to use the `\Astrotomic\ConditionalProxy\HasConditionalCalls` trait which adds a `when()` method. The `\Astrotomic\ConditionalProxy\ConditionalProxy` is made to work with methods returning `$this` because otherwise you can get unexpected results and wrong code-completion.

```
use Astrotomic\ConditionalProxy\HasConditionalCalls;

class MyClass
{
    use HasConditionalCalls;
}
```

### Conditional chained Method

[](#conditional-chained-method)

You can call the `when()` method by only passing the condition and chain the method to call if the condition is `true`.

```
// foo() bar() baz() will be called
$class->foo()->when(true)->bar()->baz();

// foo() baz() will be called
$class->foo()->when(false)->bar()->baz();
```

### Conditional Callback

[](#conditional-callback)

If you want you can also pass a callback as second parameter to the `when()` method.

```
// foo() bar() baz() will be called
$class->foo()->when(true, fn($self) => $self->bar())->baz();

// foo() baz() will be called
$class->foo()->when(false, fn($self) => $self->bar())->baz();
```

### Advanced Usage

[](#advanced-usage)

If you already have an `if()` or `when()` method and want to advance it or implement this behavior in any other method you can initialize and return the `\Astrotomic\ConditionalProxy\ConditionalProxy` yourself.

```
use Astrotomic\ConditionalProxy\ConditionalProxy;

class MyClass
{
    public function if($condition)
    {
        return new ConditionalProxy($this, $condition);
    }

    public function foo($foo)
    {
        $this->foo = $foo;

        return $this;
    }
}
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

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

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

[](#contributing)

Please see [CONTRIBUTING](https://github.com/Astrotomic/.github/blob/master/CONTRIBUTING.md) for details. You could also be interested in [CODE OF CONDUCT](https://github.com/Astrotomic/.github/blob/master/CODE_OF_CONDUCT.md).

### Security

[](#security)

If you discover any security related issues, please check [SECURITY](https://github.com/Astrotomic/.github/blob/master/SECURITY.md) for steps to report it.

Credits
-------

[](#credits)

- [Tom Witkowski](https://github.com/Gummibeer)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

Treeware
--------

[](#treeware)

You're free to use this package, but if it makes it to your production environment I would highly appreciate you buying the world a tree.

It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to [plant trees](https://www.bbc.co.uk/news/science-environment-48870920). If you contribute to my forest you’ll be creating employment for local families and restoring wildlife habitats.

You can buy trees at [offset.earth/treeware](https://plant.treeware.earth/Astrotomic/php-conditional-proxy)

Read more about Treeware at [treeware.earth](https://treeware.earth)

###  Health Score

54

—

FairBetter than 96% of packages

Maintenance84

Actively maintained with recent releases

Popularity43

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.3% 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 ~711 days

Total

4

Last Release

77d ago

PHP version history (2 changes)0.1.0PHP ^7.1

0.2.1PHP ^7.1 || ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6187884?v=4)[Tom Herrmann](/maintainers/Gummibeer)[@Gummibeer](https://github.com/Gummibeer)

---

Top Contributors

[![Gummibeer](https://avatars.githubusercontent.com/u/6187884?v=4)](https://github.com/Gummibeer "Gummibeer (55 commits)")[![szepeviktor](https://avatars.githubusercontent.com/u/952007?v=4)](https://github.com/szepeviktor "szepeviktor (5 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (2 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

---

Tags

hacktoberfesttreewareproxyconditional-proxy

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/astrotomic-php-conditional-proxy/health.svg)

```
[![Health](https://phpackages.com/badges/astrotomic-php-conditional-proxy/health.svg)](https://phpackages.com/packages/astrotomic-php-conditional-proxy)
```

###  Alternatives

[fideloper/proxy

Set trusted proxies for Laravel

7.3k176.5M561](/packages/fideloper-proxy)[ocramius/proxy-manager

A library providing utilities to generate, instantiate and generally operate with Object Proxies

5.0k83.1M242](/packages/ocramius-proxy-manager)[symfony/var-exporter

Provides tools to export, instantiate, hydrate, clone and lazy-load PHP objects

2.1k407.7M757](/packages/symfony-var-exporter)[friendsofphp/proxy-manager-lts

Adding support for a wider range of PHP versions to ocramius/proxy-manager

1.2k147.4M152](/packages/friendsofphp-proxy-manager-lts)[vkartaviy/retry

The library for repeatable and retryable operations

29231.3k2](/packages/vkartaviy-retry)

PHPackages © 2026

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