WhitespaceShell.php 4.1 KB

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