2018-03-24 19:39:13 +01:00
|
|
|
<?php
|
|
|
|
|
2019-05-02 23:17:35 +02:00
|
|
|
namespace Friendica\Console;
|
2018-03-24 19:39:13 +01:00
|
|
|
|
2019-12-19 20:11:07 +01:00
|
|
|
use Friendica\Core\Config\IConfiguration;
|
2019-02-04 00:04:16 +01:00
|
|
|
|
2018-03-24 19:39:13 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2018-09-16 01:28:38 +02:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-03-24 19:39:13 +01:00
|
|
|
*/
|
|
|
|
class Typo extends \Asika\SimpleConsole\Console
|
|
|
|
{
|
|
|
|
protected $helpOptions = ['h', 'help', '?'];
|
|
|
|
|
2019-07-28 22:06:33 +02:00
|
|
|
/**
|
2019-12-19 20:11:07 +01:00
|
|
|
* @var IConfiguration
|
2019-07-28 22:06:33 +02:00
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2018-03-24 19:39:13 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-19 20:11:07 +01:00
|
|
|
public function __construct(IConfiguration $config, array $argv = null)
|
2019-07-28 22:06:33 +02:00
|
|
|
{
|
|
|
|
parent::__construct($argv);
|
|
|
|
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2018-03-24 19:39:13 +01:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2019-07-28 22:06:33 +02:00
|
|
|
$php_path = $this->config->get('config', 'php_path', 'php');
|
2018-03-24 19:39:13 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 04:27:20 +02:00
|
|
|
if ($this->getOption('v')) {
|
|
|
|
$this->out('Directory: tests');
|
|
|
|
}
|
|
|
|
|
|
|
|
$Iterator = new \RecursiveDirectoryIterator('tests');
|
|
|
|
|
|
|
|
foreach (new \RecursiveIteratorIterator($Iterator) as $file) {
|
|
|
|
if (substr($file, -4) === '.php') {
|
|
|
|
$this->checkFile($php_path, $file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 19:39:13 +01:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
$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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|