Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions src/Reflection/Php/PhpMethodReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,30 @@ public function isPublic(): bool
private function getReturnType(): Type
{
if ($this->returnType === null) {
$this->returnType = TypehintHelper::decideType(
$this->getNativeReturnType(),
$name = strtolower($this->getName());
$returnType = $this->reflection->getReturnType();
if ($returnType === null) {
if (in_array($name, ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) {
return $this->returnType = TypehintHelper::decideType(new VoidType(), $this->phpDocReturnType);
}
if ($name === '__tostring') {
return $this->returnType = TypehintHelper::decideType(new StringType(), $this->phpDocReturnType);
}
if ($name === '__isset') {
return $this->returnType = TypehintHelper::decideType(new BooleanType(), $this->phpDocReturnType);
}
if ($name === '__sleep') {
return $this->returnType = TypehintHelper::decideType(new ArrayType(new IntegerType(), new StringType()), $this->phpDocReturnType);
}
if ($name === '__set_state') {
return $this->returnType = TypehintHelper::decideType(new ObjectWithoutClassType(), $this->phpDocReturnType);
}
}

$this->returnType = TypehintHelper::decideTypeFromReflection(
$returnType,
$this->phpDocReturnType,
$this->declaringClass,
);
}

Expand All @@ -323,28 +344,8 @@ private function getPhpDocReturnType(): Type
private function getNativeReturnType(): Type
{
if ($this->nativeReturnType === null) {
$returnType = $this->reflection->getReturnType();
if ($returnType === null) {
$name = strtolower($this->getName());
if (in_array($this->getName(), ['__construct', '__destruct', '__unset', '__wakeup', '__clone'], true)) {
return $this->nativeReturnType = new VoidType();
}
if ($name === '__tostring') {
return $this->nativeReturnType = new StringType();
}
if ($name === '__isset') {
return $this->nativeReturnType = new BooleanType();
}
if ($name === '__sleep') {
return $this->nativeReturnType = new ArrayType(new IntegerType(), new StringType());
}
if ($name === '__set_state') {
return $this->nativeReturnType = new ObjectWithoutClassType();
}
}

$this->nativeReturnType = TypehintHelper::decideTypeFromReflection(
$returnType,
$this->reflection->getReturnType(),
null,
$this->declaringClass,
);
Expand Down
5 changes: 2 additions & 3 deletions src/Type/TypehintHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public static function decideTypeFromReflection(
$type = ParserNodeTypeToPHPStanType::resolve($typeNode, $selfClass);
if ($reflectionType->allowsNull()) {
$type = TypeCombinator::addNull($type);
} elseif ($phpDocType !== null) {
$phpDocType = TypeCombinator::removeNull($phpDocType);
}

return self::decideType($type, $phpDocType);
Expand All @@ -78,9 +80,6 @@ public static function decideType(
?Type $phpDocType,
): Type
{
if ($phpDocType !== null && $type->isNull()->no()) {
$phpDocType = TypeCombinator::removeNull($phpDocType);
}
if ($type instanceof BenevolentUnionType) {
return $type;
}
Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-to-string-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace BugToStringType;

use function PHPStan\Testing\assertNativeType;
use function PHPStan\Testing\assertType;

class ParentClassWithToStringMixedReturn
{
public function __toString()
{
return 'a';
}
}

class WithParentMixedReturn extends ParentClassWithToStringMixedReturn
{
public function __toString()
{
return 'value';
}
}

class Consumer extends WithParentMixedReturn
{
public function __toString()
{
return 'value';
}
}

function test(Consumer $test): void
{
assertType('string', $test->__toString());
assertNativeType('mixed', $test->__toString());
}
Loading