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; |
19
|
|
|
|
20
|
|
|
use PDepend\Source\Parser\InvalidStateException; |
21
|
|
|
use PDepend\Source\AST\ASTCompilationUnit; |
22
|
|
|
use PDepend\Source\AST\ASTMethod; |
23
|
|
|
use PDepend\Source\AST\ASTFunction; |
24
|
|
|
use PDepend\Source\AST\ASTClass; |
25
|
|
|
use PDepend\Engine; |
26
|
|
|
use PHPMD\Node\FunctionNode; |
27
|
|
|
use PHPMD\Node\MethodNode; |
28
|
|
|
use PHPMD\Node\ClassNode; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Test case for the PHP_Depend backend adapter class. |
32
|
|
|
* |
33
|
|
|
* @covers \PHPMD\Parser |
34
|
|
|
*/ |
35
|
|
|
class ParserTest extends AbstractTest |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Tests that the metrics adapter delegates a node to a registered rule-set. |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function testAdapterDelegatesClassNodeToRuleSet() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$mock = $this->getPHPDependClassMock(); |
45
|
|
|
$mock->expects($this->once()) |
|
|
|
|
46
|
|
|
->method('isUserDefined') |
47
|
|
|
->willReturn(true); |
48
|
|
|
|
49
|
|
|
$adapter = new Parser($this->getPHPDependMock()); |
50
|
|
|
$adapter->addRuleSet($this->getRuleSetMock(ClassNode::class)); |
51
|
|
|
$adapter->setReport($this->getReportMock(0)); |
52
|
|
|
$adapter->visitClass($mock); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Tests that the metrics adapter does not delegate a node without source |
57
|
|
|
* code file to a registered rule-set. |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function testAdapterDoesNotDelegateNonSourceClassNodeToRuleSet() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$mock = $this->getPHPDependClassMock(); |
64
|
|
|
$mock->expects($this->once()) |
|
|
|
|
65
|
|
|
->method('isUserDefined') |
66
|
|
|
->willReturn(false); |
67
|
|
|
|
68
|
|
|
$adapter = new Parser($this->getPHPDependMock()); |
69
|
|
|
$adapter->addRuleSet($this->getRuleSetMock()); |
70
|
|
|
$adapter->setReport($this->getReportMock(0)); |
71
|
|
|
$adapter->visitClass($mock); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Tests that the metrics adapter delegates a node to a registered rule-set. |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function testAdapterDelegatesMethodNodeToRuleSet() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$adapter = new Parser($this->getPHPDependMock()); |
82
|
|
|
$adapter->addRuleSet($this->getRuleSetMock(MethodNode::class)); |
83
|
|
|
$adapter->setReport($this->getReportMock(0)); |
84
|
|
|
$adapter->visitMethod($this->getPHPDependMethodMock()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tests that the metrics adapter does not delegate a node without source |
89
|
|
|
* code file to a registered rule-set. |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function testAdapterDoesNotDelegateNonSourceMethodNodeToRuleSet() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$adapter = new Parser($this->getPHPDependMock()); |
96
|
|
|
$adapter->addRuleSet($this->getRuleSetMock()); |
97
|
|
|
$adapter->setReport($this->getReportMock(0)); |
98
|
|
|
$adapter->visitMethod($this->getPHPDependMethodMock(null)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Tests that the metrics adapter delegates a node to a registered rule-set. |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function testAdapterDelegatesFunctionNodeToRuleSet() |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$adapter = new Parser($this->getPHPDependMock()); |
109
|
|
|
$adapter->addRuleSet($this->getRuleSetMock(FunctionNode::class)); |
110
|
|
|
$adapter->setReport($this->getReportMock(0)); |
111
|
|
|
$adapter->visitFunction($this->getPHPDependFunctionMock()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Tests that the metrics adapter does not delegate a node without source |
116
|
|
|
* code file to a registered rule-set. |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
public function testAdapterDoesNotDelegateNonSourceFunctionNodeToRuleSet() |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$adapter = new Parser($this->getPHPDependMock()); |
123
|
|
|
$adapter->addRuleSet($this->getRuleSetMock()); |
124
|
|
|
$adapter->setReport($this->getReportMock(0)); |
125
|
|
|
$adapter->visitFunction($this->getPHPDependFunctionMock(null)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* testParserStoreParsingExceptionsInReport |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
* @since 1.2.1 |
133
|
|
|
*/ |
134
|
|
|
public function testParserStoreParsingExceptionsInReport() |
135
|
|
|
{ |
136
|
|
|
$report = $this->getReportMock(0); |
137
|
|
|
$report->expects($this->once()) |
138
|
|
|
->method('addError'); |
139
|
|
|
|
140
|
|
|
$pdepend = $this->getPHPDependMock(); |
141
|
|
|
$pdepend->expects($this->once()) |
|
|
|
|
142
|
|
|
->method('getExceptions') |
143
|
|
|
->willReturn(array( |
144
|
|
|
new InvalidStateException(42, __FILE__, 'foo') |
145
|
|
|
)); |
146
|
|
|
|
147
|
|
|
$parser = new Parser($pdepend); |
148
|
|
|
$parser->parse($report); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Creates a mocked PDepend instance. |
153
|
|
|
* |
154
|
|
|
* @return \PDepend\Engine |
155
|
|
|
*/ |
156
|
|
|
private function getPHPDependMock() |
157
|
|
|
{ |
158
|
|
|
return $this->getMock(Engine::class, array(), array(null), '', false); |
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Creates a mocked PDepend class instance. |
163
|
|
|
* |
164
|
|
|
* @return \PDepend\Source\AST\ASTClass |
165
|
|
|
*/ |
166
|
|
|
protected function getPHPDependClassMock() |
167
|
|
|
{ |
168
|
|
|
$class = $this->getMock(ASTClass::class, array(), array(null)); |
|
|
|
|
169
|
|
|
$class |
170
|
|
|
->method('getCompilationUnit') |
171
|
|
|
->willReturn($this->getPHPDependFileMock('foo.php')); |
172
|
|
|
$class |
173
|
|
|
->method('getConstants') |
174
|
|
|
->willReturn(new \ArrayIterator(array())); |
175
|
|
|
$class |
176
|
|
|
->method('getProperties') |
177
|
|
|
->willReturn(new \ArrayIterator(array())); |
178
|
|
|
$class |
179
|
|
|
->method('getMethods') |
180
|
|
|
->willReturn(new \ArrayIterator(array())); |
181
|
|
|
|
182
|
|
|
return $class; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Creates a mocked PHP_Depend function instance. |
187
|
|
|
* |
188
|
|
|
* @param string $fileName Optional file name for the source file. |
189
|
|
|
* @return PHP_Depend_Code_Function |
190
|
|
|
*/ |
191
|
|
View Code Duplication |
protected function getPHPDependFunctionMock($fileName = '/foo/bar.php') |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
$function = $this->getMock(ASTFunction::class, array(), array(null)); |
|
|
|
|
194
|
|
|
$function->expects($this->atLeastOnce()) |
195
|
|
|
->method('getCompilationUnit') |
196
|
|
|
->willReturn($this->getPHPDependFileMock($fileName)); |
197
|
|
|
|
198
|
|
|
return $function; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Creates a mocked PHP_Depend method instance. |
203
|
|
|
* |
204
|
|
|
* @param string $fileName Optional file name for the source file. |
205
|
|
|
* @return PHP_Depend_Code_CodeMethod |
206
|
|
|
*/ |
207
|
|
View Code Duplication |
protected function getPHPDependMethodMock($fileName = '/foo/bar.php') |
|
|
|
|
208
|
|
|
{ |
209
|
|
|
$method = $this->getMock(ASTMethod::class, array(), array(null)); |
|
|
|
|
210
|
|
|
$method->expects($this->atLeastOnce()) |
211
|
|
|
->method('getCompilationUnit') |
212
|
|
|
->willReturn($this->getPHPDependFileMock($fileName)); |
213
|
|
|
|
214
|
|
|
return $method; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Creates a mocked PHP_Depend file instance. |
219
|
|
|
* |
220
|
|
|
* @param string $fileName The temporary file name. |
221
|
|
|
* @return PHP_Depend_Code_File |
222
|
|
|
*/ |
223
|
|
|
protected function getPHPDependFileMock($fileName) |
224
|
|
|
{ |
225
|
|
|
$file = $this->getMock(ASTCompilationUnit::class, array(), array(null)); |
|
|
|
|
226
|
|
|
$file |
227
|
|
|
->method('getFileName') |
228
|
|
|
->willReturn($fileName); |
229
|
|
|
|
230
|
|
|
return $file; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
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.