Completed
Push — 4.9 ( eb33e7...2df7ad )
by Mikhail
01:41
created

Autocomplete::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 3
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
14
    function __construct(
15
        Config              $config,
16
        Terminal            $terminal,
17
        Filesystem          $filesystem
18
    )
19
    {
20
        $this->config               = $config;
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
21
        $this->terminal             = $terminal;
0 ignored issues
show
Bug Best Practice introduced by
The property terminal does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
22
        $this->filesystem           = $filesystem;
0 ignored issues
show
Bug Best Practice introduced by
The property filesystem does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
23
24
        $arguments  = $this->terminal->getArguments();
25
        $this->prev = empty($arguments['prev']) ? '' : $arguments['prev'];
26
        $this->cur  = empty($arguments['cur']) ? '' : $arguments['cur'];
27
    }
28
29
    /**
30
     * get list of addons at output path
31
     * 
32
     * @return array - array of strings - name of addons
33
     */
34
    public function getAddonsList()
35
    {
36
        $addonsPath = sanitize_filename(
37
            $this->config->get('path')
38
            . $this->config->get('filesystem.output_path_relative') . '../'
39
        );
40
41
        $dirs = $this->filesystem->listDirs($addonsPath);
42
43
        return array_map(function($dir) {
44
            $paths = explode('/', $dir);
45
            return end($paths);
46
        }, $dirs);
47
    }
48
49
    /**
50
     * Suggests a param which still is not used
51
     * 
52
     * @param string $argument
53
     * @param array|callable $values - possible values for this argument (option)
54
     * or function that returns $values
55
     * 
56
     * @return void|array
57
     */
58
    public function queueArgument(string $argument, $values = [])
59
    {
60
        $arguments  = $this->terminal->getArguments();
61
        $option     = '--' . $argument;
62
63
        if (empty($arguments[$argument])) {
64
            return [$option];
65
        }
66
67
        if ($this->prev === $option) {
68
            if (is_callable($values)) {
69
                return call_user_func($values);
70
            }
71
            
72
            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...
73
        }
74
    }
75
76
    /**
77
     * Combines suggestions and returns actual suggestion
78
     * 
79
     * @param array $queue
80
     * 
81
     * @return array
82
     */
83
    public function combineQueueParam(...$queue)
84
    {
85
        foreach ($queue as $argument) {
86
            if (is_array($argument)) {
87
                return $argument;
88
            }
89
        }
90
91
        return [];
92
    }
93
}
94