Skip to content

Commit d7796aa

Browse files
authored
Merge pull request #1 from MaplePHP/develop
Code quality improvements
2 parents 06178e4 + c3f5c37 commit d7796aa

17 files changed

+303
-136
lines changed

ExceptionItem.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414
class ExceptionItem
1515
{
16-
1716
private int $flag;
1817
private Throwable $exception;
1918
private SeverityLevelPool $pool;
2019

21-
function __construct(Throwable $exception, ?SeverityLevelPool $pool = null)
20+
public function __construct(Throwable $exception, ?SeverityLevelPool $pool = null)
2221
{
2322
$this->exception = $exception;
2423
$this->flag = (method_exists($exception, "getSeverity")) ? $exception->getSeverity() : 0;
@@ -48,6 +47,7 @@ public function __call(string $name, array $args): mixed
4847
if(!method_exists($this->exception, $name)) {
4948
throw new \BadMethodCallException("Method '$name' does not exist in Throwable class");
5049
}
50+
5151
return $this->exception->{$name}(...$args);
5252
}
5353

@@ -96,6 +96,7 @@ public function getSeverity(): ?string
9696
if($this->flag === 0) {
9797
return $this->getType();
9898
}
99+
99100
return SeverityLevelPool::getSeverityLevel($this->flag);
100101
}
101102

@@ -137,6 +138,7 @@ final public function isLevelFatal(): bool
137138
$errors |= E_CORE_WARNING;
138139
$errors |= E_COMPILE_ERROR;
139140
$errors |= E_COMPILE_WARNING;
141+
140142
return ($this->flag & $errors) > 0;
141143
}
142144
}

Handlers/AbstractHandler.php

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616
use MaplePHP\Blunder\ExceptionItem;
1717
use MaplePHP\Blunder\SeverityLevelPool;
1818
use MaplePHP\Http\Interfaces\StreamInterface;
19+
use Closure;
1920
use Throwable;
2021

2122
abstract class AbstractHandler implements HandlerInterface
2223
{
23-
24-
// Maximum trace depth (memory improvement)
24+
/**
25+
* Maximum trace depth (memory improvement)
26+
* @var int
27+
*/
2528
protected const MAX_TRACE_LENGTH = 40;
2629

2730
protected bool $throwException = true;
2831
protected ?HttpMessagingInterface $http = null;
29-
protected $eventCallable;
32+
protected ?Closure $eventCallable = null;
3033

3134
/**
3235
* Determine how the code block should look like
@@ -40,10 +43,10 @@ abstract protected function getCodeBlock(array $data, string $code, int $index =
4043
/**
4144
* The event callable will be triggered when an error occur.
4245
* Note: Will add PSR-14 support for dispatch in the future.
43-
* @param callable $event
46+
* @param Closure $event
4447
* @return void
4548
*/
46-
public function event(callable $event): void
49+
public function event(Closure $event): void
4750
{
4851
$this->eventCallable = $event;
4952
}
@@ -53,7 +56,7 @@ public function event(callable $event): void
5356
* @param HttpMessagingInterface $http
5457
* @return void
5558
*/
56-
function setHttp(HttpMessagingInterface $http): void
59+
public function setHttp(HttpMessagingInterface $http): void
5760
{
5861
$this->http = $http;
5962
}
@@ -62,36 +65,39 @@ function setHttp(HttpMessagingInterface $http): void
6265
* Get PSR-7 HTTP message instance
6366
* @return HttpMessagingInterface
6467
*/
65-
function getHttp(): HttpMessagingInterface
68+
public function getHttp(): HttpMessagingInterface
6669
{
6770
if(!($this->http instanceof HttpMessagingInterface)) {
6871
$this->http = new HttpMessaging();
6972
}
73+
7074
return $this->http;
7175
}
7276

7377
/**
7478
* Main error handler script
75-
* @param int $level
76-
* @param string $message
77-
* @param string $file
78-
* @param int $line
79+
* @param int $errNo
80+
* @param string $errStr
81+
* @param string $errFile
82+
* @param int $errLine
7983
* @param array $context
8084
* @return bool
8185
* @throws ErrorException
8286
*/
83-
public function errorHandler(int $level, string $message, string $file, int $line = 0, array $context = []): bool
87+
public function errorHandler(int $errNo, string $errStr, string $errFile, int $errLine = 0, array $context = []): bool
8488
{
85-
if ($level & error_reporting()) {
89+
if ($errNo & error_reporting()) {
8690
$this->cleanOutputBuffers();
87-
$exception = new ErrorException($message, 0, $level, $file, $line);
91+
$exception = new ErrorException($errStr, 0, $errNo, $errFile, $errLine);
8892
if ($this->throwException) {
8993
throw $exception;
9094
} else {
9195
$this->exceptionHandler($exception);
9296
}
97+
9398
return true;
9499
}
100+
95101
return false;
96102
}
97103

@@ -104,7 +110,7 @@ public function shutdownHandler(): void
104110
$this->throwException = false;
105111
$error = error_get_last();
106112
if($error) {
107-
$item = new ExceptionItem(new ErrorException);
113+
$item = new ExceptionItem(new ErrorException());
108114
if ($item->isLevelFatal()) {
109115
$this->errorHandler(
110116
$error['type'],
@@ -123,7 +129,7 @@ public function shutdownHandler(): void
123129
*/
124130
protected function getTrace(throwable $exception): array
125131
{
126-
$new = array();
132+
$new = [];
127133
$trace = $exception->getTrace();
128134

129135
array_unshift($trace, $this->pollyFillException([
@@ -134,11 +140,12 @@ protected function getTrace(throwable $exception): array
134140

135141
foreach ($trace as $key => $stackPoint) {
136142
$new[$key] = $stackPoint;
137-
$new[$key]['args'] = array_map('gettype', $new[$key]['args']);
138-
if($key >= (static::MAX_TRACE_LENGTH-1)) {
143+
$new[$key]['args'] = array_map('gettype', (array)$new[$key]['args']);
144+
if($key >= (static::MAX_TRACE_LENGTH - 1)) {
139145
break;
140146
}
141147
}
148+
142149
return $new;
143150
}
144151

@@ -164,7 +171,7 @@ protected function emitter(throwable $exception, ?ExceptionItem $exceptionItem =
164171
call_user_func_array($this->eventCallable, [$exceptionItem, $this->http]);
165172
}
166173
$stream->rewind();
167-
echo $stream->read($stream->getSize());
174+
echo $stream->read((int)$stream->getSize());
168175

169176
// Exit execute to prevent response under to be triggered in some cases
170177
exit();
@@ -188,7 +195,7 @@ protected function getContentsBetween(StreamInterface $stream, int $errorLine, i
188195
$startLine = 1;
189196
}
190197
while (!$stream->eof()) {
191-
$line = $stream->read($stream->getSize());
198+
$line = $stream->read((int)$stream->getSize());
192199
$lines = explode("\n", $line);
193200
foreach ($lines as $lineContent) {
194201
if ($index >= $startLine && $index <= $endLine) {
@@ -207,6 +214,7 @@ protected function getContentsBetween(StreamInterface $stream, int $errorLine, i
207214
$index++;
208215
}
209216
}
217+
210218
return $output;
211219
}
212220

@@ -215,13 +223,15 @@ protected function getContentsBetween(StreamInterface $stream, int $errorLine, i
215223
* @param Throwable $exception
216224
* @return string
217225
*/
218-
public function getSeverityBreadcrumb(throwable $exception): string {
226+
public function getSeverityBreadcrumb(throwable $exception): string
227+
{
219228

220229
$severityTitle = $this->getSeverityTitle($exception);
221230
$breadcrumb = get_class($exception);
222231
if(!is_null($severityTitle)) {
223232
$breadcrumb .= " <span class='color-green'>($severityTitle)</span>";
224233
}
234+
225235
return "<div class='text-base mb-10 color-darkgreen'>$breadcrumb</div>";
226236
}
227237

@@ -236,6 +246,7 @@ final public function getSeverityTitle(throwable $exception): ?string
236246
if ($exception instanceof ErrorException) {
237247
$severityTitle = SeverityLevelPool::getSeverityLevel($exception->getSeverity(), "Error");
238248
}
249+
239250
return $severityTitle;
240251
}
241252

@@ -247,22 +258,26 @@ final public function getSeverityTitle(throwable $exception): ?string
247258
*/
248259
final protected function getTraceCodeBlock(array $trace): array
249260
{
250-
$block = array();
261+
$block = [];
251262
foreach ($trace as $key => $stackPoint) {
252-
if(isset($stackPoint['file']) && is_file($stackPoint['file'])) {
253-
$stream = $this->http->stream($stackPoint['file']);
254-
$code = $this->getContentsBetween($stream, $stackPoint['line']);
263+
if(is_array($stackPoint) && isset($stackPoint['file']) && is_file((string)$stackPoint['file'])) {
264+
$stream = $this->getStream($stackPoint['file']);
265+
$code = $this->getContentsBetween($stream, (int)$stackPoint['line']);
255266
$block[] = $this->getCodeBlock($stackPoint, $code, $key);
256267
$stream->close();
257268
}
258269
}
270+
259271
return $block;
260272
}
261273

262274
/**
275+
* Used to fetch valid asset
276+
* @param string $file
277+
* @return string
263278
* @throws ErrorException
264279
*/
265-
public function getAssetContent($file): string
280+
public function getAssetContent(string $file): string
266281
{
267282
$ending = explode(".", $file);
268283
$ending = end($ending);
@@ -271,7 +286,8 @@ public function getAssetContent($file): string
271286
throw new ErrorException("Only JS and CSS files are allowed as assets files");
272287
}
273288
$filePath = (str_starts_with($file, "/") ? realpath($file) : realpath(__DIR__ . "/../") . "/" . $file);
274-
$stream = $this->http->stream($filePath);
289+
$stream = $this->getStream($filePath);
290+
275291
return $stream->getContents();
276292
}
277293

@@ -304,4 +320,21 @@ final protected function cleanOutputBuffers(): void
304320
}
305321
}
306322
}
307-
}
323+
324+
325+
/**
326+
* Will get valid stream
327+
* @param mixed|null $stream
328+
* @param string $permission
329+
* @return StreamInterface
330+
*/
331+
final protected function getStream(mixed $stream = null, string $permission = "r+"): StreamInterface
332+
{
333+
if(is_null($this->http)) {
334+
throw new \BadMethodCallException("You Must initialize the stream before calling this method");
335+
}
336+
337+
return $this->http->stream($stream, $permission);
338+
}
339+
340+
}

0 commit comments

Comments
 (0)