FatalErrorException::__construct()   C
last analyzed

Complexity

Conditions 15
Paths 6

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 29
c 1
b 0
f 0
nc 6
nop 9
dl 0
loc 56
rs 5.9166

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Debug\FatalExceptions;
24
25
use Throwable;
26
use ErrorException;
27
use ReflectionProperty;
28
29
/**
30
 * Fatal Error Exception.
31
 * 
32
 * @author Alexander Campo <[email protected]>
33
 */
34
class FatalErrorException extends ErrorException
35
{
36
    /**
37
     * Constructor. Initialize FatalErrorException class.
38
     * 
39
     * @param  string  $message
40
     * @param  int  $code
41
     * @param  int  $severity
42
     * @param  string  $filename
43
     * @param  int  $lineno
44
     * @param  int|null  $traceOffset
45
     * @param  bool  $traceArgs
46
     * @param  array|null  $trace
47
     * @param  \Throwable  $previous
48
     * 
49
     * @return void
50
     */
51
    public function __construct(
52
        string    $message,
53
        int       $code,
54
        int       $severity,
55
        string    $filename,
56
        int       $lineno,
57
        int       $traceOffset = null,
58
        bool      $traceArgs = true,
59
        array     $trace = [],
60
        Throwable $previous = null
61
    )
62
    {
63
        parent::__construct($message, $code, $severity, $filename, $lineno, $previous);
64
65
        if (null !== $trace) {
0 ignored issues
show
introduced by
The condition null !== $trace is always true.
Loading history...
66
            if ( ! $traceArgs) {
67
                foreach ($trace as &$frame) {
68
                    unset($frame['args'], $frame['this'], $frame);
69
                }
70
            }
71
72
            $this->setTrace($trace);
73
        } elseif (null !== $traceOffset) {
74
            if (function_exists('xdebug_get_function_stack')) {
75
                $trace = xdebug_get_function_stack();
76
77
                if ($traceOffset > 0) {
78
                    array_slice($trace, -$traceOffset);
79
                }
80
81
                foreach ($trace as &$frame) {
82
                    if ( ! isset($frame['type'])) {
83
                        if (isset($frame['class'])) {
84
                            $frame['type'] = '::';
85
                        }
86
                    } elseif ('dynamic' === $frame['type']) {
87
                        $frame['type'] = '->';
88
                    } elseif ('static' === $frame['type']) {
89
                        $frame['type'] = '::';
90
                    }
91
92
                    if ( ! $traceArgs) {
93
                        unset($frame['params'], $frame['args']);
94
                    } elseif (isset($frame['params']) && ! $frame['args']) {
95
                        $frame['args'] = $frame['params'];
96
                        unset($frame['params']);
97
                    }
98
                }
99
                
100
                unset($frame);
101
                $trace = array_reverse($trace);
102
            } else {
103
                $trace = [];
104
            }
105
106
            $this->setTrace($trace);
107
        }
108
    }
109
110
    /**
111
     * Gets reports information about of Exception class properties trace.
112
     * 
113
     * @param  array  $trace
114
     * 
115
     * @return void
116
     */
117
    protected function setTrace($trace)
118
    {
119
        $reflection = new ReflectionProperty('Exception', 'trace');
120
        $reflection->setAccessible(true);
121
        $reflection->setValue($this, $trace);
122
    }          
123
}