Utils::getopts()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Cauditor;
4
5
/**
6
 * @author Matthias Mullie <[email protected]>
7
 * @copyright Copyright (c) 2016, Matthias Mullie. All rights reserved.
8
 * @license LICENSE MIT
9
 */
10
class Utils
11
{
12
    /**
13
     * `getopts` is very convenient, but annoying how one has to check for both
14
     * the long & short versions for values; this will always return an array
15
     * with the long opt as key, even if the user passed it as short.
16
     *
17
     * @param array $options [short => long]
18
     *
19
     * @return array
20
     */
21
    public static function getopts($options)
22
    {
23
        $opts = getopt(implode('', array_keys($options)), $options);
24
25
        $result = array();
26
        foreach ($options as $short => $long) {
27
            $short = trim($short, ':');
28
            $long = trim($long, ':');
29
30
            if (isset($opts[$short])) {
31
                $result[$long] = $opts[$short];
32
            } elseif (isset($opts[$long])) {
33
                $result[$long] = $opts[$long];
34
            }
35
        }
36
37
        return $result;
38
    }
39
}
40