5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 3.2.0
47 require_once 'PHPUnit/Util/Class.php';
48 require_once 'PHPUnit/Util/Filter.php';
50 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
53 * Function- and Method-Level Metrics.
57 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
58 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
59 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
60 * @version Release: 3.3.17
61 * @link http://www.phpunit.de/
62 * @since Class available since Release 3.2.0
64 class PHPUnit_Util_Metrics_Function extends PHPUnit_Util_Metrics
68 protected $coverage = 0;
71 protected $locExecutable = 0;
72 protected $locExecuted = 0;
73 protected $parameters = 0;
79 protected $dependencies = array();
81 protected static $cache = array();
86 * @param string $scope
87 * @param ReflectionFunction|ReflectionMethod $function
88 * @param array $codeCoverage
90 protected function __construct($scope, $function, &$codeCoverage = array())
92 $this->scope = $scope;
93 $this->function = $function;
95 $source = PHPUnit_Util_Class::getMethodSource(
96 $scope, $function->getName()
99 if ($source !== FALSE) {
100 $this->tokens = token_get_all('<?php' . $source . '?>');
101 $this->parameters = $function->getNumberOfParameters();
103 $this->calculateCCN();
104 $this->calculateNPath();
105 $this->calculateDependencies();
108 $this->setCoverage($codeCoverage);
114 * @param ReflectionFunction|ReflectionMethod $function
115 * @param array $codeCoverage
116 * @return PHPUnit_Util_Metrics_Method
118 public static function factory($function, &$codeCoverage = array())
120 if ($function instanceof ReflectionMethod) {
121 $scope = $function->getDeclaringClass()->getName();
126 $name = $function->getName();
128 if (!isset(self::$cache[$scope][$name])) {
129 self::$cache[$scope][$name] = new PHPUnit_Util_Metrics_Function($scope, $function, $codeCoverage);
132 else if (!empty($codeCoverage) && self::$cache[$scope][$name]->getCoverage() == 0) {
133 self::$cache[$scope][$name]->setCoverage($codeCoverage);
136 return self::$cache[$scope][$name];
140 * @param array $codeCoverage
142 public function setCoverage(array &$codeCoverage)
144 if (!empty($codeCoverage)) {
145 $this->calculateCodeCoverage($codeCoverage);
146 $this->calculateCrapIndex();
151 * Returns the function.
153 * @return ReflectionFunction
155 public function getFunction()
157 return $this->function;
161 * Returns the method.
162 * Alias for getFunction().
164 * @return ReflectionMethod
166 public function getMethod()
168 return $this->function;
172 * Returns the names of the classes this function or method depends on.
176 public function getDependencies()
178 return $this->dependencies;
182 * Lines of Code (LOC).
186 public function getLoc()
192 * Executable Lines of Code (ELOC).
196 public function getLocExecutable()
198 return $this->locExecutable;
202 * Executed Lines of Code.
206 public function getLocExecuted()
208 return $this->locExecuted;
212 * Number of Parameters.
216 public function getParameters()
218 return $this->parameters;
222 * Returns the Cyclomatic Complexity Number (CCN) for the method.
223 * This is also known as the McCabe metric.
225 * Each method has a minimum value of 1 per default. For each of the
226 * following PHP keywords/statements this value gets incremented by one:
239 * Note that 'else', 'default', and 'finally' don't increment the value
240 * any further. On the other hand, a simple method with a 'switch'
241 * statement and a huge block of 'case 'statements can have a surprisingly
242 * high value (still it has the same value when converting a 'switch'
243 * block to an equivalent sequence of 'if' statements).
246 * @see http://en.wikipedia.org/wiki/Cyclomatic_complexity
248 public function getCCN()
254 * Returns the Change Risk Analysis and Predictions (CRAP) index for the
258 * @see http://www.artima.com/weblogs/viewpost.jsp?thread=210575
260 public function getCrapIndex()
266 * Returns the Code Coverage for the method.
270 public function getCoverage()
272 return $this->coverage;
276 * Returns the NPath Complexity for the method.
280 public function getNPath()
286 * Calculates the Cyclomatic Complexity Number (CCN) for the method.
289 protected function calculateCCN()
291 foreach ($this->tokens as $token) {
292 if (is_string($token)) {
293 $token = trim($token);
302 list ($token, $value) = $token;
324 * Calculates the NPath Complexity for the method.
327 protected function calculateNPath()
329 $npathStack = array();
332 foreach ($this->tokens as $token) {
333 if (is_string($token)) {
334 $token = trim($token);
337 $this->npath = ($this->npath + 1) * $this->npath;
342 array_push($stack, $scope);
343 array_push($npathStack, $this->npath);
346 array_push($stack, NULL);
351 $scope = array_pop($stack);
353 if ($scope !== NULL) {
362 $this->npath = ($this->npath + 1) * array_pop($npathStack);
369 $this->npath = ($this->npath - 1) + array_pop($npathStack);
379 list ($token, $value) = $token;
400 * Calculates the Code Coverage for the method.
402 * @param array $codeCoverage
404 protected function calculateCodeCoverage(&$codeCoverage)
406 $statistics = PHPUnit_Util_CodeCoverage::getStatistics(
408 $this->function->getFileName(),
409 $this->function->getStartLine(),
410 $this->function->getEndLine()
413 $this->coverage = $statistics['coverage'];
414 $this->loc = $statistics['loc'];
415 $this->locExecutable = $statistics['locExecutable'];
416 $this->locExecuted = $statistics['locExecuted'];
420 * Calculates the Change Risk Analysis and Predictions (CRAP) index for the
424 protected function calculateCrapIndex()
426 if ($this->coverage == 0) {
427 $this->crap = pow($this->ccn, 2) + $this->ccn;
430 else if ($this->coverage >= 95) {
431 $this->crap = $this->ccn;
435 $this->crap = pow($this->ccn, 2) * pow(1 - $this->coverage/100, 3) + $this->ccn;
440 * Calculates the dependencies for this function or method.
443 protected function calculateDependencies()
445 foreach ($this->function->getParameters() as $parameter) {
447 $class = $parameter->getClass();
450 $className = $class->getName();
452 if ($className != $this->scope && !in_array($className, $this->dependencies)) {
453 $this->dependencies[] = $className;
458 catch (ReflectionException $e) {
464 foreach ($this->tokens as $token) {
465 if (is_string($token)) {
466 if (trim($token) == ';') {
473 list ($token, $value) = $token;
483 if ($value != $this->scope && class_exists($value, FALSE)) {
485 $class = new ReflectionClass($value);
487 if ($class->isUserDefined() && !in_array($value, $this->dependencies)) {
488 $this->dependencies[] = $value;
492 catch (ReflectionException $e) {