ExceptionExtractorTrait::getExceptionFromContext()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 19
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace SubjectivePHP\Psr\Log;
4
5
use Psr\Log\InvalidArgumentException;
6
7
/**
8
 * Trait for extracting the exception value from context for PSR-3 log messages.
9
 */
10
trait ExceptionExtractorTrait
11
{
12
    /**
13
     * Extracts and returns the exception value from the given context.
14
     *
15
     * @param array $context Any extraneous log information that does not fit well in a string.
16
     *
17
     * @return \Exception|null
18
     */
19
    protected function getExceptionFromContext(array $context)
20
    {
21
        $exception = $context['exception'] ?? null;
22
23
        if ($exception instanceof \Exception) {
24
            return $exception;
25
        }
26
27
        if ($exception instanceof \Error) {
28
            return new \ErrorException(
29
                $exception->getMessage(),
30
                0,
31
                $exception->getCode(),
32
                $exception->getFile(),
33
                $exception->getLine()
34
            );
35
        }
36
37
        return null;
38
    }
39
}
40