Completed
Pull Request — master (#31)
by Greg
01:30
created

AliasRecord::remoteHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Consolidation\SiteAlias;
3
4
use Consolidation\Config\Config;
5
use Consolidation\Config\ConfigInterface;
6
use Consolidation\Config\Util\ArrayUtil;
7
use Consolidation\SiteAlias\Util\FsUtils;
8
9
/**
10
 * An alias record is a configuration record containing well-known items.
11
 *
12
 * @see AliasRecordInterface for documentation
13
 */
14
class AliasRecord extends Config implements AliasRecordInterface
15
{
16
    use AliasRecordTrait;
17
18
    /**
19
     * @var string
20
     */
21
    protected $name;
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function __construct(array $data = null, $name = '', $env = '')
27
    {
28
        parent::__construct($data);
29
        if (!empty($env)) {
30
            $name .= ".$env";
31
        }
32
        $this->name = $name;
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function getConfig(ConfigInterface $config, $key, $default = null)
39
    {
40
        if ($this->has($key)) {
41
            return $this->get($key, $default);
42
        }
43
        return $config->get($key, $default);
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49
    public function name()
50
    {
51
        return $this->name;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    public function setName($name)
58
    {
59
        $this->name = $name;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function exportConfig()
66
    {
67
        return $this->remap($this->export());
68
    }
69
70
    /**
71
     * Reconfigure data exported from the form it is expected to be in
72
     * inside an alias record to the form it is expected to be in when
73
     * inside a configuration file.
74
     */
75
    protected function remap($data)
76
    {
77
        foreach ($this->remapOptionTable() as $from => $to) {
78
            if (isset($data[$from])) {
79
                unset($data[$from]);
80
            }
81
            $value = $this->get($from, null);
82
            if (isset($value)) {
83
                $data['options'][$to] = $value;
84
            }
85
        }
86
87
        return new Config($data);
88
    }
89
90
    /**
91
     * Fetch the parameter-specific options from the 'alias-parameters' section of the alias.
92
     * @param string $parameterName
93
     * @return array
94
     */
95
    protected function getParameterSpecificOptions($aliasData, $parameterName)
0 ignored issues
show
Unused Code introduced by
The parameter $aliasData 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...
96
    {
97
        if (!empty($parameterName) && $this->has("alias-parameters.{$parameterName}")) {
98
            return $this->get("alias-parameters.{$parameterName}");
99
        }
100
        return [];
101
    }
102
103
    /**
104
     * Convert the data in this record to the layout that was used
105
     * in the legacy code, for backwards compatiblity.
106
     */
107
    public function legacyRecord()
108
    {
109
        $result = $this->exportConfig()->get('options', []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a string|null.

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...
110
111
        // Backend invoke needs a couple of critical items in specific locations.
112
        if ($this->has('paths.drush-script')) {
113
            $result['path-aliases']['%drush-script'] = $this->get('paths.drush-script');
114
        }
115
        if ($this->has('ssh.options')) {
116
            $result['ssh-options'] = $this->get('ssh.options');
117
        }
118
        return $result;
119
    }
120
121
    /**
122
     * Conversion table from old to new option names. These all implicitly
123
     * go in `options`, although they can come from different locations.
124
     */
125
    protected function remapOptionTable()
126
    {
127
        return [
128
            'user' => 'remote-user',
129
            'host' => 'remote-host',
130
            'root' => 'root',
131
            'uri' => 'uri',
132
        ];
133
    }
134
}
135