Passed
Push — master ( ba645d...9eba8c )
by Michael
07:09 queued 43s
created

TraceFrameObject::__construct()   F

Complexity

Conditions 20
Paths 8448

Size

Total Lines 60
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 20
eloc 39
c 1
b 0
f 0
nc 8448
nop 2
dl 0
loc 60
rs 0

How to fix   Long Method    Complexity   

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:

1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2013 Jonathan Vollebregt ([email protected]), Rokas Šleinius ([email protected])
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9
 * this software and associated documentation files (the "Software"), to deal in
10
 * the Software without restriction, including without limitation the rights to
11
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12
 * the Software, and to permit persons to whom the Software is furnished to do so,
13
 * subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in all
16
 * copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
20
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
21
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 */
25
26
namespace Kint\Object;
27
28
use Kint\Object\Representation\Representation;
29
use Kint\Object\Representation\SourceRepresentation;
30
use ReflectionFunction;
31
use ReflectionMethod;
32
33
class TraceFrameObject extends BasicObject
34
{
35
    public $trace;
36
    public $hints = array('trace_frame');
37
38
    public function __construct(BasicObject $base, array $raw_frame)
39
    {
40
        parent::__construct();
41
42
        $this->transplant($base);
43
44
        $this->trace = array(
45
            'function' => isset($raw_frame['function']) ? $raw_frame['function'] : null,
46
            'line' => isset($raw_frame['line']) ? $raw_frame['line'] : null,
47
            'file' => isset($raw_frame['file']) ? $raw_frame['file'] : null,
48
            'class' => isset($raw_frame['class']) ? $raw_frame['class'] : null,
49
            'type' => isset($raw_frame['type']) ? $raw_frame['type'] : null,
50
            'object' => null,
51
            'args' => null,
52
        );
53
54
        if ($this->trace['class'] && \method_exists($this->trace['class'], $this->trace['function'])) {
0 ignored issues
show
Bug introduced by
It seems like $this->trace['function'] can also be of type null; however, parameter $method of method_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        if ($this->trace['class'] && \method_exists($this->trace['class'], /** @scrutinizer ignore-type */ $this->trace['function'])) {
Loading history...
55
            $func = new ReflectionMethod($this->trace['class'], $this->trace['function']);
56
            $this->trace['function'] = new MethodObject($func);
57
        } elseif (!$this->trace['class'] && \function_exists($this->trace['function'])) {
0 ignored issues
show
Bug introduced by
It seems like $this->trace['function'] can also be of type null; however, parameter $function of function_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        } elseif (!$this->trace['class'] && \function_exists(/** @scrutinizer ignore-type */ $this->trace['function'])) {
Loading history...
58
            $func = new ReflectionFunction($this->trace['function']);
59
            $this->trace['function'] = new MethodObject($func);
60
        }
61
62
        foreach ($this->value->contents as $frame_prop) {
63
            if ('object' === $frame_prop->name) {
64
                $this->trace['object'] = $frame_prop;
65
                $this->trace['object']->name = null;
66
                $this->trace['object']->operator = BasicObject::OPERATOR_NONE;
67
            }
68
            if ('args' === $frame_prop->name) {
69
                $this->trace['args'] = $frame_prop->value->contents;
70
71
                if ($this->trace['function'] instanceof MethodObject) {
72
                    foreach (\array_values($this->trace['function']->parameters) as $param) {
73
                        if (isset($this->trace['args'][$param->position])) {
74
                            $this->trace['args'][$param->position]->name = $param->getName();
75
                        }
76
                    }
77
                }
78
            }
79
        }
80
81
        $this->clearRepresentations();
82
83
        if (isset($this->trace['file'], $this->trace['line']) && \is_readable($this->trace['file'])) {
84
            $this->addRepresentation(new SourceRepresentation($this->trace['file'], $this->trace['line']));
85
        }
86
87
        if ($this->trace['args']) {
88
            $args = new Representation('Arguments');
89
            $args->contents = $this->trace['args'];
90
            $this->addRepresentation($args);
91
        }
92
93
        if ($this->trace['object']) {
94
            $callee = new Representation('object');
95
            $callee->label = 'Callee object ['.$this->trace['object']->classname.']';
96
            $callee->contents[] = $this->trace['object'];
97
            $this->addRepresentation($callee);
98
        }
99
    }
100
}
101