Skip to content

Commit

Permalink
Add a --debug-emitted-issues flag (#3637)
Browse files Browse the repository at this point in the history
And support --debug-by-line in psalter and psalm-refactor.
Those were previously not supported in getopt()

Fixes #3634
  • Loading branch information
TysonAndre committed Jun 22, 2020
1 parent e8be2c5 commit bee10a2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,11 @@ class Config
*/
protected $taint_analysis_ignored_files;

/**
* @var bool whether to emit a backtrace of emitted issues to stderr
*/
public $debug_emitted_issues = false;

protected function __construct()
{
self::$instance = $this;
Expand Down
13 changes: 13 additions & 0 deletions src/Psalm/IssueBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
use function array_search;
use function array_splice;
use function count;
use function debug_print_backtrace;
use function explode;
use function file_put_contents;
use function fwrite;
use function get_class;
use function memory_get_peak_usage;
use function microtime;
use function number_format;
use function ob_get_clean;
use function ob_start;
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Internal\Analyzer\ProjectAnalyzer;
use Psalm\Issue\CodeIssue;
Expand All @@ -33,6 +37,8 @@
use function usort;
use function array_merge;
use function array_values;
use const DEBUG_BACKTRACE_IGNORE_ARGS;
use const STDERR;

class IssueBuffer
{
Expand Down Expand Up @@ -206,6 +212,13 @@ public static function add(CodeIssue $e, bool $is_fixable = false)
return false;
}

if ($config->debug_emitted_issues) {
ob_start();
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$trace = ob_get_clean();
fwrite(STDERR, "\nEmitting {$e->getShortLocation()} $issue_type {$e->getMessage()}\n$trace\n");
}

$emitted_key = $issue_type . '-' . $e->getShortLocation() . ':' . $e->getLocation()->getColumn();

if ($reporting_level === Config::REPORT_INFO) {
Expand Down
3 changes: 3 additions & 0 deletions src/command_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ function getPsalmHelpText(): string
--debug-by-line
Debug information on a line-by-line level
--debug-emitted-issues
Print a php backtrace to stderr when emitting issues.
-r, --root
If running Psalm globally you'll need to specify a project root. Defaults to cwd
Expand Down
14 changes: 11 additions & 3 deletions src/psalm-refactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

$valid_short_options = ['f:', 'm', 'h', 'r:', 'c:'];
$valid_long_options = [
'help', 'debug', 'config:', 'root:',
'help', 'debug', 'debug-by-line', 'debug-emitted-issues', 'config:', 'root:',
'threads:', 'move:', 'into:', 'rename:', 'to:',
];

Expand Down Expand Up @@ -92,7 +92,7 @@ function ($arg) use ($valid_long_options, $valid_short_options) {
-h, --help
Display this help message
--debug, --debug-by-line
--debug, --debug-by-line, --debug-emitted-issues
Debug information
-c, --config=psalm.xml
Expand Down Expand Up @@ -247,11 +247,15 @@ function ($arg) use ($valid_long_options, $valid_short_options) {
new Psalm\Internal\Provider\ProjectCacheProvider($current_dir . DIRECTORY_SEPARATOR . 'composer.lock')
);

$debug = array_key_exists('debug', $options);
$debug = array_key_exists('debug', $options) || array_key_exists('debug-by-line', $options);
$progress = $debug
? new DebugProgress()
: new DefaultProgress();

if (array_key_exists('debug-emitted-issues', $options)) {
$config->debug_emitted_issues = true;
}

$project_analyzer = new ProjectAnalyzer(
$config,
$providers,
Expand All @@ -261,6 +265,10 @@ function ($arg) use ($valid_long_options, $valid_short_options) {
$progress
);

if (array_key_exists('debug-by-line', $options)) {
$project_analyzer->debug_lines = true;
}

$config->visitComposerAutoloadFiles($project_analyzer);

$project_analyzer->refactorCodeAfterCompletion($to_refactor);
Expand Down
6 changes: 6 additions & 0 deletions src/psalm.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'config:',
'debug',
'debug-by-line',
'debug-emitted-issues',
'diff',
'diff-methods',
'disable-extension:',
Expand Down Expand Up @@ -245,6 +246,7 @@ function ($arg) {
&& $arg !== '--init'
&& $arg !== '--debug'
&& $arg !== '--debug-by-line'
&& $arg !== '--debug-emitted-issues'
&& strpos($arg, '--disable-extension=') !== 0
&& strpos($arg, '--root=') !== 0
&& strpos($arg, '--r=') !== 0;
Expand Down Expand Up @@ -390,6 +392,10 @@ function ($arg) {
$config->load_xdebug_stub = true;
}

if (isset($options['debug-emitted-issues'])) {
$config->debug_emitted_issues = true;
}

setlocale(LC_CTYPE, 'C');

if (isset($options['set-baseline'])) {
Expand Down
10 changes: 7 additions & 3 deletions src/psalter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

$valid_short_options = ['f:', 'm', 'h', 'r:', 'c:'];
$valid_long_options = [
'help', 'debug', 'debug-by-line', 'config:', 'file:', 'root:',
'help', 'debug', 'debug-by-line', 'debug-emitted-issues', 'config:', 'file:', 'root:',
'plugin:', 'issues:', 'list-supported-issues', 'php-version:', 'dry-run', 'safe-types',
'find-unused-code', 'threads:', 'codeowner:',
'allow-backwards-incompatible-changes:',
Expand Down Expand Up @@ -105,7 +105,7 @@ function ($arg) use ($valid_long_options, $valid_short_options) {
-h, --help
Display this help message
--debug, --debug-by-line
--debug, --debug-by-line, --debug-emitted-issues
Debug information
-c, --config=psalm.xml
Expand Down Expand Up @@ -223,7 +223,7 @@ function ($arg) use ($valid_long_options, $valid_short_options) {
exit();
}

$debug = array_key_exists('debug', $options);
$debug = array_key_exists('debug', $options) || array_key_exists('debug-by-line', $options);
$progress = $debug
? new DebugProgress()
: new DefaultProgress();
Expand All @@ -244,6 +244,10 @@ function ($arg) use ($valid_long_options, $valid_short_options) {
$project_analyzer->debug_lines = true;
}

if (array_key_exists('debug-emitted-issues', $options)) {
$config->debug_emitted_issues = true;
}

$config->visitComposerAutoloadFiles($project_analyzer);

if (array_key_exists('issues', $options)) {
Expand Down

0 comments on commit bee10a2

Please sign in to comment.