]> git.mxchange.org Git - friendica.git/blob - src/Core/Console/Typo.php
1) Refactor App->config[] into Core\Config
[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                 $php_path = \Friendica\Core\Config::getConfigValue('config', 'php_path', 'php');
45
46                 if ($this->getOption('v')) {
47                         $this->out('Directory: src');
48                 }
49
50                 $Iterator = new \RecursiveDirectoryIterator('src');
51
52                 foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
53                         if (substr($file, -4) === '.php') {
54                                 $this->checkFile($php_path, $file);
55                         }
56                 }
57
58                 if ($this->getOption('v')) {
59                         $this->out('Directory: mod');
60                 }
61
62                 $files = glob('mod/*.php');
63                 $this->checkFiles($php_path, $files);
64
65                 if ($this->getOption('v')) {
66                         $this->out('Directory: include');
67                 }
68
69                 $files = glob('include/*.php');
70                 $this->checkFiles($php_path, $files);
71
72                 if ($this->getOption('v')) {
73                         $this->out('Directory: addon');
74                 }
75
76                 $dirs = glob('addon/*');
77                 foreach ($dirs as $dir) {
78                         $addon = basename($dir);
79                         $files = glob($dir . '/' . $addon . '.php');
80                         $this->checkFiles($php_path, $files);
81                 }
82
83                 if ($this->getOption('v')) {
84                         $this->out('String files');
85                 }
86
87                 $files = glob('view/lang/*/strings.php');
88                 $this->checkFiles($php_path, $files);
89
90                 $this->out('No errors.');
91
92                 return 0;
93         }
94
95         private function checkFiles($php_path, array $files)
96         {
97                 foreach ($files as $file) {
98                         $this->checkFile($php_path, $file);
99                 }
100         }
101
102         private function checkFile($php_path, $file)
103         {
104                 if ($this->getOption('v')) {
105                         $this->out('Checking ' . $file);
106                 }
107
108                 $output = [];
109                 $ret = 0;
110                 exec("$php_path -l $file", $output, $ret);
111                 if ($ret !== 0) {
112                         throw new \RuntimeException('Parse error found in ' . $file . ', scan stopped.');
113                 }
114         }
115 }