|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\YamlEmptyLineAtEnd; |
|
6
|
|
|
|
|
7
|
|
|
use SebastianBergmann\Diff\Differ; |
|
8
|
|
|
use YamlStandards\Command\ProcessOutput; |
|
9
|
|
|
use YamlStandards\Model\Component\YamlService; |
|
10
|
|
|
use YamlStandards\Model\Config\StandardParametersData; |
|
11
|
|
|
use YamlStandards\Model\FixerInterface; |
|
12
|
|
|
use YamlStandards\Result\Result; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Fix yaml file has empty line at end of file |
|
16
|
|
|
*/ |
|
17
|
|
|
class YamlEmptyLineAtEndFixer implements FixerInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritDoc |
|
21
|
|
|
*/ |
|
22
|
|
|
public function fix(string $pathToYamlFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result |
|
23
|
|
|
{ |
|
24
|
|
|
$yamlContent = file_get_contents($pathToYamlFile); |
|
25
|
|
|
$yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns |
|
26
|
|
|
$yamlLines = explode("\n", $yamlContent); |
|
27
|
|
|
$filteredYamlLines = array_values(array_filter($yamlLines, [YamlService::class, 'isLineNotBlank'])); |
|
28
|
|
|
|
|
29
|
|
|
$filteredYamlLines[] = ''; |
|
30
|
|
|
$correctYamlContent = implode("\n", $filteredYamlLines); |
|
31
|
|
|
|
|
32
|
|
|
if ($yamlContent === $correctYamlContent) { |
|
33
|
|
|
return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
file_put_contents($pathToDumpFixedFile, $correctYamlContent); |
|
37
|
|
|
|
|
38
|
|
|
$differ = new Differ(); |
|
39
|
|
|
$diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent); |
|
40
|
|
|
|
|
41
|
|
|
return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|