|
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
|
|
|
* Empty Catch Block Test |
|
24
|
|
|
* |
|
25
|
|
|
* @author Grégoire Paris <[email protected]> |
|
26
|
|
|
* @author Kamil Szymanski <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
class EmptyCatchBlockTest extends AbstractTest |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* testRuleNotAppliesToMethodWithoutTryCatchBlock |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function testRuleNotAppliesToMethodWithoutTryCatchBlock() |
|
36
|
|
|
{ |
|
37
|
|
|
$rule = new EmptyCatchBlock(); |
|
38
|
|
|
$rule->setReport($this->getReportMock(0)); |
|
39
|
|
|
$rule->apply($this->getMethod()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* testRuleAppliesToFunctionWithEmptyCatchBlock |
|
44
|
|
|
* |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
|
|
public function testRuleAppliesToFunctionWithEmptyCatchBlock() |
|
48
|
|
|
{ |
|
49
|
|
|
$rule = new EmptyCatchBlock(); |
|
50
|
|
|
$rule->setReport($this->getReportMock(3)); |
|
51
|
|
|
$rule->apply($this->getFunction()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* testRuleNotAppliesToFunctionWithNonEmptyCatchBlock |
|
56
|
|
|
* |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testRuleNotAppliesToFunctionWithNonEmptyCatchBlock() |
|
60
|
|
|
{ |
|
61
|
|
|
$rule = new EmptyCatchBlock(); |
|
62
|
|
|
$rule->setReport($this->getReportMock(0)); |
|
63
|
|
|
$rule->apply($this->getFunction()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* testRuleNotAppliesToCatchBlockWithComments |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testRuleNotAppliesToCatchBlockWithComments() |
|
72
|
|
|
{ |
|
73
|
|
|
$rule = new EmptyCatchBlock(); |
|
74
|
|
|
$rule->setReport($this->getReportMock(0)); |
|
75
|
|
|
$rule->apply($this->getFunction()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* testRuleWorksWithNestedTryCatchBlocksAndNonSPLExceptions |
|
80
|
|
|
* |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
|
|
public function testRuleWorksWithNestedTryCatchBlocksAndNonSPLExceptions() |
|
84
|
|
|
{ |
|
85
|
|
|
$rule = new EmptyCatchBlock(); |
|
86
|
|
|
$rule->setReport($this->getReportMock(1)); |
|
87
|
|
|
$rule->apply($this->getFunction()); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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.