-
Notifications
You must be signed in to change notification settings - Fork 515
More precise types after assignment when strict-types=0 #3965
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
475c75e
f1b04fc
e1455c0
536af14
47a1d73
6c4aea1
cebbfcb
6f6bca9
411e447
2625bb8
72aa6ab
7a95a46
4d20e2a
ccb1889
78e8df9
872af82
1a8c921
3a64515
2c75fb4
65b9d3b
604aed1
016c260
da17fe4
9e815d3
76e1258
4bb13d8
d338a47
9d19f30
0560ae2
113c7d4
3d34649
c16582a
c6db4e7
61e0eea
a8abeae
016d44a
95186fe
b14719a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -706,6 +706,18 @@ public function toArrayKey(): Type | |
|
||
public function toCoercedArgumentType(bool $strictTypes): Type | ||
{ | ||
if (!$strictTypes) { | ||
$classReflection = $this->getClassReflection(); | ||
if ( | ||
$classReflection === null | ||
|| !$classReflection->hasNativeMethod('__toString') | ||
) { | ||
return $this; | ||
} | ||
|
||
return TypeCombinator::union($this, $this->toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the object isn't a Stringable, this should just return Also I'm not sure what happens with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added tests with links to 3v4l.org |
||
} | ||
|
||
return $this; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php // lint >= 8.4 | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Bug12393Php84; | ||
|
||
use function PHPStan\Testing\assertNativeType; | ||
use function PHPStan\Testing\assertType; | ||
|
||
|
||
class StringableFoo { | ||
private string $foo; | ||
|
||
// https://3v4l.org/2SPPj#v8.4.6 | ||
public function doFoo3(\BcMath\Number $foo): void { | ||
$this->foo = $foo; | ||
assertType('*NEVER*', $this->foo); | ||
} | ||
|
||
public function __toString(): string { | ||
return 'Foo'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,3 +143,42 @@ public function getMixed() | |
} | ||
|
||
} | ||
|
||
// https://3v4l.org/LK6Rh | ||
class CallableString { | ||
private string $foo; | ||
|
||
public function doFoo(callable $foo): void { | ||
$this->foo = $foo; // PHPStorm wrongly reports an error on this line | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be fair PHPStan also reports an error for that (which is correct, we mostly don't differentiate between strict_types 1 or 0 in |
||
assertType('callable-string|non-empty-string', $this->foo); | ||
} | ||
} | ||
|
||
// https://3v4l.org/WJ8NW | ||
class CallableArray { | ||
private array $foo; | ||
|
||
public function doFoo(callable $foo): void { | ||
$this->foo = $foo; | ||
assertType('array', $this->foo); // could be non-empty-array | ||
} | ||
} | ||
|
||
class StringableFoo { | ||
private string $foo; | ||
|
||
// https://3v4l.org/DQSgA#v8.4.6 | ||
public function doFoo(StringableFoo $foo): void { | ||
$this->foo = $foo; | ||
assertType('*NEVER*', $this->foo); | ||
} | ||
|
||
public function doFoo2(NotStringable $foo): void { | ||
$this->foo = $foo; | ||
assertType('*NEVER*', $this->foo); | ||
} | ||
|
||
public function __toString(): string { | ||
return 'Foo'; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.