]> git.mxchange.org Git - friendica.git/commitdiff
Add Typo Console
authorHypolite Petovan <mrpetovan@gmail.com>
Sun, 18 Mar 2018 23:45:35 +0000 (19:45 -0400)
committerHypolite Petovan <mrpetovan@gmail.com>
Sun, 18 Mar 2018 23:45:35 +0000 (19:45 -0400)
src/Core/Console.php
src/Core/Console/Typo.php [new file with mode: 0644]

index ebaa33f95065554c6e51f82a5479662b2fe0806e..6f4ec64a0858b8494fb28793ce64b25460f56321 100644 (file)
@@ -112,6 +112,8 @@ HELP;
                                break;\r
                        case 'po2php': $subconsole = new Console\PoToPhp($subargs);\r
                                break;\r
+                       case 'typo': $subconsole = new Console\Typo($subargs);\r
+                               break;\r
                        default:\r
                                throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist');\r
                }\r
diff --git a/src/Core/Console/Typo.php b/src/Core/Console/Typo.php
new file mode 100644 (file)
index 0000000..4213fc8
--- /dev/null
@@ -0,0 +1,119 @@
+<?php\r
+\r
+namespace Friendica\Core\Console;\r
+\r
+/**\r
+ * Tired of chasing typos and finding them after a commit.\r
+ * Run this and quickly see if we've got any parse errors in our application files.\r
+ *\r
+ * @author Hypolite Petovan <mrpetovan@gmail.com>\r
+ */\r
+class Typo extends \Asika\SimpleConsole\Console\r
+{\r
+       protected $helpOptions = ['h', 'help', '?'];\r
+\r
+       protected function getHelp()\r
+       {\r
+               $help = <<<HELP\r
+console typo - Checks for parse errors in Friendica files\r
+Usage\r
+       bin/console typo [-h|--help|-?] [-v]\r
+\r
+Description\r
+       Checks all PHP files in the Friendica file tree for parse errors\r
+\r
+Options\r
+       -h|--help|-?  Show help information\r
+       -v            Show more debug information.\r
+HELP;\r
+               return $help;\r
+       }\r
+\r
+       protected function doExecute()\r
+       {\r
+               if ($this->getOption('v')) {\r
+                       $this->out('Class: ' . __CLASS__);\r
+                       $this->out('Arguments: ' . var_export($this->args, true));\r
+                       $this->out('Options: ' . var_export($this->options, true));\r
+               }\r
+\r
+               if (count($this->args) > 0) {\r
+                       throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');\r
+               }\r
+\r
+               $a = get_app();\r
+\r
+               $php_path = $a->getConfigValue('config', 'php_path', 'php');\r
+\r
+               if ($this->getOption('v')) {\r
+                       $this->out('Directory: src');\r
+               }\r
+\r
+               $Iterator = new \RecursiveDirectoryIterator('src');\r
+\r
+               foreach (new \RecursiveIteratorIterator($Iterator) as $file) {\r
+                       if (substr($file, -4) === '.php') {\r
+                               $this->checkFile($php_path, $file);\r
+                       }\r
+               }\r
+\r
+               if ($this->getOption('v')) {\r
+                       $this->out('Directory: mod');\r
+               }\r
+\r
+               $files = glob('mod/*.php');\r
+               $this->checkFiles($php_path, $files);\r
+\r
+               if ($this->getOption('v')) {\r
+                       $this->out('Directory: include');\r
+               }\r
+\r
+               $files = glob('include/*.php');\r
+               $this->checkFiles($php_path, $files);\r
+\r
+               if ($this->getOption('v')) {\r
+                       $this->out('Directory: addon');\r
+               }\r
+\r
+               $dirs = glob('addon/*');\r
+               foreach ($dirs as $dir) {\r
+                       $addon = basename($dir);\r
+                       $files = glob($dir . '/' . $addon . '.php');\r
+                       $this->checkFiles($php_path, $files);\r
+               }\r
+\r
+               if ($this->getOption('v')) {\r
+                       $this->out('String files');\r
+               }\r
+\r
+               $this->checkFile($php_path, 'util/strings.php');\r
+\r
+               $files = glob('view/lang/*/strings.php');\r
+               $this->checkFiles($php_path, $files);\r
+\r
+               $this->out('No errors.');\r
+\r
+               return 0;\r
+       }\r
+\r
+       private function checkFiles($php_path, array $files)\r
+       {\r
+               foreach ($files as $file) {\r
+                       $this->checkFile($php_path, $file);\r
+               }\r
+       }\r
+\r
+       private function checkFile($php_path, $file)\r
+       {\r
+               if ($this->getOption('v')) {\r
+                       $this->out('Checking ' . $file);\r
+               }\r
+\r
+               $output = [];\r
+               $ret = 0;\r
+               exec("$php_path -l $file", $output, $ret);\r
+               if ($ret !== 0) {\r
+                       throw new \RuntimeException('Parse error found in ' . $file . ', scan stopped.');\r
+               }\r
+       }\r
+}\r