From b310cc07b5ae07534aeba03818bb51427296369c Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Tue, 26 Feb 2019 21:24:30 -0500 Subject: [PATCH] Array access on objects should always be treated as method call --- .../Expression/Fetch/ArrayFetchAnalyzer.php | 20 +++++++++---------- tests/ArrayAssignmentTest.php | 2 +- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php index 459fa1385..d423bfdc9 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php @@ -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; diff --git a/tests/ArrayAssignmentTest.php b/tests/ArrayAssignmentTest.php index c0a0f3ad7..df31f94b4 100644 --- a/tests/ArrayAssignmentTest.php +++ b/tests/ArrayAssignmentTest.php @@ -1087,7 +1087,7 @@ class ArrayAssignmentTest extends TestCase ' 'InvalidArrayAssignment', + 'error_message' => 'UndefinedMethod', ], 'invalidArrayAccess' => [ '