PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Isaki/le331.fr
PHP | 422 lines | 255 code | 61 blank | 106 comment | 41 complexity | f106259c09828d09bfde1ad30faedd0a 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. /**
  31. * @var array
  32. */
  33. protected $suffixesToCheck = array('php');
  34. /**
  35. * @var array
  36. */
  37. protected $acceptedReportTypes = array('cli', 'txt', 'xml', 'csv');
  38. /**
  39. * @var null
  40. */
  41. protected $reportDirectory = null;
  42. /**
  43. * @var string
  44. */
  45. protected $reportType = 'cli';
  46. /**
  47. * @var string
  48. */
  49. protected $reportFileName = 'phploc-report';
  50. /**
  51. * @var bool
  52. */
  53. protected $countTests = false;
  54. /**
  55. * @var null|PhingFile
  56. */
  57. protected $fileToCheck = null;
  58. /**
  59. * @var array
  60. */
  61. protected $filesToCheck = array();
  62. /**
  63. * @var FileSet[]
  64. */
  65. protected $fileSets = array();
  66. /**
  67. * @var bool
  68. */
  69. protected $oldVersion = false;
  70. /**
  71. * Flag for identifying if phploc library is between version 1.7.0 and 1.7.4
  72. *
  73. * @var bool
  74. */
  75. private $isOneSevenVersion = false;
  76. /**
  77. * @var string
  78. */
  79. private $pharLocation = "";
  80. /**
  81. * @param string $suffixListOrSingleSuffix
  82. */
  83. public function setSuffixes($suffixListOrSingleSuffix)
  84. {
  85. if (stripos($suffixListOrSingleSuffix, ',')) {
  86. $suffixes = explode(',', $suffixListOrSingleSuffix);
  87. $this->suffixesToCheck = array_map('trim', $suffixes);
  88. } else {
  89. $this->suffixesToCheck[] = trim($suffixListOrSingleSuffix);
  90. }
  91. }
  92. /**
  93. * @param PhingFile $file
  94. */
  95. public function setFile(PhingFile $file)
  96. {
  97. $this->fileToCheck = trim($file);
  98. }
  99. /**
  100. * @param boolean $countTests
  101. */
  102. public function setCountTests($countTests)
  103. {
  104. $this->countTests = StringHelper::booleanValue($countTests);
  105. }
  106. /**
  107. * Nested adder, adds a set of files (nested fileset attribute).
  108. *
  109. * @param FileSet $fs
  110. * @return void
  111. */
  112. public function addFileSet(FileSet $fs)
  113. {
  114. $this->fileSets[] = $fs;
  115. }
  116. /**
  117. * @param string $type
  118. */
  119. public function setReportType($type)
  120. {
  121. $this->reportType = trim($type);
  122. }
  123. /**
  124. * @param string $name
  125. */
  126. public function setReportName($name)
  127. {
  128. $this->reportFileName = trim($name);
  129. }
  130. /**
  131. * @param string $directory
  132. */
  133. public function setReportDirectory($directory)
  134. {
  135. $this->reportDirectory = trim($directory);
  136. }
  137. /**
  138. * @param string $pharLocation
  139. */
  140. public function setPharLocation($pharLocation)
  141. {
  142. $this->pharLocation = $pharLocation;
  143. }
  144. /**
  145. * @throws BuildException
  146. */
  147. protected function loadDependencies()
  148. {
  149. if (!empty($this->pharLocation)) {
  150. // hack to prevent PHPLOC from starting in CLI mode and halting Phing
  151. eval("namespace SebastianBergmann\PHPLOC\CLI;
  152. class Application
  153. {
  154. public function run() {}
  155. }");
  156. ob_start();
  157. include $this->pharLocation;
  158. ob_end_clean();
  159. }
  160. if (!class_exists('\SebastianBergmann\PHPLOC\Analyser')) {
  161. if (!@include_once 'SebastianBergmann/PHPLOC/autoload.php') {
  162. if (!@include_once 'PHPLOC/Analyser.php') {
  163. throw new BuildException(
  164. 'PHPLocTask depends on PHPLoc being installed and on include_path.',
  165. $this->getLocation()
  166. );
  167. } else {
  168. $this->oldVersion = true;
  169. }
  170. }
  171. }
  172. $versionClass = '\\SebastianBergmann\\PHPLOC\\Version';
  173. if (class_exists($versionClass)
  174. && version_compare(constant($versionClass . '::VERSION'), '1.7.0') >= 0
  175. && version_compare(constant($versionClass . '::VERSION'), '2.0.0beta1') == -1
  176. ) {
  177. $this->isOneSevenVersion = true;
  178. }
  179. }
  180. public function main()
  181. {
  182. $this->loadDependencies();
  183. $this->validateProperties();
  184. if ($this->reportDirectory !== null && !is_dir($this->reportDirectory)) {
  185. $reportOutputDir = new PhingFile($this->reportDirectory);
  186. $logMessage = "Report output directory doesn't exist, creating: "
  187. . $reportOutputDir->getAbsolutePath() . '.';
  188. $this->log($logMessage);
  189. $reportOutputDir->mkdirs();
  190. }
  191. if ($this->reportType !== 'cli') {
  192. $this->reportFileName .= '.' . $this->reportType;
  193. }
  194. if (count($this->fileSets) > 0) {
  195. foreach ($this->fileSets as $fileSet) {
  196. $directoryScanner = $fileSet->getDirectoryScanner($this->project);
  197. $files = $directoryScanner->getIncludedFiles();
  198. $directory = $fileSet->getDir($this->project)->getPath();
  199. foreach ($files as $file) {
  200. if ($this->isFileSuffixSet($file)) {
  201. $this->filesToCheck[] = $directory . DIRECTORY_SEPARATOR . $file;
  202. }
  203. }
  204. }
  205. $this->filesToCheck = array_unique($this->filesToCheck);
  206. }
  207. $this->runPhpLocCheck();
  208. }
  209. /**
  210. * @throws BuildException
  211. */
  212. private function validateProperties()
  213. {
  214. if ($this->fileToCheck === null && count($this->fileSets) === 0) {
  215. throw new BuildException('Missing either a nested fileset or the attribute "file" set.');
  216. }
  217. if ($this->fileToCheck !== null) {
  218. if (!file_exists($this->fileToCheck)) {
  219. throw new BuildException("File to check doesn't exist.");
  220. }
  221. if (!$this->isFileSuffixSet($this->fileToCheck)) {
  222. throw new BuildException('Suffix of file to check is not defined in "suffixes" attribute.');
  223. }
  224. if (count($this->fileSets) > 0) {
  225. throw new BuildException('Either use a nested fileset or "file" attribute; not both.');
  226. }
  227. }
  228. if (count($this->suffixesToCheck) === 0) {
  229. throw new BuildException('No file suffix defined.');
  230. }
  231. if ($this->reportType === null) {
  232. throw new BuildException('No report type defined.');
  233. }
  234. if ($this->reportType !== null && !in_array($this->reportType, $this->acceptedReportTypes)) {
  235. throw new BuildException('Unaccepted report type defined.');
  236. }
  237. if ($this->reportType !== 'cli' && $this->reportDirectory === null) {
  238. throw new BuildException('No report output directory defined.');
  239. }
  240. }
  241. /**
  242. * @param string $filename
  243. *
  244. * @return boolean
  245. */
  246. protected function isFileSuffixSet($filename)
  247. {
  248. return in_array(pathinfo($filename, PATHINFO_EXTENSION), $this->suffixesToCheck);
  249. }
  250. protected function runPhpLocCheck()
  251. {
  252. $files = $this->getFilesToCheck();
  253. $count = $this->getCountForFiles($files);
  254. if ($this->reportType != 'cli') {
  255. $logMessage = 'Writing report to: '
  256. . $this->reportDirectory . DIRECTORY_SEPARATOR . $this->reportFileName;
  257. $this->log($logMessage);
  258. }
  259. switch ($this->reportType) {
  260. case 'cli':
  261. if ($this->oldVersion || $this->isOneSevenVersion) {
  262. if ($this->oldVersion) {
  263. require_once 'PHPLOC/TextUI/ResultPrinter/Text.php';
  264. $printerClass = 'PHPLOC_TextUI_ResultPrinter_Text';
  265. } else {
  266. $printerClass = '\\SebastianBergmann\\PHPLOC\\TextUI\\ResultPrinter';
  267. }
  268. $printer = new $printerClass();
  269. $printer->printResult($count, $this->countTests);
  270. } else {
  271. $outputClass = '\\Symfony\\Component\\Console\\Output\\ConsoleOutput';
  272. $printerClass = '\\SebastianBergmann\\PHPLOC\\Log\\Text';
  273. $output = new $outputClass();
  274. $printer = new $printerClass();
  275. $printer->printResult($output, $count, $this->countTests);
  276. }
  277. break;
  278. case 'txt':
  279. if ($this->oldVersion || $this->isOneSevenVersion) {
  280. if ($this->oldVersion) {
  281. require_once 'PHPLOC/TextUI/ResultPrinter/Text.php';
  282. $printerClass = 'PHPLOC_TextUI_ResultPrinter_Text';
  283. } else {
  284. $printerClass = '\\SebastianBergmann\\PHPLOC\\TextUI\\ResultPrinter';
  285. }
  286. $printer = new $printerClass();
  287. ob_start();
  288. $printer->printResult($count, $this->countTests);
  289. $result = ob_get_contents();
  290. ob_end_clean();
  291. file_put_contents($this->reportDirectory . DIRECTORY_SEPARATOR . $this->reportFileName, $result);
  292. } else {
  293. $outputClass = '\\Symfony\\Component\\Console\\Output\\StreamOutput';
  294. $printerClass = '\\SebastianBergmann\\PHPLOC\\Log\\Text';
  295. $stream = fopen($this->reportDirectory . DIRECTORY_SEPARATOR . $this->reportFileName, 'a+');
  296. $output = new $outputClass($stream);
  297. $printer = new $printerClass();
  298. $printer->printResult($output, $count, $this->countTests);
  299. }
  300. break;
  301. case 'xml':
  302. if ($this->oldVersion) {
  303. require_once 'PHPLOC/TextUI/ResultPrinter/XML.php';
  304. $printerClass = 'PHPLOC_TextUI_ResultPrinter_XML';
  305. } else {
  306. $printerClass = '\\SebastianBergmann\\PHPLOC\\Log\\XML';
  307. }
  308. $printer = new $printerClass();
  309. $printer->printResult($this->reportDirectory . DIRECTORY_SEPARATOR . $this->reportFileName, $count);
  310. break;
  311. case 'csv':
  312. if ($this->oldVersion) {
  313. require_once 'PHPLOC/TextUI/ResultPrinter/CSV.php';
  314. $printerClass = 'PHPLOC_TextUI_ResultPrinter_CSV';
  315. } else {
  316. if ($this->isOneSevenVersion) {
  317. $printerClass = '\\SebastianBergmann\\PHPLOC\\Log\\CSV';
  318. } else {
  319. $printerClass = '\\SebastianBergmann\\PHPLOC\\Log\\CSV\\Single';
  320. }
  321. }
  322. $printer = new $printerClass();
  323. $printer->printResult($this->reportDirectory . DIRECTORY_SEPARATOR . $this->reportFileName, $count);
  324. break;
  325. }
  326. }
  327. /**
  328. * @return SplFileInfo[]
  329. */
  330. protected function getFilesToCheck()
  331. {
  332. $files = array();
  333. if (count($this->filesToCheck) > 0) {
  334. foreach ($this->filesToCheck as $file) {
  335. $files[] = new SplFileInfo($file);
  336. }
  337. } elseif ($this->fileToCheck !== null) {
  338. $files = array(new SplFileInfo($this->fileToCheck));
  339. }
  340. return $files;
  341. }
  342. /**
  343. * @param SplFileInfo[] $files
  344. *
  345. * @return array
  346. */
  347. protected function getCountForFiles(array $files)
  348. {
  349. $analyserClass = ($this->oldVersion ? 'PHPLOC_Analyser' : '\\SebastianBergmann\\PHPLOC\\Analyser');
  350. $analyser = new $analyserClass();
  351. return $analyser->countFiles($files, $this->countTests);
  352. }
  353. }