ErrorObjectHelper   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
eloc 18
c 3
b 0
f 0
dl 0
loc 47
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A get() 0 40 4
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
        // ErrorHandler.throwErrorException already converts Error to ErrorException,
24
        // and also clears the last error.
25
        // $lastError will contain data if an error happens before PHP script exection
26
        // (so before the error handler is registered)
27
        $lastError = \error_get_last();
28
29
        if ($lastError) {
30
            \error_clear_last();
31
            $errorException = new \ErrorException(
32
                $lastError['message'], // message
33
                0, // code
34
                $lastError['type'], // severity
35
                $lastError['file'], // filename
36
                $lastError['line'], // lineno
37
                null, // previous
38
            );
39
40
            // Handle: "Error: POST Content-Length of X bytes exceeds the limit of Y bytes in Unknown:0."
41
            if (false !== \strpos($lastError['message'], 'POST Content-Length of ')) {
42
                return new \WebServCo\Framework\Exceptions\UploadException(
43
                    \WebServCo\Framework\Files\Upload\Codes::INI_SIZE, // code
44
                    $errorException, // previous
45
                );
46
            }
47
48
            return $errorException;
49
        }
50
51
        // No error
52
53
        return null;
54
    }
55
}
56