Add Typo Console

This commit is contained in:
Hypolite Petovan 2018-03-18 19:45:35 -04:00
parent 9a012ad303
commit 620e9842e1
2 changed files with 121 additions and 0 deletions

View File

@ -112,6 +112,8 @@ HELP;
break;
case 'po2php': $subconsole = new Console\PoToPhp($subargs);
break;
case 'typo': $subconsole = new Console\Typo($subargs);
break;
default:
throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist');
}

119
src/Core/Console/Typo.php Normal file
View File

@ -0,0 +1,119 @@
<?php
namespace Friendica\Core\Console;
/**
* Tired of chasing typos and finding them after a commit.
* Run this and quickly see if we've got any parse errors in our application files.
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class Typo extends \Asika\SimpleConsole\Console
{
protected $helpOptions = ['h', 'help', '?'];
protected function getHelp()
{
$help = <<<HELP
console typo - Checks for parse errors in Friendica files
Usage
bin/console typo [-h|--help|-?] [-v]
Description
Checks all PHP files in the Friendica file tree for parse errors
Options
-h|--help|-? Show help information
-v Show more debug information.
HELP;
return $help;
}
protected function doExecute()
{
if ($this->getOption('v')) {
$this->out('Class: ' . __CLASS__);
$this->out('Arguments: ' . var_export($this->args, true));
$this->out('Options: ' . var_export($this->options, true));
}
if (count($this->args) > 0) {
throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
}
$a = get_app();
$php_path = $a->getConfigValue('config', 'php_path', 'php');
if ($this->getOption('v')) {
$this->out('Directory: src');
}
$Iterator = new \RecursiveDirectoryIterator('src');
foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
if (substr($file, -4) === '.php') {
$this->checkFile($php_path, $file);
}
}
if ($this->getOption('v')) {
$this->out('Directory: mod');
}
$files = glob('mod/*.php');
$this->checkFiles($php_path, $files);
if ($this->getOption('v')) {
$this->out('Directory: include');
}
$files = glob('include/*.php');
$this->checkFiles($php_path, $files);
if ($this->getOption('v')) {
$this->out('Directory: addon');
}
$dirs = glob('addon/*');
foreach ($dirs as $dir) {
$addon = basename($dir);
$files = glob($dir . '/' . $addon . '.php');
$this->checkFiles($php_path, $files);
}
if ($this->getOption('v')) {
$this->out('String files');
}
$this->checkFile($php_path, 'util/strings.php');
$files = glob('view/lang/*/strings.php');
$this->checkFiles($php_path, $files);
$this->out('No errors.');
return 0;
}
private function checkFiles($php_path, array $files)
{
foreach ($files as $file) {
$this->checkFile($php_path, $file);
}
}
private function checkFile($php_path, $file)
{
if ($this->getOption('v')) {
$this->out('Checking ' . $file);
}
$output = [];
$ret = 0;
exec("$php_path -l $file", $output, $ret);
if ($ret !== 0) {
throw new \RuntimeException('Parse error found in ' . $file . ', scan stopped.');
}
}
}