Skip to content

Commit

Permalink
% refactoring array serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
EdmondDantes committed Nov 14, 2024
1 parent 4d02948 commit b55274a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 253 deletions.
74 changes: 0 additions & 74 deletions src/ArraySerializerTrait.php

This file was deleted.

31 changes: 25 additions & 6 deletions src/BaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,42 @@
class BaseException extends \Exception implements BaseExceptionInterface
{
use HelperTrait;
use ArraySerializerTrait;
use TemplateHandlerTrait;

/**
* @param \Throwable|null $throwable
* @param int $recursion
*
* @return array<string, scalar|array<string, scalar[]>>
* @return array<string, scalar|array<string, scalar[]>>|null
*/
public static function serializeToArray(\Throwable|null $throwable = null): array
public static function serializeToArray(\Throwable|null $throwable = null, int $recursion = 0): array|null
{
if($throwable === null) {
return null;
}

if ($recursion > 8) {
return [
'message' => 'Depth of exceptions is greater than 8',
'code' => 0,
'source' => self::getSourceFor($throwable),
'file' => __FILE__,
'line' => __LINE__,
'previous' => null,
];
}

if ($throwable instanceof BaseExceptionInterface) {
return $throwable->toArray();
}

return [
'message' => $throwable->getMessage(),
'code' => $throwable->getCode(),
'source' => self::getSourceFor($throwable),
'file' => $throwable->getFile(),
'line' => $throwable->getLine()
'line' => $throwable->getLine(),
'previous' => self::serializeToArray($throwable->getPrevious(), $recursion + 1),
];
}

Expand Down Expand Up @@ -319,7 +336,7 @@ public function getSource(): ?array
return $this->source;
}

return $this->source = $this->getSourceFor($this);
return $this->source = self::getSourceFor($this);
}

/**
Expand Down Expand Up @@ -390,12 +407,13 @@ public function toArray(): array
$res =
[
'type' => $previous::class,
'source' => $this->getSourceFor($previous),
'source' => self::getSourceFor($previous),
'file' => $this->getFile(),
'line' => $this->getLine(),
'message' => $previous->getMessage(),
'code' => $previous->getCode(),
'data' => [],
'previous' => self::serializeToArray($previous->getPrevious()),
];
}

Expand All @@ -420,6 +438,7 @@ public function toArray(): array
'tags' => $this->getTags(),
'code' => $this->getCode(),
'data' => $this->getExceptionData(),
'previous' => self::serializeToArray($this->getPrevious())
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/HelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait HelperTrait
* The method defines the source of the exception.
* @return array<string, scalar>|string
*/
final protected function getSourceFor(\Throwable $e, bool $isString = false): array|string
final protected static function getSourceFor(\Throwable $e, bool $isString = false): array|string
{
$res = $e->getTrace()[0];

Expand Down
2 changes: 1 addition & 1 deletion src/UnhandledException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(\Throwable $exception)
{
parent::__construct([
'type' => $this->typeInfo($exception),
'source' => $this->getSourceFor($exception),
'source' => self::getSourceFor($exception),
'previous' => $exception,
]);
}
Expand Down
169 changes: 0 additions & 169 deletions tests/ArraySerializerTraitTest.php

This file was deleted.

10 changes: 9 additions & 1 deletion tests/BaseExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public function testToArrayForContainer(): void
'code' => 2,
'data' => [],
'container' => \IfCastle\Exceptions\LoggableException::class,
'previous' => null,
];

$this->assertIsArray($data, 'data must be array');
Expand Down Expand Up @@ -271,6 +272,7 @@ public function testToArrayForContainer2(): void
'code' => 0,
'data' => ['exdata' => $data],
'container' => \IfCastle\Exceptions\LoggableException::class,
'previous' => null
];

$this->assertIsArray($data, 'data must be array');
Expand All @@ -290,6 +292,7 @@ public function testToArrayForTemplate(): void
$test = new \ArrayObject([1, 2, 3]);

$exception = new \IfCastle\Exceptions\UnexpectedValueType('$test', $test, 'string');
$line = __LINE__ - 1;

$data = $exception->toArray();

Expand All @@ -298,10 +301,13 @@ public function testToArrayForTemplate(): void
'type' => UnexpectedValueType::class,
'source' => ['source' => static::class, 'type' => '->', 'function' => 'testToArrayForTemplate'],
'file' => __FILE__,
'line' => $this->line,
'line' => $line,
'message' => '',
'template' => 'Unexpected type occurred for the value {name} and type {type}. Expected {expected}',
'code' => 0,
'data' => ['name' => '$test', 'type' => \ArrayObject::class, 'expected' => 'string'],
'tags' => [],
'previous' => null,
];

$this->assertIsArray($data, 'data must be array');
Expand Down Expand Up @@ -387,13 +393,15 @@ public function testSerializeToArray(): void
$array = BaseException::serializeToArray($baseException);

$this->assertArrayHasKey('message', $array, 'message key not found');
$this->assertArrayHasKey('source', $array, 'source key not found');
$this->assertArrayHasKey('code', $array, 'code key not found');
$this->assertArrayHasKey('file', $array, 'file key not found');
$this->assertArrayHasKey('line', $array, 'line key not found');

$array = BaseException::serializeToArray(new \Exception('Some message'));

$this->assertArrayHasKey('message', $array, 'message key not found');
$this->assertArrayHasKey('source', $array, 'source key not found');
$this->assertArrayHasKey('code', $array, 'code key not found');
$this->assertArrayHasKey('file', $array, 'file key not found');
$this->assertArrayHasKey('line', $array, 'line key not found');
Expand Down
Loading

0 comments on commit b55274a

Please sign in to comment.