mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
Fix rules regarding always-defined object-like properties
This commit is contained in:
parent
7637eb5075
commit
a4a618c9e5
@ -622,7 +622,7 @@ abstract class Type
|
||||
$combined_type->failed_reconciliation = true;
|
||||
}
|
||||
|
||||
if ($type_1->possibly_undefined && $type_2->possibly_undefined) {
|
||||
if ($type_1->possibly_undefined || $type_2->possibly_undefined) {
|
||||
$combined_type->possibly_undefined = true;
|
||||
}
|
||||
|
||||
|
@ -141,8 +141,11 @@ class ObjectLike extends \Psalm\Type\Atomic
|
||||
public function getGenericValueType()
|
||||
{
|
||||
$value_type = null;
|
||||
$any_value_defined = false;
|
||||
|
||||
foreach ($this->properties as $property) {
|
||||
$any_value_defined = $any_value_defined || !$property->possibly_undefined;
|
||||
|
||||
if ($value_type === null) {
|
||||
$value_type = clone $property;
|
||||
} else {
|
||||
@ -154,7 +157,7 @@ class ObjectLike extends \Psalm\Type\Atomic
|
||||
throw new \UnexpectedValueException('$value_type should not be null here');
|
||||
}
|
||||
|
||||
$value_type->possibly_undefined = false;
|
||||
$value_type->possibly_undefined = !$any_value_defined;
|
||||
|
||||
return $value_type;
|
||||
}
|
||||
@ -166,6 +169,7 @@ class ObjectLike extends \Psalm\Type\Atomic
|
||||
{
|
||||
$key_types = [];
|
||||
$value_type = null;
|
||||
$any_value_defined = false;
|
||||
|
||||
foreach ($this->properties as $key => $property) {
|
||||
if (is_int($key)) {
|
||||
@ -174,6 +178,8 @@ class ObjectLike extends \Psalm\Type\Atomic
|
||||
$key_types[] = new Type\Atomic\TString();
|
||||
}
|
||||
|
||||
$any_value_defined = $any_value_defined || !$property->possibly_undefined;
|
||||
|
||||
if ($value_type === null) {
|
||||
$value_type = clone $property;
|
||||
} else {
|
||||
@ -185,6 +191,8 @@ class ObjectLike extends \Psalm\Type\Atomic
|
||||
throw new \UnexpectedValueException('$value_type should not be null here');
|
||||
}
|
||||
|
||||
$value_type->possibly_undefined = !$any_value_defined;
|
||||
|
||||
return new TArray([Type::combineTypes($key_types), $value_type]);
|
||||
}
|
||||
|
||||
|
@ -300,6 +300,40 @@ class FileManipulationTest extends TestCase
|
||||
['MissingReturnType'],
|
||||
true,
|
||||
],
|
||||
'addMissingObjectLikeReturnTypeSeparateStatements70' => [
|
||||
'<?php
|
||||
function foo() {
|
||||
if (rand(0, 1)) {
|
||||
return ["a" => "hello", "b" => "hello again"];
|
||||
}
|
||||
|
||||
if (rand(0, 1)) {
|
||||
return ["a" => "hello", "b" => "hello again"];
|
||||
}
|
||||
|
||||
return ["a" => "goodbye"];
|
||||
}',
|
||||
'<?php
|
||||
/**
|
||||
* @return string[]
|
||||
*
|
||||
* @psalm-return array{a:string, b?:string}
|
||||
*/
|
||||
function foo(): array {
|
||||
if (rand(0, 1)) {
|
||||
return ["a" => "hello", "b" => "hello again"];
|
||||
}
|
||||
|
||||
if (rand(0, 1)) {
|
||||
return ["a" => "hello", "b" => "hello again"];
|
||||
}
|
||||
|
||||
return ["a" => "goodbye"];
|
||||
}',
|
||||
'7.0',
|
||||
['MissingReturnType'],
|
||||
true,
|
||||
],
|
||||
'addMissingStringArrayReturnTypeFromCall71' => [
|
||||
'<?php
|
||||
/** @return string[] */
|
||||
|
Loading…
x
Reference in New Issue
Block a user