Download the PHP package smuuf/better-php-exceptions without Composer
On this page you can find all versions of the php package smuuf/better-php-exceptions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download smuuf/better-php-exceptions
More information about smuuf/better-php-exceptions
Files in smuuf/better-php-exceptions
Package better-php-exceptions
Short Description Package providing better and improved PHP exceptions.
License MIT
Informations about the package better-php-exceptions
Better PHP Exceptions
What is it about?
This small project aims to provide improved user/developer experience for PHP developers working with native PHP exceptions (thrown by the PHP interpreter itself in various situations).
Better PHP Exceptions can be created from native exceptions via unified interface:
As an example scenario, both PHP 7.*
and PHP 8.0
throw TypeError
exceptions in three distinct scenarios:
There are three scenarios where a TypeError may be thrown. The first is where the argument type being passed to a function does not match its corresponding declared parameter type. The second is where a value being returned from a function does not match the declared function return type. The third is where an invalid number of arguments are passed to a built-in PHP function (strict mode only).
~ https://www.php.net/manual/en/class.typeerror.php
Sadly, PHP or the TypeError
itself does not provide any additional information about the types expected, types passed/returned, or at which position the passed wrong type was present during invocation.
Was it an argument type error? A return type error? What was it...!?
This information is present only in the exception message and it takes an extra parsing effort to get it. Not to mention the fact that the format of these messages changed from PHP 7
to PHP 8
and thus need different parsing rules...
This is where Better PHP Exceptions come in.
Example
Which PHP versions?
Even though PHP 8.0
was released near the end of 2020, PHP 7 is still used extensively and Better PHP Exceptions aim to provide better exceptions for both current major PHP versions (at least for now).
Which native PHP exceptions can be made better?
Currently only a handful (in the beginning it was just a single one, since I needed that for my other project: Primi language: A scripting language written in PHP
). But the system is built to support easy addition of more better exceptions.
So... which ones?
-
\TypeError
can be converted into:\Smuuf\BetterExceptions\Types\ArgumentTypeError
- Provides methods:
getExpected()
: Returns a list of strings of expected argument types.getActual()
: Returns actual argument type as string.
- Provides methods:
\Smuuf\BetterExceptions\Types\ReturnTypeError
- Same as
\Smuuf\BetterExceptions\Types\ArgumentTypeError
.
- Same as
\Error
can be converted into:\Smuuf\BetterExceptions\Types\UnknownNamedParameterError
- Provides methods:
getParameterName()
: Returns the name of unknown parameter as string.
- Provides methods:
\ArgumentCountError
can be converted into:\Smuuf\BetterExceptions\Types\ArgumentCountError
- Provides methods:
getExpected()
: Returns expected number of arguments.getActual()
: Returns the actual number of arguments.
- Provides methods: