Completed
Pull Request — master (#16)
by Greg
02:24
created

DefaultsWithDescriptions::rename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Consolidation\AnnotatedCommand;
4
5
/**
6
 * An associative array that maps from key to default value;
7
 * each entry can also have a description.
8
 */
9
class DefaultsWithDescriptions
10
{
11
    /**
12
     * @var array Associative array of key : default mappings
13
     */
14
    protected $values;
15
16
    /**
17
     * @var array Associative array of key : description mappings
18
     */
19
    protected $descriptions;
20
21
    /**
22
     * @var mixed Default value that the default value of items in
23
     * the collection should take when not specified in the 'add' method.
24
     */
25
    protected $defaultDefault;
26
27
    public function __construct($values, $defaultDefault = null)
28
    {
29
        $this->values = $values;
30
        $this->descriptions = [];
31
        $this->defaultDefault = $defaultDefault;
32
    }
33
34
    /**
35
     * Return just the key : default values mapping
36
     *
37
     * @return array
38
     */
39
    public function getValues()
40
    {
41
        return $this->values;
42
    }
43
44
    /**
45
     * Check to see whether the speicifed key exists in the collection.
46
     *
47
     * @param type $key
48
     * @return type
49
     */
50
    public function exists($key)
51
    {
52
        return array_key_exists($key, $this->values);
53
    }
54
55
    public function setDefaultValue($key, $defaultValue)
56
    {
57
        $this->values[$key] = $defaultValue;
58
    }
59
60
    /**
61
     * Add another argument to this command.
62
     *
63
     * @param string $key Name of the argument.
64
     * @param string $description Help text for the argument.
65
     * @param string $defaultValue The default value for the argument.
66
     */
67
    public function add($key, $description, $defaultValue = null)
68
    {
69
        if (!$this->exists($key) || isset($defaultValue)) {
0 ignored issues
show
Documentation introduced by
$key is of type string, but the function expects a object<Consolidation\AnnotatedCommand\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
            $this->values[$key] = isset($defaultValue) ? $defaultValue : $this->defaultDefault;
71
        }
72
        unset($this->descriptions[$key]);
73
        if (isset($description)) {
74
            $this->descriptions[$key] = $description;
75
        }
76
    }
77
78
    /**
79
     * Get the description of one entry.
80
     *
81
     * @param string $key The key of the item.
82
     * @return string
83
     */
84
    public function getDescription($key)
85
    {
86
        if (array_key_exists($key, $this->descriptions)) {
87
            return $this->descriptions[$key];
88
        }
89
        return '';
90
    }
91
92
    /**
93
     * Rename an existing option to something else.
94
     */
95
    public function rename($oldName, $newName)
96
    {
97
        $this->values[$newName] = $this->values[$oldName];
98
        $this->descriptions[$newName] = $this->descriptions[$oldName];
99
        unset($this->values[$oldName]);
100
        unset($this->descriptions[$oldName]);
101
    }
102
}
103