SiteAliasManager::get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
namespace Consolidation\SiteAlias;
3
4
/**
5
 * Site Alias manager
6
 */
7
class SiteAliasManager implements SiteAliasManagerInterface, SiteAliasManagerInitializationInterface
8
{
9
    protected $aliasLoader;
10
    protected $selfSiteAlias;
11
    protected $specParser;
12
    protected $root = '';
13
14
    /**
15
     * Constructor for SiteAliasManager
16
     *
17
     * @param SiteAliasFileLoader|null $aliasLoader an alias loader
18
     */
19
    public function __construct($aliasLoader = null, $root = '')
20
    {
21
        $this->aliasLoader = $aliasLoader ?: new SiteAliasFileLoader();
22
        $this->specParser = new SiteSpecParser();
23
        $this->selfSiteAlias = new SiteAlias();
24
        $this->setRoot($root);
25
    }
26
27
    /**
28
     * Allow configuration data to be used in replacements in the alias file.
29
     */
30
    public function setReferenceData($data)
31
    {
32
        $this->aliasLoader->setReferenceData($data);
33
        return $this;
34
    }
35
36
    /**
37
     * Inject the root of the selected site
38
     *
39
     * @param string $root
40
     * @return $this
41
     */
42
    public function setRoot($root)
43
    {
44
        $this->root = $root;
45
        $this->aliasLoader->setRoot($root);
46
        return $this;
47
    }
48
49
    /**
50
     * Add a search location to our site alias discovery object.
51
     *
52
     * @param string $path
53
     *
54
     * @return $this
55
     */
56
    public function addSearchLocation($path)
57
    {
58
        $this->aliasLoader->discovery()->addSearchLocation($path);
59
        return $this;
60
    }
61
62
    /**
63
     * Add search locations to our site alias discovery object.
64
     *
65
     * @param array $paths Any path provided in --alias-path option
66
     *   or drush.path.alias-path configuration item.
67
     *
68
     * @return $this
69
     */
70
    public function addSearchLocations(array $paths)
71
    {
72
        foreach ($paths as $path) {
73
            $this->aliasLoader->discovery()->addSearchLocation($path);
74
        }
75
        return $this;
76
    }
77
78
    /**
79
     * Return all of the paths where alias files may be found.
80
     * @return string[]
81
     */
82
    public function searchLocations()
83
    {
84
        return $this->aliasLoader->discovery()->searchLocations();
85
    }
86
87
    /**
88
     * Get an alias record by name, or convert a site specification
89
     * into an alias record via the site alias spec parser. If a
90
     * simple alias name is provided (e.g. '@alias'), it is interpreted
91
     * as a sitename, and the default environment for that site is returned.
92
     *
93
     * @param string $name Alias name or site specification
94
     *
95
     * @return SiteAlias|false
96
     */
97
    public function get($name)
98
    {
99
        if (SiteAliasName::isAliasName($name)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \Consolidation\SiteAlias...ame::isAliasName($name) of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
100
            return $this->getAlias($name);
101
        }
102
103
        if ($this->specParser->validSiteSpec($name)) {
104
            return new SiteAlias($this->specParser->parse($name, $this->root), $name);
105
        }
106
107
        return false;
108
    }
109
110
    /**
111
     * Get the '@self' alias record.
112
     *
113
     * @return SiteAlias
114
     */
115
    public function getSelf()
116
    {
117
        return $this->selfSiteAlias;
118
    }
119
120
    /**
121
     * Force-set the current @self alias.
122
     *
123
     * @param SiteAlias $selfSiteAlias
124
     * @return $this
125
     */
126
    public function setSelf(SiteAlias $selfSiteAlias)
127
    {
128
        $this->selfSiteAlias = $selfSiteAlias;
129
        $this->setRoot($selfSiteAlias->localRoot());
130
        return $this;
131
    }
132
133
    /**
134
     * Get an alias record from a name. Does not accept site specifications.
135
     *
136
     * @param string $aliasName alias name
137
     *
138
     * @return SiteAlias
139
     */
140
    public function getAlias($aliasName)
141
    {
142
        $aliasName = SiteAliasName::parse($aliasName);
143
144
        if ($aliasName->isSelf()) {
145
            return $this->getSelf();
146
        }
147
148
        if ($aliasName->isNone()) {
149
            return new SiteAlias([], '@none');
150
        }
151
152
        // Search through all search locations, load
153
        // matching and potentially-matching alias files,
154
        // and return the alias matching the provided name.
155
        return $this->aliasLoader->load($aliasName);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression $this->aliasLoader->load($aliasName); of type Consolidation\SiteAlias\SiteAlias|false adds false to the return on line 155 which is incompatible with the return type declared by the interface Consolidation\SiteAlias\...agerInterface::getAlias of type Consolidation\SiteAlias\SiteAlias. It seems like you forgot to handle an error condition.
Loading history...
156
    }
157
158
    /**
159
     * Given a simple alias name, e.g. '@alias', returns all of the
160
     * environments in the specified site.
161
     *
162
     * If the provided name is a site specification et. al.,
163
     * then this method will return 'false'.
164
     *
165
     * @param string $name Alias name
166
     * @return SiteAlias[]|false
167
     */
168
    public function getMultiple($name = '')
169
    {
170
        if (empty($name)) {
171
            return $this->aliasLoader->loadAll();
172
        }
173
174
        if (!SiteAliasName::isAliasName($name)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression \Consolidation\SiteAlias...ame::isAliasName($name) of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
175
            return false;
176
        }
177
178
        // Trim off the '@'
179
        $trimmedName = ltrim($name, '@');
180
181
        // If the provided name is a location, return all aliases there
182
        $result = $this->aliasLoader->loadLocation($trimmedName);
183
        if (!empty($result)) {
184
            return $result;
185
        }
186
187
        // If the provided name is a site, return all environments
188
        $result = $this->aliasLoader->loadMultiple($trimmedName);
189
        if (!empty($result)) {
190
            return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (array) is incompatible with the return type declared by the interface Consolidation\SiteAlias\...rInterface::getMultiple of type Consolidation\SiteAlias\SiteAlias[]|false.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
191
        }
192
193
        // Special checking for @self
194
        if ($trimmedName == 'self') {
195
            $self = $this->getSelf();
196
            $result = array_merge(
197
                ['@self' => $self],
198
                $result
199
            );
200
        }
201
202
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $result; (array) is incompatible with the return type declared by the interface Consolidation\SiteAlias\...rInterface::getMultiple of type Consolidation\SiteAlias\SiteAlias[]|false.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
203
    }
204
205
    /**
206
     * Return the paths to all alias files in all search locations known
207
     * to the alias manager.
208
     *
209
     * @return string[]
210
     */
211
    public function listAllFilePaths($location = '')
212
    {
213
        return $this->aliasLoader->listAll($location);
214
    }
215
}
216