Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Typo.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Friendica\Core\Console;
  3. use Friendica\BaseObject;
  4. /**
  5. * Tired of chasing typos and finding them after a commit.
  6. * Run this and quickly see if we've got any parse errors in our application files.
  7. *
  8. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  9. */
  10. class Typo extends \Asika\SimpleConsole\Console
  11. {
  12. protected $helpOptions = ['h', 'help', '?'];
  13. protected function getHelp()
  14. {
  15. $help = <<<HELP
  16. console typo - Checks for parse errors in Friendica files
  17. Usage
  18. bin/console typo [-h|--help|-?] [-v]
  19. Description
  20. Checks all PHP files in the Friendica file tree for parse errors
  21. Options
  22. -h|--help|-? Show help information
  23. -v Show more debug information.
  24. HELP;
  25. return $help;
  26. }
  27. protected function doExecute()
  28. {
  29. if ($this->getOption('v')) {
  30. $this->out('Class: ' . __CLASS__);
  31. $this->out('Arguments: ' . var_export($this->args, true));
  32. $this->out('Options: ' . var_export($this->options, true));
  33. }
  34. if (count($this->args) > 0) {
  35. throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
  36. }
  37. $php_path = BaseObject::getApp()->getConfigCache()->get('config', 'php_path', 'php');
  38. if ($this->getOption('v')) {
  39. $this->out('Directory: src');
  40. }
  41. $Iterator = new \RecursiveDirectoryIterator('src');
  42. foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
  43. if (substr($file, -4) === '.php') {
  44. $this->checkFile($php_path, $file);
  45. }
  46. }
  47. if ($this->getOption('v')) {
  48. $this->out('Directory: mod');
  49. }
  50. $files = glob('mod/*.php');
  51. $this->checkFiles($php_path, $files);
  52. if ($this->getOption('v')) {
  53. $this->out('Directory: include');
  54. }
  55. $files = glob('include/*.php');
  56. $this->checkFiles($php_path, $files);
  57. if ($this->getOption('v')) {
  58. $this->out('Directory: addon');
  59. }
  60. $dirs = glob('addon/*');
  61. foreach ($dirs as $dir) {
  62. $addon = basename($dir);
  63. $files = glob($dir . '/' . $addon . '.php');
  64. $this->checkFiles($php_path, $files);
  65. }
  66. if ($this->getOption('v')) {
  67. $this->out('String files');
  68. }
  69. $files = glob('view/lang/*/strings.php');
  70. $this->checkFiles($php_path, $files);
  71. $this->out('No errors.');
  72. return 0;
  73. }
  74. private function checkFiles($php_path, array $files)
  75. {
  76. foreach ($files as $file) {
  77. $this->checkFile($php_path, $file);
  78. }
  79. }
  80. private function checkFile($php_path, $file)
  81. {
  82. if ($this->getOption('v')) {
  83. $this->out('Checking ' . $file);
  84. }
  85. $output = [];
  86. $ret = 0;
  87. exec("$php_path -l $file", $output, $ret);
  88. if ($ret !== 0) {
  89. throw new \RuntimeException('Parse error found in ' . $file . ', scan stopped.');
  90. }
  91. }
  92. }