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

/vendor/phing/phing/classes/phing/tasks/ext/phploc/PHPLocTask.php

https://gitlab.com/the_sociophobic/test
PHP | 266 lines | 208 code | 9 blank | 49 comment | 24 complexity | 712f43447801991c674ee37d3fdaee24 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  5. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  6. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  8. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  9. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  10. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  11. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  12. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  13. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  14. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15. *
  16. * This software consists of voluntary contributions made by many individuals
  17. * and is licensed under the LGPL. For more information please see
  18. * <http://phing.info>.
  19. */
  20. require_once 'phing/Task.php';
  21. require_once 'phing/BuildException.php';
  22. /**
  23. * Runs phploc a tool for quickly measuring the size of PHP projects.
  24. *
  25. * @package phing.tasks.ext.phploc
  26. * @author Raphael Stolt <raphael.stolt@gmail.com>
  27. */
  28. class PHPLocTask extends Task
  29. {
  30. protected $suffixesToCheck = null;
  31. protected $acceptedReportTypes = null;
  32. protected $reportDirectory = null;
  33. protected $reportType = null;
  34. protected $countTests = null;
  35. protected $fileToCheck = null;
  36. protected $filesToCheck = null;
  37. protected $reportFileName = null;
  38. protected $fileSets = null;
  39. protected $oldVersion = false;
  40. public function init() {
  41. $this->suffixesToCheck = array('php');
  42. $this->acceptedReportTypes = array('cli', 'txt', 'xml', 'csv');
  43. $this->reportType = 'cli';
  44. $this->reportFileName = 'phploc-report';
  45. $this->fileSets = array();
  46. $this->filesToCheck = array();
  47. $this->countTests = false;
  48. }
  49. /**
  50. * @param string $suffixListOrSingleSuffix
  51. */
  52. public function setSuffixes($suffixListOrSingleSuffix) {
  53. if (stripos($suffixListOrSingleSuffix, ',')) {
  54. $suffixes = explode(',', $suffixListOrSingleSuffix);
  55. $this->suffixesToCheck = array_map('trim', $suffixes);
  56. } else {
  57. array_push($this->suffixesToCheck, trim($suffixListOrSingleSuffix));
  58. }
  59. }
  60. /**
  61. * @param PhingFile $file
  62. */
  63. public function setFile(PhingFile $file) {
  64. $this->fileToCheck = trim($file);
  65. }
  66. public function setCountTests($countTests) {
  67. $this->countTests = (bool) $countTests;
  68. }
  69. /**
  70. * @return array
  71. */
  72. public function createFileSet() {
  73. $num = array_push($this->fileSets, new FileSet());
  74. return $this->fileSets[$num - 1];
  75. }
  76. /**
  77. * @param string $type
  78. */
  79. public function setReportType($type) {
  80. $this->reportType = trim($type);
  81. }
  82. /**
  83. * @param string $name
  84. */
  85. public function setReportName($name) {
  86. $this->reportFileName = trim($name);
  87. }
  88. /**
  89. * @param string $directory
  90. */
  91. public function setReportDirectory($directory) {
  92. $this->reportDirectory = trim($directory);
  93. }
  94. public function main() {
  95. /**
  96. * Find PHPLoc
  97. */
  98. if (!class_exists('\SebastianBergmann\PHPLOC\Analyser')) {
  99. if (!@include_once('SebastianBergmann/PHPLOC/autoload.php')) {
  100. if (!@include_once('PHPLOC/Analyser.php')) {
  101. throw new BuildException(
  102. 'PHPLocTask depends on PHPLoc being installed and on include_path.',
  103. $this->getLocation()
  104. );
  105. } else {
  106. $this->oldVersion = true;
  107. }
  108. }
  109. }
  110. $this->_validateProperties();
  111. if (!is_null($this->reportDirectory) && !is_dir($this->reportDirectory)) {
  112. $reportOutputDir = new PhingFile($this->reportDirectory);
  113. $logMessage = "Report output directory doesn't exist, creating: "
  114. . $reportOutputDir->getAbsolutePath() . '.';
  115. $this->log($logMessage);
  116. $reportOutputDir->mkdirs();
  117. }
  118. if ($this->reportType !== 'cli') {
  119. $this->reportFileName.= '.' . trim($this->reportType);
  120. }
  121. if (count($this->fileSets) > 0) {
  122. $project = $this->getProject();
  123. foreach ($this->fileSets as $fileSet) {
  124. $directoryScanner = $fileSet->getDirectoryScanner($project);
  125. $files = $directoryScanner->getIncludedFiles();
  126. $directory = $fileSet->getDir($this->project)->getPath();
  127. foreach ($files as $file) {
  128. if ($this->isFileSuffixSet($file)) {
  129. $this->filesToCheck[] = $directory . DIRECTORY_SEPARATOR
  130. . $file;
  131. }
  132. }
  133. }
  134. $this->filesToCheck = array_unique($this->filesToCheck);
  135. }
  136. $this->runPhpLocCheck();
  137. }
  138. /**
  139. * @throws BuildException
  140. */
  141. private function _validateProperties() {
  142. if (!isset($this->fileToCheck) && count($this->fileSets) === 0) {
  143. $exceptionMessage = "Missing either a nested fileset or the "
  144. . "attribute 'file' set.";
  145. throw new BuildException($exceptionMessage);
  146. }
  147. if (count($this->suffixesToCheck) === 0) {
  148. throw new BuildException("No file suffix defined.");
  149. }
  150. if (is_null($this->reportType)) {
  151. throw new BuildException("No report type defined.");
  152. }
  153. if (!is_null($this->reportType) &&
  154. !in_array($this->reportType, $this->acceptedReportTypes)) {
  155. throw new BuildException("Unaccepted report type defined.");
  156. }
  157. if (!is_null($this->fileToCheck) && !file_exists($this->fileToCheck)) {
  158. throw new BuildException("File to check doesn't exist.");
  159. }
  160. if ($this->reportType !== 'cli' && is_null($this->reportDirectory)) {
  161. throw new BuildException("No report output directory defined.");
  162. }
  163. if (count($this->fileSets) > 0 && !is_null($this->fileToCheck)) {
  164. $exceptionMessage = "Either use a nested fileset or 'file' "
  165. . "attribute; not both.";
  166. throw new BuildException($exceptionMessage);
  167. }
  168. if (!is_bool($this->countTests)) {
  169. $exceptionMessage = "'countTests' attribute has no boolean value";
  170. throw new BuildException($exceptionMessage);
  171. }
  172. if (!is_null($this->fileToCheck)) {
  173. if (!$this->isFileSuffixSet($file)) {
  174. $exceptionMessage = "Suffix of file to check is not defined in"
  175. . " 'suffixes' attribute.";
  176. throw new BuildException($exceptionMessage);
  177. }
  178. }
  179. }
  180. /**
  181. * @param string $filename
  182. * @return boolean
  183. */
  184. protected function isFileSuffixSet($filename) {
  185. $pathinfo = pathinfo($filename);
  186. $fileSuffix = $pathinfo['extension'];
  187. return in_array($fileSuffix, $this->suffixesToCheck);
  188. }
  189. protected function runPhpLocCheck() {
  190. $files = $this->getFilesToCheck();
  191. $result = $this->getCountForFiles($files);
  192. if ($this->reportType === 'cli' || $this->reportType === 'txt') {
  193. if ($this->oldVersion) {
  194. require_once 'PHPLOC/TextUI/ResultPrinter/Text.php';
  195. $reportClass = 'PHPLOC_TextUI_ResultPrinter_Text';
  196. } else {
  197. $reportClass = '\\SebastianBergmann\\PHPLOC\\TextUI\\ResultPrinter';
  198. }
  199. $printer = new $reportClass();
  200. ob_start();
  201. $printer->printResult($result, $this->countTests);
  202. $result = ob_get_contents();
  203. ob_end_clean();
  204. if ($this->reportType === 'txt') {
  205. file_put_contents($this->reportDirectory
  206. . DIRECTORY_SEPARATOR . $this->reportFileName, $result);
  207. $reportDir = new PhingFile($this->reportDirectory);
  208. $logMessage = "Writing report to: "
  209. . $reportDir->getAbsolutePath() . DIRECTORY_SEPARATOR
  210. . $this->reportFileName;
  211. $this->log($logMessage);
  212. } else {
  213. $this->log("\n" . $result);
  214. }
  215. } elseif ($this->reportType === 'xml' || $this->reportType === 'csv') {
  216. if ($this->oldVersion) {
  217. $printerClass = sprintf('PHPLOC_TextUI_ResultPrinter_%s', strtoupper($this->reportType)) ;
  218. $printerClassFile = str_replace('_', DIRECTORY_SEPARATOR, $printerClass) . '.php';
  219. require_once $printerClassFile;
  220. } else {
  221. $printerClass = '\\SebastianBergmann\\PHPLOC\\Log\\' . strtoupper($this->reportType);
  222. }
  223. $printer = new $printerClass();
  224. $reportDir = new PhingFile($this->reportDirectory);
  225. $logMessage = "Writing report to: " . $reportDir->getAbsolutePath()
  226. . DIRECTORY_SEPARATOR . $this->reportFileName;
  227. $this->log($logMessage);
  228. $printer->printResult($this->reportDirectory . DIRECTORY_SEPARATOR
  229. . $this->reportFileName, $result);
  230. }
  231. }
  232. /**
  233. * @return array
  234. */
  235. protected function getFilesToCheck() {
  236. if (count($this->filesToCheck) > 0) {
  237. $files = array();
  238. foreach ($this->filesToCheck as $file) {
  239. $files[] = new SPLFileInfo($file);
  240. }
  241. } elseif (!is_null($this->fileToCheck)) {
  242. $files = array(new SPLFileInfo($this->fileToCheck));
  243. }
  244. return $files;
  245. }
  246. /**
  247. * @param array $files
  248. * @return array
  249. */
  250. protected function getCountForFiles(array $files) {
  251. $analyserClass = ($this->oldVersion ? 'PHPLOC_Analyser' : '\\SebastianBergmann\\PHPLOC\\Analyser');
  252. $analyser = new $analyserClass();
  253. return $analyser->countFiles($files, $this->countTests);
  254. }
  255. }