Completed
Pull Request — master (#546)
by Richard
09:47
created

Kint_Object_Method::getModifiers()   B

Complexity

Conditions 7
Paths 48

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 48
nop 0
dl 0
loc 21
rs 7.551
c 0
b 0
f 0
1
<?php
2
3
class Kint_Object_Method extends Kint_Object
4
{
5
    public $type = 'method';
6
    public $filename;
7
    public $startline;
8
    public $endline;
9
    public $parameters = array();
10
    public $abstract;
11
    public $final;
12
    public $internal;
13
    public $returntype = null;
14
    public $hints = array('callable', 'method');
15
    public $showparams = true;
16
17
    private $paramcache = null;
18
19
    public function __construct($method)
20
    {
21
        // PHP 5.1 compat (Docs don't correctly show which version ReflectionFunctionAbstract was added)
22
        if (!($method instanceof ReflectionMethod) && !($method instanceof ReflectionFunction)) {
23
            throw new InvalidArgumentException('Argument must be an instance of ReflectionFunctionAbstract');
24
        }
25
26
        $this->name = $method->getName();
27
        $this->filename = $method->getFilename();
28
        $this->startline = $method->getStartLine();
29
        $this->endline = $method->getEndLine();
30
        $this->internal = $method->isInternal();
31
        $this->docstring = $method->getDocComment();
0 ignored issues
show
Bug introduced by
The property docstring does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
33
        foreach ($method->getParameters() as $param) {
34
            $this->parameters[] = new Kint_Object_Parameter($param);
35
        }
36
37
        if (KINT_PHP70) {
38
            $this->returntype = $method->getReturnType();
39
            if ($this->returntype) {
40
                $this->returntype = (string) $this->returntype;
41
            }
42
        }
43
44
        if (!$this->returntype && $this->docstring) {
45
            if (preg_match('/@return\s+(.*)\r?\n/m', $this->docstring, $matches)) {
46
                if (!empty($matches[1])) {
47
                    $this->returntype = $matches[1];
48
                }
49
            }
50
        }
51
52
        if ($method instanceof ReflectionMethod) {
53
            $this->static = $method->isStatic();
54
            $this->operator = $this->static ? Kint_Object::OPERATOR_STATIC : Kint_Object::OPERATOR_OBJECT;
55
            $this->abstract = $method->isAbstract();
56
            $this->final = $method->isFinal();
57
            $this->owner_class = $method->getDeclaringClass()->name;
58
            $this->access = Kint_Object::ACCESS_PUBLIC;
59 View Code Duplication
            if ($method->isProtected()) {
60
                $this->access = Kint_Object::ACCESS_PROTECTED;
61
            } elseif ($method->isPrivate()) {
62
                $this->access = Kint_Object::ACCESS_PRIVATE;
63
            }
64
        }
65
66
        $docstring = new Kint_Object_Representation_Docstring(
67
            $this->docstring,
68
            $this->filename,
69
            $this->startline
70
        );
71
72
        $docstring->implicit_label = true;
73
        $this->addRepresentation($docstring);
74
    }
75
76
    public function setAccessPathFrom(Kint_Object_Instance $parent)
77
    {
78
        static $magic = array(
79
            '__call' => true,
80
            '__callstatic' => true,
81
            '__clone' => true,
82
            '__construct' => true,
83
            '__debuginfo' => true,
84
            '__destruct' => true,
85
            '__get' => true,
86
            '__invoke' => true,
87
            '__isset' => true,
88
            '__set' => true,
89
            '__set_state' => true,
90
            '__sleep' => true,
91
            '__tostring' => true,
92
            '__unset' => true,
93
            '__wakeup' => true,
94
        );
95
96
        $name = strtolower($this->name);
97
98
        if ($name === '__construct') {
99
            if (KINT_PHP53) {
100
                $this->access_path = 'new \\'.$parent->getType();
101
            } else {
102
                $this->access_path = 'new '.$parent->getType();
103
            }
104
        } elseif ($name === '__invoke') {
105
            $this->access_path = $parent->access_path;
106
        } elseif ($name === '__clone') {
107
            $this->access_path = 'clone '.$parent->access_path;
108
            $this->showparams = false;
109
        } elseif ($name === '__tostring') {
110
            $this->access_path = '(string) '.$parent->access_path;
111
            $this->showparams = false;
112
        } elseif (isset($magic[$name])) {
113
            $this->access_path = null;
114
        } elseif ($this->static) {
115
            if (KINT_PHP53) {
116
                $this->access_path = '\\'.$this->owner_class.'::'.$this->name;
117
            } else {
118
                $this->access_path = $this->owner_class.'::'.$this->name;
119
            }
120
        } else {
121
            $this->access_path = $parent->access_path.'->'.$this->name;
122
        }
123
    }
124
125
    public function getValueShort()
126
    {
127
        if (!$this->value || !($this->value instanceof Kint_Object_Representation_Docstring)) {
128
            return parent::getValueShort();
129
        }
130
131
        $ds = explode("\n", $this->value->docstringWithoutComments());
132
133
        $out = '';
134
135
        foreach ($ds as $line) {
136
            if (strlen(trim($line)) === 0 || $line[0] === '@') {
137
                break;
138
            }
139
140
            $out .= $line.' ';
141
        }
142
143
        if (strlen($out)) {
144
            return rtrim($out);
145
        }
146
    }
147
148
    public function getModifiers()
149
    {
150
        $mods = array(
151
            $this->abstract ? 'abstract' : null,
152
            $this->final ? 'final' : null,
153
            $this->getAccess(),
154
            $this->static ? 'static' : null,
155
        );
156
157
        $out = '';
158
159
        foreach ($mods as $word) {
160
            if ($word !== null) {
161
                $out .= $word.' ';
162
            }
163
        }
164
165
        if (strlen($out)) {
166
            return rtrim($out);
167
        }
168
    }
169
170
    public function getAccessPath()
171
    {
172 View Code Duplication
        if ($this->access_path !== null) {
173
            if ($this->showparams) {
174
                return parent::getAccessPath().'('.$this->getParams().')';
175
            } else {
176
                return parent::getAccessPath();
177
            }
178
        }
179
    }
180
181
    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...
182
    {
183
        if ($this->paramcache !== null) {
184
            return $this->paramcache;
185
        }
186
187
        $out = array();
188
189
        foreach ($this->parameters as $p) {
190
            $type = $p->getType();
191
            if ($type) {
192
                $type .= ' ';
193
            }
194
195
            $default = $p->getDefault();
196
            if ($default) {
197
                $default = ' = '.$default;
198
            }
199
200
            $ref = $p->reference ? '&' : '';
201
202
            $out[] = $type.$ref.$p->getName().$default;
203
        }
204
205
        return $this->paramcache = implode(', ', $out);
206
    }
207
208
    public function getPhpDocUrl()
209
    {
210
        if (!$this->internal) {
211
            return null;
212
        }
213
214
        if ($this->owner_class) {
215
            $class = strtolower($this->owner_class);
216
        } else {
217
            $class = 'function';
218
        }
219
220
        $funcname = str_replace('_', '-', strtolower($this->name));
221
222
        if (strpos($funcname, '--') === 0 && strpos($funcname, '-', 2) !== 0) {
223
            $funcname = substr($funcname, 2);
224
        }
225
226
        return 'https://secure.php.net/'.$class.'.'.$funcname;
227
    }
228
}
229