1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Array access on objects should always be treated as method call

This commit is contained in:
Matthew Brown 2019-02-26 21:24:30 -05:00
parent 58b6ce5c03
commit b310cc07b5
2 changed files with 10 additions and 12 deletions

View File

@ -750,14 +750,7 @@ class ArrayFetchAnalyzer
$iterator_class_type = $fake_method_call->inferredType ?? null;
$array_access_type = $iterator_class_type ?: Type::getMixed();
} elseif ((strtolower($type->value) === 'arrayaccess'
|| (($codebase->classExists($type->value)
&& $codebase->classImplements($type->value, 'ArrayAccess'))
|| ($codebase->interfaceExists($type->value)
&& $codebase->interfaceExtends($type->value, 'ArrayAccess'))
))
&& ($stmt->dim || $in_assignment)
) {
} else {
if ($in_assignment) {
$fake_method_call = new PhpParser\Node\Expr\MethodCall(
$stmt->var,
@ -785,7 +778,14 @@ class ArrayFetchAnalyzer
$stmt->var,
new PhpParser\Node\Identifier('offsetGet', $stmt->var->getAttributes()),
[
new PhpParser\Node\Arg($stmt->dim)
new PhpParser\Node\Arg(
$stmt->dim
? $stmt->dim
: new PhpParser\Node\Expr\ConstFetch(
new PhpParser\Node\Name('null'),
$stmt->var->getAttributes()
)
)
]
);
}
@ -808,8 +808,6 @@ class ArrayFetchAnalyzer
$iterator_class_type = $fake_method_call->inferredType ?? null;
$array_access_type = $iterator_class_type ?: Type::getMixed();
} else {
$non_array_types[] = (string)$type;
}
} elseif (!$array_type->hasMixed()) {
$non_array_types[] = (string)$type;

View File

@ -1087,7 +1087,7 @@ class ArrayAssignmentTest extends TestCase
'<?php
class A {}
(new A)["b"] = 1;',
'error_message' => 'InvalidArrayAssignment',
'error_message' => 'UndefinedMethod',
],
'invalidArrayAccess' => [
'<?php