2016-06-06 02:25:16 +02:00
|
|
|
<?php
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm;
|
2016-06-06 02:25:16 +02:00
|
|
|
|
2020-02-17 00:24:40 +01:00
|
|
|
use Psalm\Internal\Analyzer\IssueData;
|
2018-11-06 03:57:36 +01:00
|
|
|
use Psalm\Internal\Analyzer\ProjectAnalyzer;
|
2021-03-11 06:14:22 +01:00
|
|
|
use Psalm\Internal\ExecutionEnvironment\BuildInfoCollector;
|
2017-03-13 23:06:56 +01:00
|
|
|
use Psalm\Issue\CodeIssue;
|
2019-08-18 20:27:50 +02:00
|
|
|
use Psalm\Issue\UnusedPsalmSuppress;
|
2021-03-17 00:28:18 +01:00
|
|
|
use Psalm\Plugin\EventHandler\Event\AfterAnalysisEvent;
|
2019-06-09 18:37:28 +02:00
|
|
|
use Psalm\Report\CheckstyleReport;
|
2020-11-23 21:34:51 +01:00
|
|
|
use Psalm\Report\CodeClimateReport;
|
2019-06-09 18:37:28 +02:00
|
|
|
use Psalm\Report\CompactReport;
|
|
|
|
use Psalm\Report\ConsoleReport;
|
|
|
|
use Psalm\Report\EmacsReport;
|
2020-01-23 13:14:48 +01:00
|
|
|
use Psalm\Report\GithubActionsReport;
|
2019-06-09 18:37:28 +02:00
|
|
|
use Psalm\Report\JsonReport;
|
|
|
|
use Psalm\Report\JsonSummaryReport;
|
2019-12-19 21:18:09 +01:00
|
|
|
use Psalm\Report\JunitReport;
|
2021-03-17 00:28:18 +01:00
|
|
|
use Psalm\Report\PhpStormReport;
|
2019-06-09 18:37:28 +02:00
|
|
|
use Psalm\Report\PylintReport;
|
2021-03-17 00:28:18 +01:00
|
|
|
use Psalm\Report\SarifReport;
|
2019-06-18 03:17:09 +02:00
|
|
|
use Psalm\Report\SonarqubeReport;
|
2019-06-09 18:37:28 +02:00
|
|
|
use Psalm\Report\TextReport;
|
|
|
|
use Psalm\Report\XmlReport;
|
2021-03-17 00:28:18 +01:00
|
|
|
|
|
|
|
use function array_merge;
|
|
|
|
use function array_pop;
|
|
|
|
use function array_search;
|
|
|
|
use function array_splice;
|
|
|
|
use function array_values;
|
|
|
|
use function count;
|
|
|
|
use function debug_print_backtrace;
|
|
|
|
use function dirname;
|
|
|
|
use function explode;
|
|
|
|
use function file_put_contents;
|
|
|
|
use function fwrite;
|
|
|
|
use function get_class;
|
|
|
|
use function in_array;
|
|
|
|
use function is_dir;
|
|
|
|
use function memory_get_peak_usage;
|
|
|
|
use function microtime;
|
|
|
|
use function mkdir;
|
|
|
|
use function number_format;
|
|
|
|
use function ob_get_clean;
|
|
|
|
use function ob_start;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function sha1;
|
2021-03-17 00:28:18 +01:00
|
|
|
use function sprintf;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function str_repeat;
|
|
|
|
use function str_replace;
|
|
|
|
use function usort;
|
2021-03-17 00:28:18 +01:00
|
|
|
|
2020-06-23 00:16:47 +02:00
|
|
|
use const DEBUG_BACKTRACE_IGNORE_ARGS;
|
|
|
|
use const STDERR;
|
2016-08-13 20:20:46 +02:00
|
|
|
|
2016-06-26 21:18:40 +02:00
|
|
|
class IssueBuffer
|
2016-06-06 02:25:16 +02:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
/**
|
2020-02-17 00:24:40 +01:00
|
|
|
* @var array<string, list<IssueData>>
|
2017-07-25 22:11:02 +02:00
|
|
|
*/
|
|
|
|
protected static $issues_data = [];
|
|
|
|
|
2016-12-08 04:38:57 +01:00
|
|
|
/**
|
|
|
|
* @var array<int, array>
|
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
protected static $console_issues = [];
|
2016-12-08 04:38:57 +01:00
|
|
|
|
2019-12-02 21:24:01 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, int>
|
|
|
|
*/
|
|
|
|
protected static $fixable_issue_counts = [];
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-01-14 07:24:27 +01:00
|
|
|
* @var int
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2017-01-14 07:24:27 +01:00
|
|
|
protected static $error_count = 0;
|
2016-11-01 05:39:41 +01:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
2016-10-19 00:55:53 +02:00
|
|
|
protected static $emitted = [];
|
2016-06-21 01:30:38 +02:00
|
|
|
|
2017-03-13 23:06:56 +01:00
|
|
|
/** @var int */
|
|
|
|
protected static $recording_level = 0;
|
|
|
|
|
|
|
|
/** @var array<int, array<int, CodeIssue>> */
|
|
|
|
protected static $recorded_issues = [];
|
|
|
|
|
2019-08-18 20:27:50 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, array<int, int>>
|
|
|
|
*/
|
|
|
|
protected static $unused_suppressions = [];
|
|
|
|
|
2019-08-18 21:34:32 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, array<int, bool>>
|
|
|
|
*/
|
2019-09-16 17:58:42 +02:00
|
|
|
protected static $used_suppressions = [];
|
2019-08-18 21:34:32 +02:00
|
|
|
|
2021-03-17 00:28:18 +01:00
|
|
|
/** @var array<array-key,mixed> */
|
|
|
|
private static $server = [];
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2019-06-16 15:12:32 +02:00
|
|
|
* @param string[] $suppressed_issues
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function accepts(CodeIssue $e, array $suppressed_issues = [], bool $is_fixable = false): bool
|
2019-04-17 17:12:18 +02:00
|
|
|
{
|
|
|
|
if (self::isSuppressed($e, $suppressed_issues)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-02 21:24:01 +01:00
|
|
|
return self::add($e, $is_fixable);
|
2019-04-17 17:12:18 +02:00
|
|
|
}
|
|
|
|
|
2019-08-18 20:27:50 +02:00
|
|
|
public static function addUnusedSuppression(string $file_path, int $offset, string $issue_type) : void
|
|
|
|
{
|
2020-11-17 18:44:31 +01:00
|
|
|
if (\substr($issue_type, 0, 7) === 'Tainted') {
|
2020-07-02 05:23:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-16 17:58:42 +02:00
|
|
|
if (isset(self::$used_suppressions[$file_path][$offset])) {
|
2019-08-18 21:34:32 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-18 20:27:50 +02:00
|
|
|
if (!isset(self::$unused_suppressions[$file_path])) {
|
|
|
|
self::$unused_suppressions[$file_path] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$unused_suppressions[$file_path][$offset] = $offset + \strlen($issue_type) - 1;
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:12:18 +02:00
|
|
|
/**
|
2019-06-16 15:12:32 +02:00
|
|
|
* @param string[] $suppressed_issues
|
2019-04-17 17:12:18 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function isSuppressed(CodeIssue $e, array $suppressed_issues = []) : bool
|
2016-06-06 02:25:16 +02:00
|
|
|
{
|
2016-06-10 00:08:25 +02:00
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-07-26 21:00:40 +02:00
|
|
|
$fqcn_parts = explode('\\', get_class($e));
|
|
|
|
$issue_type = array_pop($fqcn_parts);
|
2019-08-18 20:27:50 +02:00
|
|
|
$file_path = $e->getFilePath();
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2019-08-18 20:27:50 +02:00
|
|
|
if (!$config->reportIssueInFile($issue_type, $file_path)) {
|
2019-04-17 17:12:18 +02:00
|
|
|
return true;
|
2016-06-10 00:08:25 +02:00
|
|
|
}
|
|
|
|
|
2019-08-18 18:25:48 +02:00
|
|
|
$suppressed_issue_position = array_search($issue_type, $suppressed_issues);
|
|
|
|
|
|
|
|
if ($suppressed_issue_position !== false) {
|
2019-08-18 21:34:32 +02:00
|
|
|
if (\is_int($suppressed_issue_position)) {
|
2019-09-16 17:58:42 +02:00
|
|
|
self::$used_suppressions[$file_path][$suppressed_issue_position] = true;
|
2019-08-18 21:34:32 +02:00
|
|
|
}
|
|
|
|
|
2019-08-18 18:25:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parent_issue_type = Config::getParentIssueType($issue_type);
|
|
|
|
|
|
|
|
if ($parent_issue_type) {
|
|
|
|
$suppressed_issue_position = array_search($parent_issue_type, $suppressed_issues);
|
|
|
|
|
|
|
|
if ($suppressed_issue_position !== false) {
|
2019-08-18 21:34:32 +02:00
|
|
|
if (\is_int($suppressed_issue_position)) {
|
2019-09-16 17:58:42 +02:00
|
|
|
self::$used_suppressions[$file_path][$suppressed_issue_position] = true;
|
2019-08-18 21:34:32 +02:00
|
|
|
}
|
|
|
|
|
2019-08-18 18:25:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 16:38:32 +01:00
|
|
|
$suppress_all_position = array_search('all', $suppressed_issues);
|
|
|
|
|
|
|
|
if ($suppress_all_position !== false) {
|
|
|
|
if (\is_int($suppress_all_position)) {
|
|
|
|
self::$used_suppressions[$file_path][$suppress_all_position] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-08-18 18:25:48 +02:00
|
|
|
$reporting_level = $config->getReportingLevelForIssue($e);
|
2018-03-21 03:36:03 +01:00
|
|
|
|
2019-05-03 23:12:20 +02:00
|
|
|
if ($reporting_level === Config::REPORT_SUPPRESS) {
|
2019-04-17 17:12:18 +02:00
|
|
|
return true;
|
2018-05-11 06:07:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-11 04:44:35 +02:00
|
|
|
if ($e->code_location->getLineNumber() === -1) {
|
2019-04-17 17:12:18 +02:00
|
|
|
return true;
|
2019-02-10 21:01:10 +01:00
|
|
|
}
|
2018-03-18 21:39:34 +01:00
|
|
|
|
2017-03-13 23:06:56 +01:00
|
|
|
if (self::$recording_level > 0) {
|
|
|
|
self::$recorded_issues[self::$recording_level][] = $e;
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2019-04-17 17:12:18 +02:00
|
|
|
return true;
|
2017-03-13 23:06:56 +01:00
|
|
|
}
|
|
|
|
|
2019-04-17 17:12:18 +02:00
|
|
|
return false;
|
2016-06-27 19:22:16 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
|
|
|
* @throws Exception\CodeException
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function add(CodeIssue $e, bool $is_fixable = false): bool
|
2016-06-27 19:22:16 +02:00
|
|
|
{
|
|
|
|
$config = Config::getInstance();
|
|
|
|
|
2016-07-26 21:00:40 +02:00
|
|
|
$fqcn_parts = explode('\\', get_class($e));
|
|
|
|
$issue_type = array_pop($fqcn_parts);
|
2016-06-17 23:34:52 +02:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
$project_analyzer = ProjectAnalyzer::getInstance();
|
2018-01-06 01:49:27 +01:00
|
|
|
|
2018-11-11 18:01:14 +01:00
|
|
|
if (!$project_analyzer->show_issues) {
|
2018-01-06 01:49:27 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-17 18:44:31 +01:00
|
|
|
$is_tainted = \substr($issue_type, 0, 7) === 'Tainted';
|
|
|
|
|
|
|
|
if ($project_analyzer->getCodebase()->taint_flow_graph && !$is_tainted) {
|
2019-10-09 20:26:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-03 23:12:20 +02:00
|
|
|
$reporting_level = $config->getReportingLevelForIssue($e);
|
2018-03-18 22:26:28 +01:00
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
if ($reporting_level === Config::REPORT_SUPPRESS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-23 00:16:47 +02:00
|
|
|
if ($config->debug_emitted_issues) {
|
|
|
|
ob_start();
|
|
|
|
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
|
|
|
$trace = ob_get_clean();
|
2020-09-11 04:44:35 +02:00
|
|
|
fwrite(STDERR, "\nEmitting {$e->getShortLocation()} $issue_type {$e->message}\n$trace\n");
|
2020-06-23 00:16:47 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 23:41:45 +02:00
|
|
|
$emitted_key = $issue_type
|
|
|
|
. '-' . $e->getShortLocation()
|
2020-09-11 04:44:35 +02:00
|
|
|
. ':' . $e->code_location->getColumn()
|
|
|
|
. ' ' . $e->dupe_key;
|
2019-02-13 21:10:50 +01:00
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
if ($reporting_level === Config::REPORT_INFO) {
|
2020-11-17 18:44:31 +01:00
|
|
|
if ($is_tainted || !self::alreadyEmitted($emitted_key)) {
|
2020-02-17 00:24:40 +01:00
|
|
|
self::$issues_data[$e->getFilePath()][] = $e->toIssueData(Config::REPORT_INFO);
|
2020-10-15 17:41:09 +02:00
|
|
|
|
|
|
|
if ($is_fixable) {
|
|
|
|
self::addFixableIssue($issue_type);
|
|
|
|
}
|
2016-12-04 01:11:30 +01:00
|
|
|
}
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
return false;
|
2016-06-10 20:47:44 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 04:40:57 +02:00
|
|
|
if ($config->throw_exception) {
|
2019-05-17 00:36:36 +02:00
|
|
|
\Psalm\Internal\Analyzer\FileAnalyzer::clearCache();
|
|
|
|
|
2020-06-30 19:17:51 +02:00
|
|
|
$message = $e instanceof \Psalm\Issue\TaintedInput
|
|
|
|
? $e->getJourneyMessage()
|
2021-03-21 02:44:04 +01:00
|
|
|
: ($e instanceof \Psalm\Issue\MixedIssue
|
|
|
|
? $e->getMixedOriginMessage()
|
2021-03-17 06:10:42 +01:00
|
|
|
: $e->message);
|
2020-06-30 19:17:51 +02:00
|
|
|
|
2019-02-27 22:00:44 +01:00
|
|
|
throw new Exception\CodeException(
|
2019-02-27 22:16:19 +01:00
|
|
|
$issue_type
|
2019-05-27 16:07:56 +02:00
|
|
|
. ' - ' . $e->getShortLocationWithPrevious()
|
2020-09-11 04:44:35 +02:00
|
|
|
. ':' . $e->code_location->getColumn()
|
2020-06-30 19:17:51 +02:00
|
|
|
. ' - ' . $message
|
2019-02-27 22:00:44 +01:00
|
|
|
);
|
2016-06-27 04:40:57 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 18:44:31 +01:00
|
|
|
if ($is_tainted || !self::alreadyEmitted($emitted_key)) {
|
2018-09-26 00:37:24 +02:00
|
|
|
++self::$error_count;
|
2020-02-17 00:24:40 +01:00
|
|
|
self::$issues_data[$e->getFilePath()][] = $e->toIssueData(Config::REPORT_ERROR);
|
2016-06-17 01:02:29 +02:00
|
|
|
|
2020-10-15 17:41:09 +02:00
|
|
|
if ($is_fixable) {
|
|
|
|
self::addFixableIssue($issue_type);
|
|
|
|
}
|
2019-12-02 21:24:01 +01:00
|
|
|
}
|
|
|
|
|
2016-08-08 20:36:18 +02:00
|
|
|
return true;
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|
2016-06-21 01:30:38 +02:00
|
|
|
|
2020-02-09 05:01:45 +01:00
|
|
|
public static function remove(string $file_path, string $issue_type, int $file_offset) : void
|
|
|
|
{
|
|
|
|
if (!isset(self::$issues_data[$file_path])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filtered_issues = [];
|
|
|
|
|
|
|
|
foreach (self::$issues_data[$file_path] as $issue) {
|
2020-02-17 00:24:40 +01:00
|
|
|
if ($issue->type !== $issue_type || $issue->from !== $file_offset) {
|
2020-02-09 05:01:45 +01:00
|
|
|
$filtered_issues[] = $issue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 04:52:36 +02:00
|
|
|
if (empty($filtered_issues)) {
|
|
|
|
unset(self::$issues_data[$file_path]);
|
|
|
|
} else {
|
|
|
|
self::$issues_data[$file_path] = $filtered_issues;
|
|
|
|
}
|
2020-02-09 05:01:45 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 21:24:01 +01:00
|
|
|
public static function addFixableIssue(string $issue_type) : void
|
|
|
|
{
|
|
|
|
if (isset(self::$fixable_issue_counts[$issue_type])) {
|
|
|
|
self::$fixable_issue_counts[$issue_type]++;
|
|
|
|
} else {
|
|
|
|
self::$fixable_issue_counts[$issue_type] = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 04:38:57 +01:00
|
|
|
/**
|
2020-02-17 00:24:40 +01:00
|
|
|
* @return array<string, list<IssueData>>
|
2016-12-08 04:38:57 +01:00
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getIssuesData(): array
|
2016-12-08 04:38:57 +01:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
return self::$issues_data;
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|
|
|
|
|
2020-01-22 03:07:44 +01:00
|
|
|
/**
|
2020-02-17 00:24:40 +01:00
|
|
|
* @return list<IssueData>
|
2020-01-22 03:07:44 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public static function getIssuesDataForFile(string $file_path): array
|
2020-01-22 03:07:44 +01:00
|
|
|
{
|
|
|
|
return self::$issues_data[$file_path] ?? [];
|
|
|
|
}
|
|
|
|
|
2019-12-02 21:24:01 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, int>
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getFixableIssues(): array
|
2019-12-02 21:24:01 +01:00
|
|
|
{
|
|
|
|
return self::$fixable_issue_counts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, int> $fixable_issue_counts
|
|
|
|
*/
|
|
|
|
public static function addFixableIssues(array $fixable_issue_counts) : void
|
|
|
|
{
|
|
|
|
foreach ($fixable_issue_counts as $issue_type => $count) {
|
|
|
|
if (isset(self::$fixable_issue_counts[$issue_type])) {
|
|
|
|
self::$fixable_issue_counts[$issue_type] += $count;
|
|
|
|
} else {
|
|
|
|
self::$fixable_issue_counts[$issue_type] = $count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 20:27:50 +02:00
|
|
|
/**
|
|
|
|
* @return array<string, array<int, int>>
|
|
|
|
*/
|
|
|
|
public static function getUnusedSuppressions() : array
|
|
|
|
{
|
|
|
|
return self::$unused_suppressions;
|
|
|
|
}
|
|
|
|
|
2019-09-16 17:58:42 +02:00
|
|
|
/**
|
|
|
|
* @return array<string, array<int, bool>>
|
|
|
|
*/
|
|
|
|
public static function getUsedSuppressions() : array
|
|
|
|
{
|
|
|
|
return self::$used_suppressions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, array<int, int>> $unused_suppressions
|
|
|
|
*/
|
2019-08-18 20:27:50 +02:00
|
|
|
public static function addUnusedSuppressions(array $unused_suppressions) : void
|
|
|
|
{
|
|
|
|
self::$unused_suppressions += $unused_suppressions;
|
|
|
|
}
|
|
|
|
|
2019-09-16 17:58:42 +02:00
|
|
|
/**
|
|
|
|
* @param array<string, array<int, bool>> $used_suppressions
|
|
|
|
*/
|
|
|
|
public static function addUsedSuppressions(array $used_suppressions) : void
|
|
|
|
{
|
|
|
|
foreach ($used_suppressions as $file => $offsets) {
|
|
|
|
if (!isset(self::$used_suppressions[$file])) {
|
|
|
|
self::$used_suppressions[$file] = $offsets;
|
|
|
|
} else {
|
|
|
|
self::$used_suppressions[$file] += $offsets;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 20:27:50 +02:00
|
|
|
public static function processUnusedSuppressions(\Psalm\Internal\Provider\FileProvider $file_provider) : void
|
|
|
|
{
|
|
|
|
$config = Config::getInstance();
|
|
|
|
|
|
|
|
foreach (self::$unused_suppressions as $file_path => $offsets) {
|
|
|
|
if (!$offsets) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_contents = $file_provider->getContents($file_path);
|
|
|
|
|
|
|
|
foreach ($offsets as $start => $end) {
|
2019-09-16 17:58:42 +02:00
|
|
|
if (isset(self::$used_suppressions[$file_path][$start])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-18 20:27:50 +02:00
|
|
|
self::add(
|
|
|
|
new UnusedPsalmSuppress(
|
|
|
|
'This suppression is never used',
|
|
|
|
new CodeLocation\Raw(
|
|
|
|
$file_contents,
|
|
|
|
$file_path,
|
|
|
|
$config->shortenFileName($file_path),
|
|
|
|
$start,
|
|
|
|
$end
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getErrorCount(): int
|
2018-09-26 00:37:24 +02:00
|
|
|
{
|
|
|
|
return self::$error_count;
|
|
|
|
}
|
|
|
|
|
2016-12-08 04:38:57 +01:00
|
|
|
/**
|
2020-02-17 00:24:40 +01:00
|
|
|
* @param array<string, list<IssueData>> $issues_data
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-12-08 04:38:57 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public static function addIssues(array $issues_data): void
|
2016-12-08 04:38:57 +01:00
|
|
|
{
|
2020-01-22 03:07:44 +01:00
|
|
|
foreach ($issues_data as $file_path => $file_issues) {
|
|
|
|
foreach ($file_issues as $issue) {
|
2020-02-17 00:24:40 +01:00
|
|
|
$emitted_key = $issue->type
|
|
|
|
. '-' . $issue->file_name
|
|
|
|
. ':' . $issue->line_from
|
2020-09-10 23:41:45 +02:00
|
|
|
. ':' . $issue->column_from
|
2020-10-13 00:39:11 +02:00
|
|
|
. ' ' . $issue->dupe_key;
|
2020-01-22 03:07:44 +01:00
|
|
|
|
|
|
|
if (!self::alreadyEmitted($emitted_key)) {
|
|
|
|
self::$issues_data[$file_path][] = $issue;
|
|
|
|
}
|
2018-11-01 18:22:38 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2018-11-09 06:46:13 +01:00
|
|
|
* @param array<string,array<string,array{o:int, s:array<int, string>}>> $issue_baseline
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2018-01-31 23:09:09 +01:00
|
|
|
public static function finish(
|
2018-11-11 18:01:14 +01:00
|
|
|
ProjectAnalyzer $project_analyzer,
|
2018-10-30 15:32:20 +01:00
|
|
|
bool $is_full,
|
|
|
|
float $start_time,
|
|
|
|
bool $add_stats = false,
|
|
|
|
array $issue_baseline = []
|
2020-09-12 17:24:05 +02:00
|
|
|
): void {
|
2019-06-09 18:37:28 +02:00
|
|
|
if (!$project_analyzer->stdout_report_options) {
|
|
|
|
throw new \UnexpectedValueException('Cannot finish without stdout report options');
|
|
|
|
}
|
|
|
|
|
2018-11-11 18:19:53 +01:00
|
|
|
$codebase = $project_analyzer->getCodebase();
|
|
|
|
|
2018-03-18 23:04:50 +01:00
|
|
|
$error_count = 0;
|
2018-03-18 23:27:10 +01:00
|
|
|
$info_count = 0;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2020-04-01 05:38:58 +02:00
|
|
|
$issues_data = [];
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (self::$issues_data) {
|
2020-09-04 22:24:14 +02:00
|
|
|
if (in_array(
|
|
|
|
$project_analyzer->stdout_report_options->format,
|
|
|
|
[\Psalm\Report::TYPE_CONSOLE, \Psalm\Report::TYPE_PHP_STORM]
|
|
|
|
)) {
|
2020-05-03 14:44:14 +02:00
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
|
2020-01-22 03:07:44 +01:00
|
|
|
\ksort(self::$issues_data);
|
|
|
|
|
2020-01-29 15:00:13 +01:00
|
|
|
foreach (self::$issues_data as $file_path => $file_issues) {
|
2020-01-22 03:07:44 +01:00
|
|
|
usort(
|
|
|
|
$file_issues,
|
2020-02-17 00:24:40 +01:00
|
|
|
function (IssueData $d1, IssueData $d2) : int {
|
|
|
|
if ($d1->file_path === $d2->file_path) {
|
|
|
|
if ($d1->line_from === $d2->line_from) {
|
|
|
|
if ($d1->column_from === $d2->column_from) {
|
2020-01-22 03:07:44 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-17 00:24:40 +01:00
|
|
|
return $d1->column_from > $d2->column_from ? 1 : -1;
|
2017-12-10 17:22:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 00:24:40 +01:00
|
|
|
return $d1->line_from > $d2->line_from ? 1 : -1;
|
2017-12-10 17:22:36 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 00:24:40 +01:00
|
|
|
return $d1->file_path > $d2->file_path ? 1 : -1;
|
2017-12-10 17:22:36 +01:00
|
|
|
}
|
2020-01-22 03:07:44 +01:00
|
|
|
);
|
2020-01-29 15:00:13 +01:00
|
|
|
self::$issues_data[$file_path] = $file_issues;
|
2020-01-22 03:07:44 +01:00
|
|
|
}
|
2017-12-10 17:22:36 +01:00
|
|
|
|
2020-04-01 05:38:58 +02:00
|
|
|
// make a copy so what gets saved in cache is unaffected by baseline
|
|
|
|
$issues_data = self::$issues_data;
|
|
|
|
|
2018-10-30 15:32:20 +01:00
|
|
|
if (!empty($issue_baseline)) {
|
|
|
|
// Set severity for issues in baseline to INFO
|
2020-04-01 05:38:58 +02:00
|
|
|
foreach ($issues_data as $file_path => $file_issues) {
|
2020-01-22 03:07:44 +01:00
|
|
|
foreach ($file_issues as $key => $issue_data) {
|
2020-02-17 00:24:40 +01:00
|
|
|
$file = $issue_data->file_name;
|
2020-01-22 03:07:44 +01:00
|
|
|
$file = str_replace('\\', '/', $file);
|
2020-02-17 00:24:40 +01:00
|
|
|
$type = $issue_data->type;
|
2020-01-22 03:07:44 +01:00
|
|
|
|
|
|
|
if (isset($issue_baseline[$file][$type]) && $issue_baseline[$file][$type]['o'] > 0) {
|
|
|
|
if ($issue_baseline[$file][$type]['o'] === count($issue_baseline[$file][$type]['s'])) {
|
|
|
|
$position = array_search(
|
2020-02-17 00:24:40 +01:00
|
|
|
$issue_data->selected_text,
|
2020-01-22 03:07:44 +01:00
|
|
|
$issue_baseline[$file][$type]['s'],
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($position !== false) {
|
2020-02-17 00:24:40 +01:00
|
|
|
$issue_data->severity = Config::REPORT_INFO;
|
2020-01-22 03:07:44 +01:00
|
|
|
array_splice($issue_baseline[$file][$type]['s'], $position, 1);
|
|
|
|
$issue_baseline[$file][$type]['o'] = $issue_baseline[$file][$type]['o'] - 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$issue_baseline[$file][$type]['s'] = [];
|
2020-02-17 00:24:40 +01:00
|
|
|
$issue_data->severity = Config::REPORT_INFO;
|
2018-11-09 06:46:13 +01:00
|
|
|
$issue_baseline[$file][$type]['o'] = $issue_baseline[$file][$type]['o'] - 1;
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 15:32:20 +01:00
|
|
|
|
2020-04-01 05:38:58 +02:00
|
|
|
$issues_data[$file_path][$key] = $issue_data;
|
2020-01-22 03:07:44 +01:00
|
|
|
}
|
2018-10-30 15:32:20 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-22 03:07:44 +01:00
|
|
|
}
|
|
|
|
|
2020-05-03 14:44:14 +02:00
|
|
|
echo self::getOutput(
|
|
|
|
$issues_data,
|
|
|
|
$project_analyzer->stdout_report_options,
|
|
|
|
$codebase->analyzer->getTotalTypeCoverage($codebase)
|
|
|
|
);
|
|
|
|
|
2020-04-01 05:38:58 +02:00
|
|
|
foreach ($issues_data as $file_issues) {
|
2020-01-22 03:07:44 +01:00
|
|
|
foreach ($file_issues as $issue_data) {
|
2020-02-17 00:24:40 +01:00
|
|
|
if ($issue_data->severity === Config::REPORT_ERROR) {
|
2018-03-18 23:04:50 +01:00
|
|
|
++$error_count;
|
2018-03-18 23:27:10 +01:00
|
|
|
} else {
|
|
|
|
++$info_count;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2017-09-08 17:18:48 +02:00
|
|
|
}
|
2017-09-27 00:18:24 +02:00
|
|
|
}
|
2018-03-18 23:27:10 +01:00
|
|
|
|
2019-03-23 17:47:46 +01:00
|
|
|
|
2021-01-14 22:44:20 +01:00
|
|
|
if ($codebase->config->eventDispatcher->after_analysis
|
|
|
|
|| $codebase->config->eventDispatcher->legacy_after_analysis
|
|
|
|
) {
|
2021-03-17 00:28:18 +01:00
|
|
|
$source_control_info = null;
|
|
|
|
$build_info = (new BuildInfoCollector(self::$server))->collect();
|
|
|
|
|
2021-01-14 22:44:20 +01:00
|
|
|
try {
|
|
|
|
$source_control_info = (new \Psalm\Internal\ExecutionEnvironment\GitInfoCollector())->collect();
|
|
|
|
} catch (\RuntimeException $e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
2019-03-23 17:47:46 +01:00
|
|
|
|
2021-01-14 22:44:20 +01:00
|
|
|
/** @psalm-suppress ArgumentTypeCoercion due to Psalm bug */
|
|
|
|
$event = new AfterAnalysisEvent(
|
|
|
|
$codebase,
|
|
|
|
$issues_data,
|
|
|
|
$build_info,
|
|
|
|
$source_control_info
|
|
|
|
);
|
2019-03-23 17:47:46 +01:00
|
|
|
|
2021-01-14 22:44:20 +01:00
|
|
|
$codebase->config->eventDispatcher->dispatchAfterAnalysis($event);
|
|
|
|
}
|
2019-03-23 17:47:46 +01:00
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
foreach ($project_analyzer->generated_report_options as $report_options) {
|
|
|
|
if (!$report_options->output_path) {
|
|
|
|
throw new \UnexpectedValueException('Output path should not be null here');
|
|
|
|
}
|
|
|
|
|
2020-10-17 15:01:11 +02:00
|
|
|
$folder = dirname($report_options->output_path);
|
|
|
|
if (!is_dir($folder) && !mkdir($folder, 0777, true) && !is_dir($folder)) {
|
|
|
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $folder));
|
|
|
|
}
|
2017-09-27 00:18:24 +02:00
|
|
|
file_put_contents(
|
2019-06-09 18:37:28 +02:00
|
|
|
$report_options->output_path,
|
2019-05-11 00:07:13 +02:00
|
|
|
self::getOutput(
|
2020-04-01 05:38:58 +02:00
|
|
|
$issues_data,
|
2019-06-09 18:37:28 +02:00
|
|
|
$report_options,
|
2019-05-11 00:07:13 +02:00
|
|
|
$codebase->analyzer->getTotalTypeCoverage($codebase)
|
|
|
|
)
|
2017-09-27 00:18:24 +02:00
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:24:14 +02:00
|
|
|
if (in_array(
|
|
|
|
$project_analyzer->stdout_report_options->format,
|
|
|
|
[\Psalm\Report::TYPE_CONSOLE, \Psalm\Report::TYPE_PHP_STORM]
|
|
|
|
)) {
|
2018-04-16 20:05:37 +02:00
|
|
|
echo str_repeat('-', 30) . "\n";
|
2018-03-18 23:04:50 +01:00
|
|
|
|
2018-04-16 20:05:37 +02:00
|
|
|
if ($error_count) {
|
2019-07-05 22:24:00 +02:00
|
|
|
echo($project_analyzer->stdout_report_options->use_color
|
2018-04-16 20:05:37 +02:00
|
|
|
? "\e[0;31m" . $error_count . " errors\e[0m"
|
|
|
|
: $error_count . ' errors'
|
|
|
|
) . ' found' . "\n";
|
|
|
|
} else {
|
|
|
|
echo 'No errors found!' . "\n";
|
|
|
|
}
|
2018-03-18 23:27:10 +01:00
|
|
|
|
2019-12-02 21:24:01 +01:00
|
|
|
$show_info = $project_analyzer->stdout_report_options->show_info;
|
|
|
|
$show_suggestions = $project_analyzer->stdout_report_options->show_suggestions;
|
|
|
|
|
|
|
|
if ($info_count && ($show_info || $show_suggestions)) {
|
|
|
|
echo str_repeat('-', 30) . "\n";
|
|
|
|
|
|
|
|
echo $info_count . ' other issues found.' . "\n";
|
|
|
|
|
2020-02-17 22:33:28 +01:00
|
|
|
if (!$show_info) {
|
|
|
|
echo 'You can display them with ' .
|
2019-12-02 21:24:01 +01:00
|
|
|
($project_analyzer->stdout_report_options->use_color
|
2020-02-17 22:33:28 +01:00
|
|
|
? "\e[30;48;5;195m--show-info=true\e[0m"
|
|
|
|
: '--show-info=true') . "\n";
|
2019-12-02 21:24:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 06:37:40 +02:00
|
|
|
if (self::$fixable_issue_counts && $show_suggestions && !$codebase->taint_flow_graph) {
|
2018-04-16 20:05:37 +02:00
|
|
|
echo str_repeat('-', 30) . "\n";
|
2018-03-18 23:27:10 +01:00
|
|
|
|
2019-12-02 21:24:01 +01:00
|
|
|
$total_count = \array_sum(self::$fixable_issue_counts);
|
|
|
|
$command = '--alter --issues=' . \implode(',', \array_keys(self::$fixable_issue_counts));
|
|
|
|
$command .= ' --dry-run';
|
|
|
|
|
|
|
|
echo 'Psalm can automatically fix ' . $total_count
|
2020-02-17 22:33:28 +01:00
|
|
|
. ($show_info ? ' issues' : ' of these issues') . ".\n"
|
2019-12-02 21:24:01 +01:00
|
|
|
. 'Run Psalm again with ' . "\n"
|
|
|
|
. ($project_analyzer->stdout_report_options->use_color
|
|
|
|
? "\e[30;48;5;195m" . $command . "\e[0m"
|
|
|
|
: $command) . "\n"
|
|
|
|
. 'to see what it can fix.' . "\n";
|
2018-04-16 20:05:37 +02:00
|
|
|
}
|
2018-03-18 23:27:10 +01:00
|
|
|
|
2018-04-16 20:05:37 +02:00
|
|
|
echo str_repeat('-', 30) . "\n" . "\n";
|
2018-03-18 23:04:50 +01:00
|
|
|
|
2018-04-16 20:05:37 +02:00
|
|
|
if ($start_time) {
|
2018-10-10 22:05:06 +02:00
|
|
|
echo 'Checks took ' . number_format(microtime(true) - $start_time, 2) . ' seconds';
|
2018-04-16 20:05:37 +02:00
|
|
|
echo ' and used ' . number_format(memory_get_peak_usage() / (1024 * 1024), 3) . 'MB of memory' . "\n";
|
2018-01-31 22:08:52 +01:00
|
|
|
|
2019-03-23 14:50:47 +01:00
|
|
|
$analysis_summary = $codebase->analyzer->getTypeInferenceSummary($codebase);
|
|
|
|
echo $analysis_summary . "\n";
|
2018-01-31 23:09:09 +01:00
|
|
|
|
2018-04-16 20:05:37 +02:00
|
|
|
if ($add_stats) {
|
|
|
|
echo '-----------------' . "\n";
|
2018-11-06 03:57:36 +01:00
|
|
|
echo $codebase->analyzer->getNonMixedStats();
|
2018-04-16 20:05:37 +02:00
|
|
|
echo "\n";
|
|
|
|
}
|
2020-07-30 21:30:19 +02:00
|
|
|
|
|
|
|
if ($project_analyzer->debug_performance) {
|
|
|
|
echo '-----------------' . "\n";
|
|
|
|
echo 'Slow-to-analyze functions' . "\n";
|
|
|
|
echo '-----------------' . "\n\n";
|
|
|
|
|
|
|
|
$function_timings = $codebase->analyzer->getFunctionTimings();
|
|
|
|
|
|
|
|
\arsort($function_timings);
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
|
|
|
|
foreach ($function_timings as $function_id => $time) {
|
|
|
|
if (++$i > 10) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $function_id . ': ' . \round(1000 * $time, 2) . 'ms per node' . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "\n";
|
|
|
|
}
|
2018-01-31 22:08:52 +01:00
|
|
|
}
|
2016-12-15 01:24:16 +01:00
|
|
|
}
|
|
|
|
|
2016-11-06 05:59:29 +01:00
|
|
|
if ($is_full && $start_time) {
|
2018-11-11 18:19:53 +01:00
|
|
|
$codebase->file_reference_provider->removeDeletedFilesFromReferences();
|
2018-09-28 22:18:45 +02:00
|
|
|
|
2020-04-12 17:41:01 +02:00
|
|
|
if ($project_analyzer->project_cache_provider) {
|
2020-11-01 15:01:43 +01:00
|
|
|
$project_analyzer->project_cache_provider->processSuccessfulRun($start_time, \PSALM_VERSION);
|
2020-04-12 17:41:01 +02:00
|
|
|
}
|
|
|
|
|
2018-11-11 18:19:53 +01:00
|
|
|
if ($codebase->statements_provider->parser_cache_provider) {
|
2020-04-12 17:41:01 +02:00
|
|
|
$codebase->statements_provider->parser_cache_provider->processSuccessfulRun();
|
2018-09-28 22:18:45 +02:00
|
|
|
}
|
2016-10-07 06:58:08 +02:00
|
|
|
}
|
2020-03-26 05:46:39 +01:00
|
|
|
|
2020-11-18 04:49:25 +01:00
|
|
|
if ($error_count
|
|
|
|
&& !($codebase->taint_flow_graph
|
|
|
|
&& $project_analyzer->generated_report_options
|
|
|
|
&& isset($_SERVER['GITHUB_WORKFLOW']))
|
|
|
|
) {
|
2021-01-24 19:30:35 +01:00
|
|
|
exit(2);
|
2020-03-26 05:46:39 +01:00
|
|
|
}
|
2016-06-21 01:30:38 +02:00
|
|
|
}
|
2016-10-19 00:55:53 +02:00
|
|
|
|
2016-10-30 17:46:18 +01:00
|
|
|
/**
|
2020-04-01 05:38:58 +02:00
|
|
|
* @param array<string, array<int, IssueData>> $issues_data
|
2019-05-11 00:07:13 +02:00
|
|
|
* @param array{int, int} $mixed_counts
|
2017-09-08 17:18:48 +02:00
|
|
|
*
|
|
|
|
*/
|
2019-05-11 00:07:13 +02:00
|
|
|
public static function getOutput(
|
2020-04-01 05:38:58 +02:00
|
|
|
array $issues_data,
|
2019-06-09 18:37:28 +02:00
|
|
|
\Psalm\Report\ReportOptions $report_options,
|
2019-05-11 00:07:13 +02:00
|
|
|
array $mixed_counts = [0, 0]
|
2020-09-04 22:26:33 +02:00
|
|
|
): string {
|
2019-05-11 00:07:13 +02:00
|
|
|
$total_expression_count = $mixed_counts[0] + $mixed_counts[1];
|
|
|
|
$mixed_expression_count = $mixed_counts[0];
|
|
|
|
|
2020-04-01 05:38:58 +02:00
|
|
|
$normalized_data = $issues_data === [] ? [] : array_merge(...array_values($issues_data));
|
2020-01-22 03:07:44 +01:00
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
switch ($report_options->format) {
|
|
|
|
case Report::TYPE_COMPACT:
|
2020-01-22 03:07:44 +01:00
|
|
|
$output = new CompactReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
2018-12-09 21:47:20 +01:00
|
|
|
break;
|
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
case Report::TYPE_EMACS:
|
2020-01-22 03:07:44 +01:00
|
|
|
$output = new EmacsReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
2018-12-09 21:47:20 +01:00
|
|
|
break;
|
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
case Report::TYPE_TEXT:
|
2020-01-22 03:07:44 +01:00
|
|
|
$output = new TextReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
2018-12-18 19:13:21 +01:00
|
|
|
break;
|
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
case Report::TYPE_JSON:
|
2020-01-22 03:07:44 +01:00
|
|
|
$output = new JsonReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
2018-12-09 21:47:20 +01:00
|
|
|
break;
|
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
case Report::TYPE_JSON_SUMMARY:
|
|
|
|
$output = new JsonSummaryReport(
|
2020-01-22 03:07:44 +01:00
|
|
|
$normalized_data,
|
2019-12-02 21:24:01 +01:00
|
|
|
self::$fixable_issue_counts,
|
2019-06-09 18:37:28 +02:00
|
|
|
$report_options,
|
2019-05-11 00:07:13 +02:00
|
|
|
$mixed_expression_count,
|
|
|
|
$total_expression_count
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2019-06-18 03:17:09 +02:00
|
|
|
case Report::TYPE_SONARQUBE:
|
2020-01-22 03:07:44 +01:00
|
|
|
$output = new SonarqubeReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
2019-06-18 03:17:09 +02:00
|
|
|
break;
|
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
case Report::TYPE_PYLINT:
|
2020-01-22 03:07:44 +01:00
|
|
|
$output = new PylintReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
2018-12-09 21:47:20 +01:00
|
|
|
break;
|
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
case Report::TYPE_CHECKSTYLE:
|
2020-01-22 03:07:44 +01:00
|
|
|
$output = new CheckstyleReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
2019-05-08 20:26:52 +02:00
|
|
|
break;
|
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
case Report::TYPE_XML:
|
2020-01-22 03:07:44 +01:00
|
|
|
$output = new XmlReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
2018-12-09 21:47:20 +01:00
|
|
|
break;
|
|
|
|
|
2019-12-19 21:18:09 +01:00
|
|
|
case Report::TYPE_JUNIT:
|
2020-09-20 14:55:28 +02:00
|
|
|
$output = new JunitReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
2019-12-19 21:18:09 +01:00
|
|
|
break;
|
|
|
|
|
2019-06-09 18:37:28 +02:00
|
|
|
case Report::TYPE_CONSOLE:
|
2020-01-22 03:07:44 +01:00
|
|
|
$output = new ConsoleReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
2018-12-09 21:47:20 +01:00
|
|
|
break;
|
2020-01-23 13:14:48 +01:00
|
|
|
|
|
|
|
case Report::TYPE_GITHUB_ACTIONS:
|
|
|
|
$output = new GithubActionsReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
|
|
|
break;
|
2020-09-04 22:24:14 +02:00
|
|
|
|
|
|
|
case Report::TYPE_PHP_STORM:
|
|
|
|
$output = new PhpStormReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
|
|
|
break;
|
2020-11-18 04:49:25 +01:00
|
|
|
|
2020-11-17 19:23:20 +01:00
|
|
|
case Report::TYPE_SARIF:
|
|
|
|
$output = new SarifReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
|
|
|
break;
|
2020-11-23 21:34:51 +01:00
|
|
|
|
|
|
|
case Report::TYPE_CODECLIMATE:
|
|
|
|
$output = new CodeClimateReport($normalized_data, self::$fixable_issue_counts, $report_options);
|
|
|
|
break;
|
2017-09-08 17:18:48 +02:00
|
|
|
}
|
|
|
|
|
2018-12-09 21:47:20 +01:00
|
|
|
return $output->create();
|
2017-09-08 17:18:48 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
protected static function alreadyEmitted(string $message): bool
|
2016-10-19 00:55:53 +02:00
|
|
|
{
|
|
|
|
$sham = sha1($message);
|
|
|
|
|
|
|
|
if (isset(self::$emitted[$sham])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$emitted[$sham] = true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-08 21:57:18 +01:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public static function clearCache(): void
|
2016-12-08 21:57:18 +01:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
self::$issues_data = [];
|
2016-12-08 21:57:18 +01:00
|
|
|
self::$emitted = [];
|
2017-01-14 07:24:27 +01:00
|
|
|
self::$error_count = 0;
|
2017-03-13 23:06:56 +01:00
|
|
|
self::$recording_level = 0;
|
|
|
|
self::$recorded_issues = [];
|
2017-07-25 22:11:02 +02:00
|
|
|
self::$console_issues = [];
|
2019-08-18 20:27:50 +02:00
|
|
|
self::$unused_suppressions = [];
|
2019-09-16 17:58:42 +02:00
|
|
|
self::$used_suppressions = [];
|
2017-03-13 23:06:56 +01:00
|
|
|
}
|
|
|
|
|
2018-10-17 21:52:26 +02:00
|
|
|
/**
|
2020-02-17 00:24:40 +01:00
|
|
|
* @return array<string, list<IssueData>>
|
2018-10-17 21:52:26 +02:00
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function clear(): array
|
2018-10-17 21:52:26 +02:00
|
|
|
{
|
|
|
|
$current_data = self::$issues_data;
|
|
|
|
self::$issues_data = [];
|
|
|
|
self::$emitted = [];
|
2019-07-05 22:24:00 +02:00
|
|
|
|
2018-10-17 21:52:26 +02:00
|
|
|
return $current_data;
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function isRecording(): bool
|
2017-06-29 06:28:37 +02:00
|
|
|
{
|
|
|
|
return self::$recording_level > 0;
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public static function startRecording(): void
|
2017-03-13 23:06:56 +01:00
|
|
|
{
|
2017-05-27 02:05:57 +02:00
|
|
|
++self::$recording_level;
|
2017-03-13 23:06:56 +01:00
|
|
|
self::$recorded_issues[self::$recording_level] = [];
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public static function stopRecording(): void
|
2017-03-13 23:06:56 +01:00
|
|
|
{
|
|
|
|
if (self::$recording_level === 0) {
|
|
|
|
throw new \UnexpectedValueException('Cannot stop recording - already at base level');
|
|
|
|
}
|
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
--self::$recording_level;
|
2017-03-13 23:06:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<int, CodeIssue>
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function clearRecordingLevel(): array
|
2017-03-13 23:06:56 +01:00
|
|
|
{
|
|
|
|
if (self::$recording_level === 0) {
|
|
|
|
throw new \UnexpectedValueException('Not currently recording');
|
|
|
|
}
|
|
|
|
|
|
|
|
$recorded_issues = self::$recorded_issues[self::$recording_level];
|
|
|
|
|
|
|
|
self::$recorded_issues[self::$recording_level] = [];
|
|
|
|
|
|
|
|
return $recorded_issues;
|
|
|
|
}
|
|
|
|
|
2020-10-12 21:02:52 +02:00
|
|
|
public static function bubbleUp(CodeIssue $e): void
|
2017-03-13 23:06:56 +01:00
|
|
|
{
|
|
|
|
if (self::$recording_level === 0) {
|
|
|
|
self::add($e);
|
2017-05-25 04:07:49 +02:00
|
|
|
|
2017-03-13 23:06:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$recorded_issues[self::$recording_level][] = $e;
|
2016-12-08 21:57:18 +01:00
|
|
|
}
|
2021-03-17 00:28:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @param array<array-key,mixed> $server
|
|
|
|
*/
|
|
|
|
final public static function captureServer(array $server): void
|
|
|
|
{
|
|
|
|
self::$server = $server;
|
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|