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

CallablePlugin::renderMethod()   F

Complexity

Conditions 21
Paths 865

Size

Total Lines 81
Code Lines 45

Duplication

Lines 9
Ratio 11.11 %

Importance

Changes 0
Metric Value
cc 21
eloc 45
nc 865
nop 1
dl 9
loc 81
rs 2.4691
c 0
b 0
f 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
namespace Kint\Renderer\Rich;
4
5
use Kint\Object\BasicObject;
6
use Kint\Object\BlobObject;
7
use Kint\Object\ClosureObject;
8
use Kint\Object\MethodObject;
9
use Kint\Renderer\RichRenderer;
10
11
class CallablePlugin extends Plugin implements ObjectPluginInterface
12
{
13
    protected static $method_cache = array();
0 ignored issues
show
Coding Style introduced by
$method_cache 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...
14
15
    public function renderObject(BasicObject $o)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. 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...
16
    {
17
        if ($o instanceof MethodObject) {
18
            return $this->renderMethod($o);
19
        } elseif ($o instanceof ClosureObject) {
20
            return $this->renderClosure($o);
21
        } else {
22
            return $this->renderCallable($o);
23
        }
24
    }
25
26
    protected function renderClosure(ClosureObject $o)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. 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...
27
    {
28
        $children = $this->renderer->renderChildren($o);
29
30
        $header = '';
31
32
        if (($s = $o->getModifiers()) !== null) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $s. 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...
33
            $header .= '<var>'.$s.'</var> ';
34
        }
35
36 View Code Duplication
        if (($s = $o->getName()) !== 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...
37
            $header .= '<dfn>'.$this->renderer->escape($s).'('.$this->renderer->escape($o->getParams()).')</dfn>';
38
        }
39
40 View Code Duplication
        if (($s = $o->getValueShort()) !== 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...
41
            if (RichRenderer::$strlen_max && BlobObject::strlen($s) > RichRenderer::$strlen_max) {
42
                $s = substr($s, 0, RichRenderer::$strlen_max).'...';
43
            }
44
            $header .= ' '.$this->renderer->escape($s);
45
        }
46
47
        return '<dl>'.$this->renderer->renderHeaderWrapper($o, (bool) strlen($children), $header).$children.'</dl>';
48
    }
49
50
    protected function renderCallable(BasicObject $o)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. 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...
51
    {
52
        $children = $this->renderer->renderChildren($o);
53
54
        $header = '';
55
56
        if (($s = $o->getModifiers()) !== null) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $s. 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...
57
            $header .= '<var>'.$s.'</var> ';
58
        }
59
60 View Code Duplication
        if (($s = $o->getName()) !== 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...
61
            $header .= '<dfn>'.$this->renderer->escape($s).'</dfn>';
62
        }
63
64 View Code Duplication
        if (($s = $o->getValueShort()) !== 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...
65
            if (RichRenderer::$strlen_max && BlobObject::strlen($s) > RichRenderer::$strlen_max) {
66
                $s = substr($s, 0, RichRenderer::$strlen_max).'...';
67
            }
68
            $header .= ' '.$this->renderer->escape($s);
69
        }
70
71
        return '<dl>'.$this->renderer->renderHeaderWrapper($o, (bool) strlen($children), $header).$children.'</dl>';
72
    }
73
74
    protected function renderMethod(MethodObject $o)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. 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...
75
    {
76
        if (!empty(self::$method_cache[$o->owner_class][$o->name])) {
77
            $children = self::$method_cache[$o->owner_class][$o->name]['children'];
78
79
            $header = $this->renderer->renderHeaderWrapper(
80
                $o,
81
                (bool) strlen($children),
82
                self::$method_cache[$o->owner_class][$o->name]['header']
83
            );
84
85
            return '<dl>'.$header.$children.'</dl>';
86
        }
87
88
        $children = $this->renderer->renderChildren($o);
89
90
        $header = '';
91
92
        if (($s = $o->getModifiers()) !== null || $o->return_reference) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $s. 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...
93
            $header .= '<var>'.$s;
94
95
            if ($o->return_reference) {
96
                if ($s) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $s of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
97
                    $header .= ' ';
98
                }
99
                $header .= $this->renderer->escape('&');
100
            }
101
102
            $header .= '</var> ';
103
        }
104
105
        if (($s = $o->getName()) !== null) {
106
            $function = $this->renderer->escape($s).'('.$this->renderer->escape($o->getParams()).')';
107
108 View Code Duplication
            if (($url = $o->getPhpDocUrl()) !== 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...
109
                $function = '<a href="'.$url.'" target=_blank>'.$function.'</a>';
110
            }
111
112
            $header .= '<dfn>'.$function.'</dfn>';
113
        }
114
115
        if (!empty($o->returntype)) {
116
            $header .= ': <var>';
117
118
            if ($o->return_reference) {
119
                $header .= $this->renderer->escape('&');
120
            }
121
122
            $header .= $this->renderer->escape($o->returntype).'</var>';
123
        } elseif ($o->docstring) {
124
            if (preg_match('/@return\s+(.*)\r?\n/m', $o->docstring, $matches)) {
125
                if (trim($matches[1])) {
126
                    $header .= ': <var>'.$this->renderer->escape(trim($matches[1])).'</var>';
127
                }
128
            }
129
        }
130
131 View Code Duplication
        if (($s = $o->getValueShort()) !== 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...
132
            if (RichRenderer::$strlen_max && BlobObject::strlen($s) > RichRenderer::$strlen_max) {
133
                $s = substr($s, 0, RichRenderer::$strlen_max).'...';
134
            }
135
            $header .= ' '.$this->renderer->escape($s);
136
        }
137
138
        if ($o instanceof MethodObject && strlen($o->owner_class) && strlen($o->name)) {
139
            $cache = array(
140
                'header' => $header,
141
                'children' => $children,
142
            );
143
144
            if (!isset(self::$method_cache[$o->owner_class])) {
145
                self::$method_cache[$o->owner_class] = array($o->name => $cache);
146
            } elseif (!isset(self::$method_cache[$o->owner_class][$o->name])) {
147
                self::$method_cache[$o->owner_class][$o->name] = $cache;
148
            }
149
        }
150
151
        $header = $this->renderer->renderHeaderWrapper($o, (bool) strlen($children), $header);
152
153
        return '<dl>'.$header.$children.'</dl>';
154
    }
155
}
156