Skip to content

Commit

Permalink
StringAlwaysAcceptingObjectWithToStringType is supertype of Stringabl…
Browse files Browse the repository at this point in the history
…e objects
  • Loading branch information
ondrejmirtes committed Jun 14, 2023
1 parent 824cb02 commit 9de9efa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Type/StringAlwaysAcceptingObjectWithToStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,36 @@
namespace PHPStan\Type;

use PHPStan\Reflection\ReflectionProviderStaticAccessor;
use PHPStan\TrinaryLogic;

class StringAlwaysAcceptingObjectWithToStringType extends StringType
{

public function isSuperTypeOf(Type $type): TrinaryLogic
{
if ($type instanceof CompoundType) {
return $type->isSubTypeOf($this);
}

$thatClassNames = $type->getObjectClassNames();
if ($thatClassNames === []) {
return parent::isSuperTypeOf($type);
}

$result = TrinaryLogic::createNo();
$reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
foreach ($thatClassNames as $thatClassName) {
if (!$reflectionProvider->hasClass($thatClassName)) {
return TrinaryLogic::createNo();
}

$typeClass = $reflectionProvider->getClass($thatClassName);
$result = $result->or(TrinaryLogic::createFromBoolean($typeClass->hasNativeMethod('__toString')));
}

return $result;
}

public function acceptsWithReason(Type $type, bool $strictTypes): AcceptsResult
{
$thatClassNames = $type->getObjectClassNames();
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Type/StringTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public function dataIsSuperTypeOf(): array
new GenericClassStringType(new ObjectType(stdClass::class)),
TrinaryLogic::createMaybe(),
],
[
new StringAlwaysAcceptingObjectWithToStringType(),
new ObjectType(ClassWithToString::class),
TrinaryLogic::createYes(),
],
];
}

Expand Down

0 comments on commit 9de9efa

Please sign in to comment.