diff --git a/src/Psalm/Checker/Statements/Expression/AssignmentChecker.php b/src/Psalm/Checker/Statements/Expression/AssignmentChecker.php index 4ee2c7212..33c243fff 100644 --- a/src/Psalm/Checker/Statements/Expression/AssignmentChecker.php +++ b/src/Psalm/Checker/Statements/Expression/AssignmentChecker.php @@ -101,6 +101,13 @@ class AssignmentChecker continue; } + if ($assign_value instanceof PhpParser\Node\Expr\Array_ + && isset($assign_value->items[$offset]->value->inferredType) + ) { + self::check($statements_checker, $var, $assign_value->items[$offset]->value, $context, $doc_comment); + continue; + } + $list_var_id = ExpressionChecker::getVarId( $var, $statements_checker->getAbsoluteClass(), @@ -108,19 +115,14 @@ class AssignmentChecker $statements_checker->getAliasedClasses() ); - if ($assign_value instanceof PhpParser\Node\Expr\Array_ - && isset($assign_value->items[$offset]->value->inferredType) - ) { - $array_type = $assign_value->items[$offset]->value->inferredType; - } - elseif (isset($return_type->types['array']) && $return_type->types['array'] instanceof Type\Generic) { - $array_type = $return_type->types['array']->type_params[1]; - } - else { - $array_type = null; - } - if ($list_var_id) { + if (isset($return_type->types['array']) && $return_type->types['array'] instanceof Type\Generic) { + $array_type = $return_type->types['array']->type_params[1]; + } + else { + $array_type = null; + } + $context->vars_in_scope[$list_var_id] = $array_type ? clone $array_type : Type::getMixed(); $context->vars_possibly_in_scope[$list_var_id] = true; $statements_checker->registerVariable($list_var_id, $var->getLine()); diff --git a/tests/ListTest.php b/tests/ListTest.php index fe2074e21..046666574 100644 --- a/tests/ListTest.php +++ b/tests/ListTest.php @@ -9,7 +9,7 @@ use Psalm\Context; use Psalm\Checker\TypeChecker; use Psalm\Type; -class InterfaceTest extends PHPUnit_Framework_TestCase +class ListTest extends PHPUnit_Framework_TestCase { protected static $_parser; @@ -71,7 +71,38 @@ class InterfaceTest extends PHPUnit_Framework_TestCase { $stmts = self::$_parser->parse('a, $this->b) = ["a", "b"]; + + return $this->a; + } + } + '); + + $file_checker = new \Psalm\Checker\FileChecker('somefile.php', $stmts); + $context = new Context('somefile.php'); + $file_checker->check(true, true, $context); + } + + /** + * @expectedException Psalm\Exception\CodeException + * @expectedExceptionMessage InvalidPropertyAssignment - somefile.php:11 + */ + public function testThisVarWithBadType() + { + $stmts = self::$_parser->parse('logger; - } - public function setLogger(Logger $logger) { $this->logger = $logger; } diff --git a/tests/TypeTest.php b/tests/TypeTest.php index 4d7d07f03..299ed8751 100644 --- a/tests/TypeTest.php +++ b/tests/TypeTest.php @@ -162,6 +162,7 @@ class TypeTest extends PHPUnit_Framework_TestCase } class B { + /** @var A|null */ public $a; public function bar(A $a = null) { @@ -182,6 +183,7 @@ class TypeTest extends PHPUnit_Framework_TestCase } class B { + /** @var A|null */ public $a; public function bar(A $a = null) { @@ -202,6 +204,7 @@ class TypeTest extends PHPUnit_Framework_TestCase } class B { + /** @var A|null */ public $a; public function bar(A $a = null) { @@ -268,6 +271,7 @@ class TypeTest extends PHPUnit_Framework_TestCase { $stmts = self::$_parser->parse('parse('