PHPackages                             margusk/warbsorber - 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. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. margusk/warbsorber

ActiveLibrary[Debugging &amp; Profiling](/categories/debugging)

margusk/warbsorber
==================

Provides functionality to catch PHP warnings during any function or method call.

0.1.0(3y ago)038MITPHPPHP ^8.0

Since Oct 31Pushed 3y ago1 watchersCompare

[ Source](https://github.com/marguskaidja/php-warbsorber)[ Packagist](https://packagist.org/packages/margusk/warbsorber)[ RSS](/packages/margusk-warbsorber/feed)WikiDiscussions main Synced 1mo ago

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

[![Tests](https://github.com/marguskaidja/php-warbsorber/actions/workflows/tests.yml/badge.svg)](https://github.com/marguskaidja/php-warbsorber/actions/workflows/tests.yml)

Warbsorber (*Warnings absorber*)
================================

[](#warbsorber-warnings-absorber)

#### The Problem

[](#the-problem)

Many built-in PHP functions emit warning messages in case of failures. It's done very dirty - echoing directly to output and catchable only when having [an special error handler installed](https://www.php.net/manual/en/function.set-error-handler).

Modern (proper) way to transfer failure details would be of course by using *Exceptions* but due legacy reasons that's not possible for lot's of old built-in functions and will probably never be "fixed".

#### The Solution

[](#the-solution)

This library provides a wrapper for executing arbitrary *codeblock*, which has tendency to spit out warnings, notices or errors.

All those messages are "absorbed" by the wrapper and returned nicely after the *codeblock* is executed for later inspection.

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

[](#requirements)

Requires:

- PHP &gt;= 8.0

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

[](#installation)

Install with composer:

```
composer require margusk/warbsorber
```

Usage
-----

[](#usage)

```
use function margusk\Warbsorber;

$fopenWarnings = Warbsorber(function () use (&$fp) {
    $fp = fopen("php://non-existent-stream", "r");
});

if (!$fp) {
    echo "fopen() failed with messages: \n";

    foreach ($fopenWarnings as $entry) {
        echo "- " . $entry->errStr . "\n";
    }
}
```

Output:

```
fopen() failed with messages:
- fopen(): Invalid php:// URL specified
- fopen(php://non-existent-stream): Failed to open stream: operation failed
```

The code above shows how we're calling [fopen](https://www.php.net/manual/en/function.fopen.php) which is destined to fail. Instead of just getting `false` as return value we also catch textual reasons why the call failed. Those reasons can be later presented to end user for fixing.

### Remove function name from the beginning of message

[](#remove-function-name-from-the-beginning-of-message)

What if we want to concatenate all messages into single paragraph? We already know that the messages are related to `fopen()` so message beginnings (`fopen():` and `fopen(php://non-existent-stream):`) are actually irrelevant and excessive information.

Let's get rid of them by using `removeFunctionPrefix(string $funcName)` method:

```
use function margusk\Warbsorber;

$fopenWarnings = Warbsorber(function () use (&$fp) {
    $fp = fopen("php://non-existent-stream", "r");
});

if (!$fp) {
    $fopenWarnings = $fopenWarnings->removeFunctionPrefix('fopen');

    echo "fopen() failed with: "
        .implode(
            '. ',
            array_map(
                fn(Entry $e) => $e->errStr,
                $fopenWarnings->getArrayCopy()
            )
        )
        .".\n";
}
```

Now the output is much cleaner:

```
fopen() failed with: Invalid php:// URL specified. Failed to open stream: operation failed.
```

**Note** that `$fopenWarnings` is immutable object, which makes `removeFunctionPrefix` to return cloned instance with modified data.

### Catch messages only with specific error levels

[](#catch-messages-only-with-specific-error-levels)

By default all messages regardless of level (e.g. `E_WARNING`, `E_NOTICE` etc.) are catched during codeblock execution. However this can be changed if needed by adding second parameter to `Warbsorber` call:

```
use function margusk\Warbsorber;

$fopenWarnings = Warbsorber(function () use (&$fp) {
    $fp = fopen("php://non-existent-stream", "r");
}, E_WARNING | E_NOTICE | E_DEPRECATED);
```

This catches only messages with level `E_WARNING`, `E_NOTICE` or `E_DEPRECATED`

Documentation
-------------

[](#documentation)

Main wrapper `Warbsorber(\Closure $callback, int $level = E_ALL): Warnings;` returns instance of `margusk\Warbsorber\Warnings`

### Class `margusk\Warbsorber\Warnings`

[](#class-marguskwarbsorberwarnings)

- Implements `IteratorAggregate`, `Countable` and `ArrayAccess`
- Each entry is instance of `margusk\Warbsorber\Entry`
- Method `getArrayCopy(): array` returns messages in native array
- Method `removeFunctionPrefix(string $funcName): static` removes specified function name from beginning of messages and returns cloned instance of itself

### Class `margusk\Warbsorber\Entry`

[](#class-marguskwarbsorberentry)

- Holds data for a single message:
    - `$entry->errNo` gets you error level
    - `$entry->errStr` gets you error message
    - `$entry->errFile` gets you file which triggered the error
    - `$entry->errLine` gets you line which triggered the error

License
-------

[](#license)

This library is released under the MIT License.

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity42

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

1287d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3fff9760e05300639ca4afdc39b8bf8fbefc325684ed35f736a4dfbea9f06a94?d=identicon)[margusk](/maintainers/margusk)

---

Top Contributors

[![marguskaidja](https://avatars.githubusercontent.com/u/47889592?v=4)](https://github.com/marguskaidja "marguskaidja (16 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/margusk-warbsorber/health.svg)

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

###  Alternatives

[symfony/stopwatch

Provides a way to profile code

2.8k387.2M915](/packages/symfony-stopwatch)[fruitcake/laravel-debugbar

PHP Debugbar integration for Laravel

19.1k662.9k28](/packages/fruitcake-laravel-debugbar)[spatie/ignition

A beautiful error page for PHP applications.

510147.6M69](/packages/spatie-ignition)[jokkedk/webgrind

Webgrind is a Xdebug profiling web frontend in PHP5. It implements a subset of the features of kcachegrind and installs in seconds and works on all platforms. For quick'n'dirty optimizations it does the job.

3.3k193.0k](/packages/jokkedk-webgrind)[koriym/printo

An object graph visualizer.

1421.8M2](/packages/koriym-printo)[soloterm/dumps

A Laravel command to intercept dumps from your Laravel application.

125285.7k3](/packages/soloterm-dumps)

PHPackages © 2026

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