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

Fix #5258 - allow ReflectionParameeter::hasType() to inform getType() return

This commit is contained in:
Matt Brown 2021-03-06 16:54:23 -05:00
parent ca331f5afa
commit c97ee5ccdb
3 changed files with 69 additions and 2 deletions

View File

@ -455,6 +455,18 @@ class FunctionLikeDocblockParser
$line_parts[0] = CommentAnalyzer::sanitizeDocblockType($line_parts[0]);
if ($line_parts[1][0] === '$') {
$param_name_parts = explode('->', $line_parts[1]);
foreach ($param_name_parts as $i => $param_name_part) {
if (substr($param_name_part, -2) === '()') {
$param_name_parts[$i] = strtolower($param_name_part);
}
}
$line_parts[1] = implode('->', $param_name_parts);
}
return $line_parts;
}

View File

@ -20,7 +20,7 @@ class ReflectionClass implements Reflector {
/**
* @return class-string<T>
*/
public function getName(): string;
public function getName(): string {}
/**
* @param mixed ...$args
@ -39,7 +39,7 @@ class ReflectionClass implements Reflector {
/**
* @return T
*/
public function newInstanceWithoutConstructor(): object;
public function newInstanceWithoutConstructor(): object {}
/**
* @return ?array<string>
@ -99,3 +99,15 @@ class ReflectionClassConstant
*/
public function getAttributes(?string $name = null, int $flags = 0): array {}
}
/**
* @psalm-immutable
*/
class ReflectionParameter {
/**
* @psalm-assert-if-true ReflectionType $this->getType()
*/
public function hasType() : bool {}
public function getType() : ?ReflectionType {}
}

View File

@ -1453,6 +1453,40 @@ class AssertAnnotationTest extends TestCase
return $value;
}'
],
'implicitReflectionParameterAssertion' => [
'<?php
$method = new ReflectionMethod(stdClass::class);
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
if ($parameter->hasType()) {
$parameter->getType()->__toString();
}
}',
],
'withHasTypeCall' => [
'<?php
/**
* @psalm-immutable
*/
class Param {
/**
* @psalm-assert-if-true ReflectionType $this->getType()
*/
public function hasType() : bool {
return true;
}
public function getType() : ?ReflectionType {
return null;
}
}
function takesParam(Param $p) : void {
if ($p->hasType()) {
echo $p->getType()->__toString();
}
}',
],
];
}
@ -1709,6 +1743,15 @@ class AssertAnnotationTest extends TestCase
}',
'error_message' => 'RedundantConditionGivenDocblockType',
],
'withoutHasTypeCall' => [
'<?php
$method = new ReflectionMethod(stdClass::class);
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
$parameter->getType()->__toString();
}',
'error_message' => 'PossiblyNullReference',
],
];
}
}