-
Notifications
You must be signed in to change notification settings - Fork 572
Fix phpstan/phpstan#11619: Parameter #2 $callback of function uasort expects callable(Foo, Foo): int, 'strnatcasecmp' given. #5171
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 10 commits
6938ec5
2913a4c
d6bd2af
e1817a5
250133c
82945c6
cd097f9
ef20977
627c741
fc59c3b
cefb24b
97ce9f2
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php declare(strict_types = 1); // lint >= 8.1 | ||
|
|
||
| namespace Bug11619Strict; | ||
|
|
||
| final class Foo implements \Stringable { | ||
|
|
||
| private function __construct(public readonly string $value) { | ||
| } | ||
|
|
||
| public static function fromString(string $string): self { | ||
| return new self($string); | ||
| } | ||
|
|
||
| public function __toString(): string { | ||
| return $this->value; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function test(): void | ||
| { | ||
| $options = [ | ||
| Foo::fromString('c'), | ||
| Foo::fromString('b'), | ||
| Foo::fromString('a'), | ||
| ]; | ||
|
|
||
| uasort($options, 'strnatcasecmp'); | ||
|
Contributor
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. needs another test like for both, strict-types 0 or 1. there should be no error for userland comparators, see https://3v4l.org/gEURZ#veol
Collaborator
Author
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. The background |
||
| usort($options, 'strnatcasecmp'); | ||
|
|
||
| uasort($options, fn($a, $b) => strnatcasecmp($a, $b)); | ||
| uasort($options, fn(string $a, string $b) => strnatcasecmp($a, $b)); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<\Stringable> $a | ||
| * @param callable(\Stringable, \Stringable): int $f | ||
| */ | ||
| function customUsort(array &$a, callable $f): void | ||
| { | ||
| for ($i = 1; $i < count($a); $i++) | ||
| for ($j = $i; $j > 0 && $f($a[$j-1], $a[$j]) > 0; $j--) | ||
| [$a[$j-1], $a[$j]] = [$a[$j], $a[$j-1]]; | ||
| } | ||
|
|
||
| function test2(): void | ||
| { | ||
| $options = [ | ||
| Foo::fromString('c'), | ||
| Foo::fromString('b'), | ||
| Foo::fromString('a'), | ||
| ]; | ||
|
|
||
| customUsort($options, 'strnatcasecmp'); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php // lint >= 8.1 | ||
|
staabm marked this conversation as resolved.
|
||
|
|
||
| namespace Bug11619; | ||
|
|
||
| final class Foo implements \Stringable { | ||
|
|
||
| private function __construct(public readonly string $value) { | ||
| } | ||
|
|
||
| public static function fromString(string $string): self { | ||
| return new self($string); | ||
| } | ||
|
|
||
| public function __toString(): string { | ||
| return $this->value; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function test(): void | ||
| { | ||
| $options = [ | ||
| Foo::fromString('c'), | ||
| Foo::fromString('b'), | ||
| Foo::fromString('a'), | ||
| ]; | ||
|
|
||
| uasort($options, 'strnatcasecmp'); | ||
| usort($options, 'strnatcasecmp'); | ||
|
|
||
| uasort($options, fn($a, $b) => strnatcasecmp($a, $b)); | ||
| uasort($options, fn(string $a, string $b) => strnatcasecmp($a, $b)); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<\Stringable> $a | ||
| * @param callable(\Stringable, \Stringable): int $f | ||
| */ | ||
| function customUsort(array &$a, callable $f): void | ||
| { | ||
| for ($i = 1; $i < count($a); $i++) | ||
| for ($j = $i; $j > 0 && $f($a[$j-1], $a[$j]) > 0; $j--) | ||
| [$a[$j-1], $a[$j]] = [$a[$j], $a[$j-1]]; | ||
| } | ||
|
|
||
| function test2(): void | ||
| { | ||
| $options = [ | ||
| Foo::fromString('c'), | ||
| Foo::fromString('b'), | ||
| Foo::fromString('a'), | ||
| ]; | ||
|
|
||
| customUsort($options, 'strnatcasecmp'); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.