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