Skip to content

Commit c6e88f7

Browse files
committed
Initial Commit
0 parents  commit c6e88f7

14 files changed

+661
-0
lines changed

.gitattributes

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Files and folders here will be not included when creating package
2+
/tests export-ignore
3+
/.github export-ignore
4+
/README.md export-ignore
5+
/.gitignore export-ignore
6+
/.travis.yml export-ignore
7+
/phpunit.xml export-ignore
8+
/sonar-project.properties export-ignore

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.php_cs.cache
2+
/php-cs-fixer-v2.phar
3+
/.phpunit.result.cache
4+
nbproject

composer.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "webfiori/err",
3+
"description": "A library for handling PHP errors and exceptions in a better way.",
4+
"authors": [
5+
{
6+
"name": "Ibrahim BinAlshikh",
7+
"email": "ibrahim@webfiori.com"
8+
}
9+
],
10+
"require": {
11+
"php": ">=7.0"
12+
},
13+
"keywords": [
14+
"php",
15+
"error",
16+
"errors",
17+
"exception",
18+
"error handler"
19+
],
20+
"license": "MIT"
21+
}

php_cs.php.dist

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->exclude('vendor')
5+
->exclude('tests')
6+
->in(__DIR__)
7+
;
8+
9+
return PhpCsFixer\Config::create()
10+
->setRules([
11+
'align_multiline_comment' => [
12+
'comment_type' => 'phpdocs_only'
13+
],
14+
'array_indentation' => [],
15+
'array_syntax' => [
16+
'syntax' => 'short'
17+
],
18+
'binary_operator_spaces' => [
19+
'default' => 'single_space'
20+
],
21+
'blank_line_before_statement' => [
22+
'statements' => [
23+
'if','return','while','for','foreach','do'
24+
]
25+
],
26+
'blank_line_after_opening_tag' => true,
27+
'blank_line_after_namespace' => true,
28+
'elseif' => false,
29+
'explicit_string_variable' => false,
30+
'full_opening_tag' => true,
31+
'fully_qualified_strict_types' => true,
32+
'line_ending' => true,
33+
'linebreak_after_opening_tag' => true,
34+
'lowercase_cast' => true,
35+
'lowercase_keywords' => true,
36+
'lowercase_static_reference' => true,
37+
'no_alternative_syntax' => true,
38+
'no_blank_lines_after_class_opening' => true,
39+
'no_blank_lines_after_phpdoc' => true,
40+
'no_blank_lines_before_namespace' => true,
41+
'no_closing_tag' => true,
42+
'no_empty_comment' => true,
43+
'no_empty_phpdoc' => true,
44+
'no_empty_statement' => true,
45+
'no_multiline_whitespace_around_double_arrow' => true,
46+
'no_spaces_after_function_name' => true,
47+
'no_spaces_inside_parenthesis' => true,
48+
'no_unused_imports' => true,
49+
'single_import_per_statement' => true,
50+
'single_blank_line_at_eof' => true,
51+
'no_whitespace_in_blank_line' => true,
52+
'not_operator_with_space' => false,
53+
'ordered_imports' => [
54+
'sort_algorithm' => 'alpha'
55+
],
56+
'ordered_class_elements' => [
57+
'sortAlgorithm' => 'alpha',
58+
'order' => [
59+
'constant_public',
60+
'constant_protected',
61+
'constant_private',
62+
'property_public',
63+
'property_protected',
64+
'property_private',
65+
'construct',
66+
'method_public',
67+
'method_private'
68+
]
69+
],
70+
'no_mixed_echo_print' => [
71+
'use' => 'echo'
72+
],
73+
'constant_case' => [
74+
'case' => 'lower'
75+
],
76+
'increment_style' => [
77+
'style' => 'post'
78+
],
79+
'concat_space' => [
80+
'spacing' => 'none'
81+
],
82+
'braces' => [
83+
'allow_single_line_closure' => false,
84+
'position_after_functions_and_oop_constructs' => 'same',
85+
'position_after_anonymous_constructs' => 'next',
86+
'position_after_control_structures' => 'same'
87+
],
88+
'class_definition' => [
89+
'single_line' => true
90+
]
91+
])
92+
->setFinder($finder)
93+
;

phpunit.xml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit stderr="true" colors="true" bootstrap="tests/bootstrap.php">
3+
<php>
4+
</php>
5+
<filter>
6+
<whitelist addUncoveredFilesFromWhitelist="true">
7+
<file>./src/webfiori/error/TraceEntry.php</file>
8+
</whitelist>
9+
</filter>
10+
<logging>
11+
<log type="coverage-clover" target="clover.xml"/>
12+
</logging>
13+
<testsuites>
14+
<testsuite name="Tests">
15+
<directory>./tests/webfiori/framework/test/error</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

sonar-project.properties

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sonar.projectKey=WebFiori_err
2+
sonar.organization=webfiori
3+
4+
# This is the name and version displayed in the SonarCloud UI.
5+
sonar.projectName=err
6+
#sonar.projectVersion=1.0
7+
sonar.exclusions=tests/**,
8+
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
9+
#sonar.sources=.
10+
sonar.php.coverage.reportPaths=clover.xml
11+
# Encoding of the source code. Default is default system encoding
12+
sonar.sourceEncoding=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
namespace webfiori\error;
3+
4+
use Throwable;
5+
/**
6+
* This class is used to implement custom exceptions handler.
7+
*
8+
* @author Ibrahim
9+
*/
10+
abstract class AbstractExceptionHandler {
11+
private $exception;
12+
private $traceArr;
13+
/**
14+
* Creates new instance of the class.
15+
*/
16+
public function __construct() {
17+
$this->traceArr = [];
18+
}
19+
/**
20+
* Returns a string that represents the name of the class that an exception
21+
* was thrown at.
22+
*
23+
* @return string A string that represents the name of the class that an exception
24+
* was thrown at.
25+
*/
26+
public function getClass() {
27+
return TraceEntry::extractClassName($this->getException()->getFile());
28+
}
29+
/**
30+
* Returns an object that represents the exception which was thrown.
31+
*
32+
* @return Throwable An object that represents the exception which was thrown.
33+
*/
34+
public function getException() : Throwable {
35+
return $this->exception;
36+
}
37+
/**
38+
* Returns the number of line at which the exception was thrown at.
39+
*
40+
* @return string The number of line at which the exception was thrown at.
41+
*/
42+
public function getLine() : string {
43+
return $this->getException()->getLine().'';
44+
}
45+
/**
46+
* Returns a string that represents exception message.
47+
*
48+
* @return string A string that represents exception message.
49+
*/
50+
public function getMessage() : string {
51+
return $this->getException()->getMessage();
52+
}
53+
/**
54+
* Returns an array that contains objects that represents stack trace of
55+
* the call.
56+
*
57+
* @return array An array that holds objects of type 'StackEntry'
58+
*/
59+
public function getTrace() : array {
60+
return $this->traceArr;
61+
}
62+
/**
63+
* Handles the exception.
64+
*
65+
* The developer can implement this method to handle all thrown exceptions.
66+
*/
67+
public abstract function handle();
68+
/**
69+
* Sets the exception which was thrown by an error on the code.
70+
*
71+
* This method is called internally by the exception handling method.
72+
*
73+
* @param Throwable $ex The exception which was thrown by the code.
74+
*/
75+
public function setException(Throwable $ex) {
76+
$this->exception = $ex;
77+
$this->setTrace();
78+
}
79+
private function setTrace() {
80+
$ex = $this->getException();
81+
82+
if ($ex instanceof ErrorHandlerException) {
83+
$this->traceArr = $ex->getDebugTrace();
84+
} else {
85+
$trace = $ex->getTrace();
86+
87+
foreach ($trace as $traceEntry) {
88+
$this->traceArr[] = new TraceEntry($traceEntry);
89+
}
90+
}
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
namespace webfiori\error;
3+
4+
/**
5+
* The default exceptions handler.
6+
*
7+
* This simple handler will show the exception alongside the message ant trace
8+
* using the 'echo' command.
9+
*
10+
* @author Ibrahim
11+
*/
12+
class DefaultExceptionsHandler extends AbstractExceptionHandler {
13+
/**
14+
* Creates new instance of the class.
15+
*/
16+
public function __construct() {
17+
parent::__construct();
18+
}
19+
/**
20+
* Handles the exception.
21+
*/
22+
public function handle() {
23+
echo '<pre>';
24+
echo 'An exception was thrown at '.$this->getClass().' line '.$this->getLine().'.<br>';
25+
echo 'Exception message: '.$this->getMessage().'.<br>';
26+
echo 'Stack trace:<br>';
27+
$trace = $this->getTrace();
28+
29+
if (count($trace) == 0) {
30+
echo '&lt;Empty&gt;';
31+
} else {
32+
$index = '0';
33+
34+
foreach ($trace as $entry) {
35+
echo '#'.$index.' '.$entry.'<br>';
36+
$index++;
37+
}
38+
}
39+
echo '</pre>';
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace webfiori\error;
3+
4+
use Exception;
5+
/**
6+
* This class is used to represents PHP errors which was converted to exceptions.
7+
*
8+
* @author Ibrahim
9+
*/
10+
class ErrorHandlerException extends Exception {
11+
private $debugTrace;
12+
/**
13+
* Creates new instance of the class.
14+
*
15+
* @param string $message The message of the exception.
16+
*
17+
* @param int $code The error code of the PHP error.
18+
*
19+
* @param string $file The path to the file at which the error happend.
20+
*/
21+
public function __construct(string $message = "", int $code = 0, string $file = '') {
22+
parent::__construct($message, $code);
23+
$this->debugTrace = [];
24+
$trace = debug_backtrace();
25+
$line = null;
26+
27+
for ($x = 0 ; $x < count($trace) ; $x++) {
28+
if ($x != 0 && $line !== null) {
29+
$temp = $trace[$x];
30+
$temp['line'] = $line;
31+
$this->debugTrace[] = new TraceEntry($temp);
32+
}
33+
$line = $trace[$x]['line'];
34+
}
35+
$this->debugTrace[] = new TraceEntry([
36+
'file' => $file,
37+
'line' => $line
38+
]);
39+
}
40+
/**
41+
* Returns an array that contains stack trace of the error.
42+
*
43+
* @return array An array that holds objects of type 'TraceEntry'.
44+
*/
45+
public function getDebugTrace() {
46+
return $this->debugTrace;
47+
}
48+
}

0 commit comments

Comments
 (0)