|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\YamlEmptyLineAtEnd; |
|
6
|
|
|
|
|
7
|
|
|
use SebastianBergmann\Diff\Differ; |
|
8
|
|
|
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; |
|
9
|
|
|
use YamlStandards\Model\AbstractFixer; |
|
10
|
|
|
use YamlStandards\Model\Config\StandardParametersData; |
|
11
|
|
|
use YamlStandards\Result\Result; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Fix yaml file has empty line at end of file |
|
15
|
|
|
*/ |
|
16
|
|
|
class YamlEmptyLineAtEndFixer 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
|
|
|
|
|
27
|
2 |
|
$correctYamlLines = YamlEmptyLineAtEndDataFactory::getCorrectYamlContent($yamlLines); |
|
28
|
2 |
|
$correctYamlContent = implode("\n", $correctYamlLines); |
|
29
|
|
|
|
|
30
|
2 |
|
if ($yamlContent === $correctYamlContent) { |
|
31
|
1 |
|
return new Result($pathToFile, Result::RESULT_CODE_OK); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
file_put_contents($pathToDumpFixedFile, $correctYamlContent); |
|
35
|
|
|
|
|
36
|
1 |
|
$differ = new Differ(new UnifiedDiffOutputBuilder()); |
|
37
|
1 |
|
$diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent); |
|
38
|
|
|
|
|
39
|
1 |
|
return new Result($pathToFile, Result::RESULT_CODE_FIXED_INVALID_FILE_SYNTAX, $diffBetweenStrings); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritDoc |
|
44
|
|
|
*/ |
|
45
|
1 |
|
protected function runCheckYamlFileIsValid(string $pathToYamlFile): void |
|
46
|
|
|
{ |
|
47
|
1 |
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|