Completed
Push — master ( a18f4a...b0c220 )
by Greg
01:31
created

SiteAliasName   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 0
dl 0
loc 209
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 6 1
A __construct() 0 5 1
A __toString() 0 8 2
A isAliasName() 0 8 3
A sitename() 0 4 2
A setSitename() 0 4 1
A hasSitename() 0 4 1
A hasEnv() 0 4 1
A setEnv() 0 4 1
A env() 0 4 1
A isSelf() 0 4 2
A isNone() 0 4 2
B doParse() 0 30 3
A processSingleItem() 0 10 2
A isSpecialAliasName() 0 4 2
1
<?php
2
namespace Consolidation\SiteAlias;
3
4
/**
5
 * Parse a string that contains a site alias name, and provide convenience
6
 * methods to access the parts.
7
 *
8
 * When provided by users, aliases must be in one of the following forms:
9
 *
10
 *   - @sitename.env: List only sitename and environment.
11
 *
12
 *   - @env: Look up a named environment in instances where the site root
13
 *       is known (e.g. via cwd). In this form, there is an implicit sitename
14
 *       'self' which is replaced by the actual site alias name once known.
15
 *
16
 *   - @sitename: Provides only the sitename; uses the 'default' environment,
17
 *       or 'dev' if there is no 'default' (or whatever is there if there is
18
 *       only one). With this form, the site alias name has no environment
19
 *       until the appropriate default environment is looked up. This form
20
 *       is checked only after `@env` returns no matches.
21
 *
22
 * There are also two special aliases that are recognized:
23
 *
24
 *   - @self: The current bootstrapped site.
25
 *
26
 *   - @none: No alias ('root' and 'uri' unset).
27
 *
28
 * The special alias forms have no environment component.
29
 *
30
 * When provided to an API, the '@' is optional.
31
 *
32
 * Note that @sitename and @env are ambiguous. Aliases in this form
33
 * (that are not one of the special aliases) will first be assumed
34
 * to be @env, and may be converted to @sitename later.
35
 *
36
 * Note that:
37
 *
38
 * - 'sitename' and 'env' MUST NOT contain a '.' (unlike previous
39
 *     versions of Drush).
40
 * - Users SHOULD NOT create any environments that have the same name
41
 *     as any site name (and visa-versa).
42
 * - All environments in one site record SHOULD be different versions
43
 *     of the same site (e.g. dev / test / live).
44
 */
45
class SiteAliasName
46
{
47
    protected $sitename;
48
    protected $env;
49
50
    /**
51
     * Match the parts of a regex name.
52
     */
53
    const ALIAS_NAME_REGEX = '%^@?([a-zA-Z0-9_-]+)(\.[a-zA-Z0-9_-]+)?$%';
54
55
    /**
56
     * Create a new site alias name
57
     *
58
     * @param string $item
59
     * @return SiteAliasName
60
     */
61
    public static function parse($item)
62
    {
63
        $aliasName = new self();
64
        $aliasName->doParse($item);
65
        return $aliasName;
66
    }
67
68
    /**
69
     * Creae a SiteAliasName object from an alias name string.
70
     *
71
     * @param string $sitename The alias name for the site.
72
     * @param string $env The name for the site's environment.
73
     */
74
    public function __construct($sitename = null, $env = null)
75
    {
76
        $this->sitename = $sitename;
77
        $this->env = $env;
78
    }
79
80
    /**
81
     * Convert an alias name back to a string.
82
     *
83
     * @return string
84
     */
85
    public function __toString()
86
    {
87
        $parts = [ $this->sitename() ];
88
        if ($this->hasEnv()) {
89
            $parts[] = $this->env();
90
        }
91
        return '@' . implode('.', $parts);
92
    }
93
94
    /**
95
     * Determine whether or not the provided name is an alias name.
96
     *
97
     * @param string $aliasName
98
     * @return bool
99
     */
100
    public static function isAliasName($aliasName)
101
    {
102
        // Alias names provided by users must begin with '@'
103
        if (empty($aliasName) || ($aliasName[0] != '@')) {
104
            return false;
105
        }
106
        return preg_match(self::ALIAS_NAME_REGEX, $aliasName);
107
    }
108
109
    /**
110
     * Return the sitename portion of the alias name. By definition,
111
     * every alias must have a sitename. If the site name is implicit,
112
     * then 'self' is assumed.
113
     *
114
     * @return string
115
     */
116
    public function sitename()
117
    {
118
        return empty($this->sitename) ? 'self' : $this->sitename;
119
    }
120
121
    /**
122
     * Set the sitename portion of the alias name
123
     *
124
     * @param string $sitename
125
     */
126
    public function setSitename($sitename)
127
    {
128
        $this->sitename = $sitename;
129
    }
130
131
    /**
132
     * In general, all aliases have a sitename. The time when one will not
133
     * is when an environment name `@env` is used as a shortcut for `@self.env`
134
     *
135
     * @return bool
136
     */
137
    public function hasSitename()
138
    {
139
        return !empty($this->sitename);
140
    }
141
142
    /**
143
     * Return true if this alias name contains an 'env' portion.
144
     *
145
     * @return bool
146
     */
147
    public function hasEnv()
148
    {
149
        return !empty($this->env);
150
    }
151
152
    /**
153
     * Set the environment portion of the alias record.
154
     *
155
     * @param string
156
     */
157
    public function setEnv($env)
158
    {
159
        $this->env = $env;
160
    }
161
162
    /**
163
     * Return the 'env' portion of the alias record.
164
     *
165
     * @return string
166
     */
167
    public function env()
168
    {
169
        return $this->env;
170
    }
171
172
    /**
173
     * Return true if this alias name is the 'self' alias.
174
     *
175
     * @return bool
176
     */
177
    public function isSelf()
178
    {
179
        return ($this->sitename == 'self') && !isset($this->env);
180
    }
181
182
    /**
183
     * Return true if this alias name is the 'none' alias.
184
     */
185
    public function isNone()
186
    {
187
        return ($this->sitename == 'none') && !isset($this->env);
188
    }
189
190
    /**
191
     * Convert the parts of an alias name to its various component parts.
192
     *
193
     * @param string $aliasName a string representation of an alias name.
194
     */
195
    protected function doParse($aliasName)
196
    {
197
        // Example contents of $matches:
198
        //
199
        // - a.b:
200
        //     [
201
        //       0 => 'a.b',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
202
        //       1 => 'a',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
203
        //       2 => '.b',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
204
        //     ]
205
        //
206
        // - a:
207
        //     [
208
        //       0 => 'a',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
209
        //       1 => 'a',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
210
        //     ]
211
        if (!preg_match(self::ALIAS_NAME_REGEX, $aliasName, $matches)) {
212
            return false;
213
        }
214
215
        // If $matches contains only two items, then assume the alias name
216
        // contains only the environment.
217
        if (count($matches) == 2) {
218
            return $this->processSingleItem($matches[1]);
219
        }
220
221
        $this->sitename = $matches[1];
222
        $this->env = ltrim($matches[2], '.');
223
        return true;
224
    }
225
226
    /**
227
     * Process an alias name provided as '@sitename'.
228
     *
229
     * @param string $sitename
0 ignored issues
show
Bug introduced by
There is no parameter named $sitename. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
230
     * @return true
231
     */
232
    protected function processSingleItem($item)
233
    {
234
        if ($this->isSpecialAliasName($item)) {
235
            $this->setSitename($item);
236
            return true;
237
        }
238
        $this->sitename = '';
239
        $this->env = $item;
240
        return true;
241
    }
242
243
    /**
244
     * Determine whether the requested name is a special alias name.
245
     *
246
     * @param string $item
247
     * @return boolean
248
     */
249
    protected function isSpecialAliasName($item)
250
    {
251
        return ($item == 'self') || ($item == 'none');
252
    }
253
}
254