|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of PHP Mess Detector. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Manuel Pichler <[email protected]>. |
|
6
|
|
|
* All rights reserved. |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under BSD License |
|
9
|
|
|
* For full copyright and license information, please see the LICENSE file. |
|
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Manuel Pichler <[email protected]> |
|
13
|
|
|
* @copyright Manuel Pichler. All rights reserved. |
|
14
|
|
|
* @license https://opensource.org/licenses/bsd-license.php BSD License |
|
15
|
|
|
* @link http://phpmd.org/ |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace PHPMD\Rule\Design; |
|
19
|
|
|
|
|
20
|
|
|
use PHPMD\AbstractTest; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Test case for the excessive long parameter list rule. |
|
24
|
|
|
* |
|
25
|
|
|
* @covers \PHPMD\Rule\Design\LongParameterList |
|
26
|
|
|
*/ |
|
27
|
|
|
class LongParameterListTest extends AbstractTest |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* testApplyIgnoresMethodsWithLessParametersThanMinimum |
|
31
|
|
|
* |
|
32
|
|
|
* @return void |
|
33
|
|
|
*/ |
|
34
|
|
View Code Duplication |
public function testApplyIgnoresMethodsWithLessParametersThanMinimum() |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$rule = new LongParameterList(); |
|
37
|
|
|
$rule->setReport($this->getReportMock(0)); |
|
38
|
|
|
$rule->addProperty('minimum', '4'); |
|
39
|
|
|
$rule->apply($this->createMethod(3)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* testApplyReportsMethodsWithIdenticalParametersAndMinimum |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
|
View Code Duplication |
public function testApplyReportsMethodsWithIdenticalParametersAndMinimum() |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$rule = new LongParameterList(); |
|
50
|
|
|
$rule->setReport($this->getReportMock(1)); |
|
51
|
|
|
$rule->addProperty('minimum', '3'); |
|
52
|
|
|
$rule->apply($this->createMethod(3)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* testApplyReportsMethodsWithMoreParametersThanMinimum |
|
57
|
|
|
* |
|
58
|
|
|
* @return void |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function testApplyReportsMethodsWithMoreParametersThanMinimum() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$rule = new LongParameterList(); |
|
63
|
|
|
$rule->setReport($this->getReportMock(1)); |
|
64
|
|
|
$rule->addProperty('minimum', '3'); |
|
65
|
|
|
$rule->apply($this->createMethod(42)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* testApplyIgnoresFunctionsWithLessParametersThanMinimum |
|
70
|
|
|
* |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
|
View Code Duplication |
public function testApplyIgnoresFunctionsWithLessParametersThanMinimum() |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
$rule = new LongParameterList(); |
|
76
|
|
|
$rule->setReport($this->getReportMock(0)); |
|
77
|
|
|
$rule->addProperty('minimum', '4'); |
|
78
|
|
|
$rule->apply($this->createFunction(3)); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* testApplyReportsFunctionsWithIdenticalParametersAndMinimum |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
View Code Duplication |
public function testApplyReportsFunctionsWithIdenticalParametersAndMinimum() |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
$rule = new LongParameterList(); |
|
89
|
|
|
$rule->setReport($this->getReportMock(1)); |
|
90
|
|
|
$rule->addProperty('minimum', '3'); |
|
91
|
|
|
$rule->apply($this->createFunction(3)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* testApplyReportsFunctionsWithMoreParametersThanMinimum |
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
View Code Duplication |
public function testApplyReportsFunctionsWithMoreParametersThanMinimum() |
|
|
|
|
|
|
100
|
|
|
{ |
|
101
|
|
|
$rule = new LongParameterList(); |
|
102
|
|
|
$rule->setReport($this->getReportMock(1)); |
|
103
|
|
|
$rule->addProperty('minimum', '3'); |
|
104
|
|
|
$rule->apply($this->createFunction(42)); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns a mocked method instance. |
|
109
|
|
|
* |
|
110
|
|
|
* @param integer $parameterCount |
|
111
|
|
|
* @return \PHPMD\Node\MethodNode |
|
112
|
|
|
*/ |
|
113
|
|
|
private function createMethod($parameterCount) |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->initFunctionOrMethodMock($this->getMethodMock(), $parameterCount); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Creates a mocked function node instance. |
|
120
|
|
|
* |
|
121
|
|
|
* @param integer $parameterCount Number of function parameters. |
|
122
|
|
|
* |
|
123
|
|
|
* @return \PHPMD\Node\FunctionNode |
|
124
|
|
|
*/ |
|
125
|
|
|
private function createFunction($parameterCount) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->initFunctionOrMethodMock($this->createFunctionMock(), $parameterCount); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Initializes the getParameterCount() method of the given callable. |
|
132
|
|
|
* |
|
133
|
|
|
* @param \PHPMD\Node\FunctionNode|\PHPMD\Node\MethodNode $mock |
|
134
|
|
|
* @param integer $parameterCount |
|
135
|
|
|
* @return \PHPMD\Node\FunctionNode|\PHPMD\Node\MethodNode |
|
136
|
|
|
*/ |
|
137
|
|
|
private function initFunctionOrMethodMock($mock, $parameterCount) |
|
138
|
|
|
{ |
|
139
|
|
|
$mock->expects($this->once()) |
|
140
|
|
|
->method('getParameterCount') |
|
141
|
|
|
->willReturn($parameterCount); |
|
142
|
|
|
|
|
143
|
|
|
return $mock; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
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.