These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Jumilla\Versionia\Laravel; |
||
4 | |||
5 | use Illuminate\Contracts\Database\DatabaseManager; |
||
6 | use Illuminate\Contracts\Config\Repository as Config; |
||
7 | use Illuminate\Database\Schema\Blueprint; |
||
8 | use Illuminate\Support\Facades\Schema; |
||
9 | |||
10 | class Migrator |
||
11 | { |
||
12 | const VERSION_NULL = null; |
||
13 | |||
14 | /** |
||
15 | * @var \Illuminate\Database\DatabaseManager |
||
16 | */ |
||
17 | protected $db; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $table = '_migrations'; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $migrations = []; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $seeds = []; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $default_seed; |
||
38 | |||
39 | /** |
||
40 | * @param \Illuminate\Contracts\Database\DatabaseManager $db |
||
41 | * @param \Illuminate\Contracts\Config\Repository $config |
||
42 | */ |
||
43 | 43 | public function __construct(DatabaseManager $db, Config $config) |
|
44 | { |
||
45 | 43 | $this->db = $db; |
|
0 ignored issues
–
show
|
|||
46 | 43 | $this->table = $config->get('database.migrations', $this->table); |
|
47 | 43 | } |
|
48 | |||
49 | /** |
||
50 | * @param string $a |
||
51 | * @param string $b |
||
52 | * |
||
53 | * @return int |
||
54 | */ |
||
55 | 18 | public function compareMigrationVersion($a, $b) |
|
56 | { |
||
57 | 18 | return version_compare($a, $b); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $group |
||
62 | * @param string $version |
||
63 | * @param string $class |
||
64 | */ |
||
65 | 4 | public function registerMigration($group, $version, $class) |
|
66 | { |
||
67 | 4 | $this->registerMigrations($group, [ |
|
68 | 4 | $version => $class, |
|
69 | 4 | ]); |
|
70 | 4 | } |
|
71 | |||
72 | /** |
||
73 | * @param string $group |
||
74 | * @param array $versions |
||
75 | */ |
||
76 | 22 | public function registerMigrations($group, array $versions) |
|
77 | { |
||
78 | 22 | foreach ($versions as $version => $class) { |
|
79 | 21 | $this->migrations[$group][$version] = $class; |
|
80 | 22 | } |
|
81 | 22 | } |
|
82 | |||
83 | /** |
||
84 | * @return array |
||
85 | */ |
||
86 | 22 | public function migrationGroups() |
|
87 | { |
||
88 | 22 | $groups = array_keys($this->migrations); |
|
89 | |||
90 | 22 | sort($groups); |
|
91 | |||
92 | 22 | return $groups; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param string $group |
||
97 | * @param bool $descending |
||
98 | * |
||
99 | * @return array |
||
100 | */ |
||
101 | 15 | public function migrationVersions($group, $descending = false) |
|
102 | { |
||
103 | 15 | $versions = array_get($this->migrations, $group, []); |
|
104 | |||
105 | 15 | uksort($versions, [$this, 'compareMigrationVersion']); |
|
106 | |||
107 | 15 | return $descending ? array_reverse($versions, true) : $versions; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param string $group |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | 11 | public function migrationVersionsByDesc($group) |
|
116 | { |
||
117 | 11 | return $this->migrationVersions($group, true); |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param string $group |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | 4 | public function migrationLatestVersion($group) |
|
126 | { |
||
127 | 4 | foreach ($this->migrationVersionsByDesc($group) as $version => $class) { |
|
128 | 4 | return $version; |
|
129 | 1 | } |
|
130 | |||
131 | 1 | return self::VERSION_NULL; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param string $group |
||
136 | * @param string $version |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 2 | public function migrationClass($group, $version) |
|
141 | { |
||
142 | 2 | $versions = array_get($this->migrations, $group, []); |
|
143 | |||
144 | 2 | return array_get($versions, $version, null); |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * @param string $name |
||
149 | * @param string $class |
||
150 | */ |
||
151 | 2 | public function registerSeed($name, $class) |
|
152 | { |
||
153 | 2 | $this->registerSeeds([ |
|
154 | 2 | $name => $class, |
|
155 | 2 | ]); |
|
156 | 2 | } |
|
157 | |||
158 | /** |
||
159 | * @param array $seeds |
||
160 | */ |
||
161 | 9 | public function registerSeeds(array $seeds) |
|
162 | { |
||
163 | 9 | $this->seeds = array_merge($this->seeds, $seeds); |
|
164 | 9 | } |
|
165 | |||
166 | /** |
||
167 | * @return string |
||
168 | */ |
||
169 | 7 | public function defaultSeed() |
|
170 | { |
||
171 | 7 | return $this->default_seed; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * @param string $seed |
||
176 | */ |
||
177 | 7 | public function setDefaultSeed($seed) |
|
178 | { |
||
179 | 7 | $this->default_seed = $seed; |
|
180 | 7 | } |
|
181 | |||
182 | /** |
||
183 | * @return array |
||
184 | */ |
||
185 | 5 | public function seedNames() |
|
186 | { |
||
187 | 5 | return array_keys($this->seeds); |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param string $name |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | 9 | public function seedClass($name) |
|
196 | { |
||
197 | 9 | return array_get($this->seeds, $name); |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | */ |
||
202 | 2 | public function makeLogTable() |
|
203 | { |
||
204 | 2 | if (!Schema::hasTable($this->table)) { |
|
205 | Schema::create($this->table, function (Blueprint $table) { |
||
206 | 1 | $table->string('group'); |
|
207 | 1 | $table->string('version'); |
|
208 | 1 | $table->string('class'); |
|
209 | |||
210 | 1 | $table->unique(['group', 'version']); |
|
211 | 1 | }); |
|
212 | 1 | } |
|
213 | 2 | } |
|
214 | |||
215 | /** |
||
216 | * @param bool $descending |
||
217 | * |
||
218 | * @return \Illuminate\Support\Collection |
||
219 | */ |
||
220 | 1 | public function installedMigrations($descending = false) |
|
221 | { |
||
222 | return collect($this->db->table($this->table)->get())->sort(function ($a, $b) use ($descending) { |
||
223 | 1 | return $this->compareMigrationVersion($a->version, $b->version) * ($descending ? -1 : 1); |
|
224 | 1 | })->groupBy('group'); |
|
225 | } |
||
226 | /** |
||
227 | * @return \Illuminate\Support\Collection |
||
228 | */ |
||
229 | 1 | public function installedMigrationsByDesc() |
|
230 | { |
||
231 | 1 | return $this->installedMigrations(true); |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return \Illuminate\Support\Collection |
||
236 | */ |
||
237 | public function installedLatestMigrations() |
||
238 | { |
||
239 | 1 | return collect($this->db->table($this->table)->get())->reduce(function ($result, $item) { |
|
240 | 1 | if (isset($result[$item->group])) { |
|
241 | 1 | if ($this->compareMigrationVersion($item->version, $result[$item->group]->version) > 0) { |
|
242 | 1 | $result[$item->group] = $item; |
|
243 | 1 | } |
|
244 | 1 | } else { |
|
245 | 1 | $result[$item->group] = $item; |
|
246 | } |
||
247 | |||
248 | 1 | return $result; |
|
249 | 1 | }, collect()); |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * @param string $group |
||
254 | * @param string $version |
||
255 | * @param string $class |
||
256 | */ |
||
257 | 1 | public function addMigrationLog($group, $version, $class) |
|
258 | { |
||
259 | 1 | $this->db->table($this->table)->insert([ |
|
260 | 1 | 'group' => $group, |
|
261 | 1 | 'version' => $version, |
|
262 | 1 | 'class' => $class, |
|
263 | 1 | ]); |
|
264 | 1 | } |
|
265 | |||
266 | /** |
||
267 | * @param string $group |
||
268 | * @param string $version |
||
269 | */ |
||
270 | 1 | public function removeMigrationLog($group, $version) |
|
271 | { |
||
272 | 1 | $this->db->table($this->table)->where('group', $group)->where('version', $version)->delete(); |
|
273 | 1 | } |
|
274 | |||
275 | /** |
||
276 | * @param string $group |
||
277 | * @param string $version |
||
278 | * @param string $class |
||
279 | */ |
||
280 | 1 | public function doUpgrade($group, $version, $class) |
|
281 | { |
||
282 | 1 | $migration = new $class(); |
|
283 | |||
284 | 1 | $migration->up(); |
|
285 | |||
286 | 1 | $this->addMigrationLog($group, $version, $class); |
|
287 | 1 | } |
|
288 | |||
289 | /** |
||
290 | * @param string $group |
||
291 | * @param string $version |
||
292 | * @param string $class |
||
293 | */ |
||
294 | 2 | public function doDowngrade($group, $version, $class = null) |
|
295 | { |
||
296 | 2 | if ($class === null) { |
|
297 | 1 | $class = $this->migrationClass($group, $version); |
|
298 | 1 | } |
|
299 | |||
300 | 2 | $migration = new $class(); |
|
301 | |||
302 | 2 | $migration->down(); |
|
303 | |||
304 | 2 | $this->removeMigrationLog($group, $version); |
|
305 | 2 | } |
|
306 | } |
||
307 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..