| 244 |
244 |
} |
| 245 |
245 |
|
| 246 |
246 |
/** |
|
247 |
+ * testCalculatesExpectedNoamMetricForClassWithoutParent |
|
248 |
+ * |
|
249 |
+ * @return void |
|
250 |
+ * @covers PHP_Depend_Metrics_Inheritance_Analyzer |
|
251 |
+ * @group pdepend |
|
252 |
+ * @group pdepend::metrics |
|
253 |
+ * @group pdepend::metrics::inheritance |
|
254 |
+ * @group unittest |
|
255 |
+ */ |
|
256 |
+ public function testCalculatesExpectedNoamMetricForClassWithoutParent() |
|
257 |
+ { |
|
258 |
+ $this->assertEquals(0, $this->_getCalculatedMetric(__METHOD__, 'noam')); |
|
259 |
+ } |
|
260 |
+ |
|
261 |
+ /** |
|
262 |
+ * testCalculatesExpectedNoamMetricForClassWithDirectParent |
|
263 |
+ * |
|
264 |
+ * @return void |
|
265 |
+ * @covers PHP_Depend_Metrics_Inheritance_Analyzer |
|
266 |
+ * @group pdepend |
|
267 |
+ * @group pdepend::metrics |
|
268 |
+ * @group pdepend::metrics::inheritance |
|
269 |
+ * @group unittest |
|
270 |
+ */ |
|
271 |
+ public function testCalculatesExpectedNoamMetricForClassWithDirectParent() |
|
272 |
+ { |
|
273 |
+ $this->assertEquals(2, $this->_getCalculatedMetric(__METHOD__, 'noam')); |
|
274 |
+ } |
|
275 |
+ |
|
276 |
+ /** |
|
277 |
+ * testCalculatesExpectedNoamMetricForClassWithIndirectParent |
|
278 |
+ * |
|
279 |
+ * @return void |
|
280 |
+ * @covers PHP_Depend_Metrics_Inheritance_Analyzer |
|
281 |
+ * @group pdepend |
|
282 |
+ * @group pdepend::metrics |
|
283 |
+ * @group pdepend::metrics::inheritance |
|
284 |
+ * @group unittest |
|
285 |
+ */ |
|
286 |
+ public function testCalculatesExpectedNoamMetricForClassWithIndirectParent() |
|
287 |
+ { |
|
288 |
+ $this->assertEquals(2, $this->_getCalculatedMetric(__METHOD__, 'noam')); |
|
289 |
+ } |
|
290 |
+ |
|
291 |
+ /** |
|
292 |
+ * testCalculatesExpectedNoomMetricForClassWithoutParent |
|
293 |
+ * |
|
294 |
+ * @return void |
|
295 |
+ * @covers PHP_Depend_Metrics_Inheritance_Analyzer |
|
296 |
+ * @group pdepend |
|
297 |
+ * @group pdepend::metrics |
|
298 |
+ * @group pdepend::metrics::inheritance |
|
299 |
+ * @group unittest |
|
300 |
+ */ |
|
301 |
+ public function testCalculatesExpectedNoomMetricForClassWithoutParent() |
|
302 |
+ { |
|
303 |
+ $this->assertEquals(0, $this->_getCalculatedMetric(__METHOD__, 'noom')); |
|
304 |
+ } |
|
305 |
+ |
|
306 |
+ /** |
|
307 |
+ * testCalculatesExpectedNoomMetricForClassWithParent |
|
308 |
+ * |
|
309 |
+ * @return void |
|
310 |
+ * @covers PHP_Depend_Metrics_Inheritance_Analyzer |
|
311 |
+ * @group pdepend |
|
312 |
+ * @group pdepend::metrics |
|
313 |
+ * @group pdepend::metrics::inheritance |
|
314 |
+ * @group unittest |
|
315 |
+ */ |
|
316 |
+ public function testCalculatesExpectedNoomMetricForClassWithParent() |
|
317 |
+ { |
|
318 |
+ $this->assertEquals(2, $this->_getCalculatedMetric(__METHOD__, 'noom')); |
|
319 |
+ } |
|
320 |
+ |
|
321 |
+ /** |
|
322 |
+ * testCalculatesExpectedNoomMetricForClassWithParentPrivateMethods |
|
323 |
+ * |
|
324 |
+ * @return void |
|
325 |
+ * @covers PHP_Depend_Metrics_Inheritance_Analyzer |
|
326 |
+ * @group pdepend |
|
327 |
+ * @group pdepend::metrics |
|
328 |
+ * @group pdepend::metrics::inheritance |
|
329 |
+ * @group unittest |
|
330 |
+ */ |
|
331 |
+ public function testCalculatesExpectedNoomMetricForClassWithParentPrivateMethods() |
|
332 |
+ { |
|
333 |
+ $this->assertEquals(1, $this->_getCalculatedMetric(__METHOD__, 'noom')); |
|
334 |
+ } |
|
335 |
+ |
|
336 |
+ /** |
| 247 |
337 |
* Analyzes the source associated with the calling test and returns the |
| 248 |
338 |
* calculated metric value. |
| 249 |
339 |
* |
| 180 |
182 |
*/ |
| 181 |
183 |
public function analyze(PHP_Depend_Code_NodeIterator $packages) |
| 182 |
184 |
{ |
| 183 |
|
- if ($this->_derivedClasses === null) { |
|
185 |
+ if ($this->_nodeMetrics === null) { |
|
186 |
+ $this->_nodeMetrics = array(); |
| 184 |
187 |
|
| 185 |
188 |
$this->fireStartAnalyzer(); |
|
189 |
+ $this->_analyzer($packages); |
|
190 |
+ $this->fireEndAnalyzer(); |
|
191 |
+ } |
|
192 |
+ } |
| 186 |
193 |
|
| 187 |
|
- // Init runtime collections |
| 188 |
|
- $this->_derivedClasses = array(); |
|
194 |
+ /** |
|
195 |
+ * Calculates several inheritance related metrics for the given source |
|
196 |
+ * packages. |
|
197 |
+ * |
|
198 |
+ * @param PHP_Depend_Code_NodeIterator $packages The source packages. |
|
199 |
+ * |
|
200 |
+ * @return void |
|
201 |
+ */ |
|
202 |
+ private function _analyzer(PHP_Depend_Code_NodeIterator $packages) |
|
203 |
+ { |
|
204 |
+ // Process all packages |
|
205 |
+ foreach ($packages as $package) { |
|
206 |
+ $package->accept($this); |
|
207 |
+ } |
| 189 |
208 |
|
| 190 |
|
- // Process all packages |
| 191 |
|
- foreach ($packages as $package) { |
| 192 |
|
- $package->accept($this); |
| 193 |
|
- } |
| 194 |
|
- |
| 195 |
|
- if ($this->_numberOfClasses > 0) { |
| 196 |
|
- $this->_andc = ($this->_numberOfDerivedClasses / $this->_numberOfClasses); |
| 197 |
|
- } |
| 198 |
|
- if (($count = count($this->_rootClasses)) > 0) { |
| 199 |
|
- $this->_ahh = (array_sum($this->_rootClasses) / $count); |
| 200 |
|
- } |
| 201 |
|
- |
| 202 |
|
- $this->fireEndAnalyzer(); |
|
209 |
+ if ($this->_numberOfClasses > 0) { |
|
210 |
+ $this->_andc = $this->_numberOfDerivedClasses / $this->_numberOfClasses; |
| 203 |
211 |
} |
|
212 |
+ if (($count = count($this->_rootClasses)) > 0) { |
|
213 |
+ $this->_ahh = array_sum($this->_rootClasses) / $count; |
|
214 |
+ } |
| 204 |
215 |
} |
| 205 |
216 |
|
| 206 |
217 |
/** |
| 271 |
284 |
$this->_nodeMetrics[$uuid][self::M_DEPTH_OF_INHERITANCE_TREE] = $dit; |
| 272 |
285 |
} |
| 273 |
286 |
|
| 274 |
|
- private function _calculateNumberOfAddedMethods(PHP_Depend_Code_Class $class) |
| 275 |
|
- { |
|
287 |
+ /** |
|
288 |
+ * Calculates two metrics. The number of added methods and the number of |
|
289 |
+ * overwritten methods. |
|
290 |
+ * |
|
291 |
+ * @param PHP_Depend_Code_Class $class The context class instance. |
|
292 |
+ * |
|
293 |
+ * @return void |
|
294 |
+ * @since 0.9.10 |
|
295 |
+ */ |
|
296 |
+ private function _calculateNumberOfAddedAndOverwrittenMethods( |
|
297 |
+ PHP_Depend_Code_Class $class |
|
298 |
+ ) { |
| 276 |
299 |
$parentClass = $class->getParentClass(); |
| 277 |
300 |
if ($parentClass === null) { |
| 278 |
301 |
return; |
| 279 |
302 |
} |
|
303 |
+ |
|
304 |
+ $parentMethodNames = array(); |
|
305 |
+ foreach ($parentClass->getAllMethods() as $method) { |
|
306 |
+ $parentMethodNames[$method->getName()] = true; |
|
307 |
+ } |
|
308 |
+ |
|
309 |
+ $numberOfAddedMethods = 0; |
|
310 |
+ $numberOfOverwrittenMethods = 0; |
|
311 |
+ |
|
312 |
+ foreach ($class->getAllMethods() as $method) { |
|
313 |
+ if ($method->getParent() !== $class) { |
|
314 |
+ continue; |
|
315 |
+ } |
|
316 |
+ |
|
317 |
+ if (isset($parentMethodNames[$method->getName()])) { |
|
318 |
+ ++$numberOfOverwrittenMethods; |
|
319 |
+ } else { |
|
320 |
+ ++$numberOfAddedMethods; |
|
321 |
+ } |
|
322 |
+ } |
|
323 |
+ |
|
324 |
+ $uuid = $class->getUUID(); |
|
325 |
+ |
|
326 |
+ $this->_nodeMetrics[$uuid][self::M_NUMBER_OF_ADDED_METHODS] |
|
327 |
+ = $numberOfAddedMethods; |
|
328 |
+ $this->_nodeMetrics[$uuid][self::M_NUMBER_OF_OVERWRITTEN_METHODS] |
|
329 |
+ = $numberOfOverwrittenMethods; |
| 280 |
330 |
} |
| 281 |
331 |
|
|
332 |
+ /** |
|
333 |
+ * Initializes a empty metric container for the given class node. |
|
334 |
+ * |
|
335 |
+ * @param PHP_Depend_Code_Class $class The context class instance. |
|
336 |
+ * |
|
337 |
+ * @return void |
|
338 |
+ * @since 0.9.10 |
|
339 |
+ */ |
| 282 |
340 |
private function _initNodeMetricsForClass(PHP_Depend_Code_Class $class) |
| 283 |
341 |
{ |
| 284 |
342 |
if (is_object($class->getParentClass())) { |