PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PHP_CodeBrowser/Plugins/ErrorCoverage.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 185 lines | 60 code | 18 blank | 107 comment | 8 complexity | bde84c04465a72a0bb53d6f3e9adb776 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * Coverage
  4. *
  5. * PHP Version 5.3.2
  6. *
  7. * Copyright (c) 2007-2010, Mayflower GmbH
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * * Neither the name of Mayflower GmbH nor the names of his
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  33. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * @category PHP_CodeBrowser
  40. * @package PHP_CodeBrowser
  41. * @subpackage Plugins
  42. * @author Elger Thiele <elger.thiele@mayflower.de>
  43. * @author Michel Hartmann <michel.hartmann@mayflower.de>
  44. * @copyright 2007-2010 Mayflower GmbH
  45. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  46. * @version SVN: $Id$
  47. * @link http://www.phpunit.de/
  48. * @since File available since 0.1.0
  49. */
  50. /**
  51. * CbErrorCoverage
  52. *
  53. * @category PHP_CodeBrowser
  54. * @package PHP_CodeBrowser
  55. * @subpackage Plugins
  56. * @author Elger Thiele <elger.thiele@mayflower.de>
  57. * @author Michel Hartmann <michel.hartmann@mayflower.de>
  58. * @copyright 2007-2010 Mayflower GmbH
  59. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  60. * @version Release: 0.9.1
  61. * @link http://www.phpunit.de/
  62. * @since Class available since 0.1.0
  63. */
  64. class CbErrorCoverage extends CbPluginsAbstract
  65. {
  66. /**
  67. * Name of this plugin.
  68. * Used to read issues from XML.
  69. * @var String
  70. */
  71. public $pluginName = 'coverage';
  72. /**
  73. * Name of the attribute that holds the number of the first line
  74. * of the issue.
  75. * @var String
  76. */
  77. protected $_lineStartAttr = 'num';
  78. /**
  79. * Name of the attribute that holds the number of the last line
  80. * of the issue.
  81. * @var String
  82. */
  83. protected $_lineEndAttr = 'num';
  84. /**
  85. * Default string to use as source for issue.
  86. * @var String
  87. */
  88. protected $_source = 'Coverage';
  89. /**
  90. * The detailed mapper method for each single plugin, returning an array
  91. * of Issue objects.
  92. * This method provides a default behaviour an can be overloaded to
  93. * implement special behavior for other plugins.
  94. *
  95. * @param DomNode $element The XML plugin node with its errors
  96. * @param filename Name of the file to return issues for.
  97. *
  98. * @return array Array of issue objects.
  99. */
  100. public function mapIssues(DomNode $element, $filename)
  101. {
  102. $errorList = array();
  103. $children = $element->childNodes;
  104. $childCount = $children->length;
  105. $next = 0;
  106. for($next = 0; $next < $childCount; $next ++)
  107. {
  108. $child = $children->item($next);
  109. if ($this->representsUncoveredLOC($child))
  110. {
  111. $begin = $child->getAttribute('num');
  112. $end = $begin;
  113. $next += 1;
  114. while ($next < $childCount)
  115. {
  116. $child = $children->item($next);
  117. if (! $child instanceof DOMElement)
  118. {
  119. $next += 1;
  120. continue;
  121. }
  122. if (! $this->representsUncoveredLOC($child))
  123. {
  124. break;
  125. }
  126. $end = $child->getAttribute('num');
  127. $next += 1;
  128. }
  129. $errorList[] = new CbIssue($filename, $begin, $end, 'Coverage', 'Not covered', 'Notice');
  130. }
  131. }
  132. return $errorList;
  133. }
  134. /**
  135. * Check if the given object is a DOMElement representing an
  136. * uncovered line of code.
  137. */
  138. private function representsUncoveredLOC($elem)
  139. {
  140. return ($elem instanceof DOMElement && 0 === (int) $elem->getAttribute('count') && 'line' === $elem->nodeName && 'stmt' === $elem->getAttribute('type'));
  141. }
  142. /**
  143. * Get an array with all files that have issues.
  144. *
  145. * @return Array
  146. */
  147. public function getFilesWithIssues()
  148. {
  149. $filenames = array();
  150. $issueNodes = $this->_issueXml->query('/*/' . $this->pluginName . '/*/file[@name]');
  151. foreach ($issueNodes as $node)
  152. {
  153. $filenames[] = $node->getAttribute('name');
  154. }
  155. return array_unique($filenames);
  156. }
  157. /**
  158. * Get all DOMNodes that represent issues for a specific file.
  159. *
  160. * @param String $filename Name of the file to get nodes for.
  161. * @return DOMNodeList
  162. */
  163. protected function _getIssueNodes($filename)
  164. {
  165. return $this->_issueXml->query('/*/' . $this->pluginName . '/*/file[@name="' . $filename . '"]');
  166. }
  167. }