2016-06-06 07:07:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace CodeInspector;
|
|
|
|
|
2016-06-10 00:08:25 +02:00
|
|
|
use CodeInspector\Config\FileFilter;
|
|
|
|
use RecursiveDirectoryIterator;
|
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
use SimpleXMLElement;
|
|
|
|
|
2016-06-06 07:07:50 +02:00
|
|
|
class Config
|
|
|
|
{
|
2016-06-10 00:08:25 +02:00
|
|
|
protected static $_config;
|
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
public $stop_on_error = true;
|
|
|
|
public $use_docblock_return_type = false;
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
protected $inspect_files;
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
protected $base_dir;
|
2016-06-06 07:07:50 +02:00
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
protected $file_extensions = ['php'];
|
|
|
|
|
|
|
|
protected $issue_handlers = [];
|
|
|
|
|
|
|
|
protected $mock_classes = [];
|
2016-06-06 07:07:50 +02:00
|
|
|
|
|
|
|
private function __construct()
|
|
|
|
{
|
|
|
|
self::$_config = $this;
|
|
|
|
}
|
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
public static function loadFromXML($file_name)
|
2016-06-10 00:08:25 +02:00
|
|
|
{
|
|
|
|
$config = new self();
|
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
$file_contents = file_get_contents($file_name);
|
|
|
|
|
|
|
|
$config->base_dir = dirname($file_name) . '/';
|
|
|
|
|
2016-06-10 00:08:25 +02:00
|
|
|
$config_xml = new SimpleXMLElement($file_contents);
|
|
|
|
|
|
|
|
if (isset($config_xml['stopOnError'])) {
|
2016-06-10 23:20:04 +02:00
|
|
|
$config->stop_on_error = $config_xml['stopOnError'] === 'true' || $config_xml['stopOnError'] === '1';
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config_xml['useDocblockReturnType'])) {
|
2016-06-10 20:47:44 +02:00
|
|
|
$config->use_docblock_return_type = (bool) $config_xml['useDocblockReturnType'];
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config_xml->inspectFiles)) {
|
2016-06-10 20:47:44 +02:00
|
|
|
$config->inspect_files = FileFilter::loadFromXML($config_xml->inspectFiles, true);
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config_xml->fileExtensions)) {
|
2016-06-10 20:47:44 +02:00
|
|
|
$config->file_extensions = [];
|
|
|
|
|
|
|
|
foreach ($config_xml->fileExtensions->extension as $extension) {
|
|
|
|
$config->file_extensions[] = preg_replace('/^\.?/', '', $extension['name']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config_xml->mockClasses) && isset($config_xml->mockClasses->class)) {
|
|
|
|
foreach ($config_xml->mockClasses->class as $mock_class) {
|
|
|
|
$config->mock_classes[] = $mock_class['name'];
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
2016-06-10 20:47:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config_xml->issueHandler)) {
|
|
|
|
foreach ($config_xml->issueHandler->children() as $key => $issue_handler) {
|
|
|
|
if (isset($issue_handler->excludeFiles)) {
|
|
|
|
$config->issue_handlers[$key] = FileFilter::loadFromXML($issue_handler->excludeFiles, false);
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-06-06 07:07:50 +02:00
|
|
|
public static function getInstance()
|
|
|
|
{
|
|
|
|
if (self::$_config) {
|
|
|
|
return self::$_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new self();
|
|
|
|
}
|
|
|
|
|
2016-06-10 23:20:04 +02:00
|
|
|
public function shortenFileName($file_name)
|
|
|
|
{
|
|
|
|
return preg_replace('/^' . preg_quote($this->base_dir, '/') . '/', '', $file_name);
|
|
|
|
}
|
|
|
|
|
2016-06-10 00:08:25 +02:00
|
|
|
public function excludeIssueInFile($issue_type, $file_name)
|
|
|
|
{
|
2016-06-10 20:47:44 +02:00
|
|
|
$issue_type = array_pop(explode('\\', $issue_type));
|
2016-06-10 23:20:04 +02:00
|
|
|
$file_name = $this->shortenFileName($file_name);
|
2016-06-10 20:47:44 +02:00
|
|
|
|
|
|
|
if (!isset($this->issue_handlers[$issue_type])) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
return !$this->issue_handlers[$issue_type]->allows($file_name);
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-06 07:07:50 +02:00
|
|
|
public function doesInheritVariables($file_name)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
|
|
|
public function getFilesToCheck()
|
|
|
|
{
|
2016-06-10 20:47:44 +02:00
|
|
|
$files = $this->inspect_files->getIncludeFiles();
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
foreach ($this->inspect_files->getIncludeDirs() as $dir) {
|
2016-06-10 00:08:25 +02:00
|
|
|
/** @var RecursiveDirectoryIterator */
|
2016-06-10 20:47:44 +02:00
|
|
|
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->base_dir . '/' . $dir));
|
2016-06-10 00:08:25 +02:00
|
|
|
$iterator->rewind();
|
|
|
|
|
|
|
|
while ($iterator->valid()) {
|
|
|
|
if (!$iterator->isDot()) {
|
2016-06-10 20:47:44 +02:00
|
|
|
if (in_array($iterator->getExtension(), $this->file_extensions)) {
|
2016-06-10 00:08:25 +02:00
|
|
|
$files[] = $iterator->getRealPath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$iterator->next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $files;
|
|
|
|
}
|
2016-06-10 20:47:44 +02:00
|
|
|
|
|
|
|
public function getMockClasses()
|
|
|
|
{
|
|
|
|
return $this->mock_classes;
|
|
|
|
}
|
2016-06-06 07:07:50 +02:00
|
|
|
}
|