Skip to content

Commit

Permalink
Merge pull request #4 from WebFiori/dev
Browse files Browse the repository at this point in the history
Quality Improvments
  • Loading branch information
usernane committed Mar 8, 2023
2 parents 5ba6632 + 7bac3f1 commit 0d4a6fa
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 141 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/php-cs-fixer-v2.phar
/.phpunit.result.cache
nbproject
composer.lock
vendor/*
.idea/*
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,32 @@ A library for handling PHP errors and exceptions in a better way.
</p>

## Supported PHP Versions
| Build Status |
|:-----------:|
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php70.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%207.0/badge.svg?branch=main"></a>|
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php71.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%207.1/badge.svg?branch=main"></a>|
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php72.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%207.2/badge.svg?branch=main"></a>|
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php73.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%207.3/badge.svg?branch=main"></a>|
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php74.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%207.4/badge.svg?branch=main"></a>|
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php80.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.0/badge.svg?branch=main"></a>|
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php81.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.1/badge.svg?branch=main"></a>|
|<a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php82.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.2/badge.svg?branch=main"></a><br>(dev)|
| Build Status |
|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| <a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php70.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%207.0/badge.svg?branch=main"></a> |
| <a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php71.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%207.1/badge.svg?branch=main"></a> |
| <a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php72.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%207.2/badge.svg?branch=main"></a> |
| <a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php73.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%207.3/badge.svg?branch=main"></a> |
| <a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php74.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%207.4/badge.svg?branch=main"></a> |
| <a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php80.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.0/badge.svg?branch=main"></a> |
| <a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php81.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.1/badge.svg?branch=main"></a> |
| <a target="_blank" href="https://github.com/WebFiori/err/actions/workflows/php82.yml"><img src="https://github.com/WebFiori/err/workflows/Build%20PHP%208.2/badge.svg?branch=main"></a> |

## Installation
The library can be included in your project by including following entry in your `require` section of your `composer.json`: `"webfiori/err":"*"`.

## Features
* Conversion of all PHP errors to exceptions.
* Ability to create a custom exceptions handler.
* Ability to create a custom exceptions' handler.
* Provides OOP abstraction for the function `set_exception_handler()`

## Usage

The library has two main classes that the developer will work with. The first one is the class `Handler` and the second class is `AbstractExceptionHandler`. The first class is the core of the library. It is used to set custom exception handler. The second class is used to implement custom exceptions handler. Since the library will convert all PHP errors to exceptions, no need for the developer to have a custom errors handler.
The library has two main classes that the developer will work with. The first one is the class `Handler` and the second class is `AbstractExceptionHandler`. The first class is the core of the library. It is used to set custom exception handler. The second class is used to implement custom exception handler. Since the library will convert all PHP errors to exceptions, no need for the developer to have a custom errors handler.

### Implementing a Custom Exceptions Handler

First step in setting a custom exceptions handler is to implement one. Implementing a custom handler is strait forward procedure. Simply, the developer have to extends the class `AbstractExceptionHandler` and implement one abstract method of the class. The method `AbstractExceptionHandler::handle()` is used to handle the exception. The developer can have access to the properties of the thrown exception in order to handle it properly. The library comes with one default exceptions handler which can act as good example in how to implement a custom handler.
First step in setting a custom exceptions handler is to implement one. Implementing a custom handler is strait forward procedure. Simply, the developer have to extend the class `AbstractExceptionHandler` and implement one abstract method of the class. The method `AbstractExceptionHandler::handle()` is used to handle the exception. The developer can have access to the properties of the thrown exception in order to handle it properly. The library comes with one default exceptions handler which can act as good example in how to implement a custom handler.

``` php
<?php
Expand Down
6 changes: 3 additions & 3 deletions php_cs.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ $finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;

return PhpCsFixer\Config::create()
->setRules([
$config = new PhpCsFixer\Config();
return $config->setRules([
'align_multiline_comment' => [
'comment_type' => 'phpdocs_only'
],
Expand Down Expand Up @@ -54,7 +54,7 @@ return PhpCsFixer\Config::create()
'sort_algorithm' => 'alpha'
],
'ordered_class_elements' => [
'sortAlgorithm' => 'alpha',
'sort_algorithm' => 'alpha',
'order' => [
'constant_public',
'constant_protected',
Expand Down
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
$rootDir = $rootDirTrimmed.$DS;
}
define('ROOT_DIR', $rootDir);
define('DS', DIRECTORY_SEPARATOR);
const DS = DIRECTORY_SEPARATOR;
echo 'Root Directory: \''.$rootDir.'\'.'."\n";
$classesPath = $rootDir.'src'.DS.'webfiori'.DS.'error'.DS;
$classesPath = $rootDir.'webfiori'.DS.'error'.DS;

require_once $classesPath . 'ErrorHandlerException.php';
require_once $classesPath . 'AbstractHandler.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

use Throwable;
/**
* This class is used to implement custom exceptions handler.
* This class is used to implement custom exception handler.
*
* @author Ibrahim
*/
abstract class AbstractHandler {
private $exception;
private $traceArr;
private $name;
private $isCalled;
private $isExecuting;
private $name;
private $traceArr;
/**
* Creates new instance of the class.
*/
Expand All @@ -22,49 +22,14 @@ public function __construct() {
$this->isCalled = false;
$this->isExecuting = false;
}
/**
* Sets the handler as executed.
*
* This method is used to make sure that same handler won't get executed twice.
*
* @param bool $bool True to set it as executed, false to not.
*/
public function setIsExecuted(bool $bool) {
$this->isCalled = $bool;
}
/**
* Checks if the handler was executed once or not.
*
* @return bool If the method returned true, then this means the handler
* was executed.
*/
public function isExecuted() : bool {
return $this->isCalled;
}
/**
* Gives the handler a specific name.
*
* @param string $name The custom name of the handler.
*/
public function setName(string $name) {
$this->name = trim($name);
}
/**
* Returns the name of the handler.
*
* @return string The name of the handler.
*/
public function getName() : string {
return $this->name;
}
/**
* Returns a string that represents the name of the class that an exception
* was thrown at.
*
* @return string A string that represents the name of the class that an exception
* was thrown at.
*/
public function getClass() {
public function getClass() : string {
return TraceEntry::extractClassName($this->getException()->getFile());
}
/**
Expand Down Expand Up @@ -99,6 +64,14 @@ public function getLine() : string {
public function getMessage() : string {
return $this->getException()->getMessage();
}
/**
* Returns the name of the handler.
*
* @return string The name of the handler.
*/
public function getName() : string {
return $this->name;
}
/**
* Returns an array that contains objects that represents stack trace of
* the call.
Expand All @@ -121,6 +94,26 @@ public abstract function handle();
* handler will get executed. False otherwise.
*/
public abstract function isActive() : bool;
/**
* Checks if the handler was executed once or not.
*
* @return bool If the method returned true, then this means the handler
* was executed.
*/
public function isExecuted() : bool {
return $this->isCalled;
}
/**
* Check if the handler is in execution stage or not.
*
* This method is used to indicate if execution
* scope is inside the method AbstractHandler::handle() or not.
*
* @return bool True if the handler is executing. False if not.
*/
public function isExecuting() : bool {
return $this->isExecuting;
}
/**
* Checks if the handler will be called in case of error after shutdown.
*/
Expand All @@ -137,44 +130,50 @@ public function setException(Throwable $ex) {
$this->setTrace();
}
/**
* Sets the value that tells if the handler is begin executed or not.
* Sets the handler as executed.
*
* This method is used to make sure that same handler won't get executed twice.
*
* @param bool $bool True to set it as executed, false to not.
*/
public function setIsExecuted(bool $bool) {
$this->isCalled = $bool;
}
/**
* Sets the value that tells if the handler is being executed or not.
*
* This method is used internally by the library to set status of the
* handler.
*
* @param bool $isExec True to set the handler as begin executed. False
* @param bool $isExec True to set the handler as being executed. False
* to not.
*/
public function setIsExecuting(bool $isExec) {
$this->isExecuting = $isExec;
}
/**
* Check if the handler is in execution stage or not.
*
* This method is used to indicate if execution
* scope is inside the method AbstractHandler::handle() or not.
* Gives the handler a specific name.
*
* @return bool True if the handler is executing. False if not.
* @param string $name The custom name of the handler.
*/
public function isExecuting() : bool {
return $this->isExecuting;
public function setName(string $name) {
$this->name = trim($name);
}
private function setTrace() {
$ex = $this->getException();

if ($ex instanceof ErrorHandlerException) {
$this->traceArr = $ex->getDebugTrace();
} else {
$trace = $ex->getTrace();
$currentLine = isset($trace[0]['line']) ? $trace[0]['line'] : 'X';
$currentFile = isset($trace[0]['file']) ? $trace[0]['file'] : 'X';
$nextLine = '';
$nextFile = '';
$currentLine = $trace[0]['line'] ?? 'X';
$currentFile = $trace[0]['file'] ?? 'X';
$idx = 0;

foreach ($trace as $traceEntry) {
if ($idx != 0) {
$nextFile = isset($traceEntry['file']) ? $traceEntry['file'] : 'X';
$nextLine = isset($traceEntry['line']) ? $traceEntry['line'] : 'X';
$nextFile = $traceEntry['file'] ?? 'X';
$nextLine = $traceEntry['line'] ?? 'X';
$traceEntry['file'] = $currentFile;
$traceEntry['line'] = $currentLine;
$this->traceArr[] = new TraceEntry($traceEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* The default exceptions handler.
*
* This simple handler will show the exception alongside the message ant trace
* using the 'echo' command.
* using 'echo'.
*
* @author Ibrahim
*/
Expand All @@ -26,7 +26,7 @@ public function handle() {
echo 'Exception message: '.$this->getMessage().'.<br>';
echo 'Stack trace:<br>';
$trace = $this->getTrace();

if (count($trace) == 0) {
echo '&lt;Empty&gt;';
} else {
Expand All @@ -40,12 +40,21 @@ public function handle() {
echo '</pre>';
}

/**
* Checks if the handler is active or not.
*
* @return bool The method will always return true.
*/
public function isActive(): bool {
return true;
}

/**
* Checks if the handler will be executed as a shutdown handler.
*
* @return bool The method will always return true.
*/
public function isShutdownHandler(): bool {
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Exception;
/**
* This class is used to represents PHP errors which was converted to exceptions.
* This class is used to represent PHP errors which was converted to exceptions.
*
* @author Ibrahim
*/
Expand All @@ -16,7 +16,7 @@ class ErrorHandlerException extends Exception {
*
* @param int $code The error code of the PHP error.
*
* @param string $file The path to the file at which the error happend.
* @param string $file The path to the file at which the error happened.
*/
public function __construct(string $message = "", int $code = 0, string $file = '') {
parent::__construct($message, $code);
Expand All @@ -26,16 +26,16 @@ public function __construct(string $message = "", int $code = 0, string $file =

for ($x = 0 ; $x < count($trace) ; $x++) {
if ($x == 1) {
$line = isset($trace[$x]['line']) ? $trace[$x]['line'] : 'X';
$line = $trace[$x]['line'] ?? 'X';
continue;
}

if ($x > 1) {
$temp = $trace[$x];
$temp['line'] = $line;
$this->debugTrace[] = new TraceEntry($temp);
$line = isset($trace[$x]['line']) ? $trace[$x]['line'] : 'X';
$line = $trace[$x]['line'] ?? 'X';
}

}
$this->debugTrace[] = new TraceEntry([
'file' => $file,
Expand All @@ -47,7 +47,7 @@ public function __construct(string $message = "", int $code = 0, string $file =
*
* @return array An array that holds objects of type 'TraceEntry'.
*/
public function getDebugTrace() {
public function getDebugTrace() : array {
return $this->debugTrace;
}
}

0 comments on commit 0d4a6fa

Please sign in to comment.