Runs a callable, capturing all error messages, then provides a method to extract any errors raised during execution.

use Haldayne\Fox\CaptureErrors;
$helper = new CaptureErrors(function ($src, $dst) { return copy($src, $dst); });
if (false === $helper('foo', 'bar')) {
    throw new \RuntimeException($helper->getCapturedErrors()->pop()->get('message'));
}

PHP itself, as well as many libraries, rely upon triggered errors to notify developers of problems. Many frameworks provide methods to capure these errors globally and convert them to exceptions, but you do not always have access to those frameworks. Worse, you may be using code that deliberately silences errors.

Errors raised inside the CaptureErrors helper is pushed into a Map stack, with each element also a Map of the error details: error code, error string, file and line where raised, and an array context of variables set at time of error.

package Haldayne

 Methods

Create a new callable object that captures errors when the given code is invoked. By default, all errors (E_ALL) are captured. You can set which errors will be captured using the `setCapturedErrorTypes` method.

__construct(callable $code) 

Parameters

$code

callable

Invoke the helper. Passes any arguments into the callable given during helper construction. Returns any result from the callable.

__invoke() 

Get the captured errors.

getCapturedErrors() : \Haldayne\Boost\MapOfCollections

Returns

\Haldayne\Boost\MapOfCollections

Set the error types to capture. Acts as a filter: only those errors matching this value will be captured.

setCapturedErrorTypes(integer $capturedErrorTypes) : void

Parameters

$capturedErrorTypes

integer

One or more of the E_* error constants

Exceptions

\InvalidArgumentException If $capturedErrorTypes isn't an int

 Properties

 

$capturedErrorTypes

$capturedErrorTypes 

Default

null
 

$code

$code 

Default

null
 

$map

$map 

Default

null