Skip to content

Commit 97d1231

Browse files
committed
General updates
1 parent d8a7cb6 commit 97d1231

File tree

6 files changed

+62
-26
lines changed

6 files changed

+62
-26
lines changed

ErrorHandler.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ class ErrorHandler
3535
/**
3636
* Returns an array of lines from a file.
3737
*
38-
* @access public
3938
* @param string $file File in which you want to highlight a line
4039
* @param int $line Line number to highlight
4140
* @param int $padding (optional) Number of padding lines
4241
* @return array
4342
*/
44-
protected static function highlightCode($file, $line, $padding = 6)
43+
protected static function highlightCode(string $file, int $line, int $padding = 6) : array
4544
{
4645
if ( ! is_readable($file)) {
4746
return false;
@@ -78,13 +77,13 @@ protected static function highlightCode($file, $line, $padding = 6)
7877
/**
7978
* Converts errors to ErrorExceptions.
8079
*
81-
* @param integer $code The error code
80+
* @param int $code The error code
8281
* @param string $message The error message
8382
* @param string $file The filename where the error occurred
84-
* @param integer $line The line number where the error occurred
85-
* @return boolean
83+
* @param int $line The line number where the error occurred
84+
* @return bool
8685
*/
87-
public static function error($code, $message, $file, $line)
86+
public static function error(int $code, string $message, string $file, int $line) : bool
8887
{
8988
// If isset error_reporting and $code then throw new error exception
9089
if ((error_reporting() & $code) !== 0) {
@@ -111,7 +110,7 @@ public static function error($code, $message, $file, $line)
111110
ErrorHandler::writeLogs("{$error['type']}: {$error['message']} in {$error['file']} at line {$error['line']}");
112111

113112
} else {
114-
throw new ErrorException($message, $code, 0, $file, $line);
113+
throw new \ErrorException($message, $code, 0, $file, $line);
115114
}
116115
}
117116

@@ -125,7 +124,7 @@ public static function error($code, $message, $file, $line)
125124
* @param string $string String
126125
* @return string
127126
*/
128-
protected static function highlightString($string)
127+
protected static function highlightString(string $string) : string
129128
{
130129
$search = array("\r\n", "\n\r", "\r", "\n", '<code>', '</code>', '<span style="color: #0000BB">&lt;?php&nbsp;', '#$@r4!/*');
131130
$replace = array('', '', '', '', '', '', '<span style="color: #0000BB">', '/*');
@@ -136,11 +135,10 @@ protected static function highlightString($string)
136135
/**
137136
* Modifies the backtrace array.
138137
*
139-
* @access protected
140138
* @param array $backtrace Array returned by the getTrace() method of an exception object
141139
* @return array
142140
*/
143-
protected static function formatBacktrace($backtrace)
141+
protected static function formatBacktrace(array $backtrace) : array
144142
{
145143
if (is_array($backtrace) === false || count($backtrace) === 0) {
146144
return $backtrace;
@@ -223,7 +221,7 @@ public static function fatal()
223221
$e = error_get_last();
224222

225223
if ($e !== null && (error_reporting() & $e['type']) !== 0) {
226-
ErrorHandler::exception(new ErrorException($e['message'], $e['type'], 0, $e['file'], $e['line']));
224+
ErrorHandler::exception(new \ErrorException($e['message'], $e['type'], 0, $e['file'], $e['line']));
227225

228226
exit(1);
229227
}
@@ -232,11 +230,10 @@ public static function fatal()
232230
/**
233231
* Writes message to log.
234232
*
235-
* @access public
236233
* @param string $message The message to write to the log
237-
* @return boolean
234+
* @return bool
238235
*/
239-
public static function writeLogs($message)
236+
public static function writeLogs(string $message) : bool
240237
{
241238
return (bool) file_put_contents(rtrim(LOGS_PATH, '/') . '/' . gmdate('Y_m_d') . '.log',
242239
'[' . gmdate('d-M-Y H:i:s') . '] ' . $message . PHP_EOL,
@@ -246,7 +243,6 @@ public static function writeLogs($message)
246243
/**
247244
* Handles uncaught exceptions and returns a pretty error screen.
248245
*
249-
* @access public
250246
* @param Exception $exception An exception object
251247
*/
252248
public static function exception($exception)
@@ -263,7 +259,7 @@ public static function exception($exception)
263259
$error['line'] = $exception->getLine();
264260

265261
// Determine error type
266-
if ($exception instanceof ErrorException) {
262+
if ($exception instanceof \ErrorException) {
267263
$error['type'] = 'ErrorException: ';
268264
$error['type'] .= in_array($error['code'], array_keys(ErrorHandler::$levels)) ? ErrorHandler::$levels[$error['code']] : 'Unknown Error';
269265
} else {
@@ -280,7 +276,7 @@ public static function exception($exception)
280276

281277
$error['backtrace'] = $exception->getTrace();
282278

283-
if ($exception instanceof ErrorException) {
279+
if ($exception instanceof \ErrorException) {
284280
$error['backtrace'] = array_slice($error['backtrace'], 1); //Remove call to error handler from backtrace
285281
}
286282

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Force
3+
Copyright (c) 2018 Flextype Component
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
1-
# ErrorHandler
1+
# Error Handler Component
2+
![version](https://img.shields.io/badge/version-1.0.4-brightgreen.svg?style=flat-square "Version")
3+
[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://github.com/flextype-components/errorhandler/blob/master/LICENSE)
4+
5+
Error Handler Component for errors handling.
6+
7+
### Installation
8+
9+
```
10+
composer require flextype-components/errorhandler
11+
```
12+
13+
### Usage
14+
15+
```php
16+
use Flextype\Component\ErrorHandler\ErrorHandler;
17+
```
18+
19+
Set error reporting level
20+
```php
21+
$show_errors = true;
22+
23+
if ($show_errors) {
24+
define('DEVELOPMENT', true);
25+
error_reporting(-1);
26+
} else {
27+
define('DEVELOPMENT', false);
28+
error_reporting(0);
29+
}
30+
```
31+
32+
Set LOGS_PATH constant
33+
```php
34+
define('LOGS_PATH', 'path/to/logs');
35+
```
36+
37+
Set Error handler
38+
```php
39+
set_error_handler('Flextype\Component\ErrorHandler\ErrorHandler::error');
40+
register_shutdown_function('Flextype\Component\ErrorHandler\ErrorHandler::fatal');
41+
set_exception_handler('Flextype\Component\ErrorHandler\ErrorHandler::exception');
42+
```
43+
44+
## License
45+
See [LICENSE](https://github.com/flextype-components/errorhandler/blob/master/LICENSE)

Resources/Views/Errors/exception.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108

109109
<div class="error">
110110
<?php echo $error['type']; ?><?php if(isset($error['code'])): ?> <span style="color:#e1e1e1;padding:0px">[<?php echo $error['code']; ?>]</span><?php endif; ?>
111-
<span class="pull-right">Gelato</span>
111+
<span class="pull-right">Error Handler</span>
112112
</div>
113113
<div class="body">
114114
<strong>Message:</strong> <?php echo htmlspecialchars($error['message'], ENT_COMPAT, 'UTF-8', false); ?>
@@ -276,10 +276,6 @@
276276
<?php endforeach; ?>
277277
</table>
278278
</div>
279-
<div style="padding-top:20px;padding-bottom:20px; padding-left:10px;">
280-
<a href="http://monstra.org">MONSTRA</a>
281-
<a href="http://gelato.monstra.org">GELATO</a>
282-
</div>
283279
</div>
284280

285281
<script type="text/javascript">

Resources/Views/Errors/production.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html lang="en">
33
<head>
44
<meta charset="utf-8">
5-
<title>Error</title>
5+
<title>Error Handler</title>
66
<style type="text/css">
77
body {
88
height:100%;

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "library",
55
"license": "MIT",
66
"homepage": "https://github.com/flextype-components/errorhandler",
7-
"description": "",
7+
"description": "Error Handler Component for errors handling.",
88
"authors": [
99
{
1010
"name": "Sergey Romanenko",

0 commit comments

Comments
 (0)