Skip to content
Permalink
Browse files

Resolve docblock self refs as soon as possible

Fixes #1827
  • Loading branch information...
muglug committed Jun 21, 2019
1 parent 7b4dbb1 commit 2a4072b1be834287556790398926b9e894ce4cfd
Showing with 53 additions and 19 deletions.
  1. +8 −4 src/Psalm/Internal/Visitor/ReflectorVisitor.php
  2. +25 −15 src/Psalm/Type.php
  3. +20 −0 tests/AnnotationTest.php
@@ -2156,7 +2156,8 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_m
$storage,
$docblock_info->params,
$stmt,
$fake_method
$fake_method,
$class_storage ? $class_storage->name : null
);
}
@@ -2286,7 +2287,8 @@ function (FunctionLikeParameter $p) {
$docblock_return_type,
$this->aliases,
$this->function_template_types + $this->class_template_types,
$this->type_aliases
$this->type_aliases,
$class_storage ? $class_storage->name : null
);
$storage->return_type = Type::parseTokens(
@@ -2463,7 +2465,8 @@ private function improveParamsFromDocblock(
FunctionLikeStorage $storage,
array $docblock_params,
PhpParser\Node\FunctionLike $function,
bool $fake_method
bool $fake_method,
?string $fq_classlike_name
) {
$base = $this->fq_classlike_names
? $this->fq_classlike_names[count($this->fq_classlike_names) - 1] . '::'
@@ -2548,7 +2551,8 @@ private function improveParamsFromDocblock(
$docblock_param['type'],
$this->aliases,
$this->function_template_types + $this->class_template_types,
$this->type_aliases
$this->type_aliases,
$fq_classlike_name
),
null,
$this->function_template_types + $this->class_template_types
@@ -146,8 +146,10 @@ public static function parseTokens(
*
* @return string
*/
private static function fixScalarTerms($type_string, array $php_version = null)
{
private static function fixScalarTerms(
string $type_string,
?array $php_version = null
) : string {
$type_string_lc = strtolower($type_string);
switch ($type_string_lc) {
@@ -878,18 +880,18 @@ public static function tokenize($string_type, $ignore_space = true)
}
/**
* @param string $string_type
* @param Aliases $aliases
* @param array<string, mixed>|null $template_type_map
* @param array<string, array<int, string>>|null $type_aliases
*
* @return array<int, string>
*/
public static function fixUpLocalType(
$string_type,
string $string_type,
Aliases $aliases,
array $template_type_map = null,
array $type_aliases = null
array $type_aliases = null,
?string $self_fqcln = null,
?string $parent_fqcln = null
) {
$type_tokens = self::tokenize($string_type);
@@ -926,7 +928,19 @@ public static function fixUpLocalType(
$string_type_token = preg_replace('/(.+)\$.*/', '$1', $string_type_token);
}
$type_tokens[$i] = $string_type_token = self::fixScalarTerms($string_type_token);
$type_tokens[$i]
= $string_type_token
= self::fixScalarTerms($string_type_token);
if ($string_type_token === 'self' && $self_fqcln) {
$type_tokens[$i] = $self_fqcln;
continue;
}
if ($string_type_token === 'parent' && $parent_fqcln) {
$type_tokens[$i] = $parent_fqcln;
continue;
}
if (isset(self::PSALM_RESERVED_WORDS[$string_type_token])) {
continue;
@@ -974,14 +988,10 @@ public static function fixUpLocalType(
return $type_tokens;
}
/**
* @param string $class
* @param Aliases $aliases
*
* @return string
*/
public static function getFQCLNFromString($class, Aliases $aliases)
{
public static function getFQCLNFromString(
string $class,
Aliases $aliases
) : string {
if ($class === '') {
throw new \InvalidArgumentException('$class cannot be empty');
}
@@ -1038,6 +1038,26 @@ function foo(array $foo) : int {
*/
$foo = ["foo" => "", "bar" => "", "baz" => ""];'
],
'inheritedSelfAnnotation' => [
'<?php
interface I {
/**
* @param self $i
* @return self
*/
function foo(self $i) : self;
}
class C implements I {
public function foo(I $i) : I {
return $i;
}
}
function takeI(I $i) : I {
return (new C)->foo($i);
}',
],
];
}

0 comments on commit 2a4072b

Please sign in to comment.
You can’t perform that action at this time.