WhitespaceShell.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. App::uses('AppShell', 'Console/Command');
  3. App::uses('Folder', 'Utility');
  4. class WhitespaceShell extends AppShell {
  5. public $autoCorrectAll = false;
  6. # each report: [0] => found, [1] => corrected
  7. public $report = array('leading'=>array(0, 0), 'trailing'=>array(0, 0));
  8. public function main() {
  9. $App = new Folder(APP);
  10. $files = $App->findRecursive('.*\.php');
  11. $this->out("Checking *.php in ".APP);
  12. $folders = array();
  13. foreach ($files as $file) {
  14. $error = '';
  15. $action = '';
  16. $c = file_get_contents($file);
  17. if (preg_match('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', $c)) {
  18. $error = 'leading';
  19. }
  20. if (preg_match('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', $c)) {
  21. $error = 'trailing';
  22. }
  23. if (empty($error)) {
  24. continue;
  25. }
  26. $this->report[$error][0]++;
  27. $this->out('');
  28. $this->out('contains '.$error.' whitespaces: '.$this->shortPath($file));
  29. if (!$this->autoCorrectAll) {
  30. $dirname = dirname($file);
  31. if (in_array($dirname, $folders)) {
  32. $action = 'y';
  33. }
  34. while (empty($action)) {
  35. //TODO: [r]!
  36. $action = $this->in(__('Remove? [y]/[n], [a] for all in this folder, [r] for all below, [*] for all files(!), [q] to quit'), array('y','n','r','a','q','*'), 'q');
  37. }
  38. } else {
  39. $action = 'y';
  40. }
  41. if ($action == '*') {
  42. $action = 'y';
  43. $this->autoCorrectAll = true;
  44. } elseif ($action == 'a') {
  45. $action = 'y';
  46. $folders[] = $dirname;
  47. $this->out('All: '.$dirname);
  48. }
  49. if ($action == 'q') {
  50. die('Abort... Done');
  51. } elseif ($action == 'y') {
  52. if ($error == 'leading') {
  53. $res = preg_replace('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', '<?php', $c);
  54. } else { //trailing
  55. $res = preg_replace('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', '?>', $c);
  56. }
  57. file_put_contents($file, $res);
  58. $this->report[$error][1]++;
  59. $this->out('fixed '.$error.' whitespaces: '.$this->shortPath($file));
  60. }
  61. }
  62. # report
  63. $this->out('--------');
  64. $this->out('found '.$this->report['leading'][0].' leading, '.$this->report['trailing'][0].' trailing ws');
  65. $this->out('fixed '.$this->report['leading'][1].' leading, '.$this->report['trailing'][1].' trailing ws');
  66. }
  67. public function whitespaces() {
  68. if (!empty($this->args[0])) {
  69. $folder = realpath($this->args[0]);
  70. } else {
  71. $folder = APP;
  72. }
  73. $App = new Folder($folder);
  74. $this->out("Checking *.php in ".APP);
  75. $files = $App->findRecursive('.*\.php');
  76. $this->out('Found '. count($files) . ' files.');
  77. $action = $this->in(__('Continue? [y]/[n]'), array('y', 'n'), 'n');
  78. if ($action === 'n') {
  79. $this->error('Aborted');
  80. }
  81. foreach ($files as $file) {
  82. $content = $store = file_get_contents($file);
  83. $newline = PHP_EOL;
  84. $x = substr_count($content, "\r\n");
  85. if ($x > 0) {
  86. $newline = "\r\n";
  87. } else {
  88. $newline = "\n";
  89. }
  90. # add one new line at the end
  91. $content = trim($content) . $newline;
  92. if ($content !== $store) {
  93. file_put_contents($file, $content);
  94. }
  95. }
  96. }
  97. }