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