Passed
Pull Request — master (#74)
by Peter
06:51
created

YamlIndentFixer::fix()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 9.7998
cc 3
nc 4
nop 3
crap 3
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