/libs/php/pear/PHP/PMD/Report.php

https://github.com/KaRLsM/SIFO · PHP · 168 lines · 47 code · 14 blank · 107 comment · 2 complexity · 43aa12682bee89a45b54fd892169629f MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of PHP_PMD.
  4. *
  5. * PHP Version 5
  6. *
  7. * Copyright (c) 2009-2011, Manuel Pichler <mapi@phpmd.org>.
  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 Manuel Pichler 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
  40. * @package PHP_PMD
  41. * @author Manuel Pichler <mapi@phpmd.org>
  42. * @copyright 2009-2011 Manuel Pichler. All rights reserved.
  43. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  44. * @version SVN: $Id$
  45. * @link http://phpmd.org
  46. */
  47. /**
  48. * The report class collects all found violations and further information about
  49. * a PHP_PMD run.
  50. *
  51. * @category PHP
  52. * @package PHP_PMD
  53. * @author Manuel Pichler <mapi@phpmd.org>
  54. * @copyright 2009-2011 Manuel Pichler. All rights reserved.
  55. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  56. * @version Release: 1.1.1
  57. * @link http://phpmd.org
  58. */
  59. class PHP_PMD_Report
  60. {
  61. /**
  62. * List of rule violations detected in the analyzed source code.
  63. *
  64. * @var array(PHP_PMD_RuleViolations) $_ruleViolations
  65. */
  66. private $_ruleViolations = array();
  67. /**
  68. * The start time for this report.
  69. *
  70. * @var float $_startTime
  71. */
  72. private $_startTime = 0.0;
  73. /**
  74. * The end time for this report.
  75. *
  76. * @var float $_endTime
  77. */
  78. private $_endTime = 0.0;
  79. /**
  80. * Adds a rule violation to this report.
  81. *
  82. * @param PHP_PMD_RuleViolation $violation The occured rule violation.
  83. *
  84. * @return void
  85. */
  86. public function addRuleViolation(PHP_PMD_RuleViolation $violation)
  87. {
  88. $fileName = $violation->getFileName();
  89. if (!isset($this->_ruleViolations[$fileName])) {
  90. $this->_ruleViolations[$fileName] = array();
  91. }
  92. $beginLine = $violation->getBeginLine();
  93. if (!isset($this->_ruleViolations[$fileName][$beginLine])) {
  94. $this->_ruleViolations[$fileName][$beginLine] = array();
  95. }
  96. $this->_ruleViolations[$fileName][$beginLine][] = $violation;
  97. }
  98. /**
  99. * Returns <b>true</b> when this report does not contain any errors.
  100. *
  101. * @return boolean
  102. * @since 0.2.5
  103. */
  104. public function isEmpty()
  105. {
  106. return (count($this->_ruleViolations) === 0);
  107. }
  108. /**
  109. * Returns an iterator with all occured rule violations.
  110. *
  111. * @return Iterator
  112. */
  113. public function getRuleViolations()
  114. {
  115. // First sort by file name
  116. ksort($this->_ruleViolations);
  117. $violations = array();
  118. foreach ($this->_ruleViolations as $violationInLine) {
  119. // Second sort is by line number
  120. ksort($violationInLine);
  121. foreach ($violationInLine as $violation) {
  122. $violations = array_merge($violations, $violation);
  123. }
  124. }
  125. return new ArrayIterator($violations);
  126. }
  127. /**
  128. * Starts the time tracking of this report instance.
  129. *
  130. * @return void
  131. */
  132. public function start()
  133. {
  134. $this->_startTime = microtime(true) * 1000.0;
  135. }
  136. /**
  137. * Stops the time tracking of this report instance.
  138. *
  139. * @return void
  140. */
  141. public function end()
  142. {
  143. $this->_endTime = microtime(true) * 1000.0;
  144. }
  145. /**
  146. * Returns the total time elapsed for the source analysis.
  147. *
  148. * @return float
  149. */
  150. public function getElapsedTimeInMillis()
  151. {
  152. return round($this->_endTime - $this->_startTime);
  153. }
  154. }