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 too many methods rule. |
24
|
|
|
* |
25
|
|
|
* @covers \PHPMD\Rule\Design\TooManyMethods |
26
|
|
|
*/ |
27
|
|
|
class TooManyMethodsTest extends AbstractTest |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* testRuleDoesNotApplyToClassesWithLessMethodsThanThreshold |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function testRuleDoesNotApplyToClassesWithLessMethodsThanThreshold() |
35
|
|
|
{ |
36
|
|
|
$rule = new TooManyMethods(); |
37
|
|
|
$rule->setReport($this->getReportMock(0)); |
38
|
|
|
$rule->addProperty('maxmethods', '42'); |
39
|
|
|
$rule->addProperty('ignorepattern', '(^(set|get|inject))i'); |
40
|
|
|
$rule->apply($this->createClassMock(23)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* testRuleDoesNotApplyToClassesWithSameNumberOfMethodsAsThreshold |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function testRuleDoesNotApplyToClassesWithSameNumberOfMethodsAsThreshold() |
49
|
|
|
{ |
50
|
|
|
$rule = new TooManyMethods(); |
51
|
|
|
$rule->setReport($this->getReportMock(0)); |
52
|
|
|
$rule->addProperty('maxmethods', '42'); |
53
|
|
|
$rule->addProperty('ignorepattern', '(^(set|get|inject))i'); |
54
|
|
|
$rule->apply($this->createClassMock(42)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* testRuleAppliesToClassesWithMoreMethodsThanThreshold |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function testRuleAppliesToClassesWithMoreMethodsThanThreshold() |
63
|
|
|
{ |
64
|
|
|
$rule = new TooManyMethods(); |
65
|
|
|
$rule->setReport($this->getReportMock(1)); |
66
|
|
|
$rule->addProperty('maxmethods', '23'); |
67
|
|
|
$rule->addProperty('ignorepattern', '(^(set|get|inject))i'); |
68
|
|
|
$rule->apply($this->createClassMock(42, array_fill(0, 42, __FUNCTION__))); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* testRuleIgnoresGetterMethodsInTest |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function testRuleIgnoresGetterMethodsInTest() |
77
|
|
|
{ |
78
|
|
|
$rule = new TooManyMethods(); |
79
|
|
|
$rule->setReport($this->getReportMock(0)); |
80
|
|
|
$rule->addProperty('maxmethods', '1'); |
81
|
|
|
$rule->addProperty('ignorepattern', '(^(set|get|inject))i'); |
82
|
|
|
$rule->apply($this->createClassMock(2, array('invoke', 'getClass'))); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* testRuleIgnoresSetterMethodsInTest |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function testRuleIgnoresSetterMethodsInTest() |
91
|
|
|
{ |
92
|
|
|
$rule = new TooManyMethods(); |
93
|
|
|
$rule->setReport($this->getReportMock(0)); |
94
|
|
|
$rule->addProperty('maxmethods', '1'); |
95
|
|
|
$rule->addProperty('ignorepattern', '(^(set|get|inject))i'); |
96
|
|
|
$rule->apply($this->createClassMock(2, array('invoke', 'setClass'))); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* testRuleIgnoresCustomMethodsWhenRegexPropertyIsGiven |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function testRuleIgnoresCustomMethodsWhenRegexPropertyIsGiven() |
105
|
|
|
{ |
106
|
|
|
$rule = new TooManyMethods(); |
107
|
|
|
$rule->setReport($this->getReportMock(0)); |
108
|
|
|
$rule->addProperty('maxmethods', '1'); |
109
|
|
|
$rule->addProperty('ignorepattern', '(^(set|get|inject))i'); |
110
|
|
|
$rule->apply($this->createClassMock(2, array('invoke', 'injectClass'))); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* testRuleIgnoresGetterAndSetterMethodsInTest |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function testRuleIgnoresGetterAndSetterMethodsInTest() |
119
|
|
|
{ |
120
|
|
|
$rule = new TooManyMethods(); |
121
|
|
|
$rule->setReport($this->getReportMock(0)); |
122
|
|
|
$rule->addProperty('maxmethods', '2'); |
123
|
|
|
$rule->addProperty('ignorepattern', '(^(set|get|inject))i'); |
124
|
|
|
$rule->apply($this->createClassMock(3, array('invoke', 'getClass', 'setClass'))); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function testRuleIgnoresHassers() |
131
|
|
|
{ |
132
|
|
|
$rule = new TooManyMethods(); |
133
|
|
|
$rule->setReport($this->getReportMock(0)); |
134
|
|
|
$rule->addProperty('maxmethods', '1'); |
135
|
|
|
$rule->addProperty('ignorepattern', '(^(set|get|is|has|with))i'); |
136
|
|
|
$rule->apply($this->createClassMock(2, array('invoke', 'hasClass'))); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function testRuleIgnoresIssers() |
143
|
|
|
{ |
144
|
|
|
$rule = new TooManyMethods(); |
145
|
|
|
$rule->setReport($this->getReportMock(0)); |
146
|
|
|
$rule->addProperty('maxmethods', '1'); |
147
|
|
|
$rule->addProperty('ignorepattern', '(^(set|get|is|has|with))i'); |
148
|
|
|
$rule->apply($this->createClassMock(2, array('invoke', 'isClass'))); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function testRuleIgnoresWithers() |
155
|
|
|
{ |
156
|
|
|
$rule = new TooManyMethods(); |
157
|
|
|
$rule->setReport($this->getReportMock(0)); |
158
|
|
|
$rule->addProperty('maxmethods', '1'); |
159
|
|
|
$rule->addProperty('ignorepattern', '(^(set|get|is|has|with))i'); |
160
|
|
|
$rule->apply($this->createClassMock(2, array('invoke', 'withClass'))); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Creates a prepared class node mock |
165
|
|
|
* |
166
|
|
|
* @param integer $numberOfMethods |
167
|
|
|
* @param array$methodNames |
168
|
|
|
* @return \PHPMD\Node\ClassNode |
169
|
|
|
*/ |
170
|
|
|
private function createClassMock($numberOfMethods, array $methodNames = null) |
171
|
|
|
{ |
172
|
|
|
$class = $this->getClassMock('nom', $numberOfMethods); |
173
|
|
|
|
174
|
|
|
if (is_array($methodNames)) { |
175
|
|
|
$class->expects($this->once()) |
|
|
|
|
176
|
|
|
->method('getMethodNames') |
177
|
|
|
->will($this->returnValue($methodNames)); |
178
|
|
|
} |
179
|
|
|
return $class; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: