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
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
use LSS\Array2XML;
|
2016-08-13 20:20:46 +02:00
|
|
|
use Psalm\Checker\ProjectChecker;
|
2017-03-13 23:06:56 +01:00
|
|
|
use Psalm\Issue\CodeIssue;
|
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
|
|
|
/**
|
|
|
|
* @var array<int, array{severity: string, line_number: string, type: string, message: string, file_name: string,
|
|
|
|
* file_path: string, snippet: string, from: int, to: int, snippet_from: int, snippet_to: int, column: int}>
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
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 = [];
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-03-13 23:06:56 +01:00
|
|
|
* @param CodeIssue $e
|
|
|
|
* @param array $suppressed_issues
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-03-13 23:06:56 +01:00
|
|
|
public static function accepts(CodeIssue $e, array $suppressed_issues = [])
|
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);
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2017-05-27 02:05:57 +02:00
|
|
|
if (in_array($issue_type, $suppressed_issues, true)) {
|
2016-07-22 19:29:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-24 23:34:46 +01:00
|
|
|
if (!$config->reportIssueInFile($issue_type, $e->getFilePath())) {
|
2016-06-10 00:08:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-03-13 23:06:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-08 20:36:18 +02:00
|
|
|
return self::add($e);
|
2016-06-27 19:22:16 +02:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-03-13 23:06:56 +01:00
|
|
|
* @param CodeIssue $e
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @throws Exception\CodeException
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2016-11-02 07:29:00 +01:00
|
|
|
*/
|
2017-03-13 23:06:56 +01:00
|
|
|
public static function add(CodeIssue $e)
|
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-01-07 06:11:23 +01:00
|
|
|
$project_checker = ProjectChecker::getInstance();
|
2018-01-06 01:49:27 +01:00
|
|
|
|
2018-01-07 06:11:23 +01:00
|
|
|
if ($project_checker->alter_code) {
|
2018-01-06 01:49:27 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-08 04:38:57 +01:00
|
|
|
$error_message = $issue_type . ' - ' . $e->getShortLocation() . ' - ' . $e->getMessage();
|
2016-07-22 19:29:46 +02:00
|
|
|
|
2017-02-12 19:38:41 +01:00
|
|
|
$reporting_level = $config->getReportingLevelForFile($issue_type, $e->getFilePath());
|
2016-06-27 04:03:37 +02:00
|
|
|
|
2016-12-04 01:11:30 +01:00
|
|
|
if ($reporting_level === Config::REPORT_SUPPRESS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($reporting_level === Config::REPORT_INFO) {
|
2016-12-08 04:38:57 +01:00
|
|
|
if ($project_checker->show_info && !self::alreadyEmitted($error_message)) {
|
2017-07-25 22:11:02 +02:00
|
|
|
self::$issues_data[] = $e->toArray(Config::REPORT_INFO);
|
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) {
|
|
|
|
throw new Exception\CodeException($error_message);
|
|
|
|
}
|
|
|
|
|
2016-10-19 00:55:53 +02:00
|
|
|
if (!self::alreadyEmitted($error_message)) {
|
2017-07-25 22:11:02 +02:00
|
|
|
self::$issues_data[] = $e->toArray(Config::REPORT_ERROR);
|
2016-10-19 00:55:53 +02:00
|
|
|
}
|
2016-06-17 01:02:29 +02:00
|
|
|
|
2016-06-21 01:30:38 +02:00
|
|
|
if ($config->stop_on_first_error) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-12-08 04:38:57 +01:00
|
|
|
/**
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param array{severity: string, line_number: string, type: string, message: string, file_name: string,
|
|
|
|
* file_path: string, snippet: string, from: int, to: int, snippet_from: int, snippet_to: int,
|
|
|
|
* column: int} $issue_data
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-07-25 22:11:02 +02:00
|
|
|
* @return string
|
2016-12-08 04:38:57 +01:00
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
protected static function getEmacsOutput(array $issue_data)
|
2016-12-08 04:38:57 +01:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
return $issue_data['file_path'] . ':' . $issue_data['line_number'] . ':' . $issue_data['column'] . ':' .
|
|
|
|
($issue_data['severity'] === Config::REPORT_ERROR ? 'error' : 'warning') . ' - ' . $issue_data['message'];
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|
|
|
|
|
2017-01-16 04:39:26 +01:00
|
|
|
/**
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param array{severity: string, line_number: string, type: string, message: string, file_name: string,
|
|
|
|
* file_path: string, snippet: string, from: int, to: int, snippet_from: int, snippet_to: int,
|
|
|
|
* column: int} $issue_data
|
|
|
|
* @param bool $use_color
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-01-16 04:39:26 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
protected static function getConsoleOutput(array $issue_data, $use_color)
|
2017-01-16 04:39:26 +01:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
$issue_string = '';
|
2017-01-16 04:39:26 +01:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($issue_data['severity'] === Config::REPORT_ERROR) {
|
|
|
|
$issue_string .= ($use_color ? "\e[0;31mERROR\e[0m" : 'ERROR');
|
|
|
|
} else {
|
|
|
|
$issue_string .= 'INFO';
|
|
|
|
}
|
|
|
|
|
|
|
|
$issue_string .= ': ' . $issue_data['type'] . ' - ' . $issue_data['file_name'] . ':' .
|
|
|
|
$issue_data['line_number'] . ':' . $issue_data['column'] . ' - ' . $issue_data['message'] . PHP_EOL;
|
|
|
|
|
|
|
|
$snippet = $issue_data['snippet'];
|
|
|
|
|
|
|
|
if (!$use_color) {
|
|
|
|
$issue_string .= $snippet;
|
|
|
|
} else {
|
|
|
|
$selection_start = $issue_data['from'] - $issue_data['snippet_from'];
|
|
|
|
$selection_length = $issue_data['to'] - $issue_data['from'];
|
|
|
|
|
|
|
|
$issue_string .= substr($snippet, 0, $selection_start) .
|
|
|
|
"\e[97;41m" . substr($snippet, $selection_start, $selection_length) .
|
|
|
|
"\e[0m" . substr($snippet, $selection_length + $selection_start) . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $issue_string;
|
2017-01-16 04:39:26 +01:00
|
|
|
}
|
|
|
|
|
2016-12-08 04:38:57 +01:00
|
|
|
/**
|
2017-07-25 22:11:02 +02:00
|
|
|
* @return array<int, array{severity: string, line_number: string, type: string, message: string, file_name: string,
|
|
|
|
* file_path: string, snippet: string, from: int, to: int, snippet_from: int, snippet_to: int, column: int}>
|
2016-12-08 04:38:57 +01:00
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public static function getIssuesData()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param array<int, array{severity: string, line_number: string, type: string, message: string,
|
|
|
|
* file_name: string, file_path: string, snippet: string, from: int, to: int, snippet_from: int,
|
|
|
|
* snippet_to: int, column: int}> $issues_data
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-07-25 22:11:02 +02:00
|
|
|
* @return void
|
2016-12-08 04:38:57 +01:00
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public static function addIssues(array $issues_data)
|
2016-12-08 04:38:57 +01:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
self::$issues_data = array_merge($issues_data, self::$issues_data);
|
2016-12-08 04:38:57 +01:00
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2017-02-18 19:41:27 +01:00
|
|
|
* @param bool $is_full
|
2018-01-09 17:49:10 +01:00
|
|
|
* @param float $start_time
|
|
|
|
* @param array<string, bool> $scanned_files
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-09 17:49:10 +01:00
|
|
|
public static function finish(ProjectChecker $project_checker, $is_full, $start_time)
|
2016-06-21 01:30:38 +02:00
|
|
|
{
|
2018-01-09 17:49:10 +01:00
|
|
|
$scanned_files = $project_checker->getScannedFiles();
|
|
|
|
Provider\FileReferenceProvider::updateReferenceCache($project_checker, $scanned_files);
|
2016-10-05 19:24:46 +02:00
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
$has_error = false;
|
|
|
|
|
|
|
|
$project_checker = ProjectChecker::getInstance();
|
|
|
|
|
|
|
|
if (self::$issues_data) {
|
2017-12-10 17:22:36 +01:00
|
|
|
usort(
|
|
|
|
self::$issues_data,
|
|
|
|
/** @return int */
|
|
|
|
function (array $d1, array $d2) {
|
|
|
|
if ($d1['file_path'] === $d2['file_path']) {
|
|
|
|
if ($d1['line_number'] === $d2['line_number']) {
|
|
|
|
if ($d1['column'] === $d2['column']) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $d1['column'] > $d2['column'] ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $d1['line_number'] > $d2['line_number'] ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $d1['file_path'] > $d2['file_path'] ? 1 : -1;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
foreach (self::$issues_data as $issue_data) {
|
|
|
|
if ($issue_data['severity'] === Config::REPORT_ERROR) {
|
|
|
|
$has_error = true;
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2017-09-08 17:18:48 +02:00
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2017-09-08 17:18:48 +02:00
|
|
|
echo self::getOutput($project_checker->output_format, $project_checker->use_color);
|
2017-09-27 00:18:24 +02:00
|
|
|
}
|
|
|
|
foreach ($project_checker->reports as $format => $path) {
|
|
|
|
file_put_contents(
|
|
|
|
$path,
|
|
|
|
self::getOutput($format, $project_checker->use_color)
|
|
|
|
);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 21:31:18 +01:00
|
|
|
if ($start_time) {
|
2018-01-09 17:49:10 +01:00
|
|
|
echo 'Checks took ' . ((float)microtime(true) - $start_time);
|
2017-05-27 02:05:57 +02:00
|
|
|
echo ' and used ' . number_format(memory_get_peak_usage() / (1024 * 1024), 3) . 'MB' . PHP_EOL;
|
2016-12-15 01:24:16 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if ($has_error) {
|
2016-06-21 01:30:38 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
2016-10-07 06:58:08 +02:00
|
|
|
|
2016-11-06 05:59:29 +01:00
|
|
|
if ($is_full && $start_time) {
|
2017-07-25 22:11:02 +02:00
|
|
|
$project_checker->cache_provider->processSuccessfulRun($start_time);
|
2016-10-07 06:58:08 +02: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
|
|
|
/**
|
2017-09-08 17:18:48 +02:00
|
|
|
* @param string $format
|
|
|
|
* @param bool $useColor
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getOutput($format, $useColor)
|
|
|
|
{
|
|
|
|
if ($format === ProjectChecker::TYPE_JSON) {
|
|
|
|
return json_encode(self::$issues_data) . PHP_EOL;
|
|
|
|
} elseif ($format === ProjectChecker::TYPE_XML) {
|
|
|
|
$xml = Array2XML::createXML('report', ['item' => self::$issues_data]);
|
|
|
|
|
|
|
|
return $xml->saveXML();
|
|
|
|
} elseif ($format === ProjectChecker::TYPE_EMACS) {
|
|
|
|
$output = '';
|
|
|
|
foreach (self::$issues_data as $issue_data) {
|
|
|
|
$output .= self::getEmacsOutput($issue_data) . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = '';
|
|
|
|
foreach (self::$issues_data as $issue_data) {
|
|
|
|
$output .= self::getConsoleOutput($issue_data, $useColor) . PHP_EOL . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-30 17:46:18 +01:00
|
|
|
* @param string $message
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-10-30 17:46:18 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-10-19 00:55:53 +02:00
|
|
|
protected static function alreadyEmitted($message)
|
|
|
|
{
|
|
|
|
$sham = sha1($message);
|
|
|
|
|
|
|
|
if (isset(self::$emitted[$sham])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$emitted[$sham] = true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-08 21:57:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function clearCache()
|
|
|
|
{
|
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 = [];
|
2017-03-13 23:06:56 +01:00
|
|
|
}
|
|
|
|
|
2017-06-29 06:28:37 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isRecording()
|
|
|
|
{
|
|
|
|
return self::$recording_level > 0;
|
|
|
|
}
|
|
|
|
|
2017-03-13 23:06:56 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function startRecording()
|
|
|
|
{
|
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] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function stopRecording()
|
|
|
|
{
|
|
|
|
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>
|
|
|
|
*/
|
|
|
|
public static function clearRecordingLevel()
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function bubbleUp(CodeIssue $e)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
2016-06-06 02:25:16 +02:00
|
|
|
}
|