1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Take property defaults into account when suggesting missing type

This commit is contained in:
Matthew Brown 2017-04-14 23:46:55 -04:00
parent 8d46c65815
commit 0c4824b2cc
2 changed files with 58 additions and 3 deletions

View File

@ -1028,12 +1028,19 @@ abstract class ClassLikeChecker extends SourceChecker implements StatementsSourc
foreach ($stmt->props as $property) {
$property_type_location = null;
$default_type = null;
if (!$property_group_type) {
if (!$property->default || !$config->use_property_default_for_type) {
$property_type = false;
if ($property->default) {
$default_type = StatementsChecker::getSimpleType($property->default);
if (!$config->use_property_default_for_type) {
$property_type = false;
} else {
$property_type = $default_type ?: Type::getMixed();
}
} else {
$property_type = StatementsChecker::getSimpleType($property->default) ?: Type::getMixed();
$property_type = false;
}
} else {
if ($property_type_line_number) {
@ -1050,6 +1057,7 @@ abstract class ClassLikeChecker extends SourceChecker implements StatementsSourc
$storage->properties[$property->name]->location = new CodeLocation($this, $property);
$storage->properties[$property->name]->type_location = $property_type_location;
$storage->properties[$property->name]->has_default = $property->default ? true : false;
$storage->properties[$property->name]->suggested_type = $property_group_type ? null : $default_type;
if ($stmt->isPublic()) {
$storage->properties[$property->name]->visibility = self::VISIBILITY_PUBLIC;

View File

@ -271,6 +271,53 @@ class PropertyTypeTest extends PHPUnit_Framework_TestCase
$file_checker->visitAndAnalyzeMethods();
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage MissingPropertyType - somefile.php:3 - Property A::$foo does not have a declared type - consider int
* @return void
*/
public function testMissingPropertyTypeWithConstructorInitInPrivateMethod()
{
$this->markTestSkipped('Doesnt yet work');
$stmts = self::$parser->parse('<?php
class A {
public $foo;
public function __construct() : void {
$this->makeValue();
}
private function makeValue() : void {
$this->foo = 5;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage MissingPropertyType - somefile.php:3 - Property A::$foo does not have a declared type - consider int|null
* @return void
*/
public function testMissingPropertyTypeWithConstructorInitAndNullDefault()
{
$stmts = self::$parser->parse('<?php
class A {
public $foo = null;
public function __construct() : void {
$this->foo = 5;
}
}
');
$file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts);
$file_checker->visitAndAnalyzeMethods();
}
/**
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage InvalidPropertyAssignment