Issues (43)

app/autocomplete/Autocomplete.php (1 issue)

1
<?php
2
3
namespace autocomplete;
4
5
use terminal\Terminal;
6
use filesystem\Filesystem;
7
use \Config;
8
9
final class Autocomplete
10
{
11
    protected $prev;
12
    protected $cur;
13
    protected $config;
14
    protected $terminal;
15
    protected $filesystem;
16
17
    function __construct(
18
        Config              $config,
19
        Terminal            $terminal,
20
        Filesystem          $filesystem
21
    )
22
    {
23
        $this->config               = $config;
24
        $this->terminal             = $terminal;
25
        $this->filesystem           = $filesystem;
26
27
        $arguments  = $this->terminal->getArguments();
28
        $this->prev = empty($arguments['prev']) ? '' : $arguments['prev'];
29
        $this->cur  = empty($arguments['cur']) ? '' : $arguments['cur'];
30
    }
31
32
    /**
33
     * get list of addons at output path
34
     * 
35
     * @return array - array of strings - name of addons
36
     */
37
    public function getAddonsList()
38
    {
39
        $addonsPath = sanitize_filename(
40
            $this->config->get('filesystem.output_path') . '../'
41
        );
42
43
        $dirs = $this->filesystem->listDirs($addonsPath);
44
45
        return array_map(function($dir) {
46
            $paths = explode('/', $dir);
47
            return end($paths);
48
        }, $dirs);
49
    }
50
51
    /**
52
     * Suggests a param which still is not used
53
     * 
54
     * @param string $argument
55
     * @param array|callable $values - possible values for this argument (option)
56
     * or function that returns $values
57
     * 
58
     * @return void|array
59
     */
60
    public function queueArgument(string $argument, $values = [])
61
    {
62
        $arguments  = $this->terminal->getArguments();
63
        $option     = '--' . $argument;
64
65
        if (empty($arguments[$argument])) {
66
            return [$option];
67
        }
68
69
        if ($this->prev === $option) {
70
            if (is_callable($values)) {
71
                return call_user_func($values);
72
            } else {
73
                return $values;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $values also could return the type callable which is incompatible with the documented return type array.
Loading history...
74
            }
75
        }
76
    }
77
78
    /**
79
     * Combines suggestions and returns actual suggestion
80
     * 
81
     * @param array $queue
82
     * 
83
     * @return array
84
     */
85
    public function combineQueueParam(...$queue)
86
    {
87
        foreach ($queue as $argument) {
88
            if (is_array($argument)) {
89
                return $argument;
90
            }
91
        }
92
93
        return [];
94
    }
95
}
96