1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 15:09:04 +01:00
psalm/src/Psalm/Config/IssueHandler.php

175 lines
4.9 KiB
PHP
Raw Normal View History

2016-12-30 02:07:42 +01:00
<?php
namespace Psalm\Config;
use function array_filter;
use function array_map;
use function dirname;
2019-07-05 22:24:00 +02:00
use function in_array;
use function scandir;
use SimpleXMLElement;
use function strtolower;
use function substr;
use const SCANDIR_SORT_NONE;
2016-12-30 02:07:42 +01:00
class IssueHandler
{
/**
* @var string
*/
private $error_level = \Psalm\Config::REPORT_ERROR;
2016-12-30 02:07:42 +01:00
/**
* @var array<ErrorLevelFileFilter>
*/
private $custom_levels = [];
2016-12-30 02:07:42 +01:00
public static function loadFromXMLElement(SimpleXMLElement $e, string $base_dir): IssueHandler
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)) {
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) {
$handler->custom_levels[] = ErrorLevelFileFilter::loadFromXMLElement($error_level, $base_dir, true);
2016-12-30 02:07:42 +01:00
}
return $handler;
}
/**
* @return void
*/
public function setErrorLevel(string $error_level)
2016-12-30 02:07:42 +01:00
{
2017-05-27 02:05:57 +02:00
if (!in_array($error_level, \Psalm\Config::$ERROR_LEVELS, true)) {
throw new \Psalm\Exception\ConfigException('Unexpected error level ' . $error_level);
2016-12-30 02:07:42 +01:00
}
$this->error_level = $error_level;
}
public function getReportingLevelForFile(string $file_path): string
2016-12-30 02:07:42 +01:00
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allows($file_path)) {
2016-12-30 02:07:42 +01:00
return $custom_level->getErrorLevel();
}
}
return $this->error_level;
}
/**
2019-05-03 23:12:20 +02:00
* @return string|null
*/
public function getReportingLevelForClass(string $fq_classlike_name)
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allowsClass($fq_classlike_name)) {
return $custom_level->getErrorLevel();
}
}
}
/**
2019-05-03 23:12:20 +02:00
* @return string|null
*/
public function getReportingLevelForMethod(string $method_id)
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allowsMethod(strtolower($method_id))) {
return $custom_level->getErrorLevel();
}
}
}
2019-05-03 23:12:20 +02:00
/**
* @return string|null
*/
public function getReportingLevelForFunction(string $function_id)
{
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)
{
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 getReportingLevelForProperty(string $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|null
*/
public function getReportingLevelForVariable(string $var_name)
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allowsVariable($var_name)) {
return $custom_level->getErrorLevel();
}
}
}
2019-05-03 21:29:44 +02:00
/**
* @return string[]
2020-02-21 07:32:22 +01:00
* @psalm-return array<string>
2019-05-03 21:29:44 +02:00
*/
public static function getAllIssueTypes(): array
2019-05-03 21:29:44 +02:00
{
return array_filter(
array_map(
/**
* @param string $file_name
*
* @return string
*/
function ($file_name) {
return substr($file_name, 0, -4);
},
scandir(dirname(__DIR__) . '/Issue', SCANDIR_SORT_NONE)
2019-05-03 21:29:44 +02:00
),
function (string $issue_name): bool {
2019-05-03 21:29:44 +02:00
return !empty($issue_name)
&& $issue_name !== 'MethodIssue'
&& $issue_name !== 'PropertyIssue'
&& $issue_name !== 'FunctionIssue'
&& $issue_name !== 'ArgumentIssue'
&& $issue_name !== 'VariableIssue'
2019-05-03 21:29:44 +02:00
&& $issue_name !== 'ClassIssue'
&& $issue_name !== 'CodeIssue'
&& $issue_name !== 'PsalmInternalError'
&& $issue_name !== 'ParseError'
&& $issue_name !== 'PluginIssue';
}
);
}
2016-12-30 02:07:42 +01:00
}