WhitespaceShell.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /**
  9. * Whitespaces before or after
  10. *
  11. * @return void
  12. */
  13. public function find() {
  14. if (!empty($this->args[0])) {
  15. $folder = realpath($this->args[0]);
  16. } elseif ($this->params['plugin']) {
  17. $folder = CakePlugin::path(Inflector::classify($this->params['plugin']));
  18. } else {
  19. $folder = APP;
  20. }
  21. $App = new Folder($folder);
  22. $this->out("Checking *.php in " . $folder);
  23. $files = $App->findRecursive('.*\.php');
  24. $this->out('Found '. count($files) . ' files.');
  25. $action = $this->in(__('Continue? [y]/[n]'), array('y', 'n'), 'n');
  26. if ($action === 'n') {
  27. $this->error('Aborted');
  28. }
  29. $folders = array();
  30. foreach ($files as $file) {
  31. $error = '';
  32. $action = '';
  33. $c = file_get_contents($file);
  34. if (preg_match('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', $c)) {
  35. $error = 'leading';
  36. }
  37. if (preg_match('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', $c)) {
  38. $error = 'trailing';
  39. }
  40. if (empty($error)) {
  41. continue;
  42. }
  43. $this->report[$error][0]++;
  44. $this->out('');
  45. $this->out('contains '.$error.' whitespaces: '.$this->shortPath($file));
  46. if (!$this->autoCorrectAll) {
  47. $dirname = dirname($file);
  48. if (in_array($dirname, $folders)) {
  49. $action = 'y';
  50. }
  51. while (empty($action)) {
  52. //TODO: [r]!
  53. $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');
  54. }
  55. } else {
  56. $action = 'y';
  57. }
  58. if ($action === '*') {
  59. $action = 'y';
  60. $this->autoCorrectAll = true;
  61. } elseif ($action === 'a') {
  62. $action = 'y';
  63. $folders[] = $dirname;
  64. $this->out('All: '.$dirname);
  65. }
  66. if ($action === 'q') {
  67. $this->error('Abort... Done');
  68. } elseif ($action === 'y') {
  69. if ($error === 'leading') {
  70. $res = preg_replace('/^[\n\r|\n\r|\n|\r|\s]+\<\?php/', '<?php', $c);
  71. } else { //trailing
  72. $res = preg_replace('/\?\>[\n\r|\n\r|\n|\r|\s]+$/', '?>', $c);
  73. }
  74. file_put_contents($file, $res);
  75. $this->report[$error][1]++;
  76. $this->out('fixed '.$error.' whitespaces: '.$this->shortPath($file));
  77. }
  78. }
  79. # report
  80. $this->out('--------');
  81. $this->out('found '.$this->report['leading'][0].' leading, '.$this->report['trailing'][0].' trailing ws');
  82. $this->out('fixed '.$this->report['leading'][1].' leading, '.$this->report['trailing'][1].' trailing ws');
  83. }
  84. /**
  85. * Whitespaces at the end of the file
  86. */
  87. public function eof() {
  88. if (!empty($this->args[0])) {
  89. $folder = realpath($this->args[0]);
  90. } else {
  91. $folder = APP;
  92. }
  93. $App = new Folder($folder);
  94. $this->out("Checking *.php in " . $folder);
  95. $files = $App->findRecursive('.*\.php');
  96. $this->out('Found '. count($files) . ' files.');
  97. $action = $this->in(__('Continue? [y]/[n]'), array('y', 'n'), 'n');
  98. if ($action === 'n') {
  99. $this->error('Aborted');
  100. }
  101. foreach ($files as $file) {
  102. $content = $store = file_get_contents($file);
  103. $newline = PHP_EOL;
  104. $x = substr_count($content, "\r\n");
  105. if ($x > 0) {
  106. $newline = "\r\n";
  107. } else {
  108. $newline = "\n";
  109. }
  110. # add one new line at the end
  111. $content = trim($content) . $newline;
  112. if ($content !== $store) {
  113. file_put_contents($file, $content);
  114. }
  115. }
  116. $this->out('Done');
  117. }
  118. public function getOptionParser() {
  119. $subcommandParser = array(
  120. 'options' => array(
  121. 'ext' => array(
  122. 'short' => 'e',
  123. 'help' => __d('cake_console', 'Specify extensions [php|txt|...]'),
  124. 'default' => '',
  125. ),
  126. 'dry-run'=> array(
  127. 'short' => 'd',
  128. 'help' => __d('cake_console', 'Dry run the clear command, no files will actually be deleted. Should be combined with verbose!'),
  129. 'boolean' => true
  130. ),
  131. 'plugin'=> array(
  132. 'short' => 'p',
  133. 'help' => __d('cake_console', 'Plugin'),
  134. 'default' => '',
  135. ),
  136. )
  137. );
  138. return parent::getOptionParser()
  139. ->description(__d('cake_console', 'The Whitespace Shell removes uncessary/wrong whitespaces.
  140. Either provide a path as first argument, use -p PluginName or run it as it is for the complete APP dir.'))
  141. ->addSubcommand('find', array(
  142. 'help' => __d('cake_console', 'Detect any leading/trailing whitespaces'),
  143. 'parser' => $subcommandParser
  144. ))
  145. ->addSubcommand('eof', array(
  146. 'help' => __d('cake_console', 'Fix whitespaces at the end of PHP files (a single newline as per coding standards)'),
  147. 'parser' => $subcommandParser
  148. ));
  149. }
  150. }