3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Capability\IManageConfigValues;
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', '?'];
35 * @var IManageConfigValues
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(IManageConfigValues $config, array $argv = null)
58 parent::__construct($argv);
60 $this->config = $config;
63 protected function doExecute(): int
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: addon');
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);
119 if ($this->getOption('v')) {
120 $this->out('String files');
123 $files = glob('view/lang/*/strings.php');
124 $this->checkFiles($php_path, $files);
126 $this->out('No errors.');
131 private function checkFiles($php_path, array $files)
133 foreach ($files as $file) {
134 $this->checkFile($php_path, $file);
138 private function checkFile($php_path, $file)
140 if ($this->getOption('v')) {
141 $this->out('Checking ' . $file);
146 exec("$php_path -l $file", $output, $ret);
148 throw new \RuntimeException('Parse error found in ' . $file . ', scan stopped.');