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

CallablePlugin   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 145
Duplicated Lines 18.62 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 27
loc 145
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A renderObject() 0 10 3
B renderClosure() 9 23 6
B renderCallable() 9 23 6
F renderMethod() 9 81 21

How to fix   Duplicated Code   

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:

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