Completed
Push — master ( cbf66c...6440bc )
by Greg
01:29
created

SiteAlias   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 102
rs 10
c 0
b 0
f 0

7 Methods

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