]> git.mxchange.org Git - friendica.git/blob - src/Console/Typo.php
Legacy "include" fragments have been removed
[friendica.git] / src / Console / Typo.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Friendica\Core\Config\Capability\IManageConfigValues;
25
26 /**
27  * Tired of chasing typos and finding them after a commit.
28  * Run this and quickly see if we've got any parse errors in our application files.
29  */
30 class Typo extends \Asika\SimpleConsole\Console
31 {
32         protected $helpOptions = ['h', 'help', '?'];
33
34         /**
35          * @var IManageConfigValues
36          */
37         private $config;
38
39         protected function getHelp()
40         {
41                 $help = <<<HELP
42 console typo - Checks for parse errors in Friendica files
43 Usage
44         bin/console typo [-h|--help|-?] [-v]
45
46 Description
47         Checks all PHP files in the Friendica file tree for parse errors
48
49 Options
50         -h|--help|-?  Show help information
51         -v            Show more debug information.
52 HELP;
53                 return $help;
54         }
55
56         public function __construct(IManageConfigValues $config, array $argv = null)
57         {
58                 parent::__construct($argv);
59
60                 $this->config = $config;
61         }
62
63         protected function doExecute(): int
64         {
65                 if ($this->getOption('v')) {
66                         $this->out('Class: ' . __CLASS__);
67                         $this->out('Arguments: ' . var_export($this->args, true));
68                         $this->out('Options: ' . var_export($this->options, true));
69                 }
70
71                 if (count($this->args) > 0) {
72                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
73                 }
74
75                 $php_path = $this->config->get('config', 'php_path', 'php');
76
77                 if ($this->getOption('v')) {
78                         $this->out('Directory: src');
79                 }
80
81                 $Iterator = new \RecursiveDirectoryIterator('src');
82
83                 foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
84                         if (substr($file, -4) === '.php') {
85                                 $this->checkFile($php_path, $file);
86                         }
87                 }
88
89                 if ($this->getOption('v')) {
90                         $this->out('Directory: tests');
91                 }
92
93                 $Iterator = new \RecursiveDirectoryIterator('tests');
94
95                 foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
96                         if (substr($file, -4) === '.php') {
97                                 $this->checkFile($php_path, $file);
98                         }
99                 }
100
101                 if ($this->getOption('v')) {
102                         $this->out('Directory: mod');
103                 }
104
105                 $files = glob('mod/*.php');
106                 $this->checkFiles($php_path, $files);
107
108                 if ($this->getOption('v')) {
109                         $this->out('Directory: addon');
110                 }
111
112                 $dirs = glob('addon/*');
113                 foreach ($dirs as $dir) {
114                         $addon = basename($dir);
115                         $files = glob($dir . '/' . $addon . '.php');
116                         $this->checkFiles($php_path, $files);
117                 }
118
119                 if ($this->getOption('v')) {
120                         $this->out('String files');
121                 }
122
123                 $files = glob('view/lang/*/strings.php');
124                 $this->checkFiles($php_path, $files);
125
126                 $this->out('No errors.');
127
128                 return 0;
129         }
130
131         private function checkFiles($php_path, array $files)
132         {
133                 foreach ($files as $file) {
134                         $this->checkFile($php_path, $file);
135                 }
136         }
137
138         private function checkFile($php_path, $file)
139         {
140                 if ($this->getOption('v')) {
141                         $this->out('Checking ' . $file);
142                 }
143
144                 $output = [];
145                 $ret = 0;
146                 exec("$php_path -l $file", $output, $ret);
147                 if ($ret !== 0) {
148                         throw new \RuntimeException('Parse error found in ' . $file . ', scan stopped.');
149                 }
150         }
151 }