1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\YamlSpacesBetweenGroups; |
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 yaml file have space between groups |
15
|
|
|
*/ |
16
|
|
|
class YamlSpacesBetweenGroupsFixer extends AbstractFixer |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @inheritDoc |
20
|
|
|
*/ |
21
|
2 |
|
public function fix(string $pathToFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result |
22
|
|
|
{ |
23
|
2 |
|
$yamlContent = file_get_contents($pathToFile); |
24
|
2 |
|
$yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns |
25
|
2 |
|
$yamlLines = explode("\n", $yamlContent); |
26
|
2 |
|
$lastYamlElement = end($yamlLines); |
27
|
2 |
|
$filteredYamlLines = array_values(array_filter($yamlLines, [YamlService::class, 'isLineNotBlank'])); |
28
|
2 |
|
$yamlSpacesBetweenGroupsDataFactory = new YamlSpacesBetweenGroupsDataFactory(); |
29
|
|
|
|
30
|
2 |
|
$correctYamlContent = $yamlSpacesBetweenGroupsDataFactory->getCorrectYamlContentWithSpacesBetweenGroups($filteredYamlLines, $standardParametersData->getLevel()); |
31
|
|
|
|
32
|
2 |
|
if (YamlService::isLineBlank($lastYamlElement)) { |
33
|
2 |
|
$correctYamlContent .= "\n"; |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
if ($yamlContent === $correctYamlContent) { |
37
|
1 |
|
return new Result($pathToFile, Result::RESULT_CODE_OK); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
file_put_contents($pathToDumpFixedFile, $correctYamlContent); |
41
|
|
|
|
42
|
1 |
|
$differ = new Differ(); |
43
|
1 |
|
$diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent); |
44
|
|
|
|
45
|
1 |
|
return new Result($pathToFile, Result::RESULT_CODE_FIXED_INVALID_FILE_SYNTAX, $diffBetweenStrings); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|