PageRenderTime 265ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/PHP/CompatInfo/Report/Constant.php

http://github.com/llaville/php-compat-info
PHP | 213 lines | 139 code | 17 blank | 57 comment | 19 complexity | 83a689997da5427d483e8bc26c5e6e2f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Constant 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. * Constant 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_Constant extends PHP_CompatInfo_Report
  25. {
  26. /**
  27. * Prints a constant report
  28. *
  29. * @param array $report Report data to produce
  30. * @param string $base Base directory of data source
  31. * @param int $verbose Verbose level (0: none, 1: warnings, ...)
  32. *
  33. * @return void
  34. */
  35. public function generate($report, $base, $verbose)
  36. {
  37. if ($verbose < 3) {
  38. // summary report
  39. $files = array_keys($report);
  40. $constants = array();
  41. $conditions = array();
  42. foreach ($files as $filename) {
  43. foreach ($report[$filename]['constants'] as $extension => $data) {
  44. foreach ($data as $key => $values) {
  45. if (!isset($constants[$extension][$key])) {
  46. $constants[$extension][$key] = $values;
  47. } else {
  48. $constants[$extension][$key]['uses']
  49. += $values['uses'];
  50. $constants[$extension][$key]['sources'] = array_merge(
  51. $constants[$extension][$key]['sources'],
  52. $values['sources']
  53. );
  54. }
  55. }
  56. }
  57. foreach ($report[$filename]['conditions'] as $key => $values) {
  58. if (!isset($conditions[$key])) {
  59. $conditions[$key] = $values;
  60. }
  61. }
  62. }
  63. $this->total = array();
  64. $this->totalExcludes = 0;
  65. $this->globalVersions = array('4.0.0', '');
  66. $this->printTHead($base, false);
  67. $this->printTBody($constants, ($verbose == 2), $base);
  68. $this->printTFoot($conditions);
  69. } else {
  70. // group by files report
  71. foreach ($report as $filename => $elements) {
  72. $this->total = array();
  73. $this->totalExcludes = 0;
  74. $this->globalVersions = array('4.0.0', '');
  75. $this->printTHead($base, $filename);
  76. $this->printTBody($elements['constants'], false, $base);
  77. $this->printTFoot($elements['conditions']);
  78. }
  79. }
  80. echo PHP_EOL;
  81. }
  82. /**
  83. * Prints header of report
  84. *
  85. * @param string $base Base directory of data source
  86. * @param string $filename Current file
  87. *
  88. * @return void
  89. */
  90. private function printTHead($base, $filename)
  91. {
  92. echo PHP_EOL;
  93. echo 'BASE: ' . $base . PHP_EOL;
  94. echo str_replace($base, 'FILE: ', $filename) . PHP_EOL;
  95. echo str_repeat('-', $this->width) . PHP_EOL;
  96. echo 'PHP COMPAT INFO CONSTANT SUMMARY' . PHP_EOL;
  97. echo str_repeat('-', $this->width) . PHP_EOL;
  98. echo ' CONSTANT' . str_repeat(' ', ($this->width - 49))
  99. . 'EXTENSION' . str_repeat(' ', ($this->width - 70))
  100. . 'VERSION' . str_repeat(' ', ($this->width - 70))
  101. . 'COUNT' . PHP_EOL;
  102. echo str_repeat('-', $this->width) . PHP_EOL;
  103. }
  104. /**
  105. * Prints footer of report
  106. *
  107. * @param array $conditions List of conditions found on current file
  108. *
  109. * @return void
  110. */
  111. private function printTFoot($conditions)
  112. {
  113. echo str_repeat('-', $this->width) . PHP_EOL;
  114. echo 'A TOTAL OF ' . count($this->total) . ' CONSTANT(S) WERE FOUND';
  115. if ($this->totalExcludes > 0) {
  116. echo ' AND ' . $this->totalExcludes . ' EXCLUDED FROM PARSING';
  117. }
  118. echo PHP_EOL;
  119. $ccn = $this->getCCN($conditions);
  120. if ($ccn > 0) {
  121. echo 'WITH CONDITIONAL CODE LEVEL ' . $ccn . PHP_EOL;
  122. }
  123. if (count($this->total) > 0) {
  124. echo 'REQUIRED PHP ' . $this->globalVersions[0] . ' (MIN) ';
  125. if (!empty($this->globalVersions[1])) {
  126. echo $this->globalVersions[1] . ' (MAX)';
  127. }
  128. echo PHP_EOL;
  129. }
  130. echo str_repeat('-', $this->width) . PHP_EOL;
  131. echo PHP_Timer::resourceUsage() . PHP_EOL;
  132. echo str_repeat('-', $this->width) . PHP_EOL;
  133. }
  134. /**
  135. * Prints body of report
  136. *
  137. * @param array $elements List of constant to print
  138. * @param string $filename Current file
  139. * @param string $base Base directory of data source
  140. *
  141. * @return void
  142. */
  143. private function printTBody($elements, $filename, $base)
  144. {
  145. ksort($elements);
  146. foreach ($elements as $category => $items) {
  147. $constants = array_keys($items);
  148. $this->total = array_merge($this->total, $constants);
  149. asort($constants);
  150. foreach ($constants as $constant) {
  151. if ($items[$constant]['excluded']) {
  152. echo 'E';
  153. $this->totalExcludes++;
  154. } else {
  155. echo ' ';
  156. }
  157. if ('user' == $category) {
  158. $extension = '';
  159. } else {
  160. $extension = $category;
  161. }
  162. $versions = implode(' ', $items[$constant]['versions']);
  163. echo ' ';
  164. if (strlen($constant) < 38) {
  165. echo $constant
  166. . str_repeat(' ', (38 - strlen($constant)));
  167. } else {
  168. echo $constant . PHP_EOL;
  169. echo str_repeat(' ', 40);
  170. }
  171. echo $extension
  172. . str_repeat(' ', (18 - strlen($extension)));
  173. echo $versions
  174. . str_repeat(' ', (16 - strlen($versions)));
  175. echo str_repeat(
  176. ' ', (5 - strlen((string) $items[$constant]['uses']))
  177. );
  178. echo $items[$constant]['uses'] . PHP_EOL;
  179. if ($filename) {
  180. foreach ($items[$constant]['sources'] as $source) {
  181. echo ' ' . str_replace($base, '', $source) . PHP_EOL;
  182. }
  183. }
  184. if ($items[$constant]['excluded']) {
  185. continue;
  186. }
  187. $this->updateVersion(
  188. $items[$constant]['versions'][0], $this->globalVersions[0]
  189. );
  190. $this->updateVersion(
  191. $items[$constant]['versions'][1], $this->globalVersions[1]
  192. );
  193. }
  194. }
  195. }
  196. }