Passed
Pull Request — master (#74)
by Peter
08:25 queued 01:31
created

YamlAlphabeticalFixer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
eloc 11
c 1
b 0
f 0
dl 0
loc 24
ccs 11
cts 11
cp 1
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\Model\AbstractFixer;
10
use YamlStandards\Model\Config\StandardParametersData;
11
use YamlStandards\Result\Result;
12
13
/**
14
 * Fix yaml file is alphabetical sorted
15
 */
16
class YamlAlphabeticalFixer extends AbstractFixer
17
{
18
    /**
19
     * @inheritDoc
20
     */
21 3
    public function fix(string $pathToFile, string $pathToDumpFixedFile, StandardParametersData $standardParametersData): Result
22
    {
23 3
        $fileContent = file_get_contents($pathToFile);
24 3
        $fileContent = str_replace("\r", '', $fileContent); // remove carriage returns
25
26 3
        $rightFileLines = YamlAlphabeticalDataFactory::getCorrectYamlLines($pathToFile, $standardParametersData->getDepth(), $standardParametersData->getAlphabeticalPrioritizedKeys());
27
28 3
        $rightFileContent = implode("\n", $rightFileLines);
29
30 3
        if ($fileContent === $rightFileContent) {
31 1
            return new Result($pathToFile, Result::RESULT_CODE_OK);
32
        }
33
34 2
        file_put_contents($pathToDumpFixedFile, $rightFileContent);
35
36 2
        $differ = new Differ();
37 2
        $diffBetweenStrings = $differ->diff($fileContent, $rightFileContent);
38
39 2
        return new Result($pathToFile, Result::RESULT_CODE_FIXED_INVALID_FILE_SYNTAX, $diffBetweenStrings);
40
    }
41
}
42