1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix tests to all have property types

This commit is contained in:
Matthew Brown 2016-11-01 14:32:19 -04:00
parent b78e83c4c0
commit 5cc5bd4ed4
4 changed files with 53 additions and 17 deletions

View File

@ -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());

View File

@ -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('<?php
class A {
/** @var string */
public $a;
/** @var string */
public $b;
public function foo() : string
{
list($this->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('<?php
class A {
/** @var int */
public $a;
/** @var string */
public $b;
public function foo() : string

View File

@ -125,12 +125,9 @@ class Php70Test extends PHPUnit_Framework_TestCase
}
class Application {
/** @var Logger|null */
private $logger;
public function getLogger(): Logger {
return $this->logger;
}
public function setLogger(Logger $logger) {
$this->logger = $logger;
}

View File

@ -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('<?php
class One {
/** @var int|null */
public $two;
public function foo() {}
@ -390,6 +394,7 @@ class TypeTest extends PHPUnit_Framework_TestCase
{
$stmts = self::$_parser->parse('<?php
class One {
/** @var string */
public $a;
public function foo() {}
@ -945,6 +950,7 @@ class TypeTest extends PHPUnit_Framework_TestCase
}
class B {
/** @var One|null */
public $one;
public function bar(One $one = null) {