3 namespace Friendica\Console;
5 use Friendica\Core\Config\Configuration;
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.
11 * @author Hypolite Petovan <hypolite@mrpetovan.com>
13 class Typo extends \Asika\SimpleConsole\Console
15 protected $helpOptions = ['h', 'help', '?'];
22 protected function getHelp()
25 console typo - Checks for parse errors in Friendica files
27 bin/console typo [-h|--help|-?] [-v]
30 Checks all PHP files in the Friendica file tree for parse errors
33 -h|--help|-? Show help information
34 -v Show more debug information.
39 public function __construct(Configuration $config, array $argv = null)
41 parent::__construct($argv);
43 $this->config = $config;
46 protected function doExecute()
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));
54 if (count($this->args) > 0) {
55 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
58 $php_path = $this->config->get('config', 'php_path', 'php');
60 if ($this->getOption('v')) {
61 $this->out('Directory: src');
64 $Iterator = new \RecursiveDirectoryIterator('src');
66 foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
67 if (substr($file, -4) === '.php') {
68 $this->checkFile($php_path, $file);
72 if ($this->getOption('v')) {
73 $this->out('Directory: tests');
76 $Iterator = new \RecursiveDirectoryIterator('tests');
78 foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
79 if (substr($file, -4) === '.php') {
80 $this->checkFile($php_path, $file);
84 if ($this->getOption('v')) {
85 $this->out('Directory: mod');
88 $files = glob('mod/*.php');
89 $this->checkFiles($php_path, $files);
91 if ($this->getOption('v')) {
92 $this->out('Directory: include');
95 $files = glob('include/*.php');
96 $this->checkFiles($php_path, $files);
98 if ($this->getOption('v')) {
99 $this->out('Directory: addon');
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);
109 if ($this->getOption('v')) {
110 $this->out('String files');
113 $files = glob('view/lang/*/strings.php');
114 $this->checkFiles($php_path, $files);
116 $this->out('No errors.');
121 private function checkFiles($php_path, array $files)
123 foreach ($files as $file) {
124 $this->checkFile($php_path, $file);
128 private function checkFile($php_path, $file)
130 if ($this->getOption('v')) {
131 $this->out('Checking ' . $file);
136 exec("$php_path -l $file", $output, $ret);
138 throw new \RuntimeException('Parse error found in ' . $file . ', scan stopped.');