CoverageThresholdTask.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * $Id: CoverageThresholdTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. require_once 'phing/system/io/PhingFile.php';
  23. require_once 'phing/system/util/Properties.php';
  24. /**
  25. * Stops the build if any of the specified coverage threshold was not reached
  26. *
  27. * @author Benjamin Schultz <bschultz@proqrent.de>
  28. * @version $Id: CoverageThresholdTask.php 905 2010-10-05 16:28:03Z mrook $
  29. * @package phing.tasks.ext.coverage
  30. * @since 2.4.1
  31. */
  32. class CoverageThresholdTask extends Task
  33. {
  34. /**
  35. * Holds an optional classpath
  36. *
  37. * @var Path
  38. */
  39. private $_classpath = null;
  40. /**
  41. * Holds an optional database file
  42. *
  43. * @var PhingFile
  44. */
  45. private $_database = null;
  46. /**
  47. * Holds the coverage threshold for the entire project
  48. *
  49. * @var integer
  50. */
  51. private $_perProject = 25;
  52. /**
  53. * Holds the coverage threshold for any class
  54. *
  55. * @var integer
  56. */
  57. private $_perClass = 25;
  58. /**
  59. * Holds the coverage threshold for any method
  60. *
  61. * @var integer
  62. */
  63. private $_perMethod = 25;
  64. /**
  65. * Holds the minimum found coverage value for a class
  66. *
  67. * @var integer
  68. */
  69. private $_minClassCoverageFound = null;
  70. /**
  71. * Holds the minimum found coverage value for a method
  72. *
  73. * @var integer
  74. */
  75. private $_minMethodCoverageFound = null;
  76. /**
  77. * Number of statements in the entire project
  78. *
  79. * @var integer
  80. */
  81. private $_projectStatementCount = 0;
  82. /**
  83. * Number of covered statements in the entire project
  84. *
  85. * @var integer
  86. */
  87. private $_projectStatementsCovered = 0;
  88. /**
  89. * Whether to enable detailed logging
  90. *
  91. * @var boolean
  92. */
  93. private $_verbose = false;
  94. /**
  95. * Sets an optional classpath
  96. *
  97. * @param Path $classpath The classpath
  98. */
  99. public function setClasspath(Path $classpath)
  100. {
  101. if ($this->_classpath === null) {
  102. $this->_classpath = $classpath;
  103. } else {
  104. $this->_classpath->append($classpath);
  105. }
  106. }
  107. /**
  108. * Sets the optional coverage database to use
  109. *
  110. * @param PhingFile The database file
  111. */
  112. public function setDatabase(PhingFile $database)
  113. {
  114. $this->_database = $database;
  115. }
  116. /**
  117. * Create classpath object
  118. *
  119. * @return Path
  120. */
  121. public function createClasspath()
  122. {
  123. $this->classpath = new Path();
  124. return $this->classpath;
  125. }
  126. /**
  127. * Sets the coverage threshold for entire project
  128. *
  129. * @param integer $threshold Coverage threshold for entire project
  130. */
  131. public function setPerProject($threshold)
  132. {
  133. $this->_perProject = $threshold;
  134. }
  135. /**
  136. * Sets the coverage threshold for any class
  137. *
  138. * @param integer $threshold Coverage threshold for any class
  139. */
  140. public function setPerClass($threshold)
  141. {
  142. $this->_perClass = $threshold;
  143. }
  144. /**
  145. * Sets the coverage threshold for any method
  146. *
  147. * @param integer $threshold Coverage threshold for any method
  148. */
  149. public function setPerMethod($threshold)
  150. {
  151. $this->_perMethod = $threshold;
  152. }
  153. /**
  154. * Sets whether to enable detailed logging or not
  155. *
  156. * @param boolean $verbose
  157. */
  158. public function setVerbose($verbose)
  159. {
  160. $this->_verbose = StringHelper::booleanValue($verbose);
  161. }
  162. /**
  163. * Filter covered statements
  164. *
  165. * @param integer $var Coverage CODE/count
  166. * @return boolean
  167. */
  168. protected function filterCovered($var)
  169. {
  170. return ($var >= 0 || $var === -2);
  171. }
  172. /**
  173. * Calculates the coverage threshold
  174. *
  175. * @param string $filename The filename to analyse
  176. * @param array $coverageInformation Array with coverage information
  177. */
  178. protected function calculateCoverageThreshold($filename, $coverageInformation)
  179. {
  180. $classes = PHPUnitUtil::getDefinedClasses($filename, $this->_classpath);
  181. if (is_array($classes)) {
  182. foreach ($classes as $className) {
  183. $reflection = new ReflectionClass($className);
  184. $classStartLine = $reflection->getStartLine();
  185. // Strange PHP5 reflection bug, classes without parent class
  186. // or implemented interfaces seem to start one line off
  187. if ($reflection->getParentClass() === null
  188. && count($reflection->getInterfaces()) === 0
  189. ) {
  190. unset($coverageInformation[$classStartLine + 1]);
  191. } else {
  192. unset($coverageInformation[$classStartLine]);
  193. }
  194. reset($coverageInformation);
  195. $methods = $reflection->getMethods();
  196. foreach ($methods as $method) {
  197. // PHP5 reflection considers methods of a parent class
  198. // to be part of a subclass, we don't
  199. if ($method->getDeclaringClass()->getName() != $reflection->getName()) {
  200. continue;
  201. }
  202. $methodStartLine = $method->getStartLine();
  203. $methodEndLine = $method->getEndLine();
  204. // small fix for XDEBUG_CC_UNUSED
  205. if (isset($coverageInformation[$methodStartLine])) {
  206. unset($coverageInformation[$methodStartLine]);
  207. }
  208. if (isset($coverageInformation[$methodEndLine])) {
  209. unset($coverageInformation[$methodEndLine]);
  210. }
  211. if ($method->isAbstract()) {
  212. continue;
  213. }
  214. $lineNr = key($coverageInformation);
  215. while ($lineNr !== null && $lineNr < $methodStartLine) {
  216. next($coverageInformation);
  217. $lineNr = key($coverageInformation);
  218. }
  219. $methodStatementsCovered = 0;
  220. $methodStatementCount = 0;
  221. while ($lineNr !== null && $lineNr <= $methodEndLine) {
  222. $methodStatementCount++;
  223. $lineCoverageInfo = $coverageInformation[$lineNr];
  224. // set covered when CODE is other than -1 (not executed)
  225. if ($lineCoverageInfo > 0 || $lineCoverageInfo === -2) {
  226. $methodStatementsCovered++;
  227. }
  228. next($coverageInformation);
  229. $lineNr = key($coverageInformation);
  230. }
  231. if ($methodStatementCount > 0) {
  232. $methodCoverage = ( $methodStatementsCovered
  233. / $methodStatementCount) * 100;
  234. } else {
  235. $methodCoverage = 0;
  236. }
  237. if ($methodCoverage < $this->_perMethod
  238. && !$method->isAbstract()
  239. ) {
  240. throw new BuildException(
  241. 'The coverage (' . $methodCoverage . '%) '
  242. . 'for method "' . $method->getName() . '" is lower'
  243. . ' than the specified threshold ('
  244. . $this->_perMethod . '%), see file: "'
  245. . $filename . '"'
  246. );
  247. } elseif ($methodCoverage < $this->_perMethod
  248. && $method->isAbstract()
  249. ) {
  250. if ($this->_verbose === true) {
  251. $this->log(
  252. 'Skipped coverage threshold for abstract method "'
  253. . $method->getName() . '"'
  254. );
  255. }
  256. }
  257. // store the minimum coverage value for logging (see #466)
  258. if ($this->_minMethodCoverageFound !== null) {
  259. if ($this->_minMethodCoverageFound > $methodCoverage) {
  260. $this->_minMethodCoverageFound = $methodCoverage;
  261. }
  262. } else {
  263. $this->_minMethodCoverageFound = $methodCoverage;
  264. }
  265. }
  266. $classStatementCount = count($coverageInformation);
  267. $classStatementsCovered = count(
  268. array_filter(
  269. $coverageInformation,
  270. array($this, 'filterCovered')
  271. )
  272. );
  273. if ($classStatementCount > 0) {
  274. $classCoverage = ( $classStatementsCovered
  275. / $classStatementCount) * 100;
  276. } else {
  277. $classCoverage = 0;
  278. }
  279. if ($classCoverage < $this->_perClass
  280. && !$reflection->isAbstract()
  281. ) {
  282. throw new BuildException(
  283. 'The coverage (' . $classCoverage . '%) for class "'
  284. . $reflection->getName() . '" is lower than the '
  285. . 'specified threshold (' . $this->_perClass . '%), '
  286. . 'see file: "' . $filename . '"'
  287. );
  288. } elseif ($classCoverage < $this->_perClass
  289. && $reflection->isAbstract()
  290. ) {
  291. if ($this->_verbose === true) {
  292. $this->log(
  293. 'Skipped coverage threshold for abstract class "'
  294. . $reflection->getName() . '"'
  295. );
  296. }
  297. }
  298. // store the minimum coverage value for logging (see #466)
  299. if ($this->_minClassCoverageFound !== null) {
  300. if ($this->_minClassCoverageFound > $classCoverage) {
  301. $this->_minClassCoverageFound = $classCoverage;
  302. }
  303. } else {
  304. $this->_minClassCoverageFound = $classCoverage;
  305. }
  306. $this->_projectStatementCount += $classStatementCount;
  307. $this->_projectStatementsCovered += $classStatementsCovered;
  308. }
  309. }
  310. }
  311. public function main()
  312. {
  313. if ($this->_database === null) {
  314. $coverageDatabase = $this->project
  315. ->getProperty('coverage.database');
  316. if (! $coverageDatabase) {
  317. throw new BuildException(
  318. 'Either include coverage-setup in your build file or set '
  319. . 'the "database" attribute'
  320. );
  321. }
  322. $database = new PhingFile($coverageDatabase);
  323. } else {
  324. $database = $this->_database;
  325. }
  326. $this->log(
  327. 'Calculating coverage threshold: min. '
  328. . $this->_perProject . '% per project, '
  329. . $this->_perClass . '% per class and '
  330. . $this->_perMethod . '% per method is required'
  331. );
  332. $props = new Properties();
  333. $props->load($database);
  334. foreach ($props->keys() as $filename) {
  335. $file = unserialize($props->getProperty($filename));
  336. $this->calculateCoverageThreshold(
  337. $file['fullname'],
  338. $file['coverage']
  339. );
  340. }
  341. if ($this->_projectStatementCount > 0) {
  342. $coverage = ( $this->_projectStatementsCovered
  343. / $this->_projectStatementCount) * 100;
  344. } else {
  345. $coverage = 0;
  346. }
  347. if ($coverage < $this->_perProject) {
  348. throw new BuildException(
  349. 'The coverage (' . $coverage . '%) for the entire project '
  350. . 'is lower than the specified threshold ('
  351. . $this->_perProject . '%)'
  352. );
  353. }
  354. $this->log(
  355. 'Passed coverage threshold. Minimum found coverage values are: '
  356. . round($coverage, 2) . '% per project, '
  357. . round($this->_minClassCoverageFound, 2) . '% per class and '
  358. . round($this->_minMethodCoverageFound, 2) . '% per method'
  359. );
  360. }
  361. }