Completed
Push — master ( 4604a2...31c42c )
by Radu
02:05
created

ErrorObjectHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 12
c 1
b 0
f 0
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A get() 0 27 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Helpers;
6
7
class ErrorObjectHelper
8
{
9
    /*
10
    * Get a \Throwable object if an error has occured.
11
    *
12
    * Used only for error logging / information display, not actually thrown.
13
    */
14
    public static function get(?\Throwable $exception = null): ?\Throwable
15
    {
16
        // Regular Exception, nothing further to do
17
18
        if ($exception instanceof \Throwable) {
19
            return $exception;
20
        }
21
22
        // A regular Error: create an ErrorException
23
        // There is already a sys to convert and Error to ErrorException, so in theory we should never arrive here.
24
25
        $last_error = \error_get_last();
26
27
        if ($last_error) {
28
            return new \ErrorException(
29
                $last_error['message'], // message
30
                0, // code
31
                $last_error['type'], // severity
32
                $last_error['file'], // filename
33
                $last_error['line'], // lineno
34
                null, // previous
35
            );
36
        }
37
38
        // No error
39
40
        return null;
41
    }
42
}
43