1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Configuration\Migration; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
8
|
|
|
use Doctrine\Migrations\Configuration\Exception\FileNotFound; |
9
|
|
|
use Doctrine\Migrations\Configuration\Migration\Exception\YamlNotAvailable; |
10
|
|
|
use Doctrine\Migrations\Configuration\Migration\Exception\YamlNotValid; |
11
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
13
|
|
|
use function assert; |
14
|
|
|
use function class_exists; |
15
|
|
|
use function file_exists; |
16
|
|
|
use function file_get_contents; |
17
|
|
|
use function is_array; |
18
|
|
|
|
19
|
|
|
final class YamlFile extends ConfigurationFile |
20
|
|
|
{ |
21
|
11 |
|
public function getConfiguration() : Configuration |
22
|
|
|
{ |
23
|
11 |
|
if (! class_exists(Yaml::class)) { |
24
|
|
|
throw YamlNotAvailable::new(); |
25
|
|
|
} |
26
|
|
|
|
27
|
11 |
|
if (! file_exists($this->file)) { |
28
|
1 |
|
throw FileNotFound::new($this->file); |
29
|
|
|
} |
30
|
|
|
|
31
|
10 |
|
$content = file_get_contents($this->file); |
32
|
|
|
|
33
|
10 |
|
assert($content !== false); |
34
|
|
|
|
35
|
|
|
try { |
36
|
10 |
|
$config = Yaml::parse($content); |
37
|
1 |
|
} catch (ParseException $e) { |
38
|
1 |
|
throw YamlNotValid::malformed(); |
39
|
|
|
} |
40
|
|
|
|
41
|
9 |
|
if (! is_array($config)) { |
42
|
|
|
throw YamlNotValid::invalid(); |
43
|
|
|
} |
44
|
|
|
|
45
|
9 |
View Code Duplication |
if (isset($config['migrations_paths'])) { |
|
|
|
|
46
|
4 |
|
$config['migrations_paths'] = $this->getDirectoriesRelativeToFile( |
47
|
4 |
|
$config['migrations_paths'], |
48
|
4 |
|
$this->file |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
9 |
|
return (new ConfigurationArray($config))->getConfiguration(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.