PHP code example of eboreum / exceptional

1. Go to this page and download the library: Download eboreum/exceptional library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

eboreum / exceptional example snippets




use Eboreum\Exceptional\ExceptionMessageGenerator;

class Foo377464ece90d4b918254101d596d90a8
{
    /**
     * @throws RuntimeException
     */
    public function bar(int $a, bool $b, ?string $c = null): string
    {
        throw new RuntimeException(ExceptionMessageGenerator::getInstance()->makeFailureInMethodMessage(
            $this,
            new ReflectionMethod(self::class, __FUNCTION__),
            func_get_args(),
        ));
    }
}

$foo = new Foo377464ece90d4b918254101d596d90a8();

try {
    $foo->bar(42, true);
} catch (RuntimeException $e) {
    echo $e->getMessage() . PHP_EOL;
}




use Eboreum\Exceptional\ExceptionMessageGenerator;

class Foo1ff07b0e563e4efbb5a5280f7fe412d8
{
    /**
     * @throws RuntimeException
     */
    public function bar(int $a, bool $b): string
    {
        throw new RuntimeException(ExceptionMessageGenerator::getInstance()->makeFailureInMethodMessage(
            $this,
            new ReflectionMethod(self::class, __FUNCTION__),
            func_get_args(),
        ));
    }
}

$foo = new Foo1ff07b0e563e4efbb5a5280f7fe412d8();

try {
    $foo->bar(42, true, null, 'hello');
} catch (RuntimeException $e) {
    echo $e->getMessage() . PHP_EOL;
}




use Eboreum\Exceptional\ExceptionMessageGenerator;

class Fooaea91664ed3d4467aeb2dfabb2623b53
{
    public const SOME_PARENT_CONSTANT = 42;
}

class Fooc261bae9da674d679de77a943ae57779 extends Fooaea91664ed3d4467aeb2dfabb2623b53
{
    public const SOME_CONSTANT = 3.14;

    /**
     * @throws RuntimeException
     */
    public function bar(
        float $a = self::SOME_CONSTANT,
        int $b = self::SOME_PARENT_CONSTANT,
        int $c = PHP_INT_MAX
    ): void {
        throw new RuntimeException(ExceptionMessageGenerator::getInstance()->makeFailureInMethodMessage(
            $this,
            new ReflectionMethod(self::class, __FUNCTION__),
            func_get_args(),
        ));
    }
}

$foo = new Fooc261bae9da674d679de77a943ae57779();

try {
    $foo->bar();
} catch (RuntimeException $e) {
    echo $e->getMessage() . PHP_EOL;
}




use Eboreum\Exceptional\ExceptionMessageGenerator;

class Foo1a7c13d6ce9f4646a120041e36717d5a
{
    /**
     * @throws RuntimeException
     */
    public static function bar(int $a): string
    {
        throw new RuntimeException(ExceptionMessageGenerator::getInstance()->makeFailureInMethodMessage(
            static::class,
            new ReflectionMethod(self::class, __FUNCTION__),
            func_get_args(),
        ));
    }
}


try {
    Foo1a7c13d6ce9f4646a120041e36717d5a::bar(42);
} catch (RuntimeException $e) {
    echo $e->getMessage() . PHP_EOL;
}




use Eboreum\Caster\Attribute\DebugIdentifier;
use Eboreum\Caster\Collection\Formatter\ObjectFormatterCollection;
use Eboreum\Caster\Contract\CasterInterface;
use Eboreum\Caster\Contract\DebugIdentifierAttributeInterface;
use Eboreum\Caster\Contract\Formatter\ObjectFormatterInterface;
use Eboreum\Caster\Contract\TextuallyIdentifiableInterface;
use Eboreum\Caster\Formatter\Object_\DebugIdentifierAttributeInterfaceFormatter;
use Eboreum\Caster\Formatter\Object_\TextuallyIdentifiableInterfaceFormatter;
use Eboreum\Exceptional\Caster;
use Eboreum\Exceptional\ExceptionMessageGenerator;

// Using TextuallyIdentifiableInterface

class Foo1990801ff8324df1b73e323d7fca71a8 implements TextuallyIdentifiableInterface
{
    protected int $id = 42;

    /**
     * @throws RuntimeException
     */
    public function bar(int $a): string
    {
        /** @var array<ObjectFormatterInterface> $formatters */
        $formatters = [new TextuallyIdentifiableInterfaceFormatter()];

        $caster = Caster::getInstance();
        $caster = $caster->withCustomObjectFormatterCollection(new ObjectFormatterCollection($formatters));

        $exceptionMessageGenerator = ExceptionMessageGenerator::getInstance()->withCaster($caster);

        throw new RuntimeException($exceptionMessageGenerator->makeFailureInMethodMessage(
            $this,
            new ReflectionMethod(self::class, __FUNCTION__),
            func_get_args(),
        ));
    }

    public function toTextualIdentifier(CasterInterface $caster): string
    {
        return sprintf(
            'My ID is: %d',
            $this->id,
        );
    }
}


$foo = new Foo1990801ff8324df1b73e323d7fca71a8();

try {
    $foo->bar(7);
} catch (RuntimeException $e) {
    echo $e->getMessage() . PHP_EOL;
}

/**
 * Using DebugIdentifierAttributeInterface
 */

class Foo31eda25b57e8456fb2b3e8158232b5e5 implements DebugIdentifierAttributeInterface
{
    #[DebugIdentifier]
    protected int $id = 42;

    /**
     * @throws RuntimeException
     */
    public function bar(int $a): string
    {
        /** @var array<ObjectFormatterInterface> $formatters */
        $formatters = [new DebugIdentifierAttributeInterfaceFormatter()];

        $caster = Caster::getInstance();
        $caster = $caster->withCustomObjectFormatterCollection(new ObjectFormatterCollection($formatters));

        $exceptionMessageGenerator = ExceptionMessageGenerator::getInstance()->withCaster($caster);

        throw new RuntimeException($exceptionMessageGenerator->makeFailureInMethodMessage(
            $this,
            new ReflectionMethod(self::class, __FUNCTION__),
            func_get_args(),
        ));
    }
}


$foo = new Foo31eda25b57e8456fb2b3e8158232b5e5();

try {
    $foo->bar(7);
} catch (RuntimeException $e) {
    echo $e->getMessage() . PHP_EOL;
}




use Eboreum\Exceptional\Caster;
use Eboreum\Exceptional\Formatting\DefaultFormatter;

$caster = Caster::getInstance();
$defaultFormatter = new DefaultFormatter($caster);

$throwable = new Exception('foo');

$result = $defaultFormatter->format($throwable);

echo $result;




use Eboreum\Caster\CharacterEncoding;
use Eboreum\Exceptional\Caster;
use Eboreum\Exceptional\Formatting\HTML5TableFormatter;

$caster = Caster::getInstance();
$characterEncoding = new CharacterEncoding('UTF-8');
$html5TableFormatter = new HTML5TableFormatter($caster, $characterEncoding);
$html5TableFormatter = $html5TableFormatter->withIsPrettyPrinting(true);

$throwable = new Exception('foo');

$result = $html5TableFormatter->format($throwable);

echo $result;




use Eboreum\Caster\CharacterEncoding;
use Eboreum\Exceptional\Caster;
use Eboreum\Exceptional\Formatting\JSONFormatter;

$caster = Caster::getInstance();
$characterEncoding = new CharacterEncoding('UTF-8');
$jsonFormatter = new JSONFormatter($caster, $characterEncoding);
$jsonFormatter = $jsonFormatter->withFlags(JSON_PRETTY_PRINT);

$throwable = new Exception('foo');

$result = $jsonFormatter->format($throwable);

echo $result;




use Eboreum\Exceptional\Caster;
use Eboreum\Exceptional\Formatting\OnelineFormatter;

$caster = Caster::getInstance();
$onelineFormatter = new OnelineFormatter($caster);

$throwable = new Exception('foo');

$result = $onelineFormatter->format($throwable);

echo $result;




use Eboreum\Caster\CharacterEncoding;
use Eboreum\Exceptional\Caster;
use Eboreum\Exceptional\Formatting\XMLFormatter;

$caster = Caster::getInstance();
$characterEncoding = new CharacterEncoding('UTF-8');
$xmlFormatter = new XMLFormatter($caster, $characterEncoding);
$xmlFormatter = $xmlFormatter->withIsPrettyPrinting(true);

$throwable = new Exception('foo');

$result = $xmlFormatter->format($throwable);

echo $result;

json
"php": "^8.3",
"eboreum/caster": "^2.1"

Failure in \Foo377464ece90d4b918254101d596d90a8->bar($a = (int) 42, $b = (bool) true, $c = (null) null) inside (object) \Foo377464ece90d4b918254101d596d90a8


Failure in \Fooc261bae9da674d679de77a943ae57779->bar($a = (float) 3.14, $b = (int) 42, $c = (int) 9223372036854775807) inside (object) \Fooc261bae9da674d679de77a943ae57779


\Exception
Message:
    foo
File: /some/file/path/script/misc/readme/formatter/example-1-defaultformatter.php
Line: 14
Code: 0\nStacktrace:\n    #0 /path/to/some/file.php:34: fake_function()\nPrevious: (None)

{
    "class": "\\Exception",
    "file": "\/some\/file\/path\/script\/misc\/readme\/formatter\/example-3-jsonformatter.php",
    "line": "17",
    "code": "0",
    "message": "foo",
    "stacktrace": "#0 \/path\/to\/some\/file.php:34: fake_function()"
    "previous": null
}
json
"eboreum/phpunit-with-consecutive-alternative": "^1.0",
"nette/neon": "^3.2",
"phpstan/phpstan": "^2.1.5",
"phpunit/phpunit": "^11.3",
"sebastian/diff": "^6.0",
"slevomat/coding-standard": "8.15.0",
"squizlabs/php_codesniffer": "3.10.2"

cd tests
php ../vendor/bin/phpunit