Test Setup Failed
Push — test ( 528f91...abcbcc )
by Jonathan
03:20
created

MethodObject   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 215
Duplicated Lines 5.58 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 12
loc 215
rs 8.3396
c 0
b 0
f 0
wmc 44
lcom 2
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 5 51 9
C setAccessPathFrom() 0 40 7
C getValueShort() 0 22 7
B getModifiers() 0 21 7
A getAccessPath() 7 10 3
B getParams() 0 26 6
B getPhpDocUrl() 0 20 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like MethodObject often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MethodObject, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Kint\Object;
4
5
use Kint\Object\Representation\DocstringRepresentation;
6
use ReflectionFunctionAbstract;
7
use ReflectionMethod;
8
9
class MethodObject extends BasicObject
10
{
11
    public $type = 'method';
12
    public $filename;
13
    public $startline;
14
    public $endline;
15
    public $parameters = array();
16
    public $abstract;
17
    public $final;
18
    public $internal;
19
    public $docstring;
20
    public $returntype = null;
21
    public $return_reference = false;
0 ignored issues
show
Coding Style introduced by
$return_reference does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
22
    public $hints = array('callable', 'method');
23
    public $showparams = true;
24
25
    private $paramcache = null;
26
27
    public function __construct(ReflectionFunctionAbstract $method)
28
    {
29
        parent::__construct();
30
31
        $this->name = $method->getName();
32
        $this->filename = $method->getFileName();
33
        $this->startline = $method->getStartLine();
34
        $this->endline = $method->getEndLine();
35
        $this->internal = $method->isInternal();
36
        $this->docstring = $method->getDocComment();
37
        $this->return_reference = $method->returnsReference();
38
39
        foreach ($method->getParameters() as $param) {
40
            $this->parameters[] = new ParameterObject($param);
41
        }
42
43
        if (KINT_PHP70) {
44
            $this->returntype = $method->getReturnType();
45
            if ($this->returntype) {
46
                $this->returntype = (string) $this->returntype;
47
            }
48
        }
49
50
        if ($method instanceof ReflectionMethod) {
51
            $this->static = $method->isStatic();
52
            $this->operator = $this->static ? BasicObject::OPERATOR_STATIC : BasicObject::OPERATOR_OBJECT;
53
            $this->abstract = $method->isAbstract();
54
            $this->final = $method->isFinal();
55
            $this->owner_class = $method->getDeclaringClass()->name;
56
            $this->access = BasicObject::ACCESS_PUBLIC;
57 View Code Duplication
            if ($method->isProtected()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
                $this->access = BasicObject::ACCESS_PROTECTED;
59
            } elseif ($method->isPrivate()) {
60
                $this->access = BasicObject::ACCESS_PRIVATE;
61
            }
62
        }
63
64
        if ($this->internal) {
65
            return;
66
        }
67
68
        $docstring = new DocstringRepresentation(
69
            $this->docstring,
70
            $this->filename,
71
            $this->startline
72
        );
73
74
        $docstring->implicit_label = true;
75
        $this->addRepresentation($docstring);
76
        $this->value = $docstring;
77
    }
78
79
    public function setAccessPathFrom(InstanceObject $parent)
80
    {
81
        static $magic = array(
82
            '__call' => true,
83
            '__callstatic' => true,
84
            '__clone' => true,
85
            '__construct' => true,
86
            '__debuginfo' => true,
87
            '__destruct' => true,
88
            '__get' => true,
89
            '__invoke' => true,
90
            '__isset' => true,
91
            '__set' => true,
92
            '__set_state' => true,
93
            '__sleep' => true,
94
            '__tostring' => true,
95
            '__unset' => true,
96
            '__wakeup' => true,
97
        );
98
99
        $name = strtolower($this->name);
100
101
        if ($name === '__construct') {
102
            $this->access_path = 'new \\'.$parent->getType();
103
        } elseif ($name === '__invoke') {
104
            $this->access_path = $parent->access_path;
105
        } elseif ($name === '__clone') {
106
            $this->access_path = 'clone '.$parent->access_path;
107
            $this->showparams = false;
108
        } elseif ($name === '__tostring') {
109
            $this->access_path = '(string) '.$parent->access_path;
110
            $this->showparams = false;
111
        } elseif (isset($magic[$name])) {
112
            $this->access_path = null;
113
        } elseif ($this->static) {
114
            $this->access_path = '\\'.$this->owner_class.'::'.$this->name;
115
        } else {
116
            $this->access_path = $parent->access_path.'->'.$this->name;
117
        }
118
    }
119
120
    public function getValueShort()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
121
    {
122
        if (!$this->value || !($this->value instanceof DocstringRepresentation)) {
123
            return parent::getValueShort();
124
        }
125
126
        $ds = explode("\n", $this->value->docstringWithoutComments());
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ds. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
127
128
        $out = '';
129
130
        foreach ($ds as $line) {
131
            if (strlen(trim($line)) === 0 || $line[0] === '@') {
132
                break;
133
            }
134
135
            $out .= $line.' ';
136
        }
137
138
        if (strlen($out)) {
139
            return rtrim($out);
140
        }
141
    }
142
143
    public function getModifiers()
144
    {
145
        $mods = array(
146
            $this->abstract ? 'abstract' : null,
147
            $this->final ? 'final' : null,
148
            $this->getAccess(),
149
            $this->static ? 'static' : null,
150
        );
151
152
        $out = '';
153
154
        foreach ($mods as $word) {
155
            if ($word !== null) {
156
                $out .= $word.' ';
157
            }
158
        }
159
160
        if (strlen($out)) {
161
            return rtrim($out);
162
        }
163
    }
164
165
    public function getAccessPath()
166
    {
167 View Code Duplication
        if ($this->access_path !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
            if ($this->showparams) {
169
                return parent::getAccessPath().'('.$this->getParams().')';
170
            } else {
171
                return parent::getAccessPath();
172
            }
173
        }
174
    }
175
176
    public function getParams()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
177
    {
178
        if ($this->paramcache !== null) {
179
            return $this->paramcache;
180
        }
181
182
        $out = array();
183
184
        foreach ($this->parameters as $p) {
185
            $type = $p->getType();
186
            if ($type) {
187
                $type .= ' ';
188
            }
189
190
            $default = $p->getDefault();
191
            if ($default) {
192
                $default = ' = '.$default;
193
            }
194
195
            $ref = $p->reference ? '&' : '';
196
197
            $out[] = $type.$ref.$p->getName().$default;
198
        }
199
200
        return $this->paramcache = implode(', ', $out);
201
    }
202
203
    public function getPhpDocUrl()
204
    {
205
        if (!$this->internal) {
206
            return null;
207
        }
208
209
        if ($this->owner_class) {
210
            $class = strtolower($this->owner_class);
211
        } else {
212
            $class = 'function';
213
        }
214
215
        $funcname = str_replace('_', '-', strtolower($this->name));
216
217
        if (strpos($funcname, '--') === 0 && strpos($funcname, '-', 2) !== 0) {
218
            $funcname = substr($funcname, 2);
219
        }
220
221
        return 'https://secure.php.net/'.$class.'.'.$funcname;
222
    }
223
}
224