mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
Fix #4429 - any class with a __toString method in PHP8 assumed to implement Stringable
This commit is contained in:
parent
7f975045f4
commit
106747487b
@ -234,6 +234,16 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements FileSour
|
||||
|
||||
$this->functionlike_node_scanners[] = $functionlike_node_scanner;
|
||||
|
||||
if ($classlike_storage
|
||||
&& !$classlike_storage->is_interface
|
||||
&& $this->codebase->php_major_version >= 8
|
||||
&& $node instanceof PhpParser\Node\Stmt\ClassMethod
|
||||
&& strtolower($node->name->name) === '__tostring'
|
||||
) {
|
||||
$classlike_storage->class_implements['stringable'] = 'Stringable';
|
||||
$this->codebase->scanner->queueClassLikeForScanning('Stringable');
|
||||
}
|
||||
|
||||
if (!$this->scan_deep) {
|
||||
return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
|
||||
}
|
||||
|
@ -169,6 +169,21 @@ class ToStringTest extends TestCase
|
||||
[],
|
||||
'8.0'
|
||||
],
|
||||
'implicitStringable' => [
|
||||
'<?php
|
||||
function foo(Stringable $s): void {}
|
||||
|
||||
class Bar {
|
||||
public function __toString() {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
|
||||
foo(new Bar());',
|
||||
[],
|
||||
[],
|
||||
'8.0',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@ -361,6 +376,21 @@ class ToStringTest extends TestCase
|
||||
fora((string) $address);',
|
||||
'error_message' => 'UndefinedGlobalVariable',
|
||||
],
|
||||
'implicitStringableDisallowed' => [
|
||||
'<?php
|
||||
function foo(Stringable $s): void {}
|
||||
|
||||
class Bar {
|
||||
public function __toString() {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
|
||||
foo(new Bar());',
|
||||
'error_message' => 'InvalidArgument',
|
||||
[],
|
||||
'7.4',
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user