Passed
Push — master ( 9a9b0b...b778d8 )
by Peter
08:40
created

YamlAlphabeticalFixer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 11
c 1
b 0
f 0
dl 0
loc 24
ccs 10
cts 11
cp 0.9091
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A fix() 0 19 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace YamlStandards\Model\YamlAlphabetical;
6
7
use SebastianBergmann\Diff\Differ;
8
use Symfony\Component\Yaml\Yaml;
9
use YamlStandards\Command\ProcessOutput;
10
use YamlStandards\Model\AbstractFixer;
11
use YamlStandards\Model\Config\StandardParametersData;
12
use YamlStandards\Result\Result;
13
14
/**
15
 * Fix yaml file is alphabetical sorted
16
 */
17
class YamlAlphabeticalFixer extends AbstractFixer
18
{
19
    /**
20
     * @inheritDoc
21
     */
22 2
    public function fix(string $pathToYamlFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result
23
    {
24 2
        $fileContent = file_get_contents($pathToYamlFile);
25 2
        $fileContent = str_replace("\r", '', $fileContent); // remove carriage returns
26
27 2
        $rightFileLines = YamlAlphabeticalDataFactory::getCorrectYamlLines($pathToYamlFile, $standardParametersData->getDepth());
28
29 2
        $rightFileContent = implode("\n", $rightFileLines);
30
31 2
        if ($fileContent === $rightFileContent) {
32
            return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK);
33
        }
34
35 2
        file_put_contents($pathToDumpFixedFile, $rightFileContent);
36
37 2
        $differ = new Differ();
38 2
        $diffBetweenStrings = $differ->diff($fileContent, $rightFileContent);
39
40 2
        return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings);
41
    }
42
}
43