PHP Throws helpers. Short hand functions for throwing exceptions

1.0.0 2019-04-10 19:08 UTC

This package is auto-updated.

Last update: 2024-11-11 15:08:57 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

PHP Library which provides functions which Throws exception

Install

Via Composer

$ composer require aurimasniekis/throws

Usage

It is pretty simple all the functions are defined under Throws\\ namespace.

Simplest function is thr which accept exception as argument and just throws it:

<?php

use function Throws\thr;

thr(new Exception("FooBar"));

or u can throw specific exception with formatted string directly:

<?php

use function Throws\thrException;

thrException('Oh now "%s" error occurred', 'Big Error'); 

Available methods:

thr(new Exception('FooBar'));

thrBadFunctionCallException('FooBar');

thrBadMethodCallException('FooBar');

thrDomainException('FooBar');

thrInvalidArgumentException('FooBar');

thrLengthException('FooBar');

thrLogicException('FooBar');

thrOutOfBoundsException('FooBar');

thrOutOfRangeException('FooBar');

thrOverflowException('FooBar');

thrRangeException('FooBar');

thrRuntimeException('FooBar');

thrUnderflowException('FooBar');

thrUnexpectedValueException('FooBar');

thrErrorException('FooBar');

thrError('FooBar');

thrArgumentCountError('FooBar');

thrArithmeticError('FooBar');

thrAssertionError('FooBar');

thrDivisionByZeroError('FooBar');

thrCompileError('FooBar');

thrParseError('FooBar');

thrTypeError('FooBar');

Why?

You probably asking why this kind of library is even needed? Here is couple of examples:

// Instead of this
if ($foo !== 'bar') {
    throw new InvalidArgumentException(
        sprintf(
            'Oh no $foo is "%s"',
            $foo
        )
    );   
}

// Much simpler
$foo !== 'bar' || thrInvalidArgumentException('Oh no $foo is "%s"', $foo);

or this

// Instead of this
if (false === isset($array['key'])) {
    throw new InvalidArgumentException('Oh no key is missing');
} 

// Much simpler
$array['key'] ?? thrInvalidArgumentException('Oh no key is missing');
  

Testing

Run test cases

$ composer test

Run test cases with coverage (HTML format)

$ composer test-coverage

Run PHP style checker

$ composer check-style

Run PHP style fixer

$ composer fix-style

Contributing

Please see CONTRIBUTING and CONDUCT for details.

License

Please see License File for more information.