WhitespaceShell.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 find() {
  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. /**
  68. * whitespaces at the end of the file
  69. */
  70. public function eof() {
  71. if (!empty($this->args[0])) {
  72. $folder = realpath($this->args[0]);
  73. } else {
  74. $folder = APP;
  75. }
  76. $App = new Folder($folder);
  77. $this->out("Checking *.php in ".APP);
  78. $files = $App->findRecursive('.*\.php');
  79. $this->out('Found '. count($files) . ' files.');
  80. $action = $this->in(__('Continue? [y]/[n]'), array('y', 'n'), 'n');
  81. if ($action === 'n') {
  82. $this->error('Aborted');
  83. }
  84. foreach ($files as $file) {
  85. $content = $store = file_get_contents($file);
  86. $newline = PHP_EOL;
  87. $x = substr_count($content, "\r\n");
  88. if ($x > 0) {
  89. $newline = "\r\n";
  90. } else {
  91. $newline = "\n";
  92. }
  93. # add one new line at the end
  94. $content = trim($content) . $newline;
  95. if ($content !== $store) {
  96. file_put_contents($file, $content);
  97. }
  98. }
  99. }
  100. public function getOptionParser() {
  101. $subcommandParser = array(
  102. 'options' => array(
  103. 'ext' => array(
  104. 'short' => 'e',
  105. 'help' => __d('cake_console', 'Specify extensions [php|txt|...]'),
  106. 'default' => '',
  107. ),
  108. 'dry-run'=> array(
  109. 'short' => 'd',
  110. 'help' => __d('cake_console', 'Dry run the clear command, no files will actually be deleted. Should be combined with verbose!'),
  111. 'boolean' => true
  112. ),
  113. 'plugin'=> array(
  114. 'short' => 'p',
  115. 'help' => __d('cake_console', 'Plugin'),
  116. 'default' => '',
  117. ),
  118. )
  119. );
  120. return parent::getOptionParser()
  121. ->description(__d('cake_console', 'The Whitespace Shell removes uncessary/wrong whitespaces.
  122. Either provide a path as first argument, use -p PluginName or run it as it is for the complete APP dir.'))
  123. ->addSubcommand('find', array(
  124. 'help' => __d('cake_console', 'Detect any leading/trailing whitespaces'),
  125. 'parser' => $subcommandParser
  126. ))
  127. ->addSubcommand('eof', array(
  128. 'help' => __d('cake_console', 'Fix whitespaces at the end of PHP files (a single newline as per coding standards)'),
  129. 'parser' => $subcommandParser
  130. ));
  131. }
  132. }