CamelCaseMethodNameTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 229
Duplicated Lines 55.46 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 127
loc 229
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testRuleDoesNotApplyForValidMethodName() 0 12 1
A testRuleDoesApplyForMethodNameWithCapital() 14 14 1
A testRuleDoesApplyForMethodNameWithUnderscores() 14 14 1
A testRuleDoesApplyForValidMethodNameWithUnderscoreWhenNotAllowed() 13 13 1
A testRuleDoesNotApplyForValidMethodNameWithUnderscoreWhenAllowed() 13 13 1
A testRuleDoesApplyForTestMethodWithUnderscoreWhenNotAllowed() 13 13 1
A testRuleDoesNotApplyForTestMethodWithUnderscoreWhenAllowed() 12 12 1
A testRuleAppliesToTestMethodWithTwoUnderscoresEvenWhenOneIsAllowed() 12 12 1
A testRuleAppliesToTestMethodWithUnderscoreFollowedByCapital() 12 12 1
A testRuleAppliesToTestMethodWithMultipleUnderscores() 12 12 1
A testRuleDoesNotApplyForTestMethodWithMultipleUnderscoreWhenAllowed() 12 12 1
A getMethod() 0 5 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
 * 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\Controversial;
19
20
use PHPMD\AbstractTest;
21
22
/**
23
 * Test case for the camel case method name rule.
24
 *
25
 * @covers \PHPMD\Rule\Controversial\CamelCaseMethodName
26
 */
27
class CamelCaseMethodNameTest extends AbstractTest
28
{
29
    /**
30
     * Tests that the rule does not apply for a valid method name.
31
     *
32
     * @return void
33
     */
34
    public function testRuleDoesNotApplyForValidMethodName()
35
    {
36
        //$method = $this->getMethod();
37
        $report = $this->getReportMock(0);
38
39
        $rule = new CamelCaseMethodName();
40
        $rule->setReport($report);
41
        $rule->addProperty('allow-underscore', 'false');
42
        $rule->addProperty('allow-underscore-test', 'false');
43
        $rule->addProperty('allow-multiple-underscore-test', 'false');
44
        $rule->apply($this->getMethod());
45
    }
46
47
    /**
48
     * Tests that the rule does apply for an method name
49
     * starting with a capital.
50
     *
51
     * @return void
52
     */
53 View Code Duplication
    public function testRuleDoesApplyForMethodNameWithCapital()
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...
54
    {
55
        // Test method name with capital at the beginning
56
        $method = $this->getMethod();
57
        $report = $this->getReportMock(1);
58
59
        $rule = new CamelCaseMethodName();
60
        $rule->setReport($report);
61
        $rule->addProperty('allow-underscore', 'false');
62
        $rule->addProperty('allow-underscore-test', 'false');
63
        $rule->addProperty('allow-multiple-underscore-test', 'false');
64
65
        $rule->apply($method);
66
    }
67
68
    /**
69
     * Tests that the rule does apply for a method name
70
     * with underscores.
71
     *
72
     * @return void
73
     */
74 View Code Duplication
    public function testRuleDoesApplyForMethodNameWithUnderscores()
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...
75
    {
76
        // Test method name with underscores
77
        $method = $this->getMethod();
78
        $report = $this->getReportMock(1);
79
80
        $rule = new CamelCaseMethodName();
81
        $rule->setReport($report);
82
        $rule->addProperty('allow-underscore', 'false');
83
        $rule->addProperty('allow-underscore-test', 'false');
84
        $rule->addProperty('allow-multiple-underscore-test', 'false');
85
86
        $rule->apply($method);
87
    }
88
89
    /**
90
     * Tests that the rule does apply for a valid method name
91
     * with an underscore at the beginning when it is allowed.
92
     *
93
     * @return void
94
     */
95 View Code Duplication
    public function testRuleDoesApplyForValidMethodNameWithUnderscoreWhenNotAllowed()
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...
96
    {
97
        $method = $this->getMethod();
98
        $report = $this->getReportMock(1);
99
100
        $rule = new CamelCaseMethodName();
101
        $rule->setReport($report);
102
        $rule->addProperty('allow-underscore', 'false');
103
        $rule->addProperty('allow-underscore-test', 'false');
104
        $rule->addProperty('allow-multiple-underscore-test', 'false');
105
106
        $rule->apply($method);
107
    }
108
109
    /**
110
     * Tests that the rule does not apply for a valid method name
111
     * with an underscore at the beginning when it is not allowed.
112
     *
113
     * @return void
114
     */
115 View Code Duplication
    public function testRuleDoesNotApplyForValidMethodNameWithUnderscoreWhenAllowed()
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...
116
    {
117
        $method = $this->getMethod();
118
        $report = $this->getReportMock(0);
119
120
        $rule = new CamelCaseMethodName();
121
        $rule->setReport($report);
122
        $rule->addProperty('allow-underscore', 'true');
123
        $rule->addProperty('allow-underscore-test', 'false');
124
        $rule->addProperty('allow-multiple-underscore-test', 'false');
125
126
        $rule->apply($method);
127
    }
128
129
    /**
130
     * Tests that the rule does apply for a valid test method name
131
     * with an underscore.
132
     *
133
     * @return void
134
     */
135 View Code Duplication
    public function testRuleDoesApplyForTestMethodWithUnderscoreWhenNotAllowed()
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...
136
    {
137
        $method = $this->getMethod();
138
        $report = $this->getReportMock(1);
139
140
        $rule = new CamelCaseMethodName();
141
        $rule->setReport($report);
142
        $rule->addProperty('allow-underscore', 'false');
143
        $rule->addProperty('allow-underscore-test', 'false');
144
        $rule->addProperty('allow-multiple-underscore-test', 'false');
145
146
        $rule->apply($method);
147
    }
148
149
    /**
150
     * Tests that the rule does not apply for a valid test method name
151
     * with an underscore when an single underscore is allowed.
152
     *
153
     * @return void
154
     */
155 View Code Duplication
    public function testRuleDoesNotApplyForTestMethodWithUnderscoreWhenAllowed()
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...
156
    {
157
        $method = $this->getMethod();
158
        $report = $this->getReportMock(0);
159
160
        $rule = new CamelCaseMethodName();
161
        $rule->setReport($report);
162
        $rule->addProperty('allow-underscore', 'false');
163
        $rule->addProperty('allow-underscore-test', 'true');
164
        $rule->addProperty('allow-multiple-underscore-test', 'true');
165
        $rule->apply($method);
166
    }
167
168
    /**
169
     * Tests that the rule does apply for a test method name
170
     * with multiple underscores even when one is allowed.
171
     *
172
     * @return void
173
     */
174 View Code Duplication
    public function testRuleAppliesToTestMethodWithTwoUnderscoresEvenWhenOneIsAllowed()
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...
175
    {
176
        $method = $this->getMethod();
177
        $report = $this->getReportMock(1);
178
179
        $rule = new CamelCaseMethodName();
180
        $rule->setReport($report);
181
        $rule->addProperty('allow-underscore', 'false');
182
        $rule->addProperty('allow-underscore-test', 'true');
183
        $rule->addProperty('allow-multiple-underscore-test', 'false');
184
        $rule->apply($method);
185
    }
186
187
    /**
188
     * Tests that the rule does apply to for test method names that
189
     * have a capital after their single allowed underscore.
190
     *
191
     * @return void
192
     */
193 View Code Duplication
    public function testRuleAppliesToTestMethodWithUnderscoreFollowedByCapital()
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...
194
    {
195
        $method = $this->getMethod();
196
        $report = $this->getReportMock(1);
197
198
        $rule = new CamelCaseMethodName();
199
        $rule->setReport($report);
200
        $rule->addProperty('allow-underscore', 'false');
201
        $rule->addProperty('allow-underscore-test', 'true');
202
        $rule->addProperty('allow-multiple-underscore-test', 'false');
203
        $rule->apply($method);
204
    }
205
206
    /**
207
     * Tests that the rule does apply to for test method names that
208
     * have multiple underscore in the declaration of test
209
     *
210
     * @return void
211
     */
212 View Code Duplication
    public function testRuleAppliesToTestMethodWithMultipleUnderscores()
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...
213
    {
214
        $method = $this->getMethod();
215
        $report = $this->getReportMock(0);
216
217
        $rule = new CamelCaseMethodName();
218
        $rule->setReport($report);
219
        $rule->addProperty('allow-underscore', 'false');
220
        $rule->addProperty('allow-underscore-test', 'false');
221
        $rule->addProperty('allow-multiple-underscore-test', 'true');
222
        $rule->apply($method);
223
    }
224
225
    /**
226
     * Tests that the rule does apply to for test method names that
227
     * have multiple underscore in the declaration of test
228
     *
229
     * @return void
230
     */
231 View Code Duplication
    public function testRuleDoesNotApplyForTestMethodWithMultipleUnderscoreWhenAllowed()
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...
232
    {
233
        $method = $this->getMethod();
234
        $report = $this->getReportMock(1);
235
236
        $rule = new CamelCaseMethodName();
237
        $rule->setReport($report);
238
        $rule->addProperty('allow-underscore', 'false');
239
        $rule->addProperty('allow-underscore-test', 'false');
240
        $rule->addProperty('allow-multiple-underscore-test', 'false');
241
        $rule->apply($method);
242
    }
243
244
    /**
245
     * Returns the first method found in a source file related to the calling
246
     * test method.
247
     *
248
     * @return \PHPMD\Node\MethodNode
249
     */
250
    protected function getMethod()
251
    {
252
        $methods = $this->getClass()->getMethods();
253
        return reset($methods);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($methods); of type PHPMD\Node\MethodNode|false adds false to the return on line 253 which is incompatible with the return type documented by PHPMD\Rule\Controversial...thodNameTest::getMethod of type PHPMD\Node\MethodNode. It seems like you forgot to handle an error condition.
Loading history...
254
    }
255
}
256