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

Tighten up Psalm’s inference

This commit is contained in:
Brown 2019-11-25 15:20:31 -05:00
parent c60cafbf98
commit 1f07ea7ee7
11 changed files with 38 additions and 30 deletions

View File

@ -94,6 +94,10 @@ class FunctionCasingChecker implements AfterFunctionCallAnalysisInterface, After
$function_id $function_id
); );
if (!$function_storage->cased_name) {
return;
}
$function_name_parts = explode('\\', $function_storage->cased_name); $function_name_parts = explode('\\', $function_storage->cased_name);
if (end($function_name_parts) !== end($expr->name->parts)) { if (end($function_name_parts) !== end($expr->name->parts)) {

View File

@ -49,18 +49,13 @@
</plugins> </plugins>
<issueHandlers> <issueHandlers>
<MisplacedRequiredParam errorLevel="suppress"/>
<PossiblyNullOperand errorLevel="suppress"/> <PossiblyNullOperand errorLevel="suppress"/>
<MissingConstructor>
<DeprecatedMethod>
<errorLevel type="suppress"> <errorLevel type="suppress">
<file name="src/Psalm/Internal/Scanner/FunctionDocblockComment.php"/> <directory name="tests" />
<file name="src/Psalm/Storage/FunctionLikeStorage.php"/>
<file name="src/Psalm/Storage/MethodStorage.php"/>
<file name="src/Psalm/Storage/PropertyStorage.php"/>
<file name="src/Psalm/Internal/Scope/CaseScope.php"/>
</errorLevel> </errorLevel>
</MissingConstructor> </DeprecatedMethod>
<DeprecatedProperty errorLevel="suppress"/>
<DeprecatedMethod> <DeprecatedMethod>
<errorLevel type="suppress"> <errorLevel type="suppress">

View File

@ -751,6 +751,7 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
} }
if (!($storage instanceof MethodStorage) if (!($storage instanceof MethodStorage)
|| !$storage->cased_name
|| $storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE || $storage->visibility === ClassLikeAnalyzer::VISIBILITY_PRIVATE
) { ) {
if ($this->function instanceof Closure) { if ($this->function instanceof Closure) {
@ -804,6 +805,7 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
if ($storage instanceof MethodStorage if ($storage instanceof MethodStorage
&& $class_storage && $class_storage
&& $storage->cased_name
&& $storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PRIVATE && $storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PRIVATE
) { ) {
$method_id_lc = strtolower($this->getMethodId()); $method_id_lc = strtolower($this->getMethodId());
@ -816,7 +818,6 @@ abstract class FunctionLikeAnalyzer extends SourceAnalyzer
$method_id_lc $method_id_lc
); );
/** @var ClassMethod $this->function */
$method_name_lc = strtolower($storage->cased_name); $method_name_lc = strtolower($storage->cased_name);
if (!isset($class_storage->overridden_method_ids[$method_name_lc])) { if (!isset($class_storage->overridden_method_ids[$method_name_lc])) {

View File

@ -520,7 +520,7 @@ class MethodAnalyzer extends FunctionLikeAnalyzer
$prevent_method_signature_mismatch = true $prevent_method_signature_mismatch = true
) { ) {
$implementer_method_id = $implementer_classlike_storage->name . '::' $implementer_method_id = $implementer_classlike_storage->name . '::'
. strtolower($guide_method_storage->cased_name); . strtolower($guide_method_storage->cased_name ?: '');
$implementer_declaring_method_id = $codebase->methods->getDeclaringMethodId($implementer_method_id); $implementer_declaring_method_id = $codebase->methods->getDeclaringMethodId($implementer_method_id);
@ -694,7 +694,7 @@ class MethodAnalyzer extends FunctionLikeAnalyzer
$template_types = []; $template_types = [];
foreach ($map as $key => $type) { foreach ($map as $key => $type) {
if (is_string($key)) { if (is_string($key) && $implementer_method_storage->defining_fqcln) {
$template_types[$key][$implementer_method_storage->defining_fqcln] = [ $template_types[$key][$implementer_method_storage->defining_fqcln] = [
$type, $type,
]; ];

View File

@ -259,9 +259,7 @@ class SwitchAnalyzer
} }
$case_context->parent_context = $context; $case_context->parent_context = $context;
$case_scope = $case_context->case_scope = new CaseScope(); $case_scope = $case_context->case_scope = new CaseScope($case_context);
$case_scope->parent_context = $case_context;
$case_equality_expr = null; $case_equality_expr = null;

View File

@ -527,7 +527,7 @@ class Methods
list($fq_class_name, $method_name) = explode('::', $method_id); list($fq_class_name, $method_name) = explode('::', $method_id);
return $this->classlike_storage_provider->get($fq_class_name)->methods[$method_name]->variadic; return $this->classlike_storage_provider->get($fq_class_name)->methods[$method_name ?: '']->variadic;
} }
/** /**

View File

@ -638,8 +638,13 @@ class Scanner
if ($this->codebase->register_autoload_files) { if ($this->codebase->register_autoload_files) {
foreach ($file_storage->functions as $function_storage) { foreach ($file_storage->functions as $function_storage) {
if (!$this->codebase->functions->hasStubbedFunction($function_storage->cased_name)) { if ($function_storage->cased_name
$this->codebase->functions->addGlobalFunction($function_storage->cased_name, $function_storage); && !$this->codebase->functions->hasStubbedFunction($function_storage->cased_name)
) {
$this->codebase->functions->addGlobalFunction(
$function_storage->cased_name,
$function_storage
);
} }
} }

View File

@ -25,6 +25,11 @@ class CaseScope
*/ */
public $break_vars; public $break_vars;
public function __construct(Context $parent_context)
{
$this->parent_context = $parent_context;
}
public function __destruct() public function __destruct()
{ {
/** @psalm-suppress PossiblyNullPropertyAssignmentValue */ /** @psalm-suppress PossiblyNullPropertyAssignmentValue */

View File

@ -57,7 +57,7 @@ class FunctionLikeStorage
public $signature_return_type_location; public $signature_return_type_location;
/** /**
* @var string * @var ?string
*/ */
public $cased_name; public $cased_name;
@ -67,12 +67,12 @@ class FunctionLikeStorage
public $suppressed_issues = []; public $suppressed_issues = [];
/** /**
* @var bool * @var ?bool
*/ */
public $deprecated; public $deprecated;
/** /**
* @var bool * @var ?bool
*/ */
public $internal; public $internal;
@ -84,7 +84,7 @@ class FunctionLikeStorage
/** /**
* @var bool * @var bool
*/ */
public $variadic; public $variadic = false;
/** /**
* @var bool * @var bool
@ -92,7 +92,7 @@ class FunctionLikeStorage
public $returns_by_ref = false; public $returns_by_ref = false;
/** /**
* @var int * @var ?int
*/ */
public $required_param_count; public $required_param_count;

View File

@ -6,22 +6,22 @@ class MethodStorage extends FunctionLikeStorage
/** /**
* @var bool * @var bool
*/ */
public $is_static; public $is_static = false;
/** /**
* @var int * @var int
*/ */
public $visibility; public $visibility = 0;
/** /**
* @var bool * @var bool
*/ */
public $final; public $final = false;
/** /**
* @var bool * @var bool
*/ */
public $abstract; public $abstract = false;
/** /**
* @var bool * @var bool
@ -44,7 +44,7 @@ class MethodStorage extends FunctionLikeStorage
public $inherited_return_type = false; public $inherited_return_type = false;
/** /**
* @var string * @var ?string
*/ */
public $defining_fqcln; public $defining_fqcln;

View File

@ -10,14 +10,14 @@ class PropertyStorage
use CustomMetadataTrait; use CustomMetadataTrait;
/** /**
* @var bool * @var ?bool
*/ */
public $is_static; public $is_static;
/** /**
* @var int * @var int
*/ */
public $visibility; public $visibility = 0;
/** /**
* @var CodeLocation|null * @var CodeLocation|null