]> git.mxchange.org Git - friendica.git/blob - src/Console/Typo.php
Merge pull request #7540 from vinzv/patch-1
[friendica.git] / src / Console / Typo.php
1 <?php
2
3 namespace Friendica\Console;
4
5 use Friendica\Core\Config\Configuration;
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         /**
18          * @var Configuration
19          */
20         private $config;
21
22         protected function getHelp()
23         {
24                 $help = <<<HELP
25 console typo - Checks for parse errors in Friendica files
26 Usage
27         bin/console typo [-h|--help|-?] [-v]
28
29 Description
30         Checks all PHP files in the Friendica file tree for parse errors
31
32 Options
33         -h|--help|-?  Show help information
34         -v            Show more debug information.
35 HELP;
36                 return $help;
37         }
38
39         public function __construct(Configuration $config, array $argv = null)
40         {
41                 parent::__construct($argv);
42
43                 $this->config = $config;
44         }
45
46         protected function doExecute()
47         {
48                 if ($this->getOption('v')) {
49                         $this->out('Class: ' . __CLASS__);
50                         $this->out('Arguments: ' . var_export($this->args, true));
51                         $this->out('Options: ' . var_export($this->options, true));
52                 }
53
54                 if (count($this->args) > 0) {
55                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
56                 }
57
58                 $php_path = $this->config->get('config', 'php_path', 'php');
59
60                 if ($this->getOption('v')) {
61                         $this->out('Directory: src');
62                 }
63
64                 $Iterator = new \RecursiveDirectoryIterator('src');
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: tests');
74                 }
75
76                 $Iterator = new \RecursiveDirectoryIterator('tests');
77
78                 foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
79                         if (substr($file, -4) === '.php') {
80                                 $this->checkFile($php_path, $file);
81                         }
82                 }
83
84                 if ($this->getOption('v')) {
85                         $this->out('Directory: mod');
86                 }
87
88                 $files = glob('mod/*.php');
89                 $this->checkFiles($php_path, $files);
90
91                 if ($this->getOption('v')) {
92                         $this->out('Directory: include');
93                 }
94
95                 $files = glob('include/*.php');
96                 $this->checkFiles($php_path, $files);
97
98                 if ($this->getOption('v')) {
99                         $this->out('Directory: addon');
100                 }
101
102                 $dirs = glob('addon/*');
103                 foreach ($dirs as $dir) {
104                         $addon = basename($dir);
105                         $files = glob($dir . '/' . $addon . '.php');
106                         $this->checkFiles($php_path, $files);
107                 }
108
109                 if ($this->getOption('v')) {
110                         $this->out('String files');
111                 }
112
113                 $files = glob('view/lang/*/strings.php');
114                 $this->checkFiles($php_path, $files);
115
116                 $this->out('No errors.');
117
118                 return 0;
119         }
120
121         private function checkFiles($php_path, array $files)
122         {
123                 foreach ($files as $file) {
124                         $this->checkFile($php_path, $file);
125                 }
126         }
127
128         private function checkFile($php_path, $file)
129         {
130                 if ($this->getOption('v')) {
131                         $this->out('Checking ' . $file);
132                 }
133
134                 $output = [];
135                 $ret = 0;
136                 exec("$php_path -l $file", $output, $ret);
137                 if ($ret !== 0) {
138                         throw new \RuntimeException('Parse error found in ' . $file . ', scan stopped.');
139                 }
140         }
141 }