2016-12-30 02:07:42 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2016-12-30 02:07:42 +01:00
|
|
|
namespace Psalm\Config;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Config;
|
2021-12-03 20:29:06 +01:00
|
|
|
use Psalm\Exception\ConfigException;
|
2021-06-08 04:55:21 +02:00
|
|
|
use SimpleXMLElement;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
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 function strtolower;
|
|
|
|
use function substr;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2020-05-30 23:02:35 +02:00
|
|
|
use const SCANDIR_SORT_NONE;
|
2016-12-30 02:07:42 +01:00
|
|
|
|
|
|
|
class IssueHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2021-12-03 20:11:20 +01:00
|
|
|
private $error_level = 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
|
|
|
|
2020-09-07 01:36:47 +02: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'];
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
if (!in_array($handler->error_level, Config::$ERROR_LEVELS, true)) {
|
2021-12-03 20:29:06 +01:00
|
|
|
throw new ConfigException('Unexpected error level ' . $handler->error_level);
|
2016-12-30 02:07:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 03:37:19 +01:00
|
|
|
/** @var SimpleXMLElement $error_level */
|
2016-12-30 02:07:42 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-22 16:56:07 +02:00
|
|
|
public function setCustomLevels(array $customLevels, string $base_dir): void
|
|
|
|
{
|
2021-09-22 18:14:29 +02:00
|
|
|
/** @var array $customLevel */
|
2021-09-22 16:56:07 +02:00
|
|
|
foreach ($customLevels as $customLevel) {
|
|
|
|
$this->custom_levels[] = ErrorLevelFileFilter::loadFromArray($customLevel, $base_dir, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function setErrorLevel(string $error_level): void
|
2016-12-30 02:07:42 +01:00
|
|
|
{
|
2021-12-03 20:11:20 +01:00
|
|
|
if (!in_array($error_level, Config::$ERROR_LEVELS, true)) {
|
2021-12-03 20:29:06 +01:00
|
|
|
throw new ConfigException('Unexpected error level ' . $error_level);
|
2016-12-30 02:07:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->error_level = $error_level;
|
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function getReportingLevelForFile(string $file_path): string
|
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
|
|
|
|
2020-09-13 22:39:06 +02:00
|
|
|
public function getReportingLevelForClass(string $fq_classlike_name): ?string
|
2018-03-21 03:36:03 +01:00
|
|
|
{
|
|
|
|
foreach ($this->custom_levels as $custom_level) {
|
|
|
|
if ($custom_level->allowsClass($fq_classlike_name)) {
|
|
|
|
return $custom_level->getErrorLevel();
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
return null;
|
2018-03-21 03:36:03 +01:00
|
|
|
}
|
|
|
|
|
2020-09-13 22:39:06 +02:00
|
|
|
public function getReportingLevelForMethod(string $method_id): ?string
|
2018-03-21 03:36:03 +01:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
return null;
|
2018-03-21 03:36:03 +01:00
|
|
|
}
|
2018-05-11 06:07:41 +02:00
|
|
|
|
2020-09-13 22:39:06 +02:00
|
|
|
public function getReportingLevelForFunction(string $function_id): ?string
|
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();
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
return null;
|
2019-04-26 00:02:19 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 22:39:06 +02:00
|
|
|
public function getReportingLevelForArgument(string $function_id): ?string
|
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();
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
return null;
|
2019-04-26 00:02:19 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 22:39:06 +02:00
|
|
|
public function getReportingLevelForProperty(string $property_id): ?string
|
2018-05-11 06:07:41 +02:00
|
|
|
{
|
|
|
|
foreach ($this->custom_levels as $custom_level) {
|
|
|
|
if ($custom_level->allowsProperty($property_id)) {
|
|
|
|
return $custom_level->getErrorLevel();
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
return null;
|
2018-05-11 06:07:41 +02:00
|
|
|
}
|
2019-05-03 21:29:44 +02:00
|
|
|
|
2022-01-20 23:51:55 +01:00
|
|
|
public function getReportingLevelForClassConstant(string $constant_id): ?string
|
|
|
|
{
|
|
|
|
foreach ($this->custom_levels as $custom_level) {
|
|
|
|
if ($custom_level->allowsClassConstant($constant_id)) {
|
|
|
|
return $custom_level->getErrorLevel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-13 22:39:06 +02:00
|
|
|
public function getReportingLevelForVariable(string $var_name): ?string
|
2020-07-16 15:49:59 +02:00
|
|
|
{
|
|
|
|
foreach ($this->custom_levels as $custom_level) {
|
|
|
|
if ($custom_level->allowsVariable($var_name)) {
|
|
|
|
return $custom_level->getErrorLevel();
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 22:39:06 +02:00
|
|
|
|
|
|
|
return null;
|
2020-07-16 15:49:59 +02:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:29:44 +02:00
|
|
|
/**
|
2020-10-17 18:36:44 +02:00
|
|
|
* @return array<int, string>
|
2019-05-03 21:29:44 +02:00
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getAllIssueTypes(): array
|
2019-05-03 21:29:44 +02:00
|
|
|
{
|
|
|
|
return array_filter(
|
|
|
|
array_map(
|
2022-01-05 23:45:11 +01:00
|
|
|
fn(string $file_name): string => substr($file_name, 0, -4),
|
2020-05-30 23:02:35 +02:00
|
|
|
scandir(dirname(__DIR__) . '/Issue', SCANDIR_SORT_NONE)
|
2019-05-03 21:29:44 +02:00
|
|
|
),
|
2022-01-05 23:45:11 +01:00
|
|
|
fn(string $issue_name): bool => $issue_name !== ''
|
|
|
|
&& $issue_name !== 'MethodIssue'
|
|
|
|
&& $issue_name !== 'PropertyIssue'
|
2022-01-20 23:51:55 +01:00
|
|
|
&& $issue_name !== 'ClassConstantIssue'
|
2022-01-05 23:45:11 +01:00
|
|
|
&& $issue_name !== 'FunctionIssue'
|
|
|
|
&& $issue_name !== 'ArgumentIssue'
|
|
|
|
&& $issue_name !== 'VariableIssue'
|
|
|
|
&& $issue_name !== 'ClassIssue'
|
|
|
|
&& $issue_name !== 'CodeIssue'
|
|
|
|
&& $issue_name !== 'PsalmInternalError'
|
|
|
|
&& $issue_name !== 'ParseError'
|
|
|
|
&& $issue_name !== 'PluginIssue'
|
|
|
|
&& $issue_name !== 'MixedIssue'
|
|
|
|
&& $issue_name !== 'MixedIssueTrait'
|
2019-05-03 21:29:44 +02:00
|
|
|
);
|
|
|
|
}
|
2016-12-30 02:07:42 +01:00
|
|
|
}
|