Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3854cbc

Browse files
Useful PhpMethodReflection native type refactoring from #3966
1 parent 13d47f5 commit 3854cbc

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

‎src/Reflection/Php/PhpMethodReflection.php‎

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -302,30 +302,9 @@ public function isPublic(): bool
302302
private function getReturnType(): Type
303303
{
304304
if ($this->returnType === null) {
305-
$name = strtolower($this->getName());
306-
$returnType = $this->reflection->getReturnType();
307-
if ($returnType === null) {
308-
if (in_array($name, ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) {
309-
return $this->returnType = TypehintHelper::decideType(new VoidType(), $this->phpDocReturnType);
310-
}
311-
if ($name === '__tostring') {
312-
return $this->returnType = TypehintHelper::decideType(new StringType(), $this->phpDocReturnType);
313-
}
314-
if ($name === '__isset') {
315-
return $this->returnType = TypehintHelper::decideType(new BooleanType(), $this->phpDocReturnType);
316-
}
317-
if ($name === '__sleep') {
318-
return $this->returnType = TypehintHelper::decideType(new ArrayType(new IntegerType(), new StringType()), $this->phpDocReturnType);
319-
}
320-
if ($name === '__set_state') {
321-
return $this->returnType = TypehintHelper::decideType(new ObjectWithoutClassType(), $this->phpDocReturnType);
322-
}
323-
}
324-
325-
$this->returnType = TypehintHelper::decideTypeFromReflection(
326-
$returnType,
305+
$this->returnType = TypehintHelper::decideType(
306+
$this->getNativeReturnType(),
327307
$this->phpDocReturnType,
328-
$this->declaringClass,
329308
);
330309
}
331310

@@ -344,8 +323,28 @@ private function getPhpDocReturnType(): Type
344323
private function getNativeReturnType(): Type
345324
{
346325
if ($this->nativeReturnType === null) {
326+
$returnType = $this->reflection->getReturnType();
327+
if ($returnType === null) {
328+
$name = strtolower($this->getName());
329+
if (in_array($this->getName(), ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) {
330+
return $this->nativeReturnType = new VoidType();
331+
}
332+
if ($name === '__tostring') {
333+
return $this->nativeReturnType = new StringType();
334+
}
335+
if ($name === '__isset') {
336+
return $this->nativeReturnType = new BooleanType();
337+
}
338+
if ($name === '__sleep') {
339+
return $this->nativeReturnType = new ArrayType(new IntegerType(), new StringType());
340+
}
341+
if ($name === '__set_state') {
342+
return $this->nativeReturnType = new ObjectWithoutClassType();
343+
}
344+
}
345+
347346
$this->nativeReturnType = TypehintHelper::decideTypeFromReflection(
348-
$this->reflection->getReturnType(),
347+
$returnType,
349348
null,
350349
$this->declaringClass,
351350
);

‎src/Type/TypehintHelper.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ public static function decideTypeFromReflection(
6868
$type = ParserNodeTypeToPHPStanType::resolve($typeNode, $selfClass);
6969
if ($reflectionType->allowsNull()) {
7070
$type = TypeCombinator::addNull($type);
71-
} elseif ($phpDocType !== null) {
72-
$phpDocType = TypeCombinator::removeNull($phpDocType);
7371
}
7472

7573
return self::decideType($type, $phpDocType);
@@ -80,6 +78,9 @@ public static function decideType(
8078
?Type $phpDocType,
8179
): Type
8280
{
81+
if ($phpDocType !== null && $type->isNull()->no()) {
82+
$phpDocType = TypeCombinator::removeNull($phpDocType);
83+
}
8384
if ($type instanceof BenevolentUnionType) {
8485
return $type;
8586
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /