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

SiteAliasFileDiscovery   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 146
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A addSearchLocation() 0 7 2
A searchLocations() 0 4 1
A depth() 0 5 1
A findSingleSiteAliasFile() 0 8 2
A findAllSingleAliasFiles() 0 4 1
A findAllLegacyAliasFiles() 0 7 1
A createFinder() 0 9 1
A searchForAliasFiles() 0 13 3
A searchForAliasFilesKeyedByBasenamePrefix() 0 15 3
A extractKey() 0 4 1
1
<?php
2
namespace Consolidation\SiteAlias;
3
4
use Symfony\Component\Finder\Finder;
5
6
/**
7
 * Discover alias files named:
8
 *
9
 * - sitename.site.yml: contains multiple aliases, one for each of the
10
 *     environments of 'sitename'.
11
 *
12
 * Drush aliases that contain both a site name and an environment
13
 * (e.g. @site.env) will cause Drush to find the file named after
14
 * the respective site name and retrieve the specified environment
15
 * record.
16
 *
17
 * Sites may also define a special alias file self.site.yml, which
18
 * may be stored in the drush/sites directory relative to either
19
 * the Drupal root or the Composer root of the site. The environments
20
 * in this file will be merged with the available environments for
21
 * the element @self, however it is defined.
22
 */
23
class SiteAliasFileDiscovery
24
{
25
    protected $searchLocations = [];
26
    protected $depth = '<= 1';
27
28
    /**
29
     * Add a location that alias files may be found.
30
     *
31
     * @param string $path
32
     * @return $this
33
     */
34
    public function addSearchLocation($path)
35
    {
36
        if (is_dir($path)) {
37
            $this->searchLocations[] = $path;
38
        }
39
        return $this;
40
    }
41
42
    /**
43
     * Return all of the paths where alias files may be found.
44
     * @return string[]
45
     */
46
    public function searchLocations()
47
    {
48
        return $this->searchLocations;
49
    }
50
51
52
    /**
53
     * Set the search depth for finding alias files
54
     *
55
     * @param string|int $depth (@see \Symfony\Component\Finder\Finder::depth)
56
     * @return $this
57
     */
58
    public function depth($depth)
59
    {
60
        $this->depth = $depth;
0 ignored issues
show
Documentation Bug introduced by
It seems like $depth can also be of type integer. However, the property $depth is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
61
        return $this;
62
    }
63
64
    /**
65
     * Find an alias file SITENAME.site.yml in one
66
     * of the specified search locations.
67
     *
68
     * @param string $siteName
69
     * @return string|bool
70
     */
71
    public function findSingleSiteAliasFile($siteName)
72
    {
73
        $matches = $this->searchForAliasFiles("$siteName.site.yml");
74
        if (empty($matches)) {
75
            return false;
76
        }
77
        return reset($matches);
78
    }
79
80
    /**
81
     * Return a list of all SITENAME.site.yml files in any of
82
     * the search locations.
83
     *
84
     * @return string[]
85
     */
86
    public function findAllSingleAliasFiles()
87
    {
88
        return $this->searchForAliasFiles('*.site.yml');
89
    }
90
91
    /**
92
     * Return all of the legacy alias files used in previous Drush versions.
93
     *
94
     * @return string[]
95
     */
96
    public function findAllLegacyAliasFiles()
97
    {
98
        return array_merge(
99
            $this->searchForAliasFiles('*.alias.drushrc.php'),
100
            $this->searchForAliasFiles('*.aliases.drushrc.php')
101
        );
102
    }
103
104
    /**
105
     * Create a Symfony Finder object to search all available search locations
106
     * for the specified search pattern.
107
     *
108
     * @param string $searchPattern
109
     * @return Finder
110
     */
111
    protected function createFinder($searchPattern)
112
    {
113
        $finder = new Finder();
114
        $finder->files()
115
            ->name($searchPattern)
116
            ->in($this->searchLocations)
117
            ->depth($this->depth);
118
        return $finder;
119
    }
120
121
    /**
122
     * Return a list of all alias files matching the provided pattern.
123
     *
124
     * @param string $searchPattern
125
     * @return string[]
126
     */
127
    protected function searchForAliasFiles($searchPattern)
128
    {
129
        if (empty($this->searchLocations)) {
130
            return [];
131
        }
132
        $finder = $this->createFinder($searchPattern);
133
        $result = [];
134
        foreach ($finder as $file) {
135
            $path = $file->getRealPath();
136
            $result[] = $path;
137
        }
138
        return $result;
139
    }
140
141
    /**
142
     * Return a list of all alias files with the specified extension.
143
     *
144
     * @param string $filenameExensions
145
     * @return string[]
146
     */
147
    protected function searchForAliasFilesKeyedByBasenamePrefix($filenameExensions)
148
    {
149
        if (empty($this->searchLocations)) {
150
            return [];
151
        }
152
        $searchPattern = '*' . $filenameExensions;
153
        $finder = $this->createFinder($searchPattern);
154
        $result = [];
155
        foreach ($finder as $file) {
156
            $path = $file->getRealPath();
157
            $key = $this->extractKey($file->getBasename(), $filenameExensions);
158
            $result[$key] = $path;
159
        }
160
        return $result;
161
    }
162
163
    // TODO: Seems like this could just be basename()
164
    protected function extractKey($basename, $filenameExensions)
165
    {
166
        return str_replace($filenameExensions, '', $basename);
167
    }
168
}
169