mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
Fix #651 - allow Psalm to understand assertions on properties of array objects
This commit is contained in:
parent
6abbc23947
commit
c932ed7764
@ -55,8 +55,6 @@ class FunctionChecker extends FunctionLikeChecker
|
||||
if ($call_args) {
|
||||
if (in_array($call_map_key, ['str_replace', 'preg_replace', 'preg_replace_callback'], true)) {
|
||||
if (isset($call_args[2]->value->inferredType)) {
|
||||
|
||||
/** @var Type\Union */
|
||||
$subject_type = $call_args[2]->value->inferredType;
|
||||
|
||||
if (!$subject_type->hasString() && $subject_type->hasArray()) {
|
||||
@ -84,7 +82,6 @@ class FunctionChecker extends FunctionLikeChecker
|
||||
|
||||
if ($call_map_key === 'var_export') {
|
||||
if (isset($call_args[1]->value->inferredType)) {
|
||||
/** @var Type\Union */
|
||||
$subject_type = $call_args[1]->value->inferredType;
|
||||
|
||||
if ((string) $subject_type === 'true') {
|
||||
|
@ -254,7 +254,6 @@ class NewChecker extends \Psalm\Checker\Statements\Expression\CallChecker
|
||||
}
|
||||
|
||||
if ($fq_class_name === 'ArrayIterator' && isset($stmt->args[0]->value->inferredType)) {
|
||||
/** @var Type\Union */
|
||||
$first_arg_type = $stmt->args[0]->value->inferredType;
|
||||
|
||||
if ($first_arg_type->hasGeneric()) {
|
||||
|
@ -53,13 +53,13 @@ class PropertyFetchChecker
|
||||
$project_checker = $statements_checker->getFileChecker()->project_checker;
|
||||
$codebase = $project_checker->codebase;
|
||||
|
||||
$stmt_var_id = ExpressionChecker::getVarId(
|
||||
$stmt_var_id = ExpressionChecker::getArrayVarId(
|
||||
$stmt->var,
|
||||
$statements_checker->getFQCLN(),
|
||||
$statements_checker
|
||||
);
|
||||
|
||||
$var_id = ExpressionChecker::getVarId(
|
||||
$var_id = ExpressionChecker::getArrayVarId(
|
||||
$stmt,
|
||||
$statements_checker->getFQCLN(),
|
||||
$statements_checker
|
||||
|
@ -700,6 +700,16 @@ class ExpressionChecker
|
||||
}
|
||||
}
|
||||
|
||||
if ($stmt instanceof PhpParser\Node\Expr\PropertyFetch && is_string($stmt->name)) {
|
||||
$object_id = self::getArrayVarId($stmt->var, $this_class_name, $source);
|
||||
|
||||
if (!$object_id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $object_id . '->' . $stmt->name;
|
||||
}
|
||||
|
||||
return self::getVarId($stmt, $this_class_name, $source);
|
||||
}
|
||||
|
||||
|
@ -181,6 +181,24 @@ class IssetTest extends TestCase
|
||||
'<?php
|
||||
if (isset($foo["bar[]"])) {}',
|
||||
],
|
||||
'issetArrayOffsetAndProperty' => [
|
||||
'<?php
|
||||
class A {
|
||||
/** @var ?B */
|
||||
public $b;
|
||||
}
|
||||
class B {}
|
||||
|
||||
/**
|
||||
* @param A[] $arr
|
||||
*/
|
||||
function takesAList(array $arr) : B {
|
||||
if (isset($arr[1]->b)) {
|
||||
return $arr[1]->b;
|
||||
}
|
||||
throw new \Exception("bad");
|
||||
}',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user