2016-06-06 07:07:50 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm;
|
2016-06-06 07:07:50 +02:00
|
|
|
|
2016-07-26 00:37:44 +02:00
|
|
|
use Psalm\Config\FileFilter;
|
2016-06-10 00:08:25 +02:00
|
|
|
use SimpleXMLElement;
|
|
|
|
|
2016-06-06 07:07:50 +02:00
|
|
|
class Config
|
|
|
|
{
|
2016-07-26 00:39:36 +02:00
|
|
|
const DEFAULT_FILE_NAME = 'psalm.xml';
|
2016-06-25 00:18:11 +02:00
|
|
|
const REPORT_INFO = 'info';
|
|
|
|
const REPORT_ERROR = 'error';
|
2016-06-26 19:45:20 +02:00
|
|
|
const REPORT_SUPPRESS = 'suppress';
|
2016-06-25 00:18:11 +02:00
|
|
|
|
2016-06-27 21:10:13 +02:00
|
|
|
public static $ERROR_LEVELS = [
|
2016-06-25 00:18:11 +02:00
|
|
|
self::REPORT_INFO,
|
|
|
|
self::REPORT_ERROR,
|
2016-06-26 19:45:20 +02:00
|
|
|
self::REPORT_SUPPRESS
|
2016-06-25 00:18:11 +02:00
|
|
|
];
|
|
|
|
|
2016-06-10 00:08:25 +02:00
|
|
|
protected static $_config;
|
|
|
|
|
2016-06-27 04:40:57 +02:00
|
|
|
/**
|
|
|
|
* Whether or not to stop when the first error is seen
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2016-06-21 01:30:38 +02:00
|
|
|
public $stop_on_first_error = true;
|
2016-06-27 04:40:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not to use types as defined in docblocks
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2016-06-25 00:18:11 +02:00
|
|
|
public $use_docblock_types = false;
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-27 04:40:57 +02:00
|
|
|
/**
|
|
|
|
* Whether or not to throw an exception on first error
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
public $throw_exception = false;
|
|
|
|
|
2016-07-26 21:00:40 +02:00
|
|
|
/**
|
|
|
|
* Path to the autoader
|
|
|
|
* @var string|null
|
|
|
|
*/
|
|
|
|
public $autoloader;
|
|
|
|
|
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'];
|
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
protected $filetype_handlers = [];
|
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
protected $issue_handlers = [];
|
|
|
|
|
2016-07-31 01:54:53 +02:00
|
|
|
protected $custom_error_levels = [
|
|
|
|
'InvalidDocblock' => self::REPORT_INFO,
|
|
|
|
'MixedMethodCall' => self::REPORT_SUPPRESS,
|
|
|
|
'TooManyArguments' => self::REPORT_SUPPRESS
|
|
|
|
];
|
2016-06-21 01:30:38 +02:00
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
protected $mock_classes = [];
|
2016-06-06 07:07:50 +02:00
|
|
|
|
2016-06-18 20:45:55 +02:00
|
|
|
/**
|
2016-07-26 00:37:44 +02:00
|
|
|
* Psalm plugins
|
2016-06-18 20:45:55 +02:00
|
|
|
* @var array<Plugin>
|
|
|
|
*/
|
|
|
|
protected $plugins = [];
|
|
|
|
|
2016-06-06 07:07:50 +02:00
|
|
|
private function __construct()
|
|
|
|
{
|
|
|
|
self::$_config = $this;
|
|
|
|
}
|
|
|
|
|
2016-06-26 19:45:20 +02:00
|
|
|
/**
|
|
|
|
* Creates a new config object from the file
|
|
|
|
* @param string $file_name
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-06-21 01:30:38 +02:00
|
|
|
public static function loadFromXML($file_name)
|
2016-06-10 00:08:25 +02:00
|
|
|
{
|
2016-06-10 20:47:44 +02:00
|
|
|
$file_contents = file_get_contents($file_name);
|
|
|
|
|
2016-06-21 01:30:38 +02:00
|
|
|
$config = new self();
|
|
|
|
|
|
|
|
$config->base_dir = dirname($file_name) . '/';
|
2016-06-10 20:47:44 +02:00
|
|
|
|
2016-06-10 00:08:25 +02:00
|
|
|
$config_xml = new SimpleXMLElement($file_contents);
|
|
|
|
|
2016-06-21 01:30:38 +02:00
|
|
|
if (isset($config_xml['stopOnFirstError'])) {
|
|
|
|
$attribute_text = (string) $config_xml['stopOnError'];
|
|
|
|
$config->stop_on_first_error = $attribute_text === 'true' || $attribute_text === '1';
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 00:18:11 +02:00
|
|
|
if (isset($config_xml['useDocblockTypes'])) {
|
|
|
|
$attribute_text = (string) $config_xml['useDocblockTypes'];
|
|
|
|
$config->use_docblock_types = $attribute_text === 'true' || $attribute_text === '1';
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 04:40:57 +02:00
|
|
|
if (isset($config_xml['throwExceptionOnError'])) {
|
|
|
|
$attribute_text = (string) $config_xml['throwExceptionOnError'];
|
|
|
|
$config->throw_exception = $attribute_text === 'true' || $attribute_text === '1';
|
|
|
|
}
|
|
|
|
|
2016-07-26 21:00:40 +02:00
|
|
|
if (isset($config_xml['autoloader'])) {
|
|
|
|
$config->autoloader = (string) $config_xml['autoloader'];
|
|
|
|
}
|
|
|
|
|
2016-06-10 00:08:25 +02:00
|
|
|
if (isset($config_xml->inspectFiles)) {
|
2016-06-21 01:30:38 +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-21 01:30:38 +02:00
|
|
|
$config->file_extensions = [];
|
2016-06-10 20:47:44 +02:00
|
|
|
|
2016-06-21 01:30:38 +02:00
|
|
|
$config->loadFileExtensions($config_xml->fileExtensions->extension);
|
2016-06-10 20:47:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($config_xml->mockClasses) && isset($config_xml->mockClasses->class)) {
|
|
|
|
foreach ($config_xml->mockClasses->class as $mock_class) {
|
2016-06-21 01:30:38 +02:00
|
|
|
$config->mock_classes[] = $mock_class['name'];
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
2016-06-10 20:47:44 +02:00
|
|
|
}
|
|
|
|
|
2016-06-18 20:45:55 +02:00
|
|
|
// this plugin loading system borrows heavily from etsy/phan
|
|
|
|
if (isset($config_xml->plugins) && isset($config_xml->plugins->plugin)) {
|
|
|
|
foreach ($config_xml->plugins->plugin as $plugin) {
|
|
|
|
$plugin_file_name = $plugin['filename'];
|
2016-06-20 06:38:13 +02:00
|
|
|
|
2016-06-21 01:30:38 +02:00
|
|
|
$path = $config->base_dir . $plugin_file_name;
|
2016-06-20 06:38:13 +02:00
|
|
|
|
|
|
|
if (!file_exists($path)) {
|
|
|
|
throw new \InvalidArgumentException('Cannot find file ' . $path);
|
|
|
|
}
|
|
|
|
|
|
|
|
$loaded_plugin = require($path);
|
2016-06-18 20:45:55 +02:00
|
|
|
|
|
|
|
if (!$loaded_plugin) {
|
|
|
|
throw new \InvalidArgumentException('Plugins must return an instance of that plugin at the end of the file - ' . $plugin_file_name . ' does not');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($loaded_plugin instanceof Plugin)) {
|
2016-07-26 00:37:44 +02:00
|
|
|
throw new \InvalidArgumentException('Plugins must extend \Psalm\Plugin - ' . $plugin_file_name . ' does not');
|
2016-06-18 20:45:55 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 01:30:38 +02:00
|
|
|
$config->plugins[] = $loaded_plugin;
|
2016-06-18 20:45:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
if (isset($config_xml->issueHandler)) {
|
|
|
|
foreach ($config_xml->issueHandler->children() as $key => $issue_handler) {
|
2016-06-21 01:30:38 +02:00
|
|
|
if (isset($issue_handler['errorLevel'])) {
|
|
|
|
$error_level = (string) $issue_handler['errorLevel'];
|
|
|
|
|
2016-06-27 21:10:13 +02:00
|
|
|
if (!in_array($error_level, self::$ERROR_LEVELS)) {
|
2016-06-21 01:30:38 +02:00
|
|
|
throw new \InvalidArgumentException('Error level ' . $error_level . ' could not be recognised');
|
|
|
|
}
|
|
|
|
|
|
|
|
$config->custom_error_levels[$key] = $error_level;
|
|
|
|
}
|
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
if (isset($issue_handler->excludeFiles)) {
|
2016-06-21 01:30:38 +02:00
|
|
|
$config->issue_handlers[$key] = FileFilter::loadFromXML($issue_handler->excludeFiles, false);
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-26 19:45:20 +02:00
|
|
|
|
|
|
|
return $config;
|
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-20 06:38:13 +02:00
|
|
|
protected function loadFileExtensions($extensions)
|
|
|
|
{
|
|
|
|
foreach ($extensions as $extension) {
|
|
|
|
$extension_name = preg_replace('/^\.?/', '', $extension['name']);
|
|
|
|
$this->file_extensions[] = $extension_name;
|
|
|
|
|
|
|
|
if (isset($extension['filetypeHandler'])) {
|
|
|
|
$path = $this->base_dir . $extension['filetypeHandler'];
|
|
|
|
|
|
|
|
if (!file_exists($path)) {
|
2016-06-20 07:05:44 +02:00
|
|
|
throw new Exception\ConfigException('Error parsing config: cannot find file ' . $path);
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$declared_classes = FileChecker::getDeclaredClassesInFile($path);
|
|
|
|
|
|
|
|
if (count($declared_classes) !== 1) {
|
|
|
|
throw new \InvalidArgumentException('Filetype handlers must have exactly one class in the file - ' . $path . ' has ' . count($declared_classes));
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once($path);
|
|
|
|
|
2016-07-26 00:37:44 +02:00
|
|
|
if (!is_subclass_of($declared_classes[0], 'Psalm\\FileChecker')) {
|
|
|
|
throw new \InvalidArgumentException('Filetype handlers must extend \Psalm\FileChecker - ' . $path . ' does not');
|
2016-06-20 06:38:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->filetype_handlers[$extension_name] = $declared_classes[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-12 01:57:39 +02:00
|
|
|
if ($this->getReportingLevel($issue_type) === self::REPORT_SUPPRESS) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-10 23:20:04 +02:00
|
|
|
$file_name = $this->shortenFileName($file_name);
|
2016-06-10 20:47:44 +02:00
|
|
|
|
2016-08-05 21:11:20 +02:00
|
|
|
if ($this->getIncludeDirs()) {
|
|
|
|
$in_project_dir = false;
|
|
|
|
|
|
|
|
foreach ($this->getIncludeDirs() as $dir_name) {
|
|
|
|
if (preg_match('/^' . preg_quote($dir_name, '/') . '/', $file_name)) {
|
|
|
|
$in_project_dir = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$in_project_dir) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-21 01:30:38 +02:00
|
|
|
public function getReportingLevel($issue_type)
|
|
|
|
{
|
|
|
|
if (isset($this->custom_error_levels[$issue_type])) {
|
|
|
|
return $this->custom_error_levels[$issue_type];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::REPORT_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-06-13 21:33:18 +02:00
|
|
|
public function getIncludeDirs()
|
2016-06-10 00:08:25 +02:00
|
|
|
{
|
2016-06-13 21:33:18 +02:00
|
|
|
return $this->inspect_files->getIncludeDirs();
|
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-13 21:33:18 +02:00
|
|
|
public function getBaseDir()
|
|
|
|
{
|
|
|
|
return $this->base_dir;
|
|
|
|
}
|
2016-06-10 00:08:25 +02:00
|
|
|
|
2016-06-13 21:33:18 +02:00
|
|
|
public function getFileExtensions()
|
|
|
|
{
|
|
|
|
return $this->file_extensions;
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
2016-06-10 20:47:44 +02:00
|
|
|
|
2016-06-20 06:38:13 +02:00
|
|
|
public function getFiletypeHandlers()
|
|
|
|
{
|
|
|
|
return $this->filetype_handlers;
|
|
|
|
}
|
|
|
|
|
2016-06-10 20:47:44 +02:00
|
|
|
public function getMockClasses()
|
|
|
|
{
|
|
|
|
return $this->mock_classes;
|
|
|
|
}
|
2016-06-18 20:45:55 +02:00
|
|
|
|
|
|
|
public function getPlugins()
|
|
|
|
{
|
|
|
|
return $this->plugins;
|
|
|
|
}
|
2016-06-20 22:18:31 +02:00
|
|
|
|
|
|
|
public function setIssueHandler($issue_name, FileFilter $filter = null)
|
|
|
|
{
|
|
|
|
$this->issue_handlers[$issue_name] = $filter;
|
|
|
|
}
|
2016-06-06 07:07:50 +02:00
|
|
|
}
|