2016-12-30 02:07:42 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Config;
|
|
|
|
|
|
|
|
use SimpleXMLElement;
|
|
|
|
|
|
|
|
class IssueHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private $error_level = \Psalm\Config::REPORT_ERROR;
|
2016-12-30 02:07:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<ErrorLevelFileFilter>
|
|
|
|
*/
|
2017-02-12 00:56:38 +01:00
|
|
|
private $custom_levels = [];
|
2016-12-30 02:07:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param SimpleXMLElement $e
|
2017-05-04 20:25:58 +02:00
|
|
|
* @param string $base_dir
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-30 02:07:42 +01:00
|
|
|
* @return self
|
|
|
|
*/
|
2017-05-04 20:25:58 +02:00
|
|
|
public static function loadFromXMLElement(SimpleXMLElement $e, $base_dir)
|
2016-12-30 02:07:42 +01:00
|
|
|
{
|
|
|
|
$handler = new self();
|
|
|
|
|
|
|
|
if (isset($e['errorLevel'])) {
|
|
|
|
$handler->error_level = (string) $e['errorLevel'];
|
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
if (!in_array($handler->error_level, \Psalm\Config::$ERROR_LEVELS, true)) {
|
2019-03-25 02:16:36 +01:00
|
|
|
throw new \Psalm\Exception\ConfigException('Unexpected error level ' . $handler->error_level);
|
2016-12-30 02:07:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var \SimpleXMLElement $error_level */
|
|
|
|
foreach ($e->errorLevel as $error_level) {
|
2017-05-04 20:25:58 +02:00
|
|
|
$handler->custom_levels[] = ErrorLevelFileFilter::loadFromXMLElement($error_level, $base_dir, true);
|
2016-12-30 02:07:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $error_level
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-30 02:07:42 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setErrorLevel($error_level)
|
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
if (!in_array($error_level, \Psalm\Config::$ERROR_LEVELS, true)) {
|
2019-03-25 02:16:36 +01:00
|
|
|
throw new \Psalm\Exception\ConfigException('Unexpected error level ' . $error_level);
|
2016-12-30 02:07:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->error_level = $error_level;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-16 18:59:09 +01:00
|
|
|
* @param string $file_path
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-30 02:07:42 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-16 18:59:09 +01:00
|
|
|
public function getReportingLevelForFile($file_path)
|
2016-12-30 02:07:42 +01:00
|
|
|
{
|
|
|
|
foreach ($this->custom_levels as $custom_level) {
|
2017-01-16 18:59:09 +01:00
|
|
|
if ($custom_level->allows($file_path)) {
|
2016-12-30 02:07:42 +01:00
|
|
|
return $custom_level->getErrorLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->error_level;
|
|
|
|
}
|
2018-03-21 03:36:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $fq_classlike_name
|
|
|
|
*
|
2019-05-03 23:12:20 +02:00
|
|
|
* @return string|null
|
2018-03-21 03:36:03 +01:00
|
|
|
*/
|
|
|
|
public function getReportingLevelForClass($fq_classlike_name)
|
|
|
|
{
|
|
|
|
foreach ($this->custom_levels as $custom_level) {
|
|
|
|
if ($custom_level->allowsClass($fq_classlike_name)) {
|
|
|
|
return $custom_level->getErrorLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $method_id
|
|
|
|
*
|
2019-05-03 23:12:20 +02:00
|
|
|
* @return string|null
|
2018-03-21 03:36:03 +01:00
|
|
|
*/
|
|
|
|
public function getReportingLevelForMethod($method_id)
|
|
|
|
{
|
|
|
|
foreach ($this->custom_levels as $custom_level) {
|
2018-08-16 22:49:33 +02:00
|
|
|
if ($custom_level->allowsMethod(strtolower($method_id))) {
|
2018-03-21 03:36:03 +01:00
|
|
|
return $custom_level->getErrorLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-11 06:07:41 +02:00
|
|
|
|
2019-05-03 23:12:20 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getReportingLevelForFunction(string $function_id)
|
2019-04-26 00:02:19 +02:00
|
|
|
{
|
|
|
|
foreach ($this->custom_levels as $custom_level) {
|
|
|
|
if ($custom_level->allowsMethod(strtolower($function_id))) {
|
|
|
|
return $custom_level->getErrorLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 23:12:20 +02:00
|
|
|
/**
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public function getReportingLevelForArgument(string $function_id)
|
2019-04-26 00:02:19 +02:00
|
|
|
{
|
|
|
|
foreach ($this->custom_levels as $custom_level) {
|
|
|
|
if ($custom_level->allowsMethod(strtolower($function_id))) {
|
|
|
|
return $custom_level->getErrorLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 06:07:41 +02:00
|
|
|
/**
|
|
|
|
* @param string $property_id
|
|
|
|
*
|
2019-05-03 23:12:20 +02:00
|
|
|
* @return string|null
|
2018-05-11 06:07:41 +02:00
|
|
|
*/
|
|
|
|
public function getReportingLevelForProperty($property_id)
|
|
|
|
{
|
|
|
|
foreach ($this->custom_levels as $custom_level) {
|
|
|
|
if ($custom_level->allowsProperty($property_id)) {
|
|
|
|
return $custom_level->getErrorLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-03 21:29:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
* @psalm-return array<mixed, string>
|
|
|
|
*/
|
|
|
|
public static function getAllIssueTypes()
|
|
|
|
{
|
|
|
|
return array_filter(
|
|
|
|
array_map(
|
|
|
|
/**
|
|
|
|
* @param string $file_name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function ($file_name) {
|
|
|
|
return substr($file_name, 0, -4);
|
|
|
|
},
|
|
|
|
scandir(dirname(__DIR__) . '/Issue')
|
|
|
|
),
|
|
|
|
/**
|
|
|
|
* @param string $issue_name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function ($issue_name) {
|
|
|
|
return !empty($issue_name)
|
|
|
|
&& $issue_name !== 'MethodIssue'
|
|
|
|
&& $issue_name !== 'PropertyIssue'
|
|
|
|
&& $issue_name !== 'FunctionIssue'
|
|
|
|
&& $issue_name !== 'ArgumentIssue'
|
|
|
|
&& $issue_name !== 'ClassIssue'
|
|
|
|
&& $issue_name !== 'CodeIssue'
|
|
|
|
&& $issue_name !== 'PsalmInternalError'
|
|
|
|
&& $issue_name !== 'ParseError'
|
|
|
|
&& $issue_name !== 'PluginIssue';
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-12-30 02:07:42 +01:00
|
|
|
}
|