1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ninja\Sorter\Strategy; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Ninja\Sorter\Comparator\ComparatorInterface; |
7
|
|
|
use Ninja\Sorter\Comparator\UnicodeCIComparator; |
|
|
|
|
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
abstract class AbstractSortStrategy implements StrategyInterface |
11
|
|
|
{ |
12
|
|
|
private bool $preserve_keys = false; |
13
|
|
|
|
14
|
3 |
|
public function __construct(protected ?ComparatorInterface $comparator, private int $order = self::ASC) |
15
|
|
|
{ |
16
|
3 |
|
if ($this->comparator === null) { |
17
|
|
|
$this->comparator = new UnicodeCIComparator(); |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setComparator(ComparatorInterface $comparator): static |
22
|
|
|
{ |
23
|
|
|
$this->comparator = $comparator; |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
4 |
|
protected function getComparator(): ?ComparatorInterface |
29
|
|
|
{ |
30
|
4 |
|
return $this->comparator; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public function setOrder(int $order): static |
34
|
|
|
{ |
35
|
2 |
|
$this->order = $order; |
36
|
|
|
|
37
|
2 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
protected function getOrder(): int |
41
|
|
|
{ |
42
|
1 |
|
return $this->order; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function setPreserveKeys(bool $preserve_keys): static |
46
|
|
|
{ |
47
|
1 |
|
$this->preserve_keys = $preserve_keys; |
48
|
1 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Closure |
53
|
|
|
*/ |
54
|
3 |
|
protected function createSortTransformFunction(): Closure |
55
|
|
|
{ |
56
|
3 |
|
$comparator = $this->getComparator(); |
57
|
3 |
|
if ($comparator === null) { |
58
|
3 |
|
throw new RuntimeException('Comparator was not defined'); |
59
|
|
|
} |
60
|
3 |
|
|
61
|
3 |
|
$checker = $this->getValueChecker(); |
62
|
3 |
|
$sortOrder = $this->order; |
63
|
3 |
|
|
64
|
|
|
return static function (mixed $a, mixed $b) use ($comparator, $checker, $sortOrder) { |
65
|
|
|
$checker($a, $b, $comparator); |
66
|
4 |
|
return $comparator->compare($a, $b) * $sortOrder; |
67
|
|
|
}; |
68
|
4 |
|
} |
69
|
4 |
|
|
70
|
|
|
public function sort(array $collection): array |
71
|
4 |
|
{ |
72
|
|
|
$function = $this->preserve_keys ? 'uasort' : 'usort'; |
73
|
|
|
$function($collection, $this->createSortTransformFunction()); |
74
|
4 |
|
|
75
|
|
|
return $collection; |
76
|
4 |
|
} |
77
|
4 |
|
|
78
|
|
|
protected function getValueChecker(): Closure |
79
|
4 |
|
{ |
80
|
4 |
|
return static function (mixed $a, mixed $b, ComparatorInterface $comparator) { |
81
|
|
|
$exceptionMessage = 'Comparator (%s) does not support "%s"'; |
82
|
4 |
|
|
83
|
|
|
$error = null; |
84
|
|
|
if (!$comparator->supports($a)) { |
85
|
|
|
$error = sprintf($exceptionMessage, get_class($comparator), gettype($a)); |
86
|
4 |
|
} elseif (!$comparator->supports($b)) { |
87
|
|
|
$error = sprintf($exceptionMessage, get_class($comparator), gettype($a)); |
88
|
|
|
} |
89
|
4 |
|
|
90
|
|
|
if ($error !== null) { |
91
|
|
|
throw new RuntimeException($error); |
92
|
|
|
} |
93
|
|
|
}; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths