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

Fix #834 - warn about __toString in array offset

This commit is contained in:
Matthew Brown 2018-08-31 20:24:50 -04:00
parent d5b0d5a996
commit 84d7504036
2 changed files with 22 additions and 5 deletions

View File

@ -441,6 +441,8 @@ class ArrayFetchChecker
}
}
} else {
$generic_key_type = $type->getGenericKeyType();
if ($key_value !== null) {
if (isset($type->properties[$key_value]) || $replacement_type) {
$has_valid_offset = true;
@ -492,10 +494,12 @@ class ArrayFetchChecker
$array_access_type = Type::getMixed();
}
} elseif (TypeChecker::isContainedBy(
} elseif ((TypeChecker::isContainedBy(
$codebase,
$offset_type,
$type->getGenericKeyType(),
$generic_key_type->isMixed()
? new Type\Union([ new TInt, new TString ])
: $generic_key_type,
true,
$offset_type->ignore_falsable_issues,
$has_scalar_match,
@ -505,7 +509,8 @@ class ArrayFetchChecker
$type_coerced_from_scalar
)
|| $type_coerced_from_scalar
|| $in_assignment
|| $in_assignment)
&& !$to_string_cast
) {
if ($replacement_type) {
$generic_params = Type::combineUnionTypes(
@ -514,7 +519,7 @@ class ArrayFetchChecker
);
$new_key_type = Type::combineUnionTypes(
$type->getGenericKeyType(),
$generic_key_type,
$offset_type
);
@ -552,7 +557,7 @@ class ArrayFetchChecker
$has_valid_offset = true;
} else {
if (!$inside_isset || $type->sealed) {
$expected_offset_types[] = (string)$type->getGenericKeyType()->getId();
$expected_offset_types[] = (string)$generic_key_type->getId();
}
$array_access_type = Type::getMixed();

View File

@ -265,6 +265,18 @@ class ArrayAccessTest extends TestCase
}',
'error_message' => 'InvalidArrayOffset',
],
'toStringOffset' => [
'<?php
class Foo {
public function __toString() {
return "Foo";
}
}
$a = ["Foo" => "bar"];
echo $a[new Foo];',
'error_message' => 'InvalidArrayOffset',
],
];
}
}