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\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
|
1 |
|
public function fix(string $pathToYamlFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result |
22
|
|
|
{ |
23
|
1 |
|
$yamlContent = file_get_contents($pathToYamlFile); |
24
|
1 |
|
$yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns |
25
|
1 |
|
$yamlLines = explode("\n", $yamlContent); |
26
|
|
|
|
27
|
1 |
|
$correctYamlLines = YamlEmptyLineAtEndDataFactory::getCorrectYamlContent($yamlLines); |
28
|
1 |
|
$correctYamlContent = implode("\n", $correctYamlLines); |
29
|
|
|
|
30
|
1 |
|
if ($yamlContent === $correctYamlContent) { |
31
|
|
|
return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK); |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
file_put_contents($pathToDumpFixedFile, $correctYamlContent); |
35
|
|
|
|
36
|
1 |
|
$differ = new Differ(); |
37
|
1 |
|
$diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent); |
38
|
|
|
|
39
|
1 |
|
return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
45
|
|
|
protected function runCheckYamlFileIsValid(string $pathToYamlFile): void |
46
|
|
|
{ |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|