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

MethodObject::getValueShort()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 7
nop 0
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
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