Completed
Push — master ( 237b29...cbf66c )
by Greg
01:35
created

AliasRecord::exportConfig()   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 name()
39
    {
40
        return $this->name;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function setName($name)
47
    {
48
        $this->name = $name;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function exportConfig()
55
    {
56
        return $this->remap($this->export());
57
    }
58
59
    /**
60
     * Reconfigure data exported from the form it is expected to be in
61
     * inside an alias record to the form it is expected to be in when
62
     * inside a configuration file.
63
     */
64
    protected function remap($data)
65
    {
66
        foreach ($this->remapOptionTable() as $from => $to) {
67
            if (isset($data[$from])) {
68
                unset($data[$from]);
69
            }
70
            $value = $this->get($from, null);
71
            if (isset($value)) {
72
                $data['options'][$to] = $value;
73
            }
74
        }
75
76
        return new Config($data);
77
    }
78
79
    /**
80
     * Fetch the parameter-specific options from the 'alias-parameters' section of the alias.
81
     * @param string $parameterName
82
     * @return array
83
     */
84
    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...
85
    {
86
        if (!empty($parameterName) && $this->has("alias-parameters.{$parameterName}")) {
87
            return $this->get("alias-parameters.{$parameterName}");
88
        }
89
        return [];
90
    }
91
92
    /**
93
     * Convert the data in this record to the layout that was used
94
     * in the legacy code, for backwards compatiblity.
95
     */
96
    public function legacyRecord()
97
    {
98
        $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...
99
100
        // Backend invoke needs a couple of critical items in specific locations.
101
        if ($this->has('paths.drush-script')) {
102
            $result['path-aliases']['%drush-script'] = $this->get('paths.drush-script');
103
        }
104
        if ($this->has('ssh.options')) {
105
            $result['ssh-options'] = $this->get('ssh.options');
106
        }
107
        return $result;
108
    }
109
110
    /**
111
     * Conversion table from old to new option names. These all implicitly
112
     * go in `options`, although they can come from different locations.
113
     */
114
    protected function remapOptionTable()
115
    {
116
        return [
117
            'user' => 'remote-user',
118
            'host' => 'remote-host',
119
            'root' => 'root',
120
            'uri' => 'uri',
121
        ];
122
    }
123
}
124