Skip to content

Commit 11d87bb

Browse files
committed
Initial commit.
0 parents  commit 11d87bb

File tree

12 files changed

+371
-0
lines changed

12 files changed

+371
-0
lines changed

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.gitattributes export-ignore
2+
/.gitignore export-ignore
3+
/.scrutinizer.yml export-ignore
4+
/.travis.yml export-ignore
5+
/build.xml export-ignore
6+
/phpunit.xml export-ignore
7+
/README.md export-ignore
8+
/test/ export-ignore

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.idea/
2+
/.phpunit.result.cache
3+
/bin/
4+
/composer.lock
5+
/test/coverage.xml
6+
/test/report/
7+
/vendor/

.scrutinizer.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build:
2+
tests:
3+
override:
4+
-
5+
command: './bin/phing unit'
6+
coverage:
7+
file: 'test/coverage.xml'
8+
format: 'php-clover'

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
php:
3+
- '7.1'
4+
- '7.2'
5+
- '7.3'
6+
7+
install:
8+
- composer self-update
9+
- composer install
10+
11+
script:
12+
- ./bin/phing unit

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 Set Based IT Consultancy
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ErrorHandler
2+
3+
An error handler for PHP.
4+
5+
<table>
6+
<thead>
7+
<tr>
8+
<th>Social</th>
9+
<th>Legal</th>
10+
<th>Release</th>
11+
<th>Tests</th>
12+
<th>Code</th>
13+
</tr>
14+
</thead>
15+
<tbody>
16+
<tr>
17+
<td>
18+
<a href="https://gitter.im/SetBased/php-error-handler?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge"><img src="https://badges.gitter.im/SetBased/php-error-handler.svg" alt="Gitter"/></a>
19+
</td>
20+
<td>
21+
<a href="https://packagist.org/packages/setbased/error-handler"><img src="https://poser.pugx.org/setbased/error-handler/license" alt="License"/></a>
22+
</td>
23+
<td>
24+
<a href="https://packagist.org/packages/setbased/error-handler"><img src="https://poser.pugx.org/setbased/error-handler/v/stable" alt="Latest Stable Version"/></a><br/>
25+
</td>
26+
<td><a href="https://travis-ci.org/SetBased/php-error-handler"><img src="https://travis-ci.org/SetBased/php-error-handler.svg?branch=master" alt="Build Status"/></a><br/>
27+
<a href="https://scrutinizer-ci.com/g/SetBased/php-error-handler/?branch=master"><img src="https://scrutinizer-ci.com/g/SetBased/php-error-handler/badges/coverage.png?b=master" alt="Code Coverage"/></a><br/>
28+
</td>
29+
<td>
30+
<a href="https://scrutinizer-ci.com/g/SetBased/php-error-handler/?branch=master"><img src="https://scrutinizer-ci.com/g/SetBased/php-error-handler/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality"/></a>
31+
</td>
32+
</tr>
33+
</tbody>
34+
</table>
35+
36+
License
37+
=======
38+
39+
This project is licensed under the terms of the MIT license.

build.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project name="php-affirm" default="build" basedir=".">
3+
<target name="build">
4+
</target>
5+
6+
<!-- Run composer update -->
7+
<target name="update">
8+
<exec command="composer update" checkreturn="true" passthru="true"/>
9+
</target>
10+
11+
<!-- Runs all unit tests -->
12+
<target name="unit">
13+
<exec command="bin/phpunit" passthru="true" checkreturn="true"/>
14+
</target>
15+
</project>

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "setbased/error-handler",
3+
"description": "An error handler",
4+
"keywords": [
5+
"Error",
6+
"Handler"
7+
],
8+
"type": "library",
9+
"license": "MIT",
10+
"require": {
11+
"php": ">=7.1.0",
12+
"setbased/exception": "^1.1.0"
13+
},
14+
"require-dev": {
15+
"phing/phing": "^2.0.0",
16+
"phpunit/phpunit": "^7.0.0 || ^8.0.0"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"SetBased\\ErrorHandler\\": "src/"
21+
},
22+
"autoload-dev": {
23+
"psr-4": {
24+
"SetBased\\ErrorHandler\\Test\\": "test/"
25+
}
26+
}
27+
},
28+
"config": {
29+
"bin-dir": "bin"
30+
}
31+
}

phpunit.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<phpunit bootstrap="test/bootstrap.php">
2+
<testsuites>
3+
<testsuite name="Tests">
4+
<directory>test</directory>
5+
</testsuite>
6+
</testsuites>
7+
<filter>
8+
<whitelist processUncoveredFilesFromWhitelist="true">
9+
<directory suffix=".php">src</directory>
10+
</whitelist>
11+
</filter>
12+
<logging>
13+
<log type="coverage-html" target="test/report"/>
14+
<log type="coverage-clover" target="test/coverage.xml"/>
15+
</logging>
16+
<php>
17+
<const name="PHPUNIT_ERROR_HANDLER_TEST_SUITE" value="true"/>
18+
</php>
19+
</phpunit>

src/ErrorHandler.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace SetBased\ErrorHandler;
5+
6+
use SetBased\Exception\ErrorException;
7+
8+
/**
9+
* A class for handling PHP runtime errors and warnings in a uniform manner by throwing exceptions.
10+
*/
11+
class ErrorHandler
12+
{
13+
//--------------------------------------------------------------------------------------------------------------------
14+
/**
15+
* An error handler function that throws exceptions.
16+
*
17+
* @param int $errno The level of the error raised.
18+
* @param string $errstr The error message.
19+
* @param string|null $errfile The filename that the error was raised in.
20+
* @param int|null $errline The line number the error was raised at.
21+
*
22+
* @return bool
23+
*
24+
* See http://php.net/manual/en/function.set-error-handler.php.
25+
*
26+
* @throws ErrorException
27+
*/
28+
public function handleError(int $errno, string $errstr, ?string $errfile, ?int $errline): bool
29+
{
30+
if (error_reporting()===0)
31+
{
32+
// Error was suppressed with the @-operator. Don't throw an exception.
33+
return false;
34+
}
35+
36+
$exception = new ErrorException($errstr, $errno, $errno, $errfile, $errline);
37+
38+
// In case error appeared in __toString method we can't throw any exception.
39+
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
40+
array_shift($trace);
41+
foreach ($trace as $frame)
42+
{
43+
if ($frame['function']==='__toString')
44+
{
45+
$handler = set_exception_handler(null);
46+
restore_exception_handler();
47+
48+
if (is_callable($handler))
49+
{
50+
$handler($exception);
51+
}
52+
else
53+
{
54+
error_log((string)$exception);
55+
}
56+
57+
// If running unit tests return true.
58+
if (defined('PHPUNIT_ERROR_HANDLER_TEST_SUITE')) return true;
59+
60+
exit(-1);
61+
}
62+
}
63+
64+
throw $exception;
65+
}
66+
67+
//--------------------------------------------------------------------------------------------------------------------
68+
/**
69+
* Registers this error handler.
70+
*
71+
* @param null|int $errorTypes The mask for triggering this error handler. Defaults to E_ALL | E_STRICT.
72+
*/
73+
public function registerErrorHandler($errorTypes = E_ALL)
74+
{
75+
ini_set('display_errors', '0');
76+
set_error_handler([$this, 'handleError'], $errorTypes);
77+
}
78+
79+
//--------------------------------------------------------------------------------------------------------------------
80+
/**
81+
* Unregisters this error handler by restoring the PHP error and exception handlers.
82+
*/
83+
public function unregisterErrorHandler()
84+
{
85+
restore_error_handler();
86+
}
87+
88+
//--------------------------------------------------------------------------------------------------------------------
89+
}
90+
91+
//----------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)