1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-13 17:57:37 +01:00
psalm/src/Psalm/IssueBuffer.php

509 lines
15 KiB
PHP
Raw Normal View History

<?php
2016-07-26 00:37:44 +02:00
namespace Psalm;
2018-11-06 03:57:36 +01:00
use Psalm\Internal\Analyzer\ProjectAnalyzer;
2017-03-13 23:06:56 +01:00
use Psalm\Issue\CodeIssue;
use Psalm\Output\Checkstyle;
use Psalm\Output\Compact;
use Psalm\Output\Console;
use Psalm\Output\Emacs;
use Psalm\Output\Json;
use Psalm\Output\Pylint;
use Psalm\Output\Text;
use Psalm\Output\Xml;
2016-06-26 21:18:40 +02:00
class IssueBuffer
{
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
/**
* @var array<int, array{severity: string, line_from: int, line_to: int, type: string, message: string,
* file_name: string, file_path: string, snippet: string, from: int, to: int,
* snippet_from: int, snippet_to: int, column_from: int, column_to: int, selected_text: string}>
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
*/
protected static $issues_data = [];
2016-12-08 04:38:57 +01:00
/**
* @var array<int, array>
*/
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
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
/**
* @var int
2016-11-02 07:29:00 +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 = [];
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 = [])
{
if (self::isSuppressed($e, $suppressed_issues)) {
return false;
}
return self::add($e);
}
/**
* @param CodeIssue $e
* @param array $suppressed_issues
*
* @return bool
*/
public static function isSuppressed(CodeIssue $e, array $suppressed_issues = []) : bool
{
2016-06-10 00:08:25 +02:00
$config = Config::getInstance();
$fqcn_parts = explode('\\', get_class($e));
$issue_type = array_pop($fqcn_parts);
2019-01-02 18:10:52 +01:00
if (!$config->reportIssueInFile($issue_type, $e->getFilePath())) {
return true;
2016-06-10 00:08:25 +02:00
}
2019-05-03 23:12:20 +02:00
$reporting_level = $config->getReportingLevelForIssue($e, $suppressed_issues);
2019-05-03 23:12:20 +02:00
if ($reporting_level === Config::REPORT_SUPPRESS) {
return true;
}
if ($e->getLocation()->getLineNumber() === -1) {
return true;
}
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
return true;
2017-03-13 23:06:56 +01:00
}
return false;
}
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)
{
$config = Config::getInstance();
$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-11-11 18:01:14 +01:00
if (!$project_analyzer->show_issues) {
return false;
}
2019-05-03 23:12:20 +02:00
$reporting_level = $config->getReportingLevelForIssue($e);
2018-03-18 22:26:28 +01:00
if ($reporting_level === Config::REPORT_SUPPRESS) {
return false;
}
$emitted_key = $issue_type . '-' . $e->getShortLocation() . ':' . $e->getLocation()->getColumn();
if ($reporting_level === Config::REPORT_INFO) {
if (!self::alreadyEmitted($emitted_key)) {
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
self::$issues_data[] = $e->toArray(Config::REPORT_INFO);
}
2017-05-25 04:07:49 +02:00
return false;
}
if ($config->throw_exception) {
2019-02-27 22:00:44 +01:00
throw new Exception\CodeException(
2019-02-27 22:16:19 +01:00
$issue_type
. ' - ' . $e->getShortLocation()
. ':' . $e->getLocation()->getColumn()
. ' - ' . $e->getMessage()
2019-02-27 22:00:44 +01:00
);
}
if (!self::alreadyEmitted($emitted_key)) {
++self::$error_count;
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
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-08-08 20:36:18 +02:00
return true;
}
2016-12-08 04:38:57 +01:00
/**
* @return array<int, array{severity: string, line_from: int, line_to: int, type: string, message: string,
* file_name: string, file_path: string, snippet: string, from: int, to: int, snippet_from: int, snippet_to: int,
* column_from: int, column_to: int, selected_text: string}>
2016-12-08 04:38:57 +01:00
*/
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
public static function getIssuesData()
2016-12-08 04:38:57 +01:00
{
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
return self::$issues_data;
2016-12-08 04:38:57 +01:00
}
/**
* @return int
*/
public static function getErrorCount()
{
return self::$error_count;
}
2016-12-08 04:38:57 +01:00
/**
* @param array<int, array{severity: string, line_from: int, line_to: int, type: string, message: string,
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
* file_name: string, file_path: string, snippet: string, from: int, to: int, snippet_from: int,
* snippet_to: int, column_from: int, column_to: int}> $issues_data
2017-05-27 02:16:18 +02:00
*
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
* @return void
2016-12-08 04:38:57 +01:00
*/
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
public static function addIssues(array $issues_data)
2016-12-08 04:38:57 +01:00
{
2018-11-01 18:22:38 +01:00
foreach ($issues_data as $issue) {
$emitted_key = $issue['type']
. '-' . $issue['file_name']
. ':' . $issue['line_from']
. ':' . $issue['column_from'];
2018-11-01 18:22:38 +01:00
if (!self::alreadyEmitted($emitted_key)) {
2018-11-01 18:22:38 +01:00
self::$issues_data[] = $issue;
}
}
2016-12-08 04:38:57 +01:00
}
2016-11-02 07:29:00 +01:00
/**
2018-11-11 18:01:14 +01:00
* @param ProjectAnalyzer $project_analyzer
* @param bool $is_full
* @param float $start_time
* @param bool $add_stats
* @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
* @return void
*/
2018-01-31 23:09:09 +01:00
public static function finish(
2018-11-11 18:01:14 +01:00
ProjectAnalyzer $project_analyzer,
bool $is_full,
float $start_time,
bool $add_stats = false,
array $issue_baseline = []
2018-01-31 23:09:09 +01:00
) {
2018-11-11 18:01:14 +01:00
if ($project_analyzer->output_format === ProjectAnalyzer::TYPE_CONSOLE) {
echo "\n";
}
$codebase = $project_analyzer->getCodebase();
2018-03-18 23:04:50 +01:00
$error_count = 0;
$info_count = 0;
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
if (self::$issues_data) {
usort(
self::$issues_data,
/** @return int */
function (array $d1, array $d2) {
if ($d1['file_path'] === $d2['file_path']) {
if ($d1['line_from'] === $d2['line_from']) {
if ($d1['column_from'] === $d2['column_from']) {
return 0;
}
return $d1['column_from'] > $d2['column_from'] ? 1 : -1;
}
return $d1['line_from'] > $d2['line_from'] ? 1 : -1;
}
return $d1['file_path'] > $d2['file_path'] ? 1 : -1;
}
);
if (!empty($issue_baseline)) {
// Set severity for issues in baseline to INFO
foreach (self::$issues_data as $key => $issue_data) {
$file = $issue_data['file_name'];
$file = str_replace('\\', '/', $file);
$type = $issue_data['type'];
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($issue_data['selected_text'], $issue_baseline[$file][$type]['s']);
if ($position !== false) {
$issue_data['severity'] = Config::REPORT_INFO;
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'] = [];
$issue_data['severity'] = Config::REPORT_INFO;
$issue_baseline[$file][$type]['o'] = $issue_baseline[$file][$type]['o'] - 1;
}
}
self::$issues_data[$key] = $issue_data;
}
}
foreach (self::$issues_data as $issue_data) {
if ($issue_data['severity'] === Config::REPORT_ERROR) {
2018-03-18 23:04:50 +01:00
++$error_count;
} else {
++$info_count;
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
}
}
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
2018-06-04 16:19:20 +02:00
echo self::getOutput(
2018-11-11 18:01:14 +01:00
$project_analyzer->output_format,
$project_analyzer->use_color,
$project_analyzer->show_snippet,
$project_analyzer->show_info
2018-06-04 16:19:20 +02:00
);
}
$after_analysis_hooks = $codebase->config->after_analysis;
if ($after_analysis_hooks) {
$source_control_info = null;
$build_info = (new \Psalm\Internal\ExecutionEnvironment\BuildInfoCollector($_SERVER))->collect();
try {
$source_control_info = (new \Psalm\Internal\ExecutionEnvironment\GitInfoCollector())->collect();
} catch (\RuntimeException $e) {
// do nothing
}
foreach ($after_analysis_hooks as $after_analysis_hook) {
$after_analysis_hook::afterAnalysis(
$codebase,
self::$issues_data,
$build_info,
$source_control_info
);
}
}
2018-11-11 18:01:14 +01:00
foreach ($project_analyzer->reports as $format => $path) {
file_put_contents(
$path,
2018-11-11 18:01:14 +01:00
self::getOutput($format, $project_analyzer->use_color)
);
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
}
2018-11-11 18:01:14 +01:00
if ($project_analyzer->output_format === ProjectAnalyzer::TYPE_CONSOLE) {
echo str_repeat('-', 30) . "\n";
2018-03-18 23:04:50 +01:00
if ($error_count) {
2018-11-11 18:01:14 +01:00
echo ($project_analyzer->use_color
? "\e[0;31m" . $error_count . " errors\e[0m"
: $error_count . ' errors'
) . ' found' . "\n";
} else {
echo 'No errors found!' . "\n";
}
2018-11-11 18:01:14 +01:00
if ($info_count && $project_analyzer->show_info) {
echo str_repeat('-', 30) . "\n";
echo $info_count . ' other issues found.' . "\n"
. 'You can hide them with ' .
2018-11-11 18:01:14 +01:00
($project_analyzer->use_color
? "\e[30;48;5;195m--show-info=false\e[0m"
: '--show-info=false') . "\n";
}
echo str_repeat('-', 30) . "\n" . "\n";
2018-03-18 23:04:50 +01:00
if ($start_time) {
2018-10-10 22:05:06 +02:00
echo 'Checks took ' . number_format(microtime(true) - $start_time, 2) . ' seconds';
echo ' and used ' . number_format(memory_get_peak_usage() / (1024 * 1024), 3) . 'MB of memory' . "\n";
$analysis_summary = $codebase->analyzer->getTypeInferenceSummary($codebase);
echo $analysis_summary . "\n";
2018-01-31 23:09:09 +01:00
if ($add_stats) {
echo '-----------------' . "\n";
2018-11-06 03:57:36 +01:00
echo $codebase->analyzer->getNonMixedStats();
echo "\n";
}
}
2016-12-15 01:24:16 +01:00
}
2018-03-18 23:04:50 +01:00
if ($error_count) {
exit(1);
}
2016-10-07 06:58:08 +02:00
if ($is_full && $start_time) {
$codebase->file_reference_provider->removeDeletedFilesFromReferences();
if ($codebase->statements_provider->parser_cache_provider) {
$codebase->statements_provider->parser_cache_provider->processSuccessfulRun($start_time);
}
2016-10-07 06:58:08 +02:00
}
}
2016-10-19 00:55:53 +02:00
2016-10-30 17:46:18 +01:00
/**
* @param string $format
2018-06-04 16:19:20 +02:00
* @param bool $use_color
* @param bool $show_snippet
* @param bool $show_info
*
* @return string
*/
public static function getOutput($format, $use_color, $show_snippet = true, $show_info = true)
{
switch ($format) {
case ProjectAnalyzer::TYPE_COMPACT:
$output = new Compact(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
case ProjectAnalyzer::TYPE_EMACS:
$output = new Emacs(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
case ProjectAnalyzer::TYPE_TEXT:
$output = new Text(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
case ProjectAnalyzer::TYPE_JSON:
$output = new Json(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
case ProjectAnalyzer::TYPE_PYLINT:
$output = new Pylint(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
case ProjectAnalyzer::TYPE_CHECKSTYLE:
$output = new Checkstyle(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
case ProjectAnalyzer::TYPE_XML:
$output = new Xml(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
case ProjectAnalyzer::TYPE_CONSOLE:
default:
$output = new Console(self::$issues_data, $use_color, $show_snippet, $show_info);
break;
}
return $output->create();
}
/**
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;
}
/**
* @return void
*/
public static function clearCache()
{
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
self::$issues_data = [];
self::$emitted = [];
self::$error_count = 0;
2017-03-13 23:06:56 +01:00
self::$recording_level = 0;
self::$recorded_issues = [];
Refactor scanning and analysis, introducing multithreading (#191) * Add failing test * Add visitor to soup up classlike references * Move a whole bunch of code into the visitor * Move some methods back, move onto analysis stage * Use the getAliases method everywhere * Fix refs * Fix more refs * Fix some tests * Fix more tests * Fix include tests * Shift config class finding to project checker and fix bugs * Fix a few more tests * transition test to new syntax * Remove var_dump * Delete a bunch of code and fix mutation test * Remove unnecessary visitation * Transition to better mocked out file provider, breaking some cached statement loading * Use different scheme for naming anonymous classes * Fix anonymous class issues * Refactor file/statement loading * Add specific property types * Fix mapped property assignment * Improve how we deal with traits * Fix trait checking * Pass Psalm checks * Add multi-process support * Delay console output until the end * Remove PHP 7 syntax * Update file storage with classes * Fix scanning individual files and add reflection return types * Always turn XDebug off * Add quicker method of getting method mutations * Queue return types for crawling * Interpret all strings as possible classes once we see a `get_class` call * Check invalid return types again * Fix template namespacing issues * Default to class-insensitive file names for includes * Don’t overwrite existing issues data * Add var docblocks for scanning * Add null check * Fix loading of external classes in templates * Only try to populate class when we haven’t yet seen it’s not a class * Fix trait property accessibility * Only ever improve docblock param type * Make param replacement more robust * Fix static const missing inferred type * Fix a few more tests * Register constant definitions * Fix trait aliasing * Skip constant type tests for now * Fix linting issues * Make sure caching is off for tests * Remove unnecessary return * Use emulative parser if on PHP 5.6 * Cache parser for faster first-time parse * Fix constant resolution when scanning classes * Remove test that’s beyond a practical scope * Add back --diff support * Add --help for --threads * Remove unused vars
2017-07-25 22:11:02 +02:00
self::$console_issues = [];
2017-03-13 23:06:56 +01:00
}
/**
* @return array<int, array{severity: string, line_from: int, line_to: int, type: string, message: string,
* file_name: string, file_path: string, snippet: string, from: int, to: int, snippet_from: int, snippet_to: int,
* column_from: int, column_to: int}>
*/
public static function clear()
{
$current_data = self::$issues_data;
self::$issues_data = [];
self::$emitted = [];
return $current_data;
}
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;
}
}