1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\YamlIndent; |
6
|
|
|
|
7
|
|
|
use SebastianBergmann\Diff\Differ; |
8
|
|
|
use YamlStandards\Model\AbstractFixer; |
9
|
|
|
use YamlStandards\Model\Config\StandardParametersData; |
10
|
|
|
use YamlStandards\Result\Result; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Fix yaml file with right count of indent |
14
|
|
|
*/ |
15
|
|
|
class YamlIndentFixer extends AbstractFixer |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @inheritDoc |
19
|
|
|
*/ |
20
|
5 |
|
public function fix(string $pathToFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result |
21
|
|
|
{ |
22
|
5 |
|
$fileContent = file_get_contents($pathToFile); |
23
|
5 |
|
$fileContent = str_replace("\r", '', $fileContent); // remove carriage returns |
24
|
5 |
|
$fileLines = explode("\n", $fileContent); |
25
|
5 |
|
$yamlIndentDataFactory = new YamlIndentDataFactory(); |
26
|
5 |
|
$rightFileLines = []; |
27
|
|
|
|
28
|
5 |
|
foreach ($fileLines as $key => $fileLine) { |
29
|
5 |
|
$rightFileLines[] = $yamlIndentDataFactory->getRightFileLines($fileLines, $key, $standardParametersData, $fileLine); |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
$rightFileContent = implode("\n", $rightFileLines); |
33
|
|
|
|
34
|
5 |
|
if ($fileContent === $rightFileContent) { |
35
|
1 |
|
return new Result($pathToFile, Result::RESULT_CODE_OK); |
36
|
|
|
} |
37
|
|
|
|
38
|
4 |
|
file_put_contents($pathToDumpFixedFile, $rightFileContent); |
39
|
|
|
|
40
|
4 |
|
$differ = new Differ(); |
41
|
4 |
|
$diffBetweenStrings = $differ->diff($fileContent, $rightFileContent); |
42
|
|
|
|
43
|
4 |
|
return new Result($pathToFile, Result::RESULT_CODE_FIXED_INVALID_FILE_SYNTAX, $diffBetweenStrings); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|