Completed
Push — next ( 3fc9f0...5d2fc6 )
by Jonathan
9s
created

MethodObjectTest   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 271
Duplicated Lines 6.64 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 18
loc 271
rs 10
c 1
b 0
f 0
wmc 17
lcom 1
cbo 4

13 Methods

Rating   Name   Duplication   Size   Complexity  
B testConstruct() 0 40 1
A testConstructWrongType() 0 9 2
A testSetAccessPathFrom() 0 51 1
A testGetValueShort() 0 14 1
A testGetModifiers() 0 17 1
A testGetAccessPath() 0 7 1
B testGetParams() 0 31 2
A testGetParamsPhp7() 9 9 2
A testReturnType() 9 9 2
A testGetPhpDocUrl() 0 8 1
A testGetPhpDocUrlParent() 0 8 1
A testGetPhpDocUrlUserDefined() 0 5 1
A testGetPhpDocUrlFunction() 0 8 1

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\Test\Object;
4
5
use Kint\Object\BasicObject;
6
use Kint\Object\InstanceObject;
7
use Kint\Object\MethodObject;
8
use PHPUnit_Framework_TestCase;
9
use ReflectionFunction;
10
use ReflectionMethod;
11
use stdClass;
12
13
class MethodObjectTest extends PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @covers \Kint\Object\MethodObject::__construct
17
     */
18
    public function testConstruct()
19
    {
20
        $reflection = new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'mix');
21
        $m = new MethodObject($reflection);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
22
        $this->assertEquals('mix', $m->name);
23
        $this->assertEquals($reflection->getFileName(), $m->filename);
24
        $this->assertEquals($reflection->getStartLine(), $m->startline);
25
        $this->assertEquals($reflection->getEndLine(), $m->endline);
26
        $this->assertEquals(false, $m->internal);
27
        $this->assertEquals(true, $m->return_reference);
28
        $this->assertEquals($reflection->getDocComment(), $m->docstring);
29
        $this->assertEquals(BasicObject::OPERATOR_STATIC, $m->operator);
30
        $this->assertEquals(BasicObject::ACCESS_PROTECTED, $m->access);
31
        $this->assertEquals('Kint\\Test\\Fixtures\\TestClass', $m->owner_class);
32
        $this->assertTrue($m->static);
33
        $this->assertTrue($m->final);
34
        $this->assertFalse($m->abstract);
35
        $this->assertFalse($m->internal);
36
37
        $reflection = new ReflectionMethod('Kint\\Test\\Fixtures\\ChildTestClass', '__construct');
38
        $parent_reflection = new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__construct');
0 ignored issues
show
Coding Style introduced by
$parent_reflection 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...
39
        $m = new MethodObject($reflection);
40
        $this->assertEquals($parent_reflection->getDocComment(), $m->docstring);
0 ignored issues
show
Coding Style introduced by
$parent_reflection 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...
41
        $this->assertEquals(BasicObject::OPERATOR_OBJECT, $m->operator);
42
        $this->assertEquals(BasicObject::ACCESS_PUBLIC, $m->access);
43
        $this->assertEquals('Kint\\Test\\Fixtures\\TestClass', $m->owner_class);
44
45
        $reflection = new ReflectionMethod('Kint\\Test\\Fixtures\\ChildTestClass', 'classHint');
46
        $m = new MethodObject($reflection);
47
        $this->assertEquals(BasicObject::OPERATOR_OBJECT, $m->operator);
48
        $this->assertEquals(BasicObject::ACCESS_PRIVATE, $m->access);
49
        $this->assertEquals('Kint\\Test\\Fixtures\\TestClass', $m->owner_class);
50
51
        $reflection = new ReflectionFunction('explode');
52
        $m = new MethodObject($reflection);
53
        $this->assertTrue($m->internal);
54
        $this->assertEquals(BasicObject::OPERATOR_NONE, $m->operator);
55
        $this->assertEquals(BasicObject::ACCESS_NONE, $m->access);
56
        $this->assertEquals(null, $m->owner_class);
57
    }
58
59
    /**
60
     * @covers \Kint\Object\MethodObject::__construct
61
     */
62
    public function testConstructWrongType()
63
    {
64
        if (KINT_PHP70) {
65
            $this->setExpectedException('TypeError');
66
        } else {
67
            $this->setExpectedException('PHPUnit_Framework_Error');
68
        }
69
        $m = new MethodObject(new stdClass());
0 ignored issues
show
Documentation introduced by
new \stdClass() is of type object<stdClass>, but the function expects a object<ReflectionFunctionAbstract>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
$m is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
70
    }
71
72
    /**
73
     * @covers \Kint\Object\MethodObject::setAccessPathFrom
74
     * @covers \Kint\Object\MethodObject::getAccessPath
75
     */
76
    public function testSetAccessPathFrom()
77
    {
78
        $o = BasicObject::blank('$tc');
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...
79
        $o = $o->transplant(new InstanceObject());
80
        $o->classname = 'Kint\\Test\\Fixtures\\TestClass';
0 ignored issues
show
Bug introduced by
The property classname does not seem to exist in Kint\Object\BasicObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
81
82
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__construct'));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
83
        $this->assertNull($m->getAccessPath());
84
        $m->setAccessPathFrom($o);
0 ignored issues
show
Compatibility introduced by
$o of type object<Kint\Object\BasicObject> is not a sub-type of object<Kint\Object\InstanceObject>. It seems like you assume a child class of the class Kint\Object\BasicObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
85
        $this->assertEquals('new \\Kint\\Test\\Fixtures\\TestClass()', $m->getAccessPath());
86
87
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'staticMethod'));
88
        $this->assertNull($m->getAccessPath());
89
        $m->setAccessPathFrom($o);
0 ignored issues
show
Compatibility introduced by
$o of type object<Kint\Object\BasicObject> is not a sub-type of object<Kint\Object\InstanceObject>. It seems like you assume a child class of the class Kint\Object\BasicObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
90
        $this->assertEquals('\\Kint\\Test\\Fixtures\\TestClass::staticMethod()', $m->getAccessPath());
91
92
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'finalMethod'));
93
        $this->assertNull($m->getAccessPath());
94
        $m->setAccessPathFrom($o);
0 ignored issues
show
Compatibility introduced by
$o of type object<Kint\Object\BasicObject> is not a sub-type of object<Kint\Object\InstanceObject>. It seems like you assume a child class of the class Kint\Object\BasicObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
95
        $this->assertEquals('$tc->finalMethod()', $m->getAccessPath());
96
97
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'mix'));
98
        $this->assertNull($m->getAccessPath());
99
        $m->setAccessPathFrom($o);
0 ignored issues
show
Compatibility introduced by
$o of type object<Kint\Object\BasicObject> is not a sub-type of object<Kint\Object\InstanceObject>. It seems like you assume a child class of the class Kint\Object\BasicObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
100
        $this->assertEquals(
101
            '\\Kint\\Test\\Fixtures\\TestClass::mix(array &$x, Kint\\Test\\Fixtures\\TestClass $y = null, $z = array(...), $_ = \'string\')',
102
            $m->getAccessPath()
103
        );
104
105
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__clone'));
106
        $this->assertNull($m->getAccessPath());
107
        $m->setAccessPathFrom($o);
0 ignored issues
show
Compatibility introduced by
$o of type object<Kint\Object\BasicObject> is not a sub-type of object<Kint\Object\InstanceObject>. It seems like you assume a child class of the class Kint\Object\BasicObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
108
        $this->assertEquals('clone $tc', $m->getAccessPath());
109
110
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__invoke'));
111
        $this->assertNull($m->getAccessPath());
112
        $m->setAccessPathFrom($o);
0 ignored issues
show
Compatibility introduced by
$o of type object<Kint\Object\BasicObject> is not a sub-type of object<Kint\Object\InstanceObject>. It seems like you assume a child class of the class Kint\Object\BasicObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
113
        $this->assertEquals('$tc($x)', $m->getAccessPath());
114
115
        // Tests both tostring and case insensitivity
116
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__tostring'));
117
        $this->assertNull($m->getAccessPath());
118
        $m->setAccessPathFrom($o);
0 ignored issues
show
Compatibility introduced by
$o of type object<Kint\Object\BasicObject> is not a sub-type of object<Kint\Object\InstanceObject>. It seems like you assume a child class of the class Kint\Object\BasicObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
119
        $this->assertEquals('__ToStRiNg', $m->name);
120
        $this->assertEquals('(string) $tc', $m->getAccessPath());
121
122
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__get'));
123
        $this->assertNull($m->getAccessPath());
124
        $m->setAccessPathFrom($o);
0 ignored issues
show
Compatibility introduced by
$o of type object<Kint\Object\BasicObject> is not a sub-type of object<Kint\Object\InstanceObject>. It seems like you assume a child class of the class Kint\Object\BasicObject to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
125
        $this->assertNull($m->getAccessPath());
126
    }
127
128
    /**
129
     * @covers \Kint\Object\MethodObject::getValueShort
130
     */
131
    public function testGetValueShort()
132
    {
133
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', '__construct'));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
134
        $this->assertEquals(
135
            'This is a constructor for a TestClass with the first line of the docstring split into two different lines.',
136
            $m->getValueShort()
137
        );
138
139
        $m = new MethodObject(new ReflectionFunction('explode'));
140
        $this->assertNull($m->getValueShort());
141
142
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'arrayHint'));
143
        $this->assertNull($m->getValueShort());
144
    }
145
146
    /**
147
     * @covers \Kint\Object\MethodObject::getModifiers
148
     */
149
    public function testGetModifiers()
150
    {
151
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'staticMethod'));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
152
        $this->assertEquals('private static', $m->getModifiers());
153
154
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'finalMethod'));
155
        $this->assertEquals('final public', $m->getModifiers());
156
157
        $m = new MethodObject(new ReflectionMethod('ReflectionFunctionAbstract', '__toString'));
158
        $this->assertEquals('abstract public', $m->getModifiers());
159
160
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'mix'));
161
        $this->assertEquals('final protected static', $m->getModifiers());
162
163
        $m = new MethodObject(new ReflectionFunction('explode'));
164
        $this->assertNull($m->getModifiers());
165
    }
166
167
    /**
168
     * @covers \Kint\Object\MethodObject::getAccessPath
169
     */
170
    public function testGetAccessPath()
171
    {
172
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'arrayHint'));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
173
        $this->assertNull($m->getAccessPath());
174
        $m->access_path = '$m->arrayHint';
175
        $this->assertEquals('$m->arrayHint(array $x)', $m->getAccessPath());
176
    }
177
178
    /**
179
     * @covers \Kint\Object\MethodObject::getParams
180
     */
181
    public function testGetParams()
182
    {
183
        $m = new MethodObject(new ReflectionFunction('explode'));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
184
        if (defined('HHVM_VERSION')) {
185
            $this->assertStringStartsWith('HH\\string $delimiter, HH\\string $str, HH\\int $limit = ', $m->getParams());
186
        } else {
187
            $this->assertEquals('$separator, $str, $limit', $m->getParams());
188
        }
189
190
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'arrayHint'));
191
        $this->assertEquals('array $x', $m->getParams());
192
193
        // Testing cache
194
        $m->parameters = array();
195
        $this->assertEquals('array $x', $m->getParams());
196
197
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'classHint'));
198
        $this->assertEquals('Kint\\Test\\Fixtures\\TestClass $x', $m->getParams());
199
200
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'ref'));
201
        $this->assertEquals('&$x', $m->getParams());
202
203
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'defaultMethod'));
204
        $this->assertEquals('$x = 1234', $m->getParams());
205
206
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\TestClass', 'mix'));
207
        $this->assertEquals(
208
            'array &$x, Kint\\Test\\Fixtures\\TestClass $y = null, $z = array(...), $_ = \'string\'',
209
            $m->getParams()
210
        );
211
    }
212
213
    /**
214
     * @covers \Kint\Object\MethodObject::getParams
215
     */
216 View Code Duplication
    public function testGetParamsPhp7()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
217
    {
218
        if (!KINT_PHP70) {
219
            $this->markTestSkipped('Not testing PHP7+ parameter type hints on PHP5');
220
        }
221
222
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\Php7TestClass', 'typeHints'));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
223
        $this->assertEquals('string $p1, int $p2, bool $p3 = false', $m->getParams());
224
    }
225
226
    /**
227
     * @covers \Kint\Object\MethodObject::__construct
228
     */
229 View Code Duplication
    public function testReturnType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
230
    {
231
        if (!KINT_PHP70) {
232
            $this->markTestSkipped('Not testing PHP7+ return type hints on PHP5');
233
        }
234
235
        $m = new MethodObject(new ReflectionMethod('Kint\\Test\\Fixtures\\Php7TestClass', 'typeHints'));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
236
        $this->assertEquals('self', $m->returntype);
237
    }
238
239
    /**
240
     * @covers \Kint\Object\MethodObject::getPhpDocUrl
241
     */
242
    public function testGetPhpDocUrl()
243
    {
244
        $m = new MethodObject(new ReflectionMethod('ReflectionMethod', '__construct'));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
245
        $this->assertEquals(
246
            'https://secure.php.net/reflectionmethod.construct',
247
            $m->getPhpDocUrl()
248
        );
249
    }
250
251
    /**
252
     * @covers \Kint\Object\MethodObject::getPhpDocUrl
253
     */
254
    public function testGetPhpDocUrlParent()
255
    {
256
        $m = new MethodObject(new ReflectionMethod('ReflectionMethod', '__clone'));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
257
        $this->assertEquals(
258
            'https://secure.php.net/reflectionfunctionabstract.clone',
259
            $m->getPhpDocUrl()
260
        );
261
    }
262
263
    /**
264
     * @covers \Kint\Object\MethodObject::getPhpDocUrl
265
     */
266
    public function testGetPhpDocUrlUserDefined()
267
    {
268
        $m = new MethodObject(new ReflectionMethod(__CLASS__, __FUNCTION__));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
269
        $this->assertNull($m->getPhpDocUrl());
270
    }
271
272
    /**
273
     * @covers \Kint\Object\MethodObject::getPhpDocUrl
274
     */
275
    public function testGetPhpDocUrlFunction()
276
    {
277
        $m = new MethodObject(new ReflectionFunction('explode'));
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. 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...
278
        $this->assertEquals(
279
            'https://secure.php.net/function.explode',
280
            $m->getPhpDocUrl()
281
        );
282
    }
283
}
284