PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PHP_CodeBrowser/PluginsAbstract.php

https://bitbucket.org/chamilo/chamilo/
PHP | 288 lines | 89 code | 19 blank | 180 comment | 1 complexity | ad1e8e3d71a08023de22a91a6d250d1f 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. * Plugin Abstract
  4. *
  5. * PHP Version 5.3.0
  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. * @author Elger Thiele <elger.thiele@mayflower.de>
  42. * @author Michel Hartmann <michel.hartmann@mayflower.de>
  43. * @copyright 2007-2010 Mayflower GmbH
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://www.phpunit.de/
  47. * @since File available since 0.1.0
  48. */
  49. /**
  50. * CbPluginsAbstract
  51. *
  52. * @category PHP_CodeBrowser
  53. * @package PHP_CodeBrowser
  54. * @author Elger Thiele <elger.thiele@mayflower.de>
  55. * @author Michel Hartmann <michel.hartmann@mayflower.de>
  56. * @copyright 2007-2010 Mayflower GmbH
  57. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  58. * @version Release: 0.9.1
  59. * @link http://www.phpunit.de/
  60. * @since Class available since 0.1.0
  61. */
  62. abstract class CbPluginsAbstract
  63. {
  64. /**
  65. * The name of the plugin.
  66. * This should be the name that is written to the XML error files by
  67. * cruisecontrol.
  68. *
  69. * @var string
  70. */
  71. public $pluginName;
  72. /**
  73. * The CbIssueXml object
  74. *
  75. * @var CbIssueXml
  76. */
  77. protected $_issueXml;
  78. /**
  79. * Name of the attribute that holds the number of the first line
  80. * of the issue.
  81. *
  82. * @var String
  83. */
  84. protected $_lineStartAttr;
  85. /**
  86. * Name of the attribute that holds the number of the last line
  87. * of the issue.
  88. *
  89. * @var String
  90. */
  91. protected $_lineEndAttr;
  92. /**
  93. * Name of the attribute that holds message of the issue.
  94. *
  95. * @var String
  96. */
  97. protected $_descriptionAttr;
  98. /**
  99. * Name of the attribute that holds severity of the issue.
  100. *
  101. * @var String
  102. */
  103. protected $_severityAttr;
  104. /**
  105. * Default string to use as source for issue.
  106. *
  107. * @var String
  108. */
  109. protected $_source;
  110. /**
  111. * Default Constructor
  112. *
  113. * @param CbIssueXml $issueXml The cc XML document.
  114. */
  115. public function __construct(CbIssueXml $issueXml)
  116. {
  117. $this->_issueXml = $issueXml;
  118. }
  119. /**
  120. * Gets a list of CbFile objects, including their issues.
  121. *
  122. * @return Array of CbFile List of files with issues.
  123. */
  124. public function getFilelist()
  125. {
  126. $files = array();
  127. foreach ($this->getFilesWithIssues() as $name) {
  128. $files[] = new CbFile($name, $this->getIssuesByFile($name));
  129. }
  130. return $files;
  131. }
  132. /**
  133. * Parse the cc XML file for defined error type, e.g. "pmd" and map this
  134. * error to the Issue objects format.
  135. *
  136. * @param String $filename Name of the file to parse the errors for.
  137. *
  138. * @return array
  139. */
  140. public function getIssuesByFile($filename)
  141. {
  142. $issues = array();
  143. foreach ($this->_getIssueNodes($filename) as $issueNode) {
  144. $issues = array_merge(
  145. $issues,
  146. $this->mapIssues($issueNode, $filename)
  147. );
  148. }
  149. return $issues;
  150. }
  151. /**
  152. * Get an array with all files that have issues.
  153. *
  154. * @return Array
  155. */
  156. public function getFilesWithIssues()
  157. {
  158. $filenames = array();
  159. $issueNodes = $this->_issueXml->query(
  160. sprintf('/*/%s/file[@name]', $this->pluginName)
  161. );
  162. foreach ($issueNodes as $node) {
  163. $filenames[] = $node->getAttribute('name');
  164. }
  165. return array_unique($filenames);
  166. }
  167. /**
  168. * The detailed mapper method for each single plugin, returning an array
  169. * of issue objects.
  170. * This method provides a default behaviour an can be overloaded to
  171. * implement special behavior for other plugins.
  172. *
  173. * @param DomNode $element The XML plugin node with its errors
  174. * @param String $filename Name of the file to return issues for.
  175. *
  176. * @return Array Array of issue objects.
  177. */
  178. public function mapIssues(DomNode $element, $filename)
  179. {
  180. $errorList = array();
  181. foreach ($element->childNodes as $child) {
  182. if (!($child instanceof DOMElement)) {
  183. continue;
  184. }
  185. $errorList[] = new CbIssue(
  186. $filename,
  187. $this->_getLineStart($child),
  188. $this->_getLineEnd($child),
  189. $this->_getSource($child),
  190. $this->_getDescription($child),
  191. $this->_getSeverity($child)
  192. );
  193. }
  194. return $errorList;
  195. }
  196. /**
  197. * Get all DOMNodes that represent issues for a specific file.
  198. *
  199. * @param String $filename Name of the file to get nodes for.
  200. *
  201. * @return DOMNodeList
  202. */
  203. protected function _getIssueNodes($filename)
  204. {
  205. return $this->_issueXml->query(
  206. sprintf('/*/%s/file[@name="%s"]', $this->pluginName, $filename)
  207. );
  208. }
  209. /**
  210. * Default method for retrieving the first line of an issue.
  211. * @see self::mapIssues
  212. *
  213. * @param DOMElement $element
  214. *
  215. * @return Integer
  216. */
  217. protected function _getLineStart(DOMElement $element)
  218. {
  219. return (int) $element->getAttribute($this->_lineStartAttr);
  220. }
  221. /**
  222. * Default method for retrieving the last line of an issue.
  223. * @see self::mapIssues
  224. *
  225. * @param DOMElement $element
  226. *
  227. * @return Integer
  228. */
  229. protected function _getLineEnd(DOMElement $element)
  230. {
  231. return (int) $element->getAttribute($this->_lineEndAttr);
  232. }
  233. /**
  234. * Default method for retrieving the source of an issue.
  235. * @see self::mapIssues
  236. *
  237. * @param DOMElement $element
  238. *
  239. * @return String
  240. */
  241. protected function _getSource(DOMElement $element)
  242. {
  243. return $this->_source;
  244. }
  245. /**
  246. * Default method for retrieving the description of an issue.
  247. * @see self::mapIssues
  248. *
  249. * @param DOMElement $element
  250. *
  251. * @return String
  252. */
  253. protected function _getDescription(DOMElement $element)
  254. {
  255. return htmlentities($element->getAttribute($this->_descriptionAttr));
  256. }
  257. /**
  258. * Default method for retrieving the severity of an issue.
  259. * @see self::mapIssues
  260. *
  261. * @param DOMElement $element
  262. *
  263. * @return String
  264. */
  265. protected function _getSeverity(DOMElement $element)
  266. {
  267. return htmlentities($element->getAttribute($this->_severityAttr));
  268. }
  269. }