PHPackages                             jasonmccreary/orfail - 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. jasonmccreary/orfail

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

jasonmccreary/orfail
====================

PHP orFail Trait.

2.0.0(10y ago)922MITPHPPHP &gt;=5.4.0

Since May 29Pushed 10y ago1 watchersCompare

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

READMEChangelogDependencies (1)Versions (4)Used By (0)

PHP OrFail Trait
================

[](#php-orfail-trait)

OrFail is a simple [trait](http://php.net/manual/en/language.oop5.traits.php) which makes it easier to practice exceptional programming and write cleaner code.

Through [magic](http://php.net/manual/en/language.oop5.magic.php), OrFail allows you to append `OrFail` to any method name and force it to *fail* (throw an exception) if that method returns a [falsey value](http://php.net/manual/en/language.types.boolean.php).

Code without OrFail:

```
public function someMethod() {
    $value = $this->getValue();

    if ($value === null) {
        // failure code
    }

    return $value;
}

```

The same code using OrFail:

```
public function someMethod() {
    return $this->getValueOrFail();
}

```

Requirements
------------

[](#requirements)

- PHP &gt;= 5.4.0

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

[](#installation)

The recommended way to install OrFail is with [Composer](https://getcomposer.org):

```
composer require "jasonmccreary/orfail"

```

Alternatively you can download the `src` directory of this project and include it in your project.

Usage
-----

[](#usage)

Once OrFail is included in your project you may add it to any class by simply using the trait.

For example:

```
class Example {
    use OrFail\Traits\OrFail;

    public function someMethod() {
        // code
    }
}

```

Now you can call any method with *OrFail* appended. If the method returns a falsey value a `FailingReturnValue` exception will be thrown.

Configuration
-------------

[](#configuration)

OrFail has two methods you may optionally override: `orFailTest()` and `allowedOrFailMethods()`.

```
bool orFailTest ( mixed $value )

```

By default `orFailTest()` simply tests if `$value` is *falsey*. You can override this method to perform your own failure test.

```
array allowedOrFailMethods ( void )

```

By default `orFailMethods()` returns an empty array which allows all methods. You can restrict which methods allow being called with `OrFail` appended by overriding this method and returning an array of the allowed method names.

Troubleshooting
---------------

[](#troubleshooting)

Since OrFail uses magic methods, it is easy to create an infinite call loop if you are not careful. This most commonly results in PHP exhausting its memory or a *Segmentation Fault 11*.

If you wish to use the OrFail trait in a class that implements `__call()` you will need to resolve the conflict manually.

For example:

```
class YourClass {
    use OrFail\Traits\OrFail {
        OrFail::__call as __callOrFail;
    }

    public function __call($name, $parameters) {
        // your code
        $this->__callOrFail($name, $parameters);
        // your code
    }
}

```

**Note:** this is only an example of how to resolve the conflict. How you call OrFail will depend on your code.

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

[](#contributing)

OrFail is a new package and needs more integration testing with other packages and codebases using `__call()`. Please report any problems by creating an [Issue](https://github.com/jasonmccreary/orfail/issues).

If you plan to submit a [Pull Request](https://github.com/jasonmccreary/orfail/pulls), please ensure you follow the [PSR-2 Style Guide](http://www.php-fig.org/psr/psr-2/).

Thanks.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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

Every ~2 days

Total

3

Last Release

4004d ago

Major Versions

1.0.1 → 2.0.02015-06-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/92a6bfc994b500ee991e6a7c581fb07af26b68b85821ff354bed0ac786fc9211?d=identicon)[jasonmccreary](/maintainers/jasonmccreary)

---

Top Contributors

[![jasonmccreary](https://avatars.githubusercontent.com/u/161071?v=4)](https://github.com/jasonmccreary "jasonmccreary (10 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jasonmccreary-orfail/health.svg)

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

###  Alternatives

[phpdocumentor/reflection

Reflection library to do Static Analysis for PHP Projects

12521.4M109](/packages/phpdocumentor-reflection)[spatie/array-functions

Some handy array helpers

24561.1k2](/packages/spatie-array-functions)[cekurte/environment

A library to get the values from environment variables and process to php data types

5884.0k7](/packages/cekurte-environment)[forxer/gravatar

A library providing easy gravatar integration.

35202.0k8](/packages/forxer-gravatar)[netgen/layouts-ui

Netgen Layouts user interface

17110.8k2](/packages/netgen-layouts-ui)[ebidtech/collection

A set of interfaces and traits to speed up the creation of collections

13119.8k6](/packages/ebidtech-collection)

PHPackages © 2026

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