|
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 class rule. |
|
24
|
|
|
* |
|
25
|
|
|
* @covers \PHPMD\Rule\Design\LongClass |
|
26
|
|
|
*/ |
|
27
|
|
View Code Duplication |
class LongClassTest extends AbstractTest |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Tests that the rule applies for a value greater than the configured |
|
31
|
|
|
* threshold. |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testRuleAppliesForValueGreaterThanThreshold() |
|
36
|
|
|
{ |
|
37
|
|
|
$class = $this->getClassMock('loc', 42); |
|
38
|
|
|
$report = $this->getReportMock(1); |
|
39
|
|
|
|
|
40
|
|
|
$rule = new LongClass(); |
|
41
|
|
|
$rule->setReport($report); |
|
42
|
|
|
$rule->addProperty('minimum', '41'); |
|
43
|
|
|
$rule->addProperty('ignore-whitespace', false); |
|
|
|
|
|
|
44
|
|
|
$rule->apply($class); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Test that the rule applies for a value that is equal with the configured |
|
49
|
|
|
* threshold. |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testRuleAppliesForValueEqualToThreshold() |
|
54
|
|
|
{ |
|
55
|
|
|
$class = $this->getClassMock('loc', 42); |
|
56
|
|
|
$report = $this->getReportMock(1); |
|
57
|
|
|
|
|
58
|
|
|
$rule = new LongClass(); |
|
59
|
|
|
$rule->setReport($report); |
|
60
|
|
|
$rule->addProperty('minimum', '42'); |
|
61
|
|
|
$rule->addProperty('ignore-whitespace', false); |
|
|
|
|
|
|
62
|
|
|
$rule->apply($class); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Tests that the rule does not apply when the value is at least one lower |
|
67
|
|
|
* than the threshold. |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testRuleDoesNotApplyForValueLowerThanThreshold() |
|
72
|
|
|
{ |
|
73
|
|
|
$class = $this->getClassMock('loc', 22); |
|
74
|
|
|
$report = $this->getReportMock(0); |
|
75
|
|
|
|
|
76
|
|
|
$rule = new LongClass(); |
|
77
|
|
|
$rule->setReport($report); |
|
78
|
|
|
$rule->addProperty('minimum', '23'); |
|
79
|
|
|
$rule->addProperty('ignore-whitespace', false); |
|
|
|
|
|
|
80
|
|
|
$rule->apply($class); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Tests that the rule uses eloc when ignore whitespace is set |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
public function testRuleUsesElocWhenIgnoreWhitespaceSet() |
|
89
|
|
|
{ |
|
90
|
|
|
$class = $this->getClassMock('eloc', 22); |
|
91
|
|
|
$report = $this->getReportMock(0); |
|
92
|
|
|
|
|
93
|
|
|
$rule = new LongClass(); |
|
94
|
|
|
$rule->setReport($report); |
|
95
|
|
|
$rule->addProperty('minimum', '23'); |
|
96
|
|
|
$rule->addProperty('ignore-whitespace', true); |
|
|
|
|
|
|
97
|
|
|
$rule->apply($class); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
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.