1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MS\PHPMD\Tests\Unit\CleanCode; |
4
|
|
|
|
5
|
|
|
use MS\PHPMD\Rule\CleanCode\TraitPublicMethod; |
6
|
|
|
use MS\PHPMD\Tests\Unit\AbstractApplyTest; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class TraitPublicMethodTest |
10
|
|
|
* |
11
|
|
|
* @package MS\PHPMD\Tests\Unit\CleanCode |
12
|
|
|
*/ |
13
|
|
|
class TraitPublicMethodTest extends AbstractApplyTest |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @covers MS\PHPMD\Rule\CleanCode\TraitPublicMethod |
17
|
|
|
* |
18
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
19
|
|
|
*/ |
20
|
|
|
public function testFindOnePublicMethodInTrait() |
21
|
|
|
{ |
22
|
|
|
$traitNode = \Mockery::mock('PHPMD\Node\TraitNode'); |
23
|
|
|
$node = $this->getPublicMethodNode($traitNode); |
24
|
|
|
|
25
|
|
|
$this->assertRule($node, 1); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @covers MS\PHPMD\Rule\CleanCode\TraitPublicMethod |
30
|
|
|
* |
31
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
32
|
|
|
*/ |
33
|
|
|
public function testFindNoViolation() |
34
|
|
|
{ |
35
|
|
|
$classNode = \Mockery::mock('PHPMD\Node\ClassNode'); |
36
|
|
|
$node = $this->getPublicMethodNode($classNode); |
37
|
|
|
|
38
|
|
|
$this->assertRule($node, 0); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @covers MS\PHPMD\Rule\CleanCode\TraitPublicMethod |
43
|
|
|
* |
44
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
45
|
|
|
*/ |
46
|
|
|
public function testFindPrivateMethodInTrait() |
47
|
|
|
{ |
48
|
|
|
$classNode = \Mockery::mock('PHPMD\Node\TraitNode'); |
49
|
|
|
$node = $this->getPrivateMethodNode($classNode); |
50
|
|
|
|
51
|
|
|
$this->assertRule($node, 0); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return TraitPublicMethod |
56
|
|
|
*/ |
57
|
|
|
protected function getRule() |
58
|
|
|
{ |
59
|
|
|
$rule = new TraitPublicMethod(); |
60
|
|
|
|
61
|
|
|
return $rule; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param mixed $parentType |
66
|
|
|
* |
67
|
|
|
* @return \Mockery\MockInterface |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
protected function getPublicMethodNode($parentType) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$node = parent::getMethodNode('UserComparator', 'doThings'); |
|
|
|
|
72
|
|
|
$node->shouldReceive('getParentType')->andReturn($parentType); |
73
|
|
|
$node->shouldReceive('isPublic')->andReturn(true); |
74
|
|
|
|
75
|
|
|
return $node; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $parentType |
80
|
|
|
* |
81
|
|
|
* @return \Mockery\MockInterface |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
protected function getPrivateMethodNode($parentType) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$node = parent::getMethodNode('UserComparator', 'doThings'); |
|
|
|
|
86
|
|
|
$node->shouldReceive('getParentType')->andReturn($parentType); |
87
|
|
|
$node->shouldReceive('isPublic')->andReturn(false); |
88
|
|
|
|
89
|
|
|
return $node; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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.