1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +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; $quote_char = null;
$escaped = false; $escaped = false;
$expectation = false;
for ($i = 0, $l = strlen($return_block); $i < $l; ++$i) { for ($i = 0, $l = strlen($return_block); $i < $l; ++$i) {
$char = $return_block[$i]; $char = $return_block[$i];
@ -827,7 +826,7 @@ class CommentAnalyzer
} }
if ($char === ':' && $last_char === ')') { if ($char === ':' && $last_char === ')') {
$expectation = true; $expects_callable_return = true;
$type .= $char; $type .= $char;
@ -848,8 +847,8 @@ class CommentAnalyzer
throw new DocblockParseException('Invalid string ' . $return_block); throw new DocblockParseException('Invalid string ' . $return_block);
} }
} elseif ($char === ' ') { } elseif ($char === ' ') {
if ($brackets || $expectation) { if ($brackets) {
$expectation = false; $expects_callable_return = false;
continue; continue;
} }
@ -875,8 +874,6 @@ class CommentAnalyzer
continue; continue;
} }
//var_dump($type);
$remaining = trim(substr($return_block, $i + 1)); $remaining = trim(substr($return_block, $i + 1));
if ($remaining) { if ($remaining) {
@ -886,7 +883,7 @@ class CommentAnalyzer
return [$type]; return [$type];
} }
$expectation = false; $expects_callable_return = false;
$type .= $char; $type .= $char;
} }

View File

@ -809,9 +809,16 @@ class PropertyFetchAnalyzer
$property = $class_storage->properties[$prop_name]; $property = $class_storage->properties[$prop_name];
if ($var_id) { if ($var_id) {
$context->vars_in_scope[$var_id] = $property->type if ($property->type) {
? clone $property->type $context->vars_in_scope[$var_id] = ExpressionAnalyzer::fleshOutType(
: Type::getMixed(); $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]; $stmt->inferredType = clone $context->vars_in_scope[$var_id];

View File

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