1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Add --disable-on-change option

This commit is contained in:
Brown 2018-10-30 18:58:22 -04:00
parent 6732c5f5c2
commit 727b0d3221
5 changed files with 68 additions and 3 deletions

View File

@ -145,6 +145,11 @@ class ProjectChecker
*/
public $only_replace_php_types_with_non_docblock_types = false;
/**
* @var ?int
*/
public $onchange_line_limit;
const TYPE_CONSOLE = 'console';
const TYPE_PYLINT = 'pylint';
const TYPE_JSON = 'json';

View File

@ -168,7 +168,8 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
if ($this->textDocument === null) {
$this->textDocument = new TextDocument(
$this,
$this->project_checker->codebase
$this->project_checker->codebase,
$this->project_checker->onchange_line_limit
);
}

View File

@ -61,12 +61,17 @@ class TextDocument
*/
protected $codebase;
/** @var ?int */
protected $onchange_line_limit;
public function __construct(
LanguageServer $server,
Codebase $codebase
Codebase $codebase,
?int $onchange_line_limit
) {
$this->server = $server;
$this->codebase = $codebase;
$this->onchange_line_limit = $onchange_line_limit;
}
/**
@ -130,6 +135,19 @@ class TextDocument
$time = microtime(true);
$this->codebase->addTemporaryFileChanges($file_path, $contentChanges);
if ($this->onchange_line_limit !== null) {
if ($this->onchange_line_limit === 0) {
return;
}
$c = $this->codebase->getFileContents($file_path);
if (substr_count($c, "\n") > $this->onchange_line_limit) {
return;
}
}
$this->server->analyzePath($file_path);
$this->server->emitIssues($textDocument->uri);
$diff = microtime(true) - $time;

View File

@ -24,6 +24,7 @@ $valid_long_options = [
'use-ini-defaults',
'version',
'tcp:',
'disable-on-change::'
];
$args = array_slice($argv, 1);
@ -38,7 +39,10 @@ array_map(
if (substr($arg, 0, 2) === '--' && $arg !== '--') {
$arg_name = preg_replace('/=.*$/', '', substr($arg, 2));
if (!in_array($arg_name, $valid_long_options) && !in_array($arg_name . ':', $valid_long_options)) {
if (!in_array($arg_name, $valid_long_options)
&& !in_array($arg_name . ':', $valid_long_options)
&& !in_array($arg_name . '::', $valid_long_options)
) {
echo 'Unrecognised argument "--' . $arg_name . '"' . PHP_EOL
. 'Type --help to see a list of supported arguments'. PHP_EOL;
exit(1);
@ -112,6 +116,9 @@ Options:
--tcp=url
Use TCP mode (by default Psalm uses STDIO)
--disable-on-change[=line-number-threshold]
If added, the language server will not respond to onChange events.
You can also specify a line count over which Psalm will not run on-change events.
HELP;
exit;
@ -214,6 +221,13 @@ $project_checker = new ProjectChecker(
$providers
);
error_log(var_export($options, true));
if (isset($options['disable-on-change'])) {
error_log('disabling on change');
$project_checker->onchange_line_limit = (int) $options['disable-on-change'];
}
$config->visitComposerAutoloadFiles($project_checker);
if ($find_dead_code) {

View File

@ -678,6 +678,33 @@ class TemporaryUpdateTest extends \Psalm\Tests\TestCase
],
'error_positions' => [[], [197]],
],
'addUseShouldValidate' => [
[
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
class A {
public function foo() : void {
throw new Exception();
}
}',
],
[
getcwd() . DIRECTORY_SEPARATOR . 'A.php' => '<?php
namespace Foo;
use Exception;
class A {
public function foo() : void {
throw new Exception();
}
}',
],
],
'error_positions' => [[197], []],
],
];
}
}