Autocomplete::complete()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-07-05 
5
 */
6
7
namespace Net\Bazzline\Component\Cli\Readline;
8
9
class Autocomplete
10
{
11
    /** array */
12
    private $configuration;
13
14
    /**
15
     * @param string $input
16
     * @param int $index
17
     * @return array|bool
18
     */
19
    public function __invoke($input, $index)
20
    {
21
        return $this->complete($input, $index);
22
    }
23
24
    /**
25
     * @param string $input
26
     * @param int $index
27
     * @return array|bool
28
     */
29
    public function complete($input, $index)
0 ignored issues
show
Unused Code introduced by
The parameter $input is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31
        $configuration = $this->configuration;
32
33
        if ($index == 0) {
34
            $completion = array_keys($configuration);
35
        } else {
36
            $buffer     = preg_replace('/\s+/', ' ', trim(readline_info('line_buffer')));
37
            $tokens     = explode(' ', $buffer);
38
            $completion = $this->fetchCompletion($configuration, $tokens);
39
        }
40
41
        return $completion;
42
    }
43
44
    /**
45
     * @param array $configuration
46
     * @return $this
47
     */
48
    public function setConfiguration(array $configuration)
49
    {
50
        $this->configuration = $configuration;
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param array $configuration
57
     * @param array $tokens
58
     * @return array|bool
59
     */
60
    private function fetchCompletion(array $configuration, array &$tokens)
61
    {
62
        $completion = false;
63
        $index      = current($tokens);
64
65
        if (isset($configuration[$index])) {
66
            if (next($tokens) !== false) {
67
                $completion = $this->fetchCompletion($configuration[$index], $tokens);
68
            } else {
69
                $arrayOrExecutable = $configuration[$index];
70
71
                if (is_array($arrayOrExecutable)) {
72
                    $completion = array_keys($arrayOrExecutable);
73
                } else {
74
                    $completion = false;
75
                }
76
            }
77
        } else {
78
            $indexLengthIsGreaterZero   = (strlen($index) > 0);
79
80
            if ($indexLengthIsGreaterZero) {
81
                $position   = strlen($index);
82
                $values     = array_keys($configuration);
83
                $completion = [];
84
85
                foreach ($values as $value) {
86
                    if (substr($value, 0, $position) === $index) {
87
                        $completion[] = $value;
88
                    }
89
                }
90
91
                if (empty($completion)) {
92
                    $completion = false;
93
                }
94
            }
95
        }
96
97
        return $completion;
98
    }
99
}
100