YamlAlphabeticalChecker::check()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.108

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 17
ccs 7
cts 10
cp 0.7
rs 9.9666
cc 2
nc 2
nop 2
crap 2.108
1
<?php
2
3
declare(strict_types=1);
4
5
namespace YamlStandards\Model\YamlAlphabetical;
6
7
use SebastianBergmann\Diff\Differ;
8
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
9
use Symfony\Component\Yaml\Yaml;
10
use YamlStandards\Model\AbstractChecker;
11
use YamlStandards\Model\Config\StandardParametersData;
12
use YamlStandards\Result\Result;
13
14
/**
15
 * Check yaml file is alphabetical sorted
16
 */
17
class YamlAlphabeticalChecker extends AbstractChecker
18
{
19
    /**
20
     * @inheritDoc
21
     */
22 3
    public function check(string $pathToFile, StandardParametersData $standardParametersData): Result
23
    {
24 3
        $fileContent = file_get_contents($pathToFile);
25 3
        $fileContent = str_replace("\r", '', $fileContent); // remove carriage returns
26
27 3
        $rightFileLines = YamlAlphabeticalDataFactory::getCorrectYamlLines($pathToFile, $standardParametersData->getDepth(), $standardParametersData->getAlphabeticalPrioritizedKeys());
28
29 3
        $rightFileContent = implode("\n", $rightFileLines);
30
31 3
        if ($fileContent === $rightFileContent) {
32 3
            return new Result($pathToFile, Result::RESULT_CODE_OK);
33
        }
34
35
        $differ = new Differ(new UnifiedDiffOutputBuilder());
36
        $diffBetweenStrings = $differ->diff($fileContent, $rightFileContent);
37
38
        return new Result($pathToFile, Result::RESULT_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings);
39
    }
40
}
41