Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
4.76% |
1 / 21 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
NamespaceSort | |
4.76% |
1 / 21 |
|
33.33% |
1 / 3 |
37.10 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__invoke | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
sort | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace BrianHenryIE\Strauss\Helpers; |
4 | |
5 | class NamespaceSort |
6 | { |
7 | |
8 | const LONGEST = false; |
9 | const SHORTEST = true; |
10 | |
11 | protected bool $order; |
12 | |
13 | public function __construct(bool $order = self::SHORTEST) |
14 | { |
15 | $this->order = $order; |
16 | } |
17 | |
18 | public function __invoke(string $a, string $b) |
19 | { |
20 | return $this->order === self::LONGEST |
21 | ? $this->sort($a, $b) |
22 | : $this->sort($b, $a); |
23 | } |
24 | |
25 | protected function sort($a, $b) |
26 | { |
27 | |
28 | $aParts = explode('\\', $a); |
29 | $bParts = explode('\\', $b); |
30 | |
31 | $aPathParts = array_slice($aParts, 0, -1); |
32 | $bPathParts = array_slice($bParts, 0, -1); |
33 | |
34 | // 0 is a valid string length for the global namespace |
35 | |
36 | $aPath = implode('/', $aPathParts); |
37 | $bPath = implode('/', $bPathParts); |
38 | $aPathLength = strlen($aPath); |
39 | $bPathLength = strlen($bPath); |
40 | |
41 | // This isn't done right yet, when the path length is equal, the comparison should be done inccludingthe partent directory/path. |
42 | |
43 | if ($aPathLength === $bPathLength) { |
44 | // What happens with count() < 1/0? |
45 | $aa = implode('/', array_slice($aParts, -2)); |
46 | $bb = implode('/', array_slice($bParts, -2)); |
47 | |
48 | return strlen($bb) - strlen($aa); |
49 | } |
50 | |
51 | if ($aPathLength !== $bPathLength) { |
52 | return $bPathLength - $aPathLength; |
53 | } |
54 | |
55 | $bPop = array_pop($bPathParts); |
56 | $aPop = array_pop($aPathParts); |
57 | |
58 | return strlen($bPop) - strlen($aPop); |
59 | } |
60 | } |