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/Metrics.php';
49 require_once 'PHPUnit/Util/Filter.php';
51 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
58 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
59 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
60 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
61 * @version Release: 3.3.17
62 * @link http://www.phpunit.de/
63 * @since Class available since Release 3.2.0
65 class PHPUnit_Util_Metrics_File extends PHPUnit_Util_Metrics
67 protected $coverage = 0;
71 protected $locExecutable = 0;
72 protected $locExecuted = 0;
75 protected $classes = array();
76 protected $functions = array();
77 protected $lines = array();
78 protected $tokens = array();
80 protected static $cache = array();
85 * @param string $filename
86 * @param array $codeCoverage
87 * @throws RuntimeException
89 protected function __construct($filename, &$codeCoverage = array())
91 if (!file_exists($filename)) {
92 throw new RuntimeException(
94 'File "%s" not found.',
100 $this->filename = $filename;
101 $this->lines = file($filename);
102 $this->tokens = token_get_all(file_get_contents($filename));
105 $this->setCoverage($codeCoverage);
107 foreach (PHPUnit_Util_Class::getClassesInFile($filename) as $class) {
108 $this->classes[$class->getName()] = PHPUnit_Util_Metrics_Class::factory($class, $codeCoverage);
111 foreach (PHPUnit_Util_Class::getFunctionsInFile($filename) as $function) {
112 $this->functions[$function->getName()] = PHPUnit_Util_Metrics_Function::factory($function, $codeCoverage);
119 * @param string $filename
120 * @param array $codeCoverage
121 * @return PHPUnit_Util_Metrics_File
123 public static function factory($filename, &$codeCoverage = array())
125 if (!isset(self::$cache[$filename])) {
126 self::$cache[$filename] = new PHPUnit_Util_Metrics_File($filename, $codeCoverage);
129 else if (!empty($codeCoverage) && self::$cache[$filename]->getCoverage() == 0) {
130 self::$cache[$filename]->setCoverage($codeCoverage);
133 return self::$cache[$filename];
137 * @param array $codeCoverage
139 public function setCoverage(array &$codeCoverage)
141 if (!empty($codeCoverage)) {
142 $this->calculateCodeCoverage($codeCoverage);
144 foreach ($this->classes as $class) {
145 $class->setCoverage($codeCoverage);
148 foreach ($this->functions as $function) {
149 $function->setCoverage($codeCoverage);
155 * Returns the path to the file.
159 public function getPath()
161 return $this->filename;
169 public function getClasses()
171 return $this->classes;
177 * @param string $className
178 * @return ReflectionClass
180 public function getClass($className)
182 return $this->classes[$className];
190 public function getFunctions()
192 return $this->functions;
198 * @param string $functionName
199 * @return ReflectionClass
201 public function getFunction($functionName)
203 return $this->functions[$functionName];
211 public function getLines()
221 public function getTokens()
223 return $this->tokens;
227 * Returns the Code Coverage for the file.
231 public function getCoverage()
233 return $this->coverage;
237 * Lines of Code (LOC).
241 public function getLoc()
247 * Executable Lines of Code (ELOC).
251 public function getLocExecutable()
253 return $this->locExecutable;
257 * Executed Lines of Code.
261 public function getLocExecuted()
263 return $this->locExecuted;
267 * Comment Lines of Code (CLOC).
271 public function getCloc()
277 * Non-Comment Lines of Code (NCLOC).
281 public function getNcloc()
287 * Calculates the Code Coverage for the class.
289 * @param array $codeCoverage
291 protected function calculateCodeCoverage(&$codeCoverage)
293 $statistics = PHPUnit_Util_CodeCoverage::getStatistics(
300 $this->coverage = $statistics['coverage'];
301 $this->loc = $statistics['loc'];
302 $this->locExecutable = $statistics['locExecutable'];
303 $this->locExecuted = $statistics['locExecuted'];
308 protected function countLines()
310 $this->loc = count($this->lines);
313 foreach ($this->tokens as $i => $token) {
314 if (is_string($token)) {
318 list ($token, $value) = $token;
320 if ($token == T_COMMENT || $token == T_DOC_COMMENT) {
321 $this->cloc += count(explode("\n", $value));
325 $this->ncloc = $this->loc - $this->cloc;