Skip to content
Snippets Groups Projects
Commit 30991ada authored by Jan Kuchař's avatar Jan Kuchař
Browse files

INITIAL COMMIT

parents
No related branches found
No related tags found
No related merge requests found
/vendor/
{
"name": "grifart/suppressed-exceptions",
"description": "Adds support for suppressed exceptions as known in Java. https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html#suppressed-exceptions",
"type": "library",
"require": {
"php": ">=7.1"
},
"require-dev": {
"nette/tester": "^2.1",
"phpstan/phpstan": "^0.10.5",
"jakub-onderka/php-parallel-lint": "^1.0",
"jakub-onderka/php-console-highlighter": "^0.4.0"
},
"license": "MIT",
"authors": [
{
"name": "Jan Kuchař",
"email": "honza.kuchar@grifart.cz"
}
],
"autoload": {
"psr-4": {
"Grifart\\SuppressedExceptions\\": "src"
}
}
}
<?php declare(strict_types=1);
namespace Grifart\SuppressedExceptions;
/**
* Simple implementation of WithSuppressedExceptions interface
*/
trait SuppressedExceptions /* implements WithSuppressedExceptions */
{
private $suppressedExceptions = [];
public function addSuppressed(\Throwable $e): void
{
$this->suppressedExceptions[] = $e;
}
public function getSuppressed(): array
{
return $this->suppressedExceptions;
}
}
\ No newline at end of file
<?php declare(strict_types=1);
namespace Grifart\SuppressedExceptions;
/**
* Provides interface similar to Java suppressed exceptions.
*
* @link https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#addSuppressed(java.lang.Throwable)
*/
interface WithSuppressedExceptions
{
/**
* Appends the specified exception to the exceptions that were suppressed in order to deliver this exception.
*
* Note that when one exception causes another exception, the first exception is usually caught and then the second exception is thrown in response.
* In other words, there is a causal connection between the two exceptions.
* In contrast, there are situations where two independent exceptions can be thrown in sibling code blocks.
* In these situations, only one of the thrown exceptions can be propagated.
*
* An exception may have suppressed exceptions while also being caused by another exception.
* Whether or not an exception has a cause is semantically known at the time of its creation,
* unlike whether or not an exception will suppress other exceptions which is typically only determined
* after an exception is thrown.
*
* Note that programmer written code is also able to take advantage of calling this method in situations
* where there are multiple sibling exceptions and only one can be propagated.
*
* @param \Throwable $e the exception to be added to the list of suppressed exceptions
*/
public function addSuppressed(\Throwable $e): void;
/**
* Returns an array containing all of the exceptions that were suppressed.
* If no exceptions were suppressed, an empty array is returned.
* This method is thread-safe. Writes to the returned array do not affect future calls to this method.
*
* @return \Throwable|array an array containing all of the exceptions that were suppressed to deliver this exception.
*/
public function getSuppressed(): array;
}
\ No newline at end of file
<?php declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
\Tester\Environment::setup();
<?php declare(strict_types=1);
namespace Grifart\SuppressedExceptions\__tests;
use Grifart\SuppressedExceptions\SuppressedExceptions;
use Grifart\SuppressedExceptions\WithSuppressedExceptions;
use Symfony\Component\Console\Exception\RuntimeException;
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
class TestingSuppressedExceptionsException extends \RuntimeException implements WithSuppressedExceptions
{
use SuppressedExceptions;
}
$previous = new TestingSuppressedExceptionsException('previous', -1, new RuntimeException());
$exception = new TestingSuppressedExceptionsException('message', 42, $previous);
$exception->addSuppressed($suppressed1 = new \RuntimeException());
$exception->addSuppressed($suppressed2 = new \LogicException());
$exception->addSuppressed($suppressed3 = new \Error());
$exception->addSuppressed($suppressed4 = new \Exception());
$exception->addSuppressed($suppressed5 = new TestingSuppressedExceptionsException());
// test that can be thrown
Assert::exception(function() use ($exception) {
throw $exception;
}, TestingSuppressedExceptionsException::class);
// previous
Assert::same($previous, $exception->getPrevious());
// suppressed exceptions
Assert::same(
[$suppressed1, $suppressed2, $suppressed3, $suppressed4, $suppressed5],
$exception->getSuppressed()
);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment