1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

make skipping of checks after invalid includes configurable

as suggested in the PR it's best to make the setting configurable.

In order not to break existing installations, we default to keeping the
old behaviour, but in a later version of psalm, we might change the
default.
This commit is contained in:
Philip Hofstetter 2020-02-25 13:40:46 +01:00 committed by Matthew Brown
parent f2277ebb6d
commit d315822bfa
5 changed files with 50 additions and 0 deletions

View File

@ -68,6 +68,7 @@
<xs:attribute name="useDocblockPropertyTypes" type="xs:boolean" default="false" />
<xs:attribute name="usePhpDocMethodsWithoutMagicCall" type="xs:boolean" default="false" />
<xs:attribute name="usePhpDocPropertiesWithoutMagicCall" type="xs:boolean" default="false" />
<xs:attribute name="skipChecksOnUnresolvableIncludes" type="xs:boolean" default="true" />
</xs:complexType>
<xs:complexType name="ProjectFilesType">

View File

@ -275,6 +275,17 @@ Set the php version psalm should assume when checking and/or fixing the project.
This can be overridden on the command-line using the `--php-version=` flag which takes the highest precedence over both the `phpVersion` setting and the version derived from `composer.json`.
#### skipChecksOnUnresolvableIncludes
```xml
<psalm
skipChecksOnUnresolvableIncludes="[bool]"
>
```
When `true`, Psalm will skip checking classes, variables and functions after it comes across an `include` or `require` it cannot resolve. This allows code to reference functions and classes unknown to Psalm.
For backwards compatibility, this defaults to `true`, but if you do not rely on dynamically generated includes to cause classes otherwise unknown to psalm to come into existence, it's recommended you set this to `false` in order to reliably detect errors that would be fatal to PHP at runtime.
### Running Psalm
#### autoloader

View File

@ -291,6 +291,11 @@ class Config
*/
public $use_phpdoc_property_without_magic_or_parent = false;
/**
* @var bool
*/
public $skip_checks_on_unresolvable_includes = true;
/**
* @var bool
*/
@ -765,6 +770,7 @@ class Config
'ensureArrayStringOffsetsExist' => 'ensure_array_string_offsets_exist',
'ensureArrayIntOffsetsExist' => 'ensure_array_int_offsets_exist',
'reportMixedIssues' => 'show_mixed_issues',
'skipChecksOnUnresolvableIncludes' => 'skip_checks_on_unresolvable_includes'
];
foreach ($booleanAttributes as $xmlName => $internalName) {

View File

@ -160,6 +160,11 @@ class IncludeAnalyzer
$global_context
);
} catch (\Psalm\Exception\UnpreparedAnalysisException $e) {
if ($config->skip_checks_on_unresolvable_includes) {
$context->check_classes = false;
$context->check_variables = false;
$context->check_functions = false;
}
}
foreach ($include_file_analyzer->getRequiredFilePaths() as $required_file_path) {
@ -200,6 +205,12 @@ class IncludeAnalyzer
}
}
if ($config->skip_checks_on_unresolvable_includes) {
$context->check_classes = false;
$context->check_variables = false;
$context->check_functions = false;
}
return null;
}

View File

@ -37,6 +37,7 @@ class IncludeTest extends TestCase
}
$config = $codebase->config;
$config->skip_checks_on_unresolvable_includes = true;
foreach ($error_levels as $error_level) {
$config->setCustomErrorLevel($error_level, \Psalm\Config::REPORT_SUPPRESS);
@ -83,6 +84,7 @@ class IncludeTest extends TestCase
}
$config = $codebase->config;
$config->skip_checks_on_unresolvable_includes = false;
$this->expectException(\Psalm\Exception\CodeException::class);
$this->expectExceptionMessageRegExp('/\b' . preg_quote($error_message, '/') . '\b/');
@ -569,6 +571,25 @@ class IncludeTest extends TestCase
getcwd() . DIRECTORY_SEPARATOR . 'a' . DIRECTORY_SEPARATOR . 'b' . DIRECTORY_SEPARATOR . 'c' . DIRECTORY_SEPARATOR . 'd' . DIRECTORY_SEPARATOR . 'script.php',
],
],
'undefinedMethodAfterInvalidRequire' => [
'files' => [
getcwd() . DIRECTORY_SEPARATOR . 'file2.php' => '<?php
/** @psalm-suppress MissingFile */
require("doesnotexist.php");
require("file1.php");
foo();
bar();
',
getcwd() . DIRECTORY_SEPARATOR . 'file1.php' => '<?php
function bar(): void {}
',
],
'files_to_check' => [
getcwd() . DIRECTORY_SEPARATOR . 'file2.php',
],
],
];
}