PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PHP_CodeBrowser/SourceHandler.php

https://bitbucket.org/chamilo/chamilo/
PHP | 211 lines | 74 code | 13 blank | 124 comment | 8 complexity | 025a4c8f93439a610cb608159cd00b21 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. * Source handler
  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. * @author Simon Kohlmeyer <simon.kohlmeyer@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.2.0
  49. */
  50. /**
  51. * CbSourceHandler
  52. *
  53. * This class manages lists of source files and their issues.
  54. * For providing these lists the prior generated CbIssueXml is parsed.
  55. *
  56. * @category PHP_CodeBrowser
  57. * @package PHP_CodeBrowser
  58. * @author Elger Thiele <elger.thiele@mayflower.de>
  59. * @author Christopher Weckerle <christopher.weckerle@mayflower.de>
  60. * @author Michel Hartmann <michel.hartmann@mayflower.de>
  61. * @author Simon Kohlmeyer <simon.kohlmeyer@mayflower.de>
  62. * @copyright 2007-2010 Mayflower GmbH
  63. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  64. * @version Release: 0.9.1
  65. * @link http://www.phpunit.de/
  66. * @since Class available since 0.2.0
  67. */
  68. class CbSourceHandler
  69. {
  70. /**
  71. * Files to be included in the report
  72. *
  73. * @var Array of CbFile
  74. */
  75. protected $_files = array();
  76. /**
  77. * Pear Log object where debug output should go to.
  78. *
  79. * @var Log
  80. */
  81. protected $_debugLog;
  82. /**
  83. * Default constructor
  84. *
  85. * @param Array $plugins The plugins to get issues from.
  86. */
  87. public function __construct ($debugLog, array $plugins = array())
  88. {
  89. $this->_debugLog = $debugLog;
  90. array_walk($plugins, array($this, 'addPlugin'));
  91. }
  92. /**
  93. * Add a new plugin to the handler.
  94. *
  95. * @param CbPluginsAbstract $plugin The plugin to add.
  96. */
  97. public function addPlugin(CbPluginsAbstract $plugin)
  98. {
  99. foreach ($plugin->getFilelist() as $file) {
  100. if (array_key_exists($file->name(), $this->_files)) {
  101. $this->_files[$file->name()]->mergeWith($file);
  102. } else {
  103. $this->_files[$file->name()] = $file;
  104. }
  105. }
  106. }
  107. /**
  108. * Add source files to the list.
  109. *
  110. * @param Array of SplFileInfo|String $files The files to add
  111. */
  112. public function addSourceFiles($files)
  113. {
  114. foreach ($files as $f) {
  115. $this->addSourceFile($f);
  116. }
  117. }
  118. /**
  119. * Add a source file.
  120. *
  121. * @param String|SplFileInfo $file The file to add
  122. */
  123. public function addSourceFile($file)
  124. {
  125. if (is_string($file)) {
  126. $filename = $file;
  127. $file = realpath($file);
  128. } else {
  129. $filename = $file->getPathName();
  130. $file = $file->getRealPath();
  131. }
  132. if (!$file) {
  133. throw new Exception("$filename is no regular file");
  134. }
  135. if (!array_key_exists($file, $this->_files)) {
  136. $this->_files[$file] = new CbFile($file);
  137. }
  138. }
  139. /**
  140. * Retrieves the parent directory all files have in common.
  141. *
  142. * @return String
  143. */
  144. public function getCommonPathPrefix()
  145. {
  146. return CbIOHelper::getCommonPathPrefix(array_keys($this->_files));
  147. }
  148. /**
  149. * Returns a sorted array of the files that should be in the report.
  150. *
  151. * @return Array of CbFile
  152. */
  153. public function getFiles()
  154. {
  155. CbFile::sort($this->_files);
  156. return $this->_files;
  157. }
  158. /**
  159. * Get a unique list of all filenames with issues.
  160. *
  161. * @return Array
  162. */
  163. public function getFilesWithIssues()
  164. {
  165. return array_keys($this->_files);
  166. }
  167. /**
  168. * Remove all files that match the given PCRE.
  169. *
  170. * @param String $expr The PCRE specifying which files to remove.
  171. * @return void.
  172. */
  173. public function excludeMatchingPCRE($expr)
  174. {
  175. foreach (array_keys($this->_files) as $filename) {
  176. if (preg_match($expr, $filename)) {
  177. $this->_debugLog->debug("Excluding $filename, it matches PCRE $expr");
  178. unset($this->_files[$filename]);
  179. }
  180. }
  181. }
  182. /**
  183. * Remove all files that match the given shell wildcard pattern
  184. * as accepted by fnmatch().
  185. *
  186. * @param String $pattern The pattern.
  187. * @return void.
  188. */
  189. public function excludeMatchingPattern($pattern)
  190. {
  191. foreach (array_keys($this->_files) as $filename) {
  192. if (fnmatch($pattern, $filename)) {
  193. $this->_debugLog->debug("Excluding $filename, it matches pattern $pattern");
  194. unset($this->_files[$filename]);
  195. }
  196. }
  197. }
  198. }