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