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

Fix #458 - fully support optional array keys

This commit is contained in:
Matthew Brown 2018-03-17 18:05:50 -04:00
parent 4175d1a887
commit e61815bb72
6 changed files with 26 additions and 3 deletions

View File

@ -57,6 +57,10 @@ class TypeChecker
return true;
}
if ($input_type->possibly_undefined && !$container_type->possibly_undefined) {
return false;
}
foreach ($input_type->getTypes() as $input_type_part) {
if ($input_type_part instanceof TNull && $ignore_null) {
continue;

View File

@ -284,7 +284,7 @@ class FunctionDocblockManipulator
if ($docblock) {
$parsed_docblock = CommentChecker::parseDocComment((string)$docblock, null, true);
} else {
$parsed_docblock = ['description' => ''];
$parsed_docblock = ['description' => '', 'specials' => []];
}
foreach ($this->new_phpdoc_param_types as $param_name => $phpdoc_type) {

View File

@ -55,7 +55,7 @@ class FunctionDocblockComment
public $template_types = [];
/**
* @var array<int, array{template_type: string, param_name: string, line_number: int}>
* @var array<int, array{template_type: string, param_name: string, line_number?: int}>
*/
public $template_typeofs = [];
}

View File

@ -210,7 +210,7 @@ class IssueBuffer
}
/**
* @param array<int, array{severity: string, line_from: int, type: string, message: string,
* @param 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}> $issues_data
*

View File

@ -154,6 +154,8 @@ class ObjectLike extends \Psalm\Type\Atomic
throw new \UnexpectedValueException('$value_type should not be null here');
}
$value_type->possibly_undefined = false;
return $value_type;
}
@ -183,6 +185,8 @@ class ObjectLike extends \Psalm\Type\Atomic
throw new \UnexpectedValueException('$value_type should not be null here');
}
$value_type->possibly_undefined = false;
return new TArray([Type::combineTypes($key_types), $value_type]);
}

View File

@ -464,6 +464,13 @@ class ReturnTypeTest extends TestCase
'assertions' => [],
'error_levels' => ['MixedAssignment', 'MixedArgument'],
],
'objectLikeArrayOptionalKeyReturn' => [
'<?php
/** @return array{a: int, b?: int} */
function foo() : array {
return rand(0, 1) ? ["a" => 1, "b" => 2] : ["a" => 2];
}',
],
];
}
@ -710,6 +717,14 @@ class ReturnTypeTest extends TestCase
}',
'error_message' => 'InvalidReturnStatement',
],
'objectLikeArrayOptionalKeyWithNonOptionalReturn' => [
'<?php
/** @return array{a: int, b: int} */
function foo() : array {
return rand(0, 1) ? ["a" => 1, "b" => 2] : ["a" => 2];
}',
'error_message' => 'LessSpecificReturnStatement',
],
];
}
}