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