|
| 1 | +<?php declare(strict_types = 1); // lint >= 8.1 |
| 2 | + |
| 3 | +namespace Bug11619Strict; |
| 4 | + |
| 5 | +final class Foo implements \Stringable { |
| 6 | + |
| 7 | + private function __construct(public readonly string $value) { |
| 8 | + } |
| 9 | + |
| 10 | + public static function fromString(string $string): self { |
| 11 | + return new self($string); |
| 12 | + } |
| 13 | + |
| 14 | + public function __toString(): string { |
| 15 | + return $this->value; |
| 16 | + } |
| 17 | + |
| 18 | +} |
| 19 | + |
| 20 | +function test(): void |
| 21 | +{ |
| 22 | + $options = [ |
| 23 | + Foo::fromString('c'), |
| 24 | + Foo::fromString('b'), |
| 25 | + Foo::fromString('a'), |
| 26 | + ]; |
| 27 | + |
| 28 | + uasort($options, 'strnatcasecmp'); |
| 29 | + usort($options, 'strnatcasecmp'); |
| 30 | + |
| 31 | + uasort($options, fn($a, $b) => strnatcasecmp($a, $b)); |
| 32 | + uasort($options, fn(string $a, string $b) => strnatcasecmp($a, $b)); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * @param array<\Stringable> $a |
| 37 | + * @param callable(\Stringable, \Stringable): int $f |
| 38 | + */ |
| 39 | +function customUsort(array &$a, callable $f): void |
| 40 | +{ |
| 41 | + for ($i = 1; $i < count($a); $i++) |
| 42 | + for ($j = $i; $j > 0 && $f($a[$j-1], $a[$j]) > 0; $j--) |
| 43 | + [$a[$j-1], $a[$j]] = [$a[$j], $a[$j-1]]; |
| 44 | +} |
| 45 | + |
| 46 | +function userlandComparator(string $a, string $b): int { |
| 47 | + return strnatcasecmp($a, $b); |
| 48 | +} |
| 49 | + |
| 50 | +function test2(): void |
| 51 | +{ |
| 52 | + $options = [ |
| 53 | + Foo::fromString('c'), |
| 54 | + Foo::fromString('b'), |
| 55 | + Foo::fromString('a'), |
| 56 | + ]; |
| 57 | + |
| 58 | + customUsort($options, 'strnatcasecmp'); |
| 59 | + |
| 60 | + uasort($options, 'Bug11619Strict\userlandComparator'); |
| 61 | + usort($options, 'Bug11619Strict\userlandComparator'); |
| 62 | +} |
0 commit comments