Passed
Branch master (014a21)
by Php Easy Api
04:56 queued 02:05
created

ExceptionTrace::exceptionTranslate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Exception;
4
5
use Resta\Support\Utils;
6
use Resta\Support\Dependencies;
7
use Resta\Foundation\ApplicationProvider;
8
9
class ExceptionTrace extends ApplicationProvider
10
{
11
    /**
12
     * ExceptionTrace constructor.
13
     * @param $app
14
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
15
     * @param array $params
16
     */
17
    public function __construct($app,$name=null,$params=array())
18
    {
19
        parent::__construct($app);
20
21
        // we help the user to pull a special message from
22
        // the translate section to be specified by the user for the exception.
23
        $this->exceptionTranslate($name,$params);
24
25
        // for real file path with
26
        // debug backtrace method are doing follow.
27
        $this->debugBackTrace();
28
    }
29
30
    /**
31
     * get exception translate params
32
     *
33
     * @param $name
34
     * @param array $params
35
     */
36
    private function exceptionTranslate($name,$params=array())
37
    {
38
        if($name!==null){
39
            if(count($params)){
40
                $this->app->register('exceptionTranslateParams',$name,$params);
41
            }
42
            $this->app->register('exceptionTranslate',$name);
43
        }
44
    }
45
46
    /**
47
     * get debug backtrace for exception
48
     *
49
     * @return mixed|void
50
     */
51
    public function debugBackTrace()
52
    {
53
        foreach (debug_backtrace() as $key=>$value){
54
55
            if(isset(debug_backtrace()[$key],debug_backtrace()[$key]['file']))
56
            {
57
                $this->app->register('exceptionFile',debug_backtrace()[$key]['file']);
58
                $this->app->register('exceptionLine',debug_backtrace()[$key]['line']);
59
            }
60
61
            Dependencies::loadBootstrapperNeedsForException();
62
63
            if(isset($value['file']) && isset(core()->urlComponent)){
64
                if(preg_match('@'.core()->urlComponent['project'].'|boot|providers@',$value['file'])){
65
66
                    $this->app->terminate('exceptionFile');
67
                    $this->app->terminate('exceptionLine');
68
                    $this->app->register('exceptionFile',$value['file']);
69
                    $this->app->register('exceptionLine',$value['line']);
70
71
                    break;
72
                }
73
            }
74
        }
75
    }
76
77
    /**
78
     * @param $name
79
     */
80
    public function __get($name)
81
    {
82
        $this->customException($name);
83
    }
84
85
    /**
86
     * get custom exception with message
87
     *
88
     * @param $name
89
     * @param null|string $msg
90
     */
91
    public function customException($name,$msg=null)
92
    {
93
        //We use the magic method for the exception and
94
        //call the exception class in the application to get the instance.
95
        $nameException = ucfirst($name).'Exception';
96
        $nameNamespace = app()->namespace()->exception().'\\'.$nameException;
97
98
        // first, you are looking for an exception
99
        // in the application directory class.
100
        if(Utils::isNamespaceExists($nameNamespace)){
101
            $callNamespace = new $nameNamespace;
102
        }
103
        else{
104
105
            // if you do not have an exception in the application directory,
106
            // this time we are looking for an exception in the core directory.
107
            $nameNamespace = __NAMESPACE__.'\\'.$nameException;
108
            if(Utils::isNamespaceExists($nameNamespace)){
109
                $callNamespace = new $nameNamespace;
110
            }
111
        }
112
113
        if(isset($callNamespace)){
114
115
            // we will set the information about the exception trace,
116
            // and then bind it specifically to the event method.
117
            $customExceptionTrace                       = Utils::trace(1);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $customExceptionTrace is correct as Resta\Support\Utils::trace(1) targeting Resta\Support\Utils::trace() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
118
            $customExceptionTrace['exception']          = $nameNamespace;
119
            $customExceptionTrace['callNamespace']      = $callNamespace;
120
            $customExceptionTrace['parameters']['get']  = get();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $customExceptionTrace['parameters']['get'] is correct as get() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
121
            $customExceptionTrace['parameters']['post'] = post();
122
123
124
            // we register the custom exception trace value with the global kernel object.
125
            $this->app->register('exceptiontrace',$customExceptionTrace);
126
127
            //If the developer wants to execute an event when calling a special exception,
128
            //we process the event method.
129
            if(method_exists($callNamespace,'event')){
130
                $callNamespace->event($customExceptionTrace);
131
            }
132
133
            //throw exception
134
            if($msg===null){
135
                throw new $callNamespace;
136
            }
137
            else{
138
                throw new $callNamespace($msg);
139
            }
140
141
        }
142
    }
143
144
    /**
145
     * set custom exception with message
146
     *
147
     * @param $name
148
     * @param $arguments
149
     */
150
    public function __call($name, $arguments)
151
    {
152
        $this->customException($name,current($arguments));
153
    }
154
}