Arbit - project tracking

PHP Depend

History

Diff

1083 1084 /tests/PHP/Depend/Metrics/Inheritance/AnalyzerTest.php
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 *
1083 1084 /tests/PHP/Depend/_code/metrics/Inheritance/testCalculatesExpectedNoomMetricForClassWithoutParent.php
2 +<?php
3 +class testCalculatesExpectedNoomMetricForClassWithoutParent
4 +{
5 + private function foo() {}
6 + public function bar() {}
7 + protected function baz() {}
8 +}
1083 1084 /tests/PHP/Depend/_code/metrics/Inheritance/testCalculatesExpectedNoomMetricForClassWithParent.php
2 +<?php
3 +class testCalculatesExpectedNoomMetricForClassWithParent extends ParentCalculatesExpectedNoomMetricForClassWithParent
4 +{
5 + public function bar() {}
6 + protected function baz() {}
7 +}
8 +
9 +class ParentCalculatesExpectedNoomMetricForClassWithParent
10 +{
11 + private function foo() {}
12 + public function bar() {}
13 + protected function baz() {}
14 +}
1083 1084 /tests/PHP/Depend/_code/metrics/Inheritance/testCalculatesExpectedNoamMetricForClassWithIndirectParent.php
2 +<?php
3 +class testCalculatesExpectedNoamMetricForClassWithIndirectParent extends ParentCalculatesExpectedNoamMetricForClassWithIndirectParent
4 +{
5 + public function foo() {}
6 + public function bar() {}
7 +}
8 +
9 +class ParentCalculatesExpectedNoamMetricForClassWithIndirectParent extends ParentParentCalculatesExpectedNoamMetricForClassWithIndirectParent
10 +{
11 +}
12 +
13 +class ParentParentCalculatesExpectedNoamMetricForClassWithIndirectParent
14 +{
15 + public function _baz() {}
16 +}
1083 1084 /tests/PHP/Depend/_code/metrics/Inheritance/testCalculatesExpectedNoamMetricForClassWithoutParent.php
2 +<?php
3 +class testCalculatesExpectedNoamMetricForClassWithoutParent
4 +{
5 + public function foo() {}
6 + protected function bar() {}
7 + private function baz() {}
8 +}
1083 1084 /tests/PHP/Depend/_code/metrics/Inheritance/testCalculatesExpectedNoamMetricForClassWithDirectParent.php
2 +<?php
3 +class testCalculatesExpectedNoamMetricForClassWithDirectParent extends ParentCalculatesExpectedNoamMetricForClassWithDirectParent
4 +{
5 + public function foo() {}
6 + public function bar() {}
7 +}
8 +
9 +class ParentCalculatesExpectedNoamMetricForClassWithDirectParent
10 +{
11 + public function _baz() {}
12 +}
1083 1084 /tests/PHP/Depend/_code/metrics/Inheritance/testCalculatesExpectedNoomMetricForClassWithParentPrivateMethods.php
2 +<?php
3 +class testCalculatesExpectedNoomMetricForClassWithParentPrivateMethods extends ParentCalculatesExpectedNoomMetricForClassWithParentPrivateMethods
4 +{
5 + private function foo() {}
6 + private function bar() {}
7 +}
8 +
9 +class ParentCalculatesExpectedNoomMetricForClassWithParentPrivateMethods
10 +{
11 + private function foo() {}
12 +}
1083 1084 /PHP/Depend/Metrics/Inheritance/Analyzer.php
93 93 M_NUMBER_OF_DERIVED_CLASSES = 'nocc';
94 94
95 95 /**
96 - * Contains the number of derived classes for each processed class. The array
97 - * size is equal to the number of analyzed classes.
98 - *
99 - * @var array(integer) $_derivedClasses
100 - */
101 - private $_derivedClasses = null;
102 -
103 - /**
104 96 * Contains the max inheritance depth for all root classes within the
105 97 * analyzed system. The array size is equal to the number of analyzed root
106 98 * classes.
123 115 */
124 116 private $_ahh = 0;
125 117
118 + /**
119 + * Total number of classes.
120 + *
121 + * @var integer
122 + */
126 123 private $_numberOfClasses = 0;
127 124
125 + /**
126 + * Total number of derived classes.
127 + *
128 + * @var integer
129 + */
128 130 private $_numberOfDerivedClasses = 0;
129 131
130 132 /**
132 134 *
133 135 * @var array(string=>array)
134 136 */
135 - private $_nodeMetrics = array();
137 + private $_nodeMetrics = null;
136 138
137 139 /**
138 140 * This method will return an <b>array</b> with all generated metric values
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 /**
218 229 $this->_initNodeMetricsForClass($class);
219 230
220 231 $this->_calculateNumberOfDerivedClasses($class);
221 - $this->_calculateHIT($class);
232 + $this->_calculateNumberOfAddedAndOverwrittenMethods($class);
233 + $this->_calculateDepthOfInheritanceTree($class);
222 234
223 235 $this->fireEndClass($class);
224 236 }
253 265 * @param PHP_Depend_Code_Class $class The context class instance.
254 266 *
255 267 * @return void
268 + * @since 0.9.10
256 269 */
257 - private function _calculateHIT(PHP_Depend_Code_Class $class)
270 + private function _calculateDepthOfInheritanceTree(PHP_Depend_Code_Class $class)
258 271 {
259 272 $dit = 0;
260 273 $uuid = $class->getUUID();
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())) {
1083 1084 /PHP/Depend/Code/AbstractClassOrInterface.php
379 379 $methods = array();
380 380 foreach ($this->getInterfaces() as $interface) {
381 381 foreach ($interface->getAllMethods() as $method) {
382 - $method[$method->getName()] = $method;
382 + $methods[$method->getName()] = $method;
383 383 }
384 384 }
385 385