1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\YamlServiceAliasing; |
6
|
|
|
|
7
|
|
|
use SebastianBergmann\Diff\Differ; |
8
|
|
|
use YamlStandards\Model\AbstractFixer; |
9
|
|
|
use YamlStandards\Model\Component\YamlService; |
10
|
|
|
use YamlStandards\Model\Config\StandardParametersData; |
11
|
|
|
use YamlStandards\Result\Result; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Fix service file has uniform aliasing |
15
|
|
|
*/ |
16
|
|
|
class YamlServiceAliasingFixer extends AbstractFixer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @inheritDoc |
20
|
|
|
*/ |
21
|
3 |
|
public function fix(string $pathToFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result |
22
|
|
|
{ |
23
|
3 |
|
$yamlContent = file_get_contents($pathToFile); |
24
|
3 |
|
$yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns |
25
|
3 |
|
$yamlLines = explode("\n", $yamlContent); |
26
|
|
|
|
27
|
3 |
|
if (YamlServiceAliasingDataFactory::existsServicesInHighestParent($yamlLines) === false) { |
28
|
1 |
|
return new Result($pathToFile, Result::RESULT_CODE_OK); |
29
|
|
|
} |
30
|
|
|
|
31
|
3 |
|
$correctYamlLines = YamlServiceAliasingDataFactory::getCorrectYamlLines($yamlLines, YamlService::getYamlData($pathToFile), $standardParametersData); |
32
|
3 |
|
$correctYamlContent = implode("\n", $correctYamlLines); |
33
|
|
|
|
34
|
3 |
|
if ($yamlContent === $correctYamlContent) { |
35
|
1 |
|
return new Result($pathToFile, Result::RESULT_CODE_OK); |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
file_put_contents($pathToDumpFixedFile, $correctYamlContent); |
39
|
|
|
|
40
|
2 |
|
$differ = new Differ(); |
41
|
2 |
|
$diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent); |
42
|
|
|
|
43
|
2 |
|
return new Result($pathToFile, Result::RESULT_CODE_FIXED_INVALID_FILE_SYNTAX, $diffBetweenStrings); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|