PageRenderTime 41ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/PHP/CompatInfo/Report/Source.php

http://github.com/llaville/php-compat-info
PHP | 137 lines | 83 code | 16 blank | 38 comment | 13 complexity | cf46985e583a9239e4111fac64eb41b4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Source tokens report
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CompatInfo
  9. * @author Laurent Laville <pear@laurent-laville.org>
  10. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  11. * @version SVN: $Id$
  12. * @link http://php5.laurent-laville.org/compatinfo/
  13. */
  14. /**
  15. * Source tokens report
  16. *
  17. * @category PHP
  18. * @package PHP_CompatInfo
  19. * @author Laurent Laville <pear@laurent-laville.org>
  20. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  21. * @version Release: @package_version@
  22. * @link http://php5.laurent-laville.org/compatinfo/
  23. */
  24. class PHP_CompatInfo_Report_Source extends PHP_CompatInfo_Report
  25. {
  26. /**
  27. * Class constructor of source tokens report
  28. *
  29. * @param string $source Data source
  30. * @param array $options Options for parser
  31. * @param array $warnings List of warning messages already produced
  32. */
  33. public function __construct($source, $options, $warnings)
  34. {
  35. if (isset($options['exclude']['files'])) {
  36. $excludes = $options['exclude']['files'];
  37. } else {
  38. $excludes = array();
  39. }
  40. if (isset($options['fileExtensions'])) {
  41. $excludes[-1] = '\.(' . implode('|', $options['fileExtensions']) . ')$';
  42. }
  43. $files = PHP_CompatInfo::getFilelist(
  44. $source, $options['recursive'], $excludes
  45. );
  46. $report = array_fill_keys($files, $options['verbose']);
  47. $base = realpath(dirname($source));
  48. if (isset($options['reportFile'])) {
  49. ob_start();
  50. }
  51. $this->generate($report, $base, $options['verbose']);
  52. if (count($warnings) > 0 && $options['verbose'] > 0) {
  53. echo 'Warning messages : (' . count($warnings) . ')' . PHP_EOL;
  54. echo PHP_EOL;
  55. foreach ($warnings as $warning) {
  56. echo ' ' . $warning . PHP_EOL;
  57. }
  58. }
  59. if (isset($options['reportFile'])) {
  60. $generatedReport = ob_get_contents();
  61. ob_end_flush();
  62. file_put_contents(
  63. $options['reportFile'], $generatedReport,
  64. $options['reportFileFlags']
  65. );
  66. }
  67. }
  68. /**
  69. * Prints a source tokens report
  70. *
  71. * @param array $report Report data to produce
  72. * @param string $base Base directory of data source
  73. * @param int $verbose Verbose level (0: none, 1: warnings, ...)
  74. *
  75. * @return void
  76. */
  77. public function generate($report, $base, $verbose)
  78. {
  79. foreach ($report as $filename => $verbose) {
  80. $reflect = new PHP_Reflect();
  81. $tokens = $reflect->scan($filename);
  82. echo PHP_EOL;
  83. echo 'BASE: ' . $base . PHP_EOL;
  84. echo str_replace($base, 'FILE: ', $filename) . PHP_EOL;
  85. echo str_repeat('-', $this->width) . PHP_EOL;
  86. echo 'PHP COMPAT INFO SOURCE SUMMARY' . PHP_EOL;
  87. if ($verbose == 1) {
  88. echo str_repeat('-', $this->width) . PHP_EOL;
  89. echo 'LINE TOKEN TEXT' . PHP_EOL;
  90. echo str_repeat('-', $this->width).PHP_EOL;
  91. foreach ($tokens as $token) {
  92. if ($token[0] == 'T_WHITESPACE') {
  93. $text = '';
  94. } else {
  95. $text = str_replace(array("\r", "\n"), '', $token[1]);
  96. if (strlen($text) > 40) {
  97. $text = explode("\n", wordwrap($text, 40));
  98. $text = $text[0];
  99. }
  100. }
  101. printf(
  102. "%5d %-30s %s\n",
  103. $token[2],
  104. $token[0],
  105. $text
  106. );
  107. }
  108. }
  109. $total = $reflect->getLinesOfCode();
  110. echo str_repeat('-', $this->width) . PHP_EOL;
  111. echo 'A TOTAL OF ' . $total['loc'] . ' LINE(S)';
  112. echo ' WITH ' . $total['cloc'] . ' COMMENT LINE(S)';
  113. echo ' AND ' . $total['ncloc'] . ' CODE LINE(S)'
  114. . PHP_EOL;
  115. echo str_repeat('-', $this->width) . PHP_EOL;
  116. echo PHP_Timer::resourceUsage() . PHP_EOL;
  117. echo str_repeat('-', $this->width) . PHP_EOL;
  118. }
  119. echo PHP_EOL;
  120. }
  121. }