Test Failed
Push — master ( 766a39...696a12 )
by Dispositif
09:33
created

WikiArrayTrait::completeFantasyOrder()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 17
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 29
rs 8.8333
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 : Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
declare(strict_types=1);
8
9
namespace App\Domain\Utils;
10
11
trait WikiArrayTrait
12
{
13
    /**
14
     * Legacy.
15
     * Generate the complete list of parameters from the fantasy order list
16
     * with additionnal parameters inserted in the place of the cleanOrder list.
17
     * Génère la liste complète de paramètres possibles d'après l'ordre
18
     * fantaisiste (humain) et l'ordre officiel (documentation).
19
     *
20
     * @param array $fantasyOrder
21
     * @param array $cleanOrder
22
     *
23
     * @return array
24
     */
25
    public function completeFantasyOrder(array $fantasyOrder, array $cleanOrder): array
26
    {
27
        $lastFantasy = null;
28
        $firstParameters = [];
29
        $before = [];
30
31
        // relation between param name et its place in the fantasy order
32
        foreach ($cleanOrder as $param) {
33
            if (in_array($param, $fantasyOrder)) {
34
                $lastFantasy = $param;
35
                continue;
36
            }
37
            if (($lastFantasy === null) && !in_array($param, $fantasyOrder)) {
38
                $firstParameters[] = $param;
39
                continue;
40
            }
41
            $before[$param] = $lastFantasy;
42
        }
43
44
        $result = $firstParameters;
45
        foreach ($fantasyOrder as $param) {
46
            $result[] = $param;
47
            // additional parameters added in the end of the list
48
            if (in_array($param, $before)) {
49
                $result = array_merge($result, array_keys($before, $param));
50
            }
51
        }
52
53
        return $result;
54
    }
55
}