3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Console;
24 use Friendica\Core\Config\IConfig;
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.
30 class Typo extends \Asika\SimpleConsole\Console
32 protected $helpOptions = ['h', 'help', '?'];
39 protected function getHelp()
42 console typo - Checks for parse errors in Friendica files
44 bin/console typo [-h|--help|-?] [-v]
47 Checks all PHP files in the Friendica file tree for parse errors
50 -h|--help|-? Show help information
51 -v Show more debug information.
56 public function __construct(IConfig $config, array $argv = null)
58 parent::__construct($argv);
60 $this->config = $config;
63 protected function doExecute()
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));
71 if (count($this->args) > 0) {
72 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
75 $php_path = $this->config->get('config', 'php_path', 'php');
77 if ($this->getOption('v')) {
78 $this->out('Directory: src');
81 $Iterator = new \RecursiveDirectoryIterator('src');
83 foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
84 if (substr($file, -4) === '.php') {
85 $this->checkFile($php_path, $file);
89 if ($this->getOption('v')) {
90 $this->out('Directory: tests');
93 $Iterator = new \RecursiveDirectoryIterator('tests');
95 foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
96 if (substr($file, -4) === '.php') {
97 $this->checkFile($php_path, $file);
101 if ($this->getOption('v')) {
102 $this->out('Directory: mod');
105 $files = glob('mod/*.php');
106 $this->checkFiles($php_path, $files);
108 if ($this->getOption('v')) {
109 $this->out('Directory: include');
112 $files = glob('include/*.php');
113 $this->checkFiles($php_path, $files);
115 if ($this->getOption('v')) {
116 $this->out('Directory: addon');
119 $dirs = glob('addon/*');
120 foreach ($dirs as $dir) {
121 $addon = basename($dir);
122 $files = glob($dir . '/' . $addon . '.php');
123 $this->checkFiles($php_path, $files);
126 if ($this->getOption('v')) {
127 $this->out('String files');
130 $files = glob('view/lang/*/strings.php');
131 $this->checkFiles($php_path, $files);
133 $this->out('No errors.');
138 private function checkFiles($php_path, array $files)
140 foreach ($files as $file) {
141 $this->checkFile($php_path, $file);
145 private function checkFile($php_path, $file)
147 if ($this->getOption('v')) {
148 $this->out('Checking ' . $file);
153 exec("$php_path -l $file", $output, $ret);
155 throw new \RuntimeException('Parse error found in ' . $file . ', scan stopped.');