Skip to content

Commit 22ebfeb

Browse files
committed
Initial
0 parents  commit 22ebfeb

File tree

7 files changed

+622
-0
lines changed

7 files changed

+622
-0
lines changed

.github/workflows/acceptance.yml

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
name: Acceptance
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test-7-2:
7+
runs-on: ubuntu-latest
8+
name: Test PHP 7.2
9+
steps:
10+
- name: Checkout
11+
uses: actions/checkout@v2
12+
- name: Set up PHP 7.2
13+
uses: shivammathur/setup-php@v2
14+
with:
15+
php-version: '7.2'
16+
- name: Composer
17+
run: make deps-install
18+
- name: Test
19+
run: make test
20+
21+
test-7-3:
22+
runs-on: ubuntu-latest
23+
name: Test PHP 7.3
24+
steps:
25+
- name: Checkout
26+
uses: actions/checkout@v2
27+
- name: Set up PHP 7.3
28+
uses: shivammathur/setup-php@v2
29+
with:
30+
php-version: '7.3'
31+
- name: Composer
32+
run: make deps-install
33+
- name: Test
34+
run: make test
35+
36+
test-7-4:
37+
runs-on: ubuntu-latest
38+
name: Test PHP 7.4
39+
steps:
40+
- name: Checkout
41+
uses: actions/checkout@v2
42+
- name: Set up PHP 7.4
43+
uses: shivammathur/setup-php@v2
44+
with:
45+
php-version: '7.4'
46+
- name: Composer
47+
run: make deps-install
48+
- name: Test
49+
run: make test
50+
51+
test-8-0:
52+
runs-on: ubuntu-latest
53+
name: Test PHP 8.0
54+
steps:
55+
- name: Checkout
56+
uses: actions/checkout@v2
57+
- name: Set up PHP 8.0
58+
uses: shivammathur/setup-php@v2
59+
with:
60+
php-version: '8.0'
61+
- name: Composer
62+
run: make deps-install
63+
- name: Test
64+
run: make test
65+
66+
cs-check:
67+
runs-on: ubuntu-latest
68+
name: Code standard
69+
steps:
70+
- name: Checkout
71+
uses: actions/checkout@v2
72+
- name: Set up PHP 8.0
73+
uses: shivammathur/setup-php@v2
74+
with:
75+
php-version: '8.0'
76+
- name: Composer
77+
run: make deps-install
78+
- name: Code standard
79+
run: make cs-check
80+
81+
coverage:
82+
runs-on: ubuntu-latest
83+
name: Code coverage
84+
steps:
85+
- name: Checkout
86+
uses: actions/checkout@v2
87+
- name: Set up PHP 8.0
88+
uses: shivammathur/setup-php@v2
89+
with:
90+
php-version: '8.0'
91+
extensions: xdebug
92+
- name: Composer
93+
run: make deps-install
94+
- name: Code coverage
95+
env:
96+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
97+
run: make coverage

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
.phpunit.result.cache
3+
build/
4+
composer.lock
5+
composer.phar
6+
vendor/

Makefile

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Default
2+
all: deps-install
3+
4+
5+
# DEPENDENCY MANAGEMENT
6+
7+
# Updates dependencies according to lock file
8+
deps-install: composer.phar
9+
./composer.phar --no-interaction install
10+
11+
# Updates dependencies according to json file
12+
deps-update: composer.phar
13+
./composer.phar self-update
14+
./composer.phar --no-interaction update
15+
16+
17+
# TESTS AND REPORTS
18+
19+
# Code standard check
20+
cs-check: composer.lock
21+
./vendor/bin/phpcs --standard=PSR1,PSR12 --encoding=UTF-8 --report=full --colors src tests
22+
23+
# Run tests
24+
test: composer.lock
25+
./vendor/bin/phpunit
26+
27+
# Run tests with clover coverage report
28+
coverage: composer.lock
29+
XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
30+
./vendor/bin/php-coveralls -v
31+
32+
33+
# INITIAL INSTALL
34+
35+
# Ensures composer is installed
36+
composer.phar:
37+
curl -sS https://getcomposer.org/installer | php
38+
39+
# Ensures composer is installed and dependencies loaded
40+
composer.lock: composer.phar
41+
./composer.phar --no-interaction install

composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "phrity/util-errorhandler",
3+
"type": "library",
4+
"description": "Inline error handler; catch and resolve errors for code block.",
5+
"homepage": "https://phrity.sirn.se/util-errorhandler",
6+
"keywords": ["error", "warning"],
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Sören Jensen",
11+
"email": "[email protected]",
12+
"homepage": "https://phrity.sirn.se"
13+
}
14+
],
15+
"autoload": {
16+
"psr-4": {
17+
"": "src/"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"": "tests/"
23+
}
24+
},
25+
"require": {
26+
"php": "^7.2|^8.0",
27+
"psr/log": "^1 | ^2 | ^3"
28+
},
29+
"require-dev": {
30+
"phpunit/phpunit": "^8.0|^9.0",
31+
"php-coveralls/php-coveralls": "^2.0",
32+
"squizlabs/php_codesniffer": "^3.5"
33+
}
34+
}

phpunit.xml.dist

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit colors="true" bootstrap="vendor/autoload.php">
4+
<testsuites>
5+
<testsuite name="Phrity Util/ErrorHandler tests">
6+
<directory>./tests/</directory>
7+
</testsuite>
8+
</testsuites>
9+
10+
<filter>
11+
<whitelist>
12+
<directory suffix=".php">./src/</directory>
13+
</whitelist>
14+
</filter>
15+
</phpunit>

src/Phrity/Util/ErrorHandler.php

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
/**
4+
* File for ErrorHandler utility class.
5+
* @package Phrity > Util > ErrorHandler
6+
*/
7+
8+
namespace Phrity\Util;
9+
10+
use ErrorException;
11+
use Throwable;
12+
13+
/**
14+
* ErrorHandler utility class.
15+
* Allows catching and resolving errors inline.
16+
*/
17+
class ErrorHandler
18+
{
19+
/* ----------------- Public methods ---------------------------------------------- */
20+
21+
/**
22+
* Set error handler to run until removed.
23+
* @param mixed $handling
24+
* - If null, handler will throw ErrorException
25+
* - If Throwable $t, throw $t with ErrorException attached as previous
26+
* - If callable, will invoke callback with ErrorException as argument
27+
* @param int $levels Error levels to catch, all errors by default
28+
* @return mixed Previously registered error handler, if any
29+
*/
30+
public function set($handling = null, int $levels = E_ALL)
31+
{
32+
return set_error_handler($this->getHandler($handling), $levels);
33+
}
34+
35+
/**
36+
* Remove error handler.
37+
* @return bool True if removed
38+
*/
39+
public function restore(): bool
40+
{
41+
return restore_error_handler();
42+
}
43+
44+
/**
45+
* Run code with error handling, breaks on first encountered error.
46+
* @param callable $callback The code to run
47+
* @param mixed $handling
48+
* - If null, handler will throw ErrorException
49+
* - If Throwable $t, throw $t with ErrorException attached as previous
50+
* - If callable, will invoke callback with ErrorException as argument
51+
* @param int $levels Error levels to catch, all errors by default
52+
* @return mixed Return what $callback returns, or what $handling retuns on error
53+
*/
54+
public function with(callable $callback, $handling = null, int $levels = E_ALL)
55+
{
56+
$error = null;
57+
try {
58+
$this->set(null, $levels);
59+
$result = $callback();
60+
} catch (ErrorException $e) {
61+
$error = $this->handle($handling, $e);
62+
}
63+
$this->restore();
64+
return $error ?: $result;
65+
}
66+
67+
/**
68+
* Run code with error handling, comletes code before handling errors
69+
* @param callable $callback The code to run
70+
* @param mixed $handling
71+
* - If null, handler will throw ErrorException
72+
* - If Throwable $t, throw $t with ErrorException attached as previous
73+
* - If callable, will invoke callback with ErrorException as argument
74+
* @param int $levels Error levels to catch, all errors by default
75+
* @return mixed Return what $callback returns, or what $handling retuns on error
76+
*/
77+
public function withAll(callable $callback, $handling = null, int $levels = E_ALL)
78+
{
79+
$errors = [];
80+
$this->set(function (ErrorException $e) use (&$errors) {
81+
$errors[] = $e;
82+
}, $levels);
83+
$result = $callback();
84+
$error = empty($errors) ? null : $this->handle($handling, $errors, $result);
85+
$this->restore();
86+
return $error ?: $result;
87+
}
88+
89+
/* ----------------- Private helpers --------------------------------------------- */
90+
91+
// Get handler function
92+
private function getHandler($handling)
93+
{
94+
return function ($severity, $message, $file, $line) use ($handling) {
95+
$error = new ErrorException($message, 0, $severity, $file, $line);
96+
$this->handle($handling, $error);
97+
};
98+
}
99+
100+
// Handle error according to $handlig type
101+
private function handle($handling, $error, $result = null)
102+
{
103+
if (is_callable($handling)) {
104+
return $handling($error, $result);
105+
}
106+
if (is_array($error)) {
107+
$error = array_shift($error);
108+
}
109+
if ($handling instanceof Throwable) {
110+
try {
111+
throw $error;
112+
} finally {
113+
throw $handling;
114+
}
115+
}
116+
throw $error;
117+
}
118+
}

0 commit comments

Comments
 (0)