1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #1267 - flesh out self static property types

This commit is contained in:
Matthew Brown 2019-02-01 08:58:52 -05:00
parent 21d383d6c6
commit 509709d6bc
5 changed files with 69 additions and 11 deletions

View File

@ -787,7 +787,6 @@ class CommentAnalyzer
$quote_char = null;
$escaped = false;
$expectation = false;
for ($i = 0, $l = strlen($return_block); $i < $l; ++$i) {
$char = $return_block[$i];
@ -827,7 +826,7 @@ class CommentAnalyzer
}
if ($char === ':' && $last_char === ')') {
$expectation = true;
$expects_callable_return = true;
$type .= $char;
@ -848,8 +847,8 @@ class CommentAnalyzer
throw new DocblockParseException('Invalid string ' . $return_block);
}
} elseif ($char === ' ') {
if ($brackets || $expectation) {
$expectation = false;
if ($brackets) {
$expects_callable_return = false;
continue;
}
@ -875,8 +874,6 @@ class CommentAnalyzer
continue;
}
//var_dump($type);
$remaining = trim(substr($return_block, $i + 1));
if ($remaining) {
@ -886,7 +883,7 @@ class CommentAnalyzer
return [$type];
}
$expectation = false;
$expects_callable_return = false;
$type .= $char;
}

View File

@ -809,9 +809,16 @@ class PropertyFetchAnalyzer
$property = $class_storage->properties[$prop_name];
if ($var_id) {
$context->vars_in_scope[$var_id] = $property->type
? clone $property->type
: Type::getMixed();
if ($property->type) {
$context->vars_in_scope[$var_id] = ExpressionAnalyzer::fleshOutType(
$codebase,
clone $property->type,
$declaring_property_class,
$declaring_property_class
);
} else {
$context->vars_in_scope[$var_id] = Type::getMixed();
}
$stmt->inferredType = clone $context->vars_in_scope[$var_id];

View File

@ -1090,7 +1090,7 @@ class CallableTest extends TestCase
};',
'error_message' => 'DuplicateParam'
],
'callableWithSpacesBadVarArg' => [
'callableWithSpaceAfterColonBadVarArg' => [
'<?php
class C {
/**
@ -1098,6 +1098,38 @@ class CallableTest extends TestCase
*/
public $p;
public function __construct() {
$this->p = function (string $s, string $t): stdClass {
return new stdClass;
};
}
}',
'error_message' => 'InvalidPropertyAssignmentValue',
],
'callableWithSpaceBeforeColonBadVarArg' => [
'<?php
class C {
/**
* @var callable(string, string) :bool $p
*/
public $p;
public function __construct() {
$this->p = function (string $s, string $t): stdClass {
return new stdClass;
};
}
}',
'error_message' => 'InvalidPropertyAssignmentValue',
],
'callableWithSpacesEitherSideOfColonBadVarArg' => [
'<?php
class C {
/**
* @var callable(string, string) : bool $p
*/
public $p;
public function __construct() {
$this->p = function (string $s, string $t): stdClass {
return new stdClass;

View File

@ -281,6 +281,16 @@ class MethodCallTest extends TestCase
}
}'
],
'varSelfCall' => [
'<?php
class Foo {
/** @var self */
public static $current;
public function bar() : void {}
}
Foo::$current->bar();',
],
];
}

View File

@ -1254,6 +1254,18 @@ class PropertyTypeTest extends TestCase
}
}',
],
'staticVarSelf' => [
'<?php
class Foo {
/** @var self */
public static $current;
}
$a = Foo::$current;',
[
'$a' => 'Foo',
]
],
];
}